Skip to content

Commit 6f4c4d4

Browse files
committed
Plugin installation implemented. Bugs fixed.
1 parent f36cd07 commit 6f4c4d4

File tree

4 files changed

+90
-25
lines changed

4 files changed

+90
-25
lines changed

gvs.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@ function gvs_main()
4141
// check if supported plugins installed
4242
$work_with_plugin = isset($_POST['plugin_inner_name']) ? $_POST['plugin_inner_name'] : null;
4343
$url = isset($_POST['gvs_select']) ? $_POST['gvs_select'] : null;
44+
$action = isset($_POST['plugin_action']) ? strtolower($_POST['plugin_action']) : null;
4445

4546
if ( $work_with_plugin && $url ) {
4647

4748
// set plugin slug as working with
4849
$gvs->process_plugin = $gvs->plugins_data[$work_with_plugin];
4950

5051
// run processes
51-
$gvs->downloadPluginZip($url)
52-
->unpackZip()
53-
->prepareDirectories()
54-
->doBackup()
55-
->replaceActivePlugin()
56-
->deleteTempFiles()
57-
->saveLogToState();
52+
if ($action === 'rewrite') {
53+
$gvs->replacePlugin($url);
54+
}
5855

59-
// add a notice of success
60-
$gvs->setNotice('Plugin '. $gvs->plugin_version_short_name .' successfully replaced.', 'success');
56+
if ($action === 'install') {
57+
$gvs->installPlugin($url);
58+
}
6159

6260
// do redirect
6361
wp_redirect(get_admin_url() . '?page=gvs_page');
@@ -91,7 +89,9 @@ function gvs_construct_settings_page()
9189
$supported_plugins = $gvs->detectSupportedPlugins();
9290
foreach ( $supported_plugins as $plugin_inner_name => $status ) {
9391
if ( $status === 'active' ) {
94-
$html .= $gvs->getDownloadInterfaceForm($plugin_inner_name);
92+
$html .= $gvs->getDownloadInterfaceForm($plugin_inner_name, 'rewrite');
93+
} elseif ($status === 'inactive') {
94+
$html .= $gvs->getDownloadInterfaceForm($plugin_inner_name, 'install');
9595
}
9696
}
9797

inc/gvs_helper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,18 @@ function gvs_prepare_filesystem()
7272
return true;
7373
}
7474

75-
function gvs_get_plugin_version_short_name($url)
75+
function gvs_get_plugin_version_short_name($url, $process_plugin)
7676
{
77-
$regex_github = '/download\/([a-z,0-9].*)\/' . $this->process_plugin->plugin_slug . '/';
77+
$regex_github = '/download\/([a-z,0-9].*)\/' . $process_plugin->plugin_slug . '/';
7878
preg_match_all('/plugin\/([a-z,0-9].*\.zip)/', $url,$matches_wp);
7979
preg_match_all($regex_github, $url,$matches_github);
8080

8181
if (isset($matches_wp[1],$matches_wp[1][0])) {
8282
$short = $matches_wp[1][0];
8383
}
8484
if (isset($matches_github[1],$matches_github[1][0])) {
85-
$short = $this->process_plugin->inner_name . '-' . $matches_github[1][0];
85+
$short = $process_plugin->inner_name . '-' . $matches_github[1][0];
8686
}
8787

88-
return !empty($short) ? $short : $this->process_plugin->inner_name;
88+
return !empty($short) ? $short : $process_plugin->inner_name;
8989
}

lib/GVS/GVS.php

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,33 @@ private function getVersionsList($plugin_inner_name)
185185
return $versions_found;
186186
}
187187

