From 6f4c4d4508e3c7a40c75fcc5aa360ce3f446bd43 Mon Sep 17 00:00:00 2001 From: alexandergull Date: Sat, 19 Aug 2023 00:25:34 +0500 Subject: [PATCH] Plugin installation implemented. Bugs fixed. --- gvs.php | 20 +++++------ inc/gvs_helper.php | 8 ++--- lib/GVS/GVS.php | 77 ++++++++++++++++++++++++++++++++++++----- templates/gvs_form.html | 10 ++++-- 4 files changed, 90 insertions(+), 25 deletions(-) diff --git a/gvs.php b/gvs.php index 4d845ff..aeb48b9 100644 --- a/gvs.php +++ b/gvs.php @@ -41,6 +41,7 @@ function gvs_main() // check if supported plugins installed $work_with_plugin = isset($_POST['plugin_inner_name']) ? $_POST['plugin_inner_name'] : null; $url = isset($_POST['gvs_select']) ? $_POST['gvs_select'] : null; + $action = isset($_POST['plugin_action']) ? strtolower($_POST['plugin_action']) : null; if ( $work_with_plugin && $url ) { @@ -48,16 +49,13 @@ function gvs_main() $gvs->process_plugin = $gvs->plugins_data[$work_with_plugin]; // run processes - $gvs->downloadPluginZip($url) - ->unpackZip() - ->prepareDirectories() - ->doBackup() - ->replaceActivePlugin() - ->deleteTempFiles() - ->saveLogToState(); + if ($action === 'rewrite') { + $gvs->replacePlugin($url); + } - // add a notice of success - $gvs->setNotice('Plugin '. $gvs->plugin_version_short_name .' successfully replaced.', 'success'); + if ($action === 'install') { + $gvs->installPlugin($url); + } // do redirect wp_redirect(get_admin_url() . '?page=gvs_page'); @@ -91,7 +89,9 @@ function gvs_construct_settings_page() $supported_plugins = $gvs->detectSupportedPlugins(); foreach ( $supported_plugins as $plugin_inner_name => $status ) { if ( $status === 'active' ) { - $html .= $gvs->getDownloadInterfaceForm($plugin_inner_name); + $html .= $gvs->getDownloadInterfaceForm($plugin_inner_name, 'rewrite'); + } elseif ($status === 'inactive') { + $html .= $gvs->getDownloadInterfaceForm($plugin_inner_name, 'install'); } } diff --git a/inc/gvs_helper.php b/inc/gvs_helper.php index 94b1f2b..3c13990 100644 --- a/inc/gvs_helper.php +++ b/inc/gvs_helper.php @@ -72,9 +72,9 @@ function gvs_prepare_filesystem() return true; } -function gvs_get_plugin_version_short_name($url) +function gvs_get_plugin_version_short_name($url, $process_plugin) { - $regex_github = '/download\/([a-z,0-9].*)\/' . $this->process_plugin->plugin_slug . '/'; + $regex_github = '/download\/([a-z,0-9].*)\/' . $process_plugin->plugin_slug . '/'; preg_match_all('/plugin\/([a-z,0-9].*\.zip)/', $url,$matches_wp); preg_match_all($regex_github, $url,$matches_github); @@ -82,8 +82,8 @@ function gvs_get_plugin_version_short_name($url) $short = $matches_wp[1][0]; } if (isset($matches_github[1],$matches_github[1][0])) { - $short = $this->process_plugin->inner_name . '-' . $matches_github[1][0]; + $short = $process_plugin->inner_name . '-' . $matches_github[1][0]; } - return !empty($short) ? $short : $this->process_plugin->inner_name; + return !empty($short) ? $short : $process_plugin->inner_name; } diff --git a/lib/GVS/GVS.php b/lib/GVS/GVS.php index 86dd5dd..122184f 100644 --- a/lib/GVS/GVS.php +++ b/lib/GVS/GVS.php @@ -185,6 +185,33 @@ private function getVersionsList($plugin_inner_name) return $versions_found; } + public function replacePlugin($url){ + // run processes + $this->downloadPluginZip($url) + ->unpackZip() + ->prepareDirectories('rewrite') + ->doBackup() + ->replacePluginFiles() + ->deleteTempFiles() + ->saveLogToState(); + + // add a notice of success + $this->setNotice('Plugin '. $this->plugin_version_short_name .' successfully replaced.', 'success'); + } + + public function installPlugin($url){ + // run processes + $this->downloadPluginZip($url) + ->unpackZip() + ->prepareDirectories('install') + ->setPluginFiles() + ->deleteTempFiles() + ->saveLogToState(); + + // add a notice of success + $this->setNotice('Plugin '. $this->plugin_version_short_name .' successfully installed.', 'success'); + } + public function downloadPluginZip($url) { $output_path = $this->process_plugin->new_version_zip_directory; @@ -195,7 +222,7 @@ public function downloadPluginZip($url) throw new \Exception('This URL is not allowed: ' . $url); } - $this->plugin_version_short_name = gvs_get_plugin_version_short_name($url); + $this->plugin_version_short_name = gvs_get_plugin_version_short_name($url, $this->process_plugin); $this->writeStreamLog("Downloading content of $url to $output_path ..."); @@ -266,18 +293,25 @@ public function unpackZip() return $this; } - public function prepareDirectories() + public function prepareDirectories($mode) { // delete if backup path already persists - if ( is_dir($this->process_plugin->backup_plugin_directory) ) { + if ($mode === 'rewrite' && is_dir($this->process_plugin->backup_plugin_directory) ) { gvs_delete_folder_recursive($this->process_plugin->backup_plugin_directory); } // check if active plugin directory exists - if ( !is_dir($this->process_plugin->active_plugin_directory) ) { + if ($mode === 'rewrite' && !is_dir($this->process_plugin->active_plugin_directory) ) { throw new \Exception('Invalid active plugin path'); } + if ($mode === 'install' && !is_dir($this->process_plugin->active_plugin_directory) ) { + $result = mkdir($this->process_plugin->active_plugin_directory); + if (!$result) { + throw new \Exception('Can not create plugin folder.'); + } + } + // prepare filesystem if ( !gvs_prepare_filesystem() ) { throw new \Exception('Can not init WordPress filesystem.'); @@ -295,7 +329,7 @@ public function doBackup() return $this; } - public function replaceActivePlugin() + public function replacePluginFiles() { // enable maintenance mode $this->writeStreamLog('Enabling maintenance mode..'); @@ -305,7 +339,7 @@ public function replaceActivePlugin() gvs_delete_folder_recursive($this->process_plugin->active_plugin_directory); // replace active plugin - $this->writeStreamLog('Replacing active plugin ' . $this->process_plugin->active_plugin_directory); + $this->writeStreamLog('Rewrite active plugin files ' . $this->process_plugin->active_plugin_directory); $result = copy_dir($this->process_plugin->new_version_dir, $this->process_plugin->active_plugin_directory); if ( !$result ) { $this->writeStreamLog('Disabling maintenance mode..'); @@ -315,7 +349,20 @@ public function replaceActivePlugin() $this->writeStreamLog('Disabling maintenance mode..'); gvs_maintenance_mode__disable(); - $this->writeStreamLog('Replaced successfully.'); + $this->writeStreamLog('Rewrote successfully.'); + return $this; + } + + public function setPluginFiles() + { + // replace active plugin + $this->writeStreamLog('Install active plugin ' . $this->process_plugin->active_plugin_directory); + $result = copy_dir($this->process_plugin->new_version_dir, $this->process_plugin->active_plugin_directory); + if ( !$result ) { + throw new \Exception('Can not install active plugin.'); + } + + $this->writeStreamLog('Installed successfully.'); return $this; } @@ -343,17 +390,31 @@ public function deleteTempFiles() * @return array|false|string|string[] * @throws Exception */ - public function getDownloadInterfaceForm($plugin_inner_name) + public function getDownloadInterfaceForm($plugin_inner_name, $action) { $versions = $this->getVersionsList($plugin_inner_name); $html = file_get_contents(GVS_PLUGIN_DIR . '/templates/gvs_form.html'); $options = ''; + $attention = ''; foreach ( $versions as $version ) { $options .= ''; } $html = str_replace('%GVS_OPTIONS%', $options, $html); $html = str_replace('%PLUGIN_INNER_NAME%', $plugin_inner_name, $html); $html = str_replace('%PLUGIN_INNER_NAME_UPPER%', strtoupper($plugin_inner_name), $html); + if ($action === 'rewrite') { + $plugin_action = 'Rewrite'; + $attention = 'WARNING: This action will delete all non-plugin files like .git or .idea. Be sure you know what do you do.'; + $plugin_meta = 'Plugin persists in file system, files will be rewrote: ' . $this->plugins_data[$plugin_inner_name]->active_plugin_directory; + } elseif ($action === 'install') { + $plugin_action = 'Install'; + $plugin_meta = 'Plugin does not persist in file system, files will be placed to: ' . $this->plugins_data[$plugin_inner_name]->active_plugin_directory; + } + if (!empty($plugin_meta)) { + $html = str_replace('%PLUGIN_META%', $plugin_meta, $html); + $html = str_replace('%PLUGIN_ACTION%', $plugin_action, $html); + } + $html = str_replace('%ATTENTION%', $attention, $html); return $html; } diff --git a/templates/gvs_form.html b/templates/gvs_form.html index 8c21b33..36c5642 100644 --- a/templates/gvs_form.html +++ b/templates/gvs_form.html @@ -1,17 +1,21 @@
-

Select %PLUGIN_INNER_NAME_UPPER% version you want to install:

+

Select %PLUGIN_INNER_NAME_UPPER% version you want to handle:

'
+
+

(!) %PLUGIN_META%

+
- + - WARNING: This action will delete all non-plugin files like .git or .idea. Be sure you know what do you do. + %ATTENTION%
+