@@ -185,6 +185,33 @@ private function getVersionsList($plugin_inner_name)
185
185
return $ versions_found ;
186
186
}
187
187
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
+
188
215
public function downloadPluginZip ($ url )
189
216
{
190
217
$ output_path = $ this ->process_plugin ->new_version_zip_directory ;
@@ -195,7 +222,7 @@ public function downloadPluginZip($url)
195
222
throw new \Exception ('This URL is not allowed: ' . $ url );
196
223
}
197
224
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 );
199
226
200
227
$ this ->writeStreamLog ("Downloading content of $ url to $ output_path ... " );
201
228
@@ -266,18 +293,25 @@ public function unpackZip()
266
293
return $ this ;
267
294
}
268
295
269
- public function prepareDirectories ()
296
+ public function prepareDirectories ($ mode )
270
297
{
271
298
// 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 ) ) {
273
300
gvs_delete_folder_recursive ($ this ->process_plugin ->backup_plugin_directory );
274
301
}
275
302
276
303
// 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 ) ) {
278
305
throw new \Exception ('Invalid active plugin path ' );
279
306
}
280
307
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
+
281
315
// prepare filesystem
282
316
if ( !gvs_prepare_filesystem () ) {
283
317
throw new \Exception ('Can not init WordPress filesystem. ' );
@@ -295,7 +329,7 @@ public function doBackup()
295
329
return $ this ;
296
330
}
297
331
298
- public function replaceActivePlugin ()
332
+ public function replacePluginFiles ()
299
333
{
300
334
// enable maintenance mode
301
335
$ this ->writeStreamLog ('Enabling maintenance mode.. ' );
@@ -305,7 +339,7 @@ public function replaceActivePlugin()
305
339
gvs_delete_folder_recursive ($ this ->process_plugin ->active_plugin_directory );
306
340
307
341
// 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 );
309
343
$ result = copy_dir ($ this ->process_plugin ->new_version_dir , $ this ->process_plugin ->active_plugin_directory );
310
344
if ( !$ result ) {
311
345
$ this ->writeStreamLog ('Disabling maintenance mode.. ' );
@@ -315,7 +349,20 @@ public function replaceActivePlugin()
315
349
316
350
$ this ->writeStreamLog ('Disabling maintenance mode.. ' );
317
351
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. ' );
319
366
return $ this ;
320
367
}
321
368
@@ -343,17 +390,31 @@ public function deleteTempFiles()
343
390
* @return array|false|string|string[]
344
391
* @throws Exception
345
392
*/
346
- public function getDownloadInterfaceForm ($ plugin_inner_name )
393
+ public function getDownloadInterfaceForm ($ plugin_inner_name, $ action )
347
394
{
348
395
$ versions = $ this ->getVersionsList ($ plugin_inner_name );
349
396
$ html = file_get_contents (GVS_PLUGIN_DIR . '/templates/gvs_form.html ' );
350
397
$ options = '' ;
398
+ $ attention = '' ;
351
399
foreach ( $ versions as $ version ) {
352
400
$ options .= '<option value=" ' . esc_url ($ version ) . '"> ' . esc_url ($ version ) . '</option> ' ;
353
401
}
354
402
$ html = str_replace ('%GVS_OPTIONS% ' , $ options , $ html );
355
403
$ html = str_replace ('%PLUGIN_INNER_NAME% ' , $ plugin_inner_name , $ html );
356
404
$ 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 );
357
418
358
419
return $ html ;
359
420
}
0 commit comments