188+
public function replacePlugin($url){
189+
// run processes
190+
$this->downloadPluginZip($url)
191+
->unpackZip()
192+
->prepareDirectories('rewrite')
193+
->doBackup()
194+
->replacePluginFiles()
195+
->deleteTempFiles()
196+
->saveLogToState();
197+
198+
// add a notice of success
199+
$this->setNotice('Plugin '. $this->plugin_version_short_name .' successfully replaced.', 'success');
200+
}
201+
202+
public function installPlugin($url){
203+
// run processes
204+
$this->downloadPluginZip($url)
205+
->unpackZip()
206+
->prepareDirectories('install')
207+
->setPluginFiles()
208+
->deleteTempFiles()
209+
->saveLogToState();
210+
211+
// add a notice of success
212+
$this->setNotice('Plugin '. $this->plugin_version_short_name .' successfully installed.', 'success');
213+
}
214+
188215
public function downloadPluginZip($url)
189216
{
190217
$output_path = $this->process_plugin->new_version_zip_directory;
@@ -195,7 +222,7 @@ public function downloadPluginZip($url)
195222
throw new \Exception('This URL is not allowed: ' . $url);
196223
}
197224

198-
$this->plugin_version_short_name = gvs_get_plugin_version_short_name($url);
225+
$this->plugin_version_short_name = gvs_get_plugin_version_short_name($url, $this->process_plugin);
199226

200227
$this->writeStreamLog("Downloading content of $url to $output_path ...");
201228

@@ -266,18 +293,25 @@ public function unpackZip()
266293
return $this;
267294
}
268295

