pryley /
blackbar
| 1 | <?php |
||
| 2 | |||
| 3 | defined('ABSPATH') || exit; |
||
| 4 | |||
| 5 | /** |
||
| 6 | * Check for minimum system requirements on plugin activation. |
||
| 7 | * @version 6.0.0 |
||
| 8 | */ |
||
| 9 | class GL_Plugin_Check_v6 |
||
| 10 | { |
||
| 11 | const MIN_PHP_VERSION = '7.2'; |
||
| 12 | const MIN_WORDPRESS_VERSION = '5.8'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | public $versions; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $file; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $file |
||
| 26 | */ |
||
| 27 | public function __construct($file) |
||
| 28 | { |
||
| 29 | $this->file = realpath($file); |
||
| 30 | $versionRequirements = get_file_data($this->file, [ |
||
| 31 | 'php' => 'Requires PHP', |
||
| 32 | 'wordpress' => 'Requires at least', |
||
| 33 | ]); |
||
| 34 | $this->versions = wp_parse_args(array_filter($versionRequirements), [ |
||
| 35 | 'php' => static::MIN_PHP_VERSION, |
||
| 36 | 'wordpress' => static::MIN_WORDPRESS_VERSION, |
||
| 37 | ]); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @return bool |
||
| 42 | */ |
||
| 43 | public function canProceed() |
||
| 44 | { |
||
| 45 | if ($this->isValid()) { |
||
| 46 | return true; |
||
| 47 | } |
||
| 48 | add_action('activated_plugin', [$this, 'deactivate']); |
||
| 49 | add_action('admin_notices', [$this, 'deactivate']); |
||
| 50 | return false; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function isPhpValid() |
||
| 57 | { |
||
| 58 | return version_compare(PHP_VERSION, $this->versions['php'], '>='); |
||
|
1 ignored issue
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function isValid() |
||
| 65 | { |
||
| 66 | return $this->isPhpValid() && $this->isWpValid(); |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return bool |
||
| 71 | */ |
||
| 72 | public function isWpValid() |
||
| 73 | { |
||
| 74 | global $wp_version; |
||
| 75 | return version_compare($wp_version, $this->versions['wordpress'], '>='); |
||
|
1 ignored issue
–
show
|
|||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $plugin |
||
| 80 | * @return void |
||
| 81 | */ |
||
| 82 | public function deactivate($plugin) |
||
| 83 | { |
||
| 84 | if ($this->isValid()) { |
||
| 85 | return; |
||
| 86 | } |
||
| 87 | $pluginSlug = plugin_basename($this->file); |
||
| 88 | if ($plugin == $pluginSlug) { |
||
| 89 | $this->redirect(); // exit |
||
| 90 | } |
||
| 91 | $pluginData = get_file_data($this->file, ['name' => 'Plugin Name'], 'plugin'); |
||
| 92 | deactivate_plugins($pluginSlug); |
||
| 93 | $this->printNotice($pluginData['name']); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | protected function getMessages() |
||
| 100 | { |
||
| 101 | return [ |
||
| 102 | 'notice' => _x('The %s plugin was deactivated.', 'admin-text', 'blackbar'), |
||
| 103 | 'php_version' => _x('PHP version', 'admin-text', 'blackbar'), |
||
| 104 | 'rollback' => _x('You can use the %s plugin to restore %s to the previous version.', 'admin-text', 'blackbar'), |
||
| 105 | 'update_php' => _x('Please contact your hosting provider or server administrator to upgrade the version of PHP on your server (your server is running PHP version %s), or try to find an alternative plugin.', 'admin-text', 'blackbar'), |
||
| 106 | 'update_wp' => _x('Update WordPress', 'admin-text', 'blackbar'), |
||
| 107 | 'wp_version' => _x('WordPress version', 'admin-text', 'blackbar'), |
||
| 108 | 'wrong_version' => _x('This plugin requires %s or greater in order to work properly.', 'admin-text', 'blackbar'), |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @param string $pluginName |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | protected function printNotice($pluginName) |
||
| 117 | { |
||
| 118 | $noticeTemplate = '<div id="message" class="notice notice-error error is-dismissible"><p><strong>%s</strong></p><p>%s</p><p>%s</p></div>'; |
||
| 119 | $messages = $this->getMessages(); |
||
| 120 | $rollbackMessage = sprintf('<strong>'.$messages['rollback'].'</strong>', '<a href="https://wordpress.org/plugins/wp-rollback/" target="_blank">WP Rollback</a>', $pluginName); |
||
| 121 | if (!$this->isPhpValid()) { |
||
| 122 | printf($noticeTemplate, |
||
| 123 | sprintf($messages['notice'], $pluginName), |
||
| 124 | sprintf($messages['wrong_version'], $messages['php_version'].' '.$this->versions['php']), |
||
| 125 | sprintf($messages['update_php'], PHP_VERSION).'</p><p>'.$rollbackMessage |
||
| 126 | ); |
||
| 127 | } elseif (!$this->isWpValid()) { |
||
| 128 | printf($noticeTemplate, |
||
| 129 | sprintf($messages['notice'], $pluginName), |
||
| 130 | sprintf($messages['wrong_version'], $messages['wp_version'].' '.$this->versions['wordpress']), |
||
| 131 | $rollbackMessage.'</p><p>'.sprintf('<a href="%s">%s</a>', admin_url('update-core.php'), $messages['update_wp']) |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | protected function redirect() |
||
| 140 | { |
||
| 141 | wp_safe_redirect(self_admin_url(sprintf('plugins.php?plugin_status=%s&paged=%s&s=%s', |
||
| 142 | filter_input(INPUT_GET, 'plugin_status'), |
||
| 143 | filter_input(INPUT_GET, 'paged'), |
||
| 144 | filter_input(INPUT_GET, 's') |
||
| 145 | ))); |
||
| 146 | exit; |
||
| 147 | } |
||
| 148 | } |
||
| 149 |