| 1 |
<?php |
| 2 |
/* |
| 3 |
* SOFTWARE LICENSE INFORMATION |
| 4 |
* |
| 5 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 6 |
* |
| 7 |
* This file is part of Buttonizer |
| 8 |
* |
| 9 |
* For detailed information regarding to the licensing of |
| 10 |
* this software, please review the license.txt or visit: |
| 11 |
* https://buttonizer.pro/license/ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Buttonizer\Migration; |
| 15 |
|
| 16 |
# No script kiddies |
| 17 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 18 |
|
| 19 |
/** |
| 20 |
* Takes the source plugin out of the way once Buttonizer can serve its users. |
| 21 |
* |
| 22 |
* Deactivating is deliberately silent and never deletes anything: the options |
| 23 |
* stay in place, the backup stays in place, and the user can reactivate the |
| 24 |
* plugin if something went wrong. |
| 25 |
*/ |
| 26 |
class Handoff |
| 27 |
{ |
| 28 |
/** Plugin was deactivated by us. */ |
| 29 |
const RESULT_DEACTIVATED = 'deactivated'; |
| 30 |
|
| 31 |
/** Plugin was not active to begin with. */ |
| 32 |
const RESULT_NOT_ACTIVE = 'not_active'; |
| 33 |
|
| 34 |
/** Current user may not deactivate plugins. */ |
| 35 |
const RESULT_NO_PERMISSION = 'no_permission'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Deactivate the source plugin. |
| 39 |
* |
| 40 |
* @param SourcePlugin $source Source plugin descriptor. |
| 41 |
* |
| 42 |
* @return array{deactivated: bool, result: string} |
| 43 |
*/ |
| 44 |
public static function deactivateSource(SourcePlugin $source): array |
| 45 |
{ |
| 46 |
if (!self::currentUserCanManagePlugins()) { |
| 47 |
return ['deactivated' => false, 'result' => self::RESULT_NO_PERMISSION]; |
| 48 |
} |
| 49 |
|
| 50 |
// Deep scan allowed here: we are committed to this migration, so it is |
| 51 |
// worth finding the plugin even under an unexpected folder name. |
| 52 |
$baseName = $source->resolveBaseName(true); |
| 53 |
|
| 54 |
if (!$baseName || !$source->isActive()) { |
| 55 |
return ['deactivated' => false, 'result' => self::RESULT_NOT_ACTIVE]; |
| 56 |
} |
| 57 |
|
| 58 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 59 |
|
| 60 |
// Silent: the source plugin's own deactivation hooks must not run, this |
| 61 |
// is a handover and not the user walking away from the product. |
| 62 |
deactivate_plugins($baseName, true); |
| 63 |
|
| 64 |
return ['deactivated' => true, 'result' => self::RESULT_DEACTIVATED]; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Send the source plugin's admin page to Buttonizer. |
| 69 |
* |
| 70 |
* Bookmarks and old menu links keep working after the plugin is gone, |
| 71 |
* the same way the acquired plugin already redirects its own pre-Buttonizer |
| 72 |
* settings page. |
| 73 |
* |
| 74 |
* @param SourcePlugin $source Source plugin descriptor. |
| 75 |
*/ |
| 76 |
public static function redirectSourcePage(SourcePlugin $source): void |
| 77 |
{ |
| 78 |
$pageSlug = $source->pageSlug(); |
| 79 |
|
| 80 |
if (!$pageSlug || !isset($_GET['page']) || $_GET['page'] !== $pageSlug) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
// While the source plugin is still active it serves its own page |
| 85 |
if ($source->isActive()) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
wp_safe_redirect(admin_url('admin.php?page=' . \Buttonizer\Core\PluginConfig::pageSlug())); |
| 90 |
exit; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* May the current user activate/deactivate plugins? |
| 95 |
*/ |
| 96 |
public static function currentUserCanManagePlugins(): bool |
| 97 |
{ |
| 98 |
return current_user_can(is_multisite() ? 'manage_options' : 'activate_plugins'); |
| 99 |
} |
| 100 |
} |
| 101 |
|