269-
public function prepareDirectories()
296+
public function prepareDirectories($mode)
270297
{
271298
// delete if backup path already persists
272-
if ( is_dir($this->process_plugin->backup_plugin_directory) ) {
299+
if ($mode === 'rewrite' && is_dir($this->process_plugin->backup_plugin_directory) ) {
273300
gvs_delete_folder_recursive($this->process_plugin->backup_plugin_directory);
274301
}
275302

276303
// check if active plugin directory exists
277-
if ( !is_dir($this->process_plugin->active_plugin_directory) ) {
304+
if ($mode === 'rewrite' && !is_dir($this->process_plugin->active_plugin_directory) ) {
278305
throw new \Exception('Invalid active plugin path');
279306
}
280307

308+
if ($mode === 'install' && !is_dir($this->process_plugin->active_plugin_directory) ) {
309+
$result = mkdir($this->process_plugin->active_plugin_directory);
310+
if (!$result) {
311+
throw new \Exception('Can not create plugin folder.');
312+
}
313+
}
314+
281315
// prepare filesystem
282316
if ( !gvs_prepare_filesystem() ) {
283317
throw new \Exception('Can not init WordPress filesystem.');
@@ -295,7 +329,7 @@ public function doBackup()
295329
return $this;
296330
}
297331

298-
public function replaceActivePlugin()
332+
public function replacePluginFiles()
299333
{
300334
// enable maintenance mode
301335
$this->writeStreamLog('Enabling maintenance mode..');
@@ -305,7 +339,7 @@ public function replaceActivePlugin()
305339
gvs_delete_folder_recursive($this->process_plugin->active_plugin_directory);
306340

307341
// replace active plugin
308-
$this->writeStreamLog('Replacing active plugin ' . $this->process_plugin->active_plugin_directory);
342+
$this->writeStreamLog('Rewrite active plugin files ' . $this->process_plugin->active_plugin_directory);
309343
$result = copy_dir($this->process_plugin->new_version_dir, $this->process_plugin->active_plugin_directory);
310344
if ( !$result ) {
311345
$this->writeStreamLog('Disabling maintenance mode..');
@@ -315,7 +349,20 @@ public function replaceActivePlugin()
315349

316350
$this->writeStreamLog('Disabling maintenance mode..');
317351
gvs_maintenance_mode__disable();
318-
$this->writeStreamLog('Replaced successfully.');
352+
$this->writeStreamLog('Rewrote successfully.');
353+
return $this;
354+
}
355+
356+
public function setPluginFiles()
357+
{
358+
// replace active plugin
359+
$this->writeStreamLog('Install active plugin ' . $this->process_plugin->active_plugin_directory);
360+
$result = copy_dir($this->process_plugin->new_version_dir, $this->process_plugin->active_plugin_directory);
361+
if ( !$result ) {
362+
throw new \Exception('Can not install active plugin.');
363+
}
364+
365+
$this->writeStreamLog('Installed successfully.');
319366
return $this;
320367
}
321368

@@ -343,17 +390,31 @@ public function deleteTempFiles()
343390
* @return array|false|string|string[]
344391
* @throws Exception
345392
*/
346-
public function getDownloadInterfaceForm($plugin_inner_name)
393+
public function getDownloadInterfaceForm($plugin_inner_name, $action)
347394
{
348395
$versions = $this->getVersionsList($plugin_inner_name);
349396
$html = file_get_contents(GVS_PLUGIN_DIR . '/templates/gvs_form.html');
350397
$options = '';
398+
$attention = '';
351399
foreach ( $versions as $version ) {
352400
$options .= '<option value="'. esc_url($version) . '">' . esc_url($version) . '</option>';
353401
}
354402
$html = str_replace('%GVS_OPTIONS%', $options, $html);
355403
$html = str_replace('%PLUGIN_INNER_NAME%', $plugin_inner_name, $html);
356404
$html = str_replace('%PLUGIN_INNER_NAME_UPPER%', strtoupper($plugin_inner_name), $html);
405+
if ($action === 'rewrite') {
406+
$plugin_action = 'Rewrite';
407+
$attention = 'WARNING: This action will delete all non-plugin files like .git or .idea. Be sure you know what do you do.';
408+
$plugin_meta = 'Plugin persists in file system, files will be rewrote: ' . $this->plugins_data[$plugin_inner_name]->active_plugin_directory;
409+
} elseif ($action === 'install') {
410+
$plugin_action = 'Install';
411+
$plugin_meta = 'Plugin does not persist in file system, files will be placed to: ' . $this->plugins_data[$plugin_inner_name]->active_plugin_directory;
412+
}
413+
if (!empty($plugin_meta)) {
414+
$html = str_replace('%PLUGIN_META%', $plugin_meta, $html);
415+
$html = str_replace('%PLUGIN_ACTION%', $plugin_action, $html);
416+
}
417+
$html = str_replace('%ATTENTION%', $attention, $html);
357418

358419
return $html;
359420
}

templates/gvs_form.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
<form name="gvs-select-form" id="gvs-select-%PLUGIN_INNER_NAME%" method="post" action="">
22
<div style="border-style: groove; margin: 15px; max-width: 70%">
33
<div id="gvs_version_selector" style="margin: 15px">
4-
<p style="font-size: larger">Select <b>%PLUGIN_INNER_NAME_UPPER%</b> version you want to install:</p>
4+
<p style="font-size: larger">Select <b>%PLUGIN_INNER_NAME_UPPER%</b> version you want to handle:</p>
55
<select name="gvs_select" style="max-width: 90%">
66
%GVS_OPTIONS%
77
</select>'
88
</div>
9+
<div id="gvs_version_meta" style="margin: 15px">
10+
<p><b>(!) %PLUGIN_META%</b></p>
11+
</div>
912
<div id="gvs_warning" style="margin: 15px">
10-
<input type="submit" value="Install" class="button-primary">
13+
<input type="submit" value="%PLUGIN_ACTION%" class="button-primary">
1114
<b>
12-
WARNING: This action will delete all non-plugin files like .git or .idea. Be sure you know what do you do.
15+
%ATTENTION%
1316
</b>
1417
</div>
1518
<input type="hidden" name="plugin_inner_name" id="hidden_name_%PLUGIN_INNER_NAME%" value="%PLUGIN_INNER_NAME%">
19+
<input type="hidden" name="plugin_action" id="hidden_action_%PLUGIN_ACTION%" value=%PLUGIN_ACTION%>
1620
</div>
1721
</form>

0 commit comments

Comments
 (0)