Skip to content

Commit

Permalink
Plugin installation implemented. Bugs fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandergull committed Aug 18, 2023
1 parent f36cd07 commit 6f4c4d4
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 25 deletions.
20 changes: 10 additions & 10 deletions gvs.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,21 @@ 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 ) {

// set plugin slug as working with
$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');
Expand Down Expand Up @@ -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');
}
}

Expand Down
8 changes: 4 additions & 4 deletions inc/gvs_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ 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);

if (isset($matches_wp[1],$matches_wp[1][0])) {
$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;
}
77 changes: 69 additions & 8 deletions lib/GVS/GVS.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 ...");

Expand Down Expand Up @@ -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.');
Expand All @@ -295,7 +329,7 @@ public function doBackup()
return $this;
}

public function replaceActivePlugin()
public function replacePluginFiles()
{
// enable maintenance mode
$this->writeStreamLog('Enabling maintenance mode..');
Expand All @@ -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..');
Expand All @@ -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;
}

Expand Down Expand Up @@ -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 .= '<option value="'. esc_url($version) . '">' . esc_url($version) . '</option>';
}
$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;
}
Expand Down
10 changes: 7 additions & 3 deletions templates/gvs_form.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
<form name="gvs-select-form" id="gvs-select-%PLUGIN_INNER_NAME%" method="post" action="">
<div style="border-style: groove; margin: 15px; max-width: 70%">
<div id="gvs_version_selector" style="margin: 15px">
<p style="font-size: larger">Select <b>%PLUGIN_INNER_NAME_UPPER%</b> version you want to install:</p>
<p style="font-size: larger">Select <b>%PLUGIN_INNER_NAME_UPPER%</b> version you want to handle:</p>
<select name="gvs_select" style="max-width: 90%">
%GVS_OPTIONS%
</select>'
</div>
<div id="gvs_version_meta" style="margin: 15px">
<p><b>(!) %PLUGIN_META%</b></p>
</div>
<div id="gvs_warning" style="margin: 15px">
<input type="submit" value="Install" class="button-primary">
<input type="submit" value="%PLUGIN_ACTION%" class="button-primary">
<b>
WARNING: This action will delete all non-plugin files like .git or .idea. Be sure you know what do you do.
%ATTENTION%
</b>
</div>
<input type="hidden" name="plugin_inner_name" id="hidden_name_%PLUGIN_INNER_NAME%" value="%PLUGIN_INNER_NAME%">
<input type="hidden" name="plugin_action" id="hidden_action_%PLUGIN_ACTION%" value=%PLUGIN_ACTION%>
</div>
</form>

0 comments on commit 6f4c4d4

Please sign in to comment.