| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to check if the current WordPress and PHP versions meet our requirements. |
| 4 |
* |
| 5 |
* @since 1.9 |
| 6 |
* @source Based on class WP_Rocket_Requirements_Check from WPÂ Rocket plugin. |
| 7 |
* @author Grégory Viguier |
| 8 |
* @author Remy Perona |
| 9 |
*/ |
| 10 |
class Imagify_Requirements_Check { |
| 11 |
/** |
| 12 |
* Plugin Name. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
* @since 1.9 |
| 16 |
* @access private |
| 17 |
* @author Grégory Viguier |
| 18 |
*/ |
| 19 |
private $plugin_name; |
| 20 |
|
| 21 |
/** |
| 22 |
* Plugin filepath. |
| 23 |
* |
| 24 |
* @var string |
| 25 |
* @since 1.9 |
| 26 |
* @access private |
| 27 |
* @author Grégory Viguier |
| 28 |
*/ |
| 29 |
private $plugin_file; |
| 30 |
|
| 31 |
/** |
| 32 |
* Plugin version. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @since 1.9 |
| 36 |
* @access private |
| 37 |
* @author Grégory Viguier |
| 38 |
*/ |
| 39 |
private $plugin_version; |
| 40 |
|
| 41 |
/** |
| 42 |
* Last plugin version handling the current version of WP. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
* @since 1.9 |
| 46 |
* @access private |
| 47 |
* @author Grégory Viguier |
| 48 |
*/ |
| 49 |
private $wp_last_version; |
| 50 |
|
| 51 |
/** |
| 52 |
* Last plugin version handling the current version of PHP. |
| 53 |
* |
| 54 |
* @var string |
| 55 |
* @since 1.9 |
| 56 |
* @access private |
| 57 |
* @author Grégory Viguier |
| 58 |
*/ |
| 59 |
private $php_last_version; |
| 60 |
|
| 61 |
/** |
| 62 |
* Required WordPress version. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
* @since 1.9 |
| 66 |
* @access private |
| 67 |
* @author Grégory Viguier |
| 68 |
*/ |
| 69 |
private $wp_version; |
| 70 |
|
| 71 |
/** |
| 72 |
* Required PHP version. |
| 73 |
* |
| 74 |
* @var string |
| 75 |
* @since 1.9 |
| 76 |
* @access private |
| 77 |
* @author Grégory Viguier |
| 78 |
*/ |
| 79 |
private $php_version; |
| 80 |
|
| 81 |
/** |
| 82 |
* Constructor. |
| 83 |
* |
| 84 |
* @since 1.9 |
| 85 |
* @access public |
| 86 |
* @author Grégory Viguier |
| 87 |
* |
| 88 |
* @param array $args { |
| 89 |
* Arguments to populate the class properties. |
| 90 |
* |
| 91 |
* @type string $plugin_name Plugin name. |
| 92 |
* @type string $plugin_file Plugin filepath. |
| 93 |
* @type string $plugin_version Plugin version. |
| 94 |
* @type string $wp_last_version Last plugin version handling the current version of WP. |
| 95 |
* @type string $php_last_version Last plugin version handling the current version of PHP. |
| 96 |
* @type string $wp_version Required WordPress version. |
| 97 |
* @type string $php_version Required PHP version. |
| 98 |
* } |
| 99 |
*/ |
| 100 |
public function __construct( $args ) { |
| 101 |
foreach ( [ 'plugin_name', 'plugin_file', 'plugin_version', 'wp_last_version', 'php_last_version', 'wp_version', 'php_version' ] as $setting ) { |
| 102 |
if ( isset( $args[ $setting ] ) ) { |
| 103 |
$this->$setting = $args[ $setting ]; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if ( empty( $this->wp_last_version ) ) { |
| 108 |
$this->wp_last_version = '1.6.14.2'; |
| 109 |
} |
| 110 |
|
| 111 |
if ( empty( $this->php_last_version ) ) { |
| 112 |
$this->php_last_version = '1.8.4.1'; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Check if all requirements are ok, if not, display a notice and the rollback. |
| 118 |
* |
| 119 |
* @since 1.9 |
| 120 |
* @access public |
| 121 |
* @author Grégory Viguier |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public function check() { |
| 126 |
if ( ! $this->php_passes() || ! $this->wp_passes() ) { |
| 127 |
add_action( 'admin_notices', [ $this, 'print_notice' ] ); |
| 128 |
add_action( 'admin_post_imagify_rollback', [ $this, 'rollback' ] ); |
| 129 |
|
| 130 |
return false; |
| 131 |
} |
| 132 |
|
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Check if the current PHP version is equal or superior to the required PHP version. |
| 138 |
* |
| 139 |
* @since 1.9 |
| 140 |
* @access private |
| 141 |
* @author Grégory Viguier |
| 142 |
* |
| 143 |
* @return bool |
| 144 |
*/ |
| 145 |
private function php_passes() { |
| 146 |
return version_compare( PHP_VERSION, $this->php_version ) >= 0; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Check if the current WordPress version is equal or superior to the required PHP version. |
| 151 |
* |
| 152 |
* @since 1.9 |
| 153 |
* @access private |
| 154 |
* @author Grégory Viguier |
| 155 |
* |
| 156 |
* @return bool |
| 157 |
*/ |
| 158 |
private function wp_passes() { |
| 159 |
global $wp_version; |
| 160 |
|
| 161 |
return version_compare( $wp_version, $this->wp_version ) >= 0; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Get the last version of the plugin that can run with the current WP and PHP versions. |
| 166 |
* |
| 167 |
* @since 1.9 |
| 168 |
* @access private |
| 169 |
* @author Grégory Viguier |
| 170 |
* |
| 171 |
* @return string |
| 172 |
*/ |
| 173 |
private function get_last_version() { |
| 174 |
$last_version = ''; |
| 175 |
|
| 176 |
if ( ! $this->php_passes() ) { |
| 177 |
$last_version = $this->php_last_version; |
| 178 |
} |
| 179 |
|
| 180 |
if ( ! $this->wp_passes() ) { |
| 181 |
$last_version = ! $last_version || version_compare( $last_version, $this->wp_last_version ) > 0 ? $this->wp_last_version : $last_version; |
| 182 |
} |
| 183 |
|
| 184 |
return $last_version; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Tell if the current user can rollback. |
| 189 |
* |
| 190 |
* @since 1.9 |
| 191 |
* @access private |
| 192 |
* @author Grégory Viguier |
| 193 |
* |
| 194 |
* @return bool |
| 195 |
*/ |
| 196 |
private function current_user_can() { |
| 197 |
$describer = 'manage'; |
| 198 |
$capacity = $this->is_active_for_network() ? 'manage_network_options' : 'manage_options'; |
| 199 |
// This filter is documented in classes/Context/AbstractContext.php. |
| 200 |
$capacity = (string) apply_filters( 'imagify_capacity', $capacity, $describer, 'wp' ); |
| 201 |
|
| 202 |
$user_can = current_user_can( $capacity ); |
| 203 |
// This filter is documented in classes/Context/AbstractContext.php. |
| 204 |
$user_can = (bool) apply_filters( 'imagify_current_user_can', $user_can, $capacity, $describer, null, 'wp' ); |
| 205 |
|
| 206 |
return $user_can; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Tell if Imagify is activated on the network. |
| 211 |
* |
| 212 |
* @since 1.9 |
| 213 |
* @access private |
| 214 |
* @author Grégory Viguier |
| 215 |
* |
| 216 |
* return bool True if Imagify is activated on the network. |
| 217 |
*/ |
| 218 |
private function is_active_for_network() { |
| 219 |
if ( ! is_multisite() ) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
if ( ! function_exists( 'is_plugin_active_for_network' ) ) { |
| 224 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 225 |
} |
| 226 |
|
| 227 |
return is_plugin_active_for_network( plugin_basename( $this->plugin_file ) ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Warn if PHP version is less than 5.4 and offers to rollback. |
| 232 |
* |
| 233 |
* @since 1.9 |
| 234 |
*/ |
| 235 |
public function print_notice() { |
| 236 |
if ( ! $this->current_user_can() ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
$message = []; |
| 241 |
$required = []; |
| 242 |
$rollback_url = wp_nonce_url( admin_url( 'admin-post.php?action=imagify_rollback' ), 'imagify_rollback' ); |
| 243 |
|
| 244 |
if ( ! $this->php_passes() ) { |
| 245 |
/* translators: %1$s = Plugin name, %2$s = PHP version required. */ |
| 246 |
$message[] = sprintf( esc_html__( 'To use this %1$s version, please ask your web host how to upgrade your server to PHP %2$s or higher.', 'imagify' ), $this->plugin_name, $this->php_version ); |
| 247 |
$required[] = 'PHP ' . $this->php_version; |
| 248 |
} |
| 249 |
|
| 250 |
if ( ! $this->wp_passes() ) { |
| 251 |
/* translators: %1$s = Plugin name, %2$s = WordPress version required. */ |
| 252 |
$message[] = sprintf( esc_html__( 'To use this %1$s version, please upgrade WordPress to version %2$s or higher.', 'imagify' ), $this->plugin_name, $this->wp_version ); |
| 253 |
$required[] = 'WordPress ' . $this->wp_version; |
| 254 |
} |
| 255 |
|
| 256 |
$message = '<p>' . implode( '<br/>', $message ) . "</p>\n"; |
| 257 |
$required = wp_sprintf_l( '%l', $required ); |
| 258 |
|
| 259 |
/* translators: %1$s = Plugin name, %2$s = Plugin version, $3$s is something like "PHP 5.4" or "PHP 5.4 and WordPress 4.0". */ |
| 260 |
$message = '<p>' . sprintf( esc_html__( 'To function properly, %1$s %2$s requires at least %3$s.', 'imagify' ), '<strong>' . $this->plugin_name . '</strong>', $this->plugin_version, $required ) . "</p>\n" . $message; |
| 261 |
|
| 262 |
$message .= '<p>' . esc_html__( 'If you are not able to upgrade, you can rollback to the previous version by using the button below.', 'imagify' ) . "</p>\n"; |
| 263 |
/* translators: %s = Previous plugin version. */ |
| 264 |
$message .= '<p class="submit"><a href="' . esc_url( $rollback_url ) . '" class="button">' . sprintf( __( 'Re-install version %s', 'imagify' ), $this->get_last_version() ) . '</a></p>'; |
| 265 |
|
| 266 |
echo '<div class="notice notice-error">' . $message . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Do the rollback. |
| 271 |
* |
| 272 |
* @since 1.9 |
| 273 |
*/ |
| 274 |
public function rollback() { |
| 275 |
check_ajax_referer( 'imagify_rollback' ); |
| 276 |
|
| 277 |
if ( ! $this->current_user_can() ) { |
| 278 |
wp_die(); |
| 279 |
} |
| 280 |
|
| 281 |
$plugin_transient = get_site_transient( 'update_plugins' ); |
| 282 |
$plugin_basename = plugin_basename( $this->plugin_file ); |
| 283 |
$plugin_folder = dirname( $plugin_basename ); |
| 284 |
$last_version = $this->get_last_version(); |
| 285 |
$package_filename = $plugin_folder . '.' . $last_version . '.zip'; |
| 286 |
|
| 287 |
$plugin_transient->checked[ $plugin_basename ] = $last_version; |
| 288 |
|
| 289 |
if ( ! empty( $plugin_transient->response[ $plugin_basename ] ) ) { |
| 290 |
$tmp_obj = $plugin_transient->response[ $plugin_basename ]; |
| 291 |
} elseif ( ! empty( $plugin_transient->no_update[ $plugin_basename ] ) ) { |
| 292 |
$tmp_obj = $plugin_transient->no_update[ $plugin_basename ]; |
| 293 |
} else { |
| 294 |
$tmp_obj = (object) [ |
| 295 |
'id' => 'w.org/plugins/' . $plugin_folder, |
| 296 |
'slug' => $plugin_folder, |
| 297 |
'plugin' => $plugin_basename, |
| 298 |
'new_version' => $last_version, |
| 299 |
'url' => 'https://wordpress.org/plugins/' . $plugin_folder . '/', |
| 300 |
'package' => 'https://downloads.wordpress.org/plugin/' . $package_filename, |
| 301 |
'icons' => [], |
| 302 |
'banners' => [], |
| 303 |
'banners_rtl' => [], |
| 304 |
]; |
| 305 |
} |
| 306 |
|
| 307 |
$tmp_obj->new_version = $last_version; |
| 308 |
$tmp_obj->package = preg_replace( '@/[^/]+$@', '/' . $package_filename, $tmp_obj->package ); |
| 309 |
|
| 310 |
$plugin_transient->response[ $plugin_basename ] = $tmp_obj; |
| 311 |
unset( $plugin_transient->no_update[ $plugin_basename ] ); |
| 312 |
|
| 313 |
set_site_transient( 'update_plugins', $plugin_transient ); |
| 314 |
|
| 315 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 316 |
|
| 317 |
/* translators: %s is the plugin name. */ |
| 318 |
$title = sprintf( __( '%s Update Rollback', 'imagify' ), $this->plugin_name ); |
| 319 |
$nonce = 'upgrade-plugin_' . $plugin_basename; |
| 320 |
$url = 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $plugin_basename ); |
| 321 |
$upgrader_skin = new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) ); |
| 322 |
$upgrader = new Plugin_Upgrader( $upgrader_skin ); |
| 323 |
|
| 324 |
$upgrader->upgrade( $plugin_basename ); |
| 325 |
|
| 326 |
wp_die( |
| 327 |
'', |
| 328 |
// translators: %s is the plugin name. |
| 329 |
sprintf( esc_html__( '%s Update Rollback', 'imagify' ), esc_html( $this->plugin_name ) ), |
| 330 |
[ 'response' => 200 ] |
| 331 |
); |
| 332 |
} |
| 333 |
} |
| 334 |
|