wp-smushit
Last commit date
_src
2 years ago
app
2 years ago
core
2 years ago
languages
2 years ago
vendor
2 years ago
readme.txt
2 years ago
uninstall.php
2 years ago
wp-smush.php
2 years ago
wp-smush.php
624 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WP Smush plugin |
| 4 | * |
| 5 | * Reduce image file sizes, improve performance and boost your SEO using the free |
| 6 | * <a href="https://wpmudev.com/">WPMU DEV</a> WordPress Smush API. |
| 7 | * |
| 8 | * @link http://wpmudev.com/project/wp-smush-pro/ |
| 9 | * @since 1.0.0 |
| 10 | * @package WP_Smush |
| 11 | * |
| 12 | * @wordpress-plugin |
| 13 | * Plugin Name: Smush |
| 14 | * Plugin URI: http://wordpress.org/plugins/wp-smushit/ |
| 15 | * Description: Reduce image file sizes, improve performance and boost your SEO using the free <a href="https://wpmudev.com/">WPMU DEV</a> WordPress Smush API. |
| 16 | * Version: 3.16.4 |
| 17 | * Requires PHP: 7.4 |
| 18 | * Author: WPMU DEV |
| 19 | * Author URI: https://profiles.wordpress.org/wpmudev/ |
| 20 | * License: GPLv2 |
| 21 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 22 | * Text Domain: wp-smushit |
| 23 | * Domain Path: /languages/ |
| 24 | * Network: true |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | This plugin was originally developed by Alex Dunae (http://dialect.ca/). |
| 29 | |
| 30 | Copyright 2007-2022 Incsub (http://incsub.com) |
| 31 | |
| 32 | This program is free software; you can redistribute it and/or modify |
| 33 | it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by |
| 34 | the Free Software Foundation. |
| 35 | |
| 36 | This program is distributed in the hope that it will be useful, |
| 37 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 38 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 39 | GNU General Public License for more details. |
| 40 | |
| 41 | You should have received a copy of the GNU General Public License |
| 42 | along with this program; if not, write to the Free Software |
| 43 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 44 | */ |
| 45 | |
| 46 | // If this file is called directly, abort. |
| 47 | if ( ! defined( 'WPINC' ) ) { |
| 48 | die; |
| 49 | } |
| 50 | |
| 51 | if ( ! defined( 'WP_SMUSH_VERSION' ) ) { |
| 52 | define( 'WP_SMUSH_VERSION', '3.16.4' ); |
| 53 | } |
| 54 | // Used to define body class. |
| 55 | if ( ! defined( 'WP_SHARED_UI_VERSION' ) ) { |
| 56 | define( 'WP_SHARED_UI_VERSION', 'sui-2-12-23' ); |
| 57 | } |
| 58 | if ( ! defined( 'WP_SMUSH_BASENAME' ) ) { |
| 59 | define( 'WP_SMUSH_BASENAME', plugin_basename( __FILE__ ) ); |
| 60 | } |
| 61 | if ( ! defined( 'WP_SMUSH_API' ) ) { |
| 62 | define( 'WP_SMUSH_API', 'https://smushpro.wpmudev.com/1.0/' ); |
| 63 | } |
| 64 | if ( ! defined( 'WP_SMUSH_UA' ) ) { |
| 65 | define( 'WP_SMUSH_UA', 'WP Smush/' . WP_SMUSH_VERSION . '; ' . network_home_url() ); |
| 66 | } |
| 67 | if ( ! defined( 'WP_SMUSH_DIR' ) ) { |
| 68 | define( 'WP_SMUSH_DIR', plugin_dir_path( __FILE__ ) ); |
| 69 | } |
| 70 | if ( ! defined( 'WP_SMUSH_URL' ) ) { |
| 71 | define( 'WP_SMUSH_URL', plugin_dir_url( __FILE__ ) ); |
| 72 | } |
| 73 | if ( ! defined( 'WP_SMUSH_MAX_BYTES' ) ) { |
| 74 | define( 'WP_SMUSH_MAX_BYTES', 5242880 ); // 5MB |
| 75 | } |
| 76 | if ( ! defined( 'WP_SMUSH_PREMIUM_MAX_BYTES' ) ) { |
| 77 | define( 'WP_SMUSH_PREMIUM_MAX_BYTES', 268435456 ); |
| 78 | } |
| 79 | if ( ! defined( 'WP_SMUSH_TIMEOUT' ) ) { |
| 80 | define( 'WP_SMUSH_TIMEOUT', 420 ); // 7 minutes |
| 81 | } |
| 82 | if ( ! defined( 'WP_SMUSH_RETRY_ATTEMPTS' ) ) { |
| 83 | define( 'WP_SMUSH_RETRY_ATTEMPTS', 3 ); |
| 84 | } |
| 85 | if ( ! defined( 'WP_SMUSH_RETRY_WAIT' ) ) { |
| 86 | define( 'WP_SMUSH_RETRY_WAIT', 1 ); |
| 87 | } |
| 88 | if ( ! defined( 'WP_SMUSH_PARALLEL' ) ) { |
| 89 | define( 'WP_SMUSH_PARALLEL', true ); |
| 90 | } |
| 91 | if ( ! defined( 'WP_SMUSH_BACKGROUND' ) ) { |
| 92 | define( 'WP_SMUSH_BACKGROUND', true ); |
| 93 | } |
| 94 | |
| 95 | if ( ! defined( 'WP_SMUSH_MIN_PHP_VERSION' ) ) { |
| 96 | define( 'WP_SMUSH_MIN_PHP_VERSION', '7.4' ); |
| 97 | } |
| 98 | |
| 99 | if ( version_compare( PHP_VERSION, WP_SMUSH_MIN_PHP_VERSION, '<' ) ) { |
| 100 | if ( ! function_exists( 'wp_smush_php_deprecated_notice' ) ) { |
| 101 | function wp_smush_php_deprecated_notice() { |
| 102 | // Only show the deprecated notice for admin and only network side for MU site. |
| 103 | if ( ! current_user_can( 'manage_options' ) || ( is_multisite() && ! is_network_admin() ) ) { |
| 104 | return; |
| 105 | } |
| 106 | ?> |
| 107 | <div class="notice notice-error is-dismissible"> |
| 108 | <p> |
| 109 | <?php |
| 110 | printf( |
| 111 | wp_kses( /* translators: %1$s - Requires PHP version - %2$s URL to an article about our hosting benefits. */ |
| 112 | __( 'Your site is running an outdated version of PHP that is no longer supported or receiving security updates. Please update PHP to at least version %1$s at your hosting provider in order to activate Smush, or consider switching to <a href="%2$s" target="_blank" rel="noopener noreferrer">WPMU DEV Hosting</a>.', 'wp-smushit' ), |
| 113 | array( |
| 114 | 'a' => array( |
| 115 | 'href' => array(), |
| 116 | 'target' => array(), |
| 117 | 'rel' => array(), |
| 118 | ), |
| 119 | 'strong' => array(), |
| 120 | ) |
| 121 | ), |
| 122 | esc_html( WP_SMUSH_MIN_PHP_VERSION ), |
| 123 | 'https://wpmudev.com/hosting/' |
| 124 | ); |
| 125 | ?> |
| 126 | </p> |
| 127 | </div> |
| 128 | <?php |
| 129 | |
| 130 | // Deactivate plugin. |
| 131 | deactivate_plugins( WP_SMUSH_BASENAME, false, is_network_admin() ); |
| 132 | } |
| 133 | } |
| 134 | add_action( 'admin_notices', 'wp_smush_php_deprecated_notice' ); |
| 135 | add_action( 'network_admin_notices', 'wp_smush_php_deprecated_notice' ); |
| 136 | return; |
| 137 | } |
| 138 | /** |
| 139 | * To support Smushing on staging sites like SiteGround staging where staging site urls are different |
| 140 | * but redirects to main site url. Remove the protocols and www, and get the domain name.* |
| 141 | * If Set to false, WP Smush switch backs to the Old Sync Optimisation. |
| 142 | */ |
| 143 | $site_url = str_replace( array( 'http://', 'https://', 'www.' ), '', site_url() ); |
| 144 | // Compat with WPMU DEV staging. |
| 145 | $wpmu_host = isset( $_SERVER['WPMUDEV_HOSTING_ENV'] ) && 'staging' === sanitize_text_field( wp_unslash( $_SERVER['WPMUDEV_HOSTING_ENV'] ) ); |
| 146 | if ( ! defined( 'WP_SMUSH_ASYNC' ) ) { |
| 147 | if ( ( ! empty( $_SERVER['SERVER_NAME'] ) && 0 !== strpos( $site_url, sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) ) ) ) || $wpmu_host ) { |
| 148 | define( 'WP_SMUSH_ASYNC', false ); |
| 149 | } else { |
| 150 | define( 'WP_SMUSH_ASYNC', true ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * If we are activating a version, while having another present and activated. |
| 156 | * Leave in the Pro version, if it is available. |
| 157 | * |
| 158 | * @since 2.9.1 |
| 159 | */ |
| 160 | if ( WP_SMUSH_BASENAME !== plugin_basename( __FILE__ ) ) { |
| 161 | $pro_installed = false; |
| 162 | if ( file_exists( WP_PLUGIN_DIR . '/wp-smush-pro/wp-smush.php' ) ) { |
| 163 | $pro_installed = true; |
| 164 | } |
| 165 | |
| 166 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 167 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 168 | } |
| 169 | |
| 170 | if ( is_plugin_active( 'wp-smush-pro/wp-smush.php' ) ) { |
| 171 | deactivate_plugins( plugin_basename( __FILE__ ) ); |
| 172 | update_site_option( 'smush_deactivated', 1 ); |
| 173 | return; // Return to avoid errors with free-dashboard module. |
| 174 | } elseif ( $pro_installed && is_plugin_active( WP_SMUSH_BASENAME ) ) { |
| 175 | deactivate_plugins( WP_SMUSH_BASENAME ); |
| 176 | // If WordPress is already in the process of activating - return. |
| 177 | if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) { |
| 178 | return; |
| 179 | } |
| 180 | activate_plugin( plugin_basename( __FILE__ ) ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* @noinspection PhpIncludeInspection */ |
| 185 | require_once WP_SMUSH_DIR . 'core/class-installer.php'; |
| 186 | register_activation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_activated' ) ); |
| 187 | register_deactivation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_deactivated' ) ); |
| 188 | |
| 189 | register_activation_hook( __FILE__, function () { |
| 190 | update_option( 'wp-smush-plugin-activated', true ); |
| 191 | } ); |
| 192 | |
| 193 | // Init the plugin and load the plugin instance for the first time. |
| 194 | add_action( 'plugins_loaded', array( 'WP_Smush', 'get_instance' ) ); |
| 195 | |
| 196 | if ( ! class_exists( 'WP_Smush' ) ) { |
| 197 | /** |
| 198 | * Class WP_Smush |
| 199 | */ |
| 200 | final class WP_Smush { |
| 201 | |
| 202 | /** |
| 203 | * Plugin instance. |
| 204 | * |
| 205 | * @since 2.9.0 |
| 206 | * @var null|WP_Smush |
| 207 | */ |
| 208 | private static $instance = null; |
| 209 | |
| 210 | /** |
| 211 | * Plugin core. |
| 212 | * |
| 213 | * @since 2.9.0 |
| 214 | * @var Smush\Core\Core |
| 215 | */ |
| 216 | private $core; |
| 217 | |
| 218 | /** |
| 219 | * Plugin admin. |
| 220 | * |
| 221 | * @since 2.9.0 |
| 222 | * @var Smush\App\Admin |
| 223 | */ |
| 224 | private $admin; |
| 225 | |
| 226 | /** |
| 227 | * Plugin API. |
| 228 | * |
| 229 | * @since 3.0 |
| 230 | * @var Smush\Core\Api\Smush_API |
| 231 | */ |
| 232 | private $api = ''; |
| 233 | |
| 234 | /** |
| 235 | * Media library UI. |
| 236 | * |
| 237 | * @since 3.4.0 |
| 238 | * @var Smush\App\Media_Library |
| 239 | */ |
| 240 | private $library; |
| 241 | |
| 242 | /** |
| 243 | * Stores the value of validate_install function. |
| 244 | * |
| 245 | * @var bool $is_pro |
| 246 | */ |
| 247 | private static $is_pro; |
| 248 | |
| 249 | /** |
| 250 | * Return the plugin instance. |
| 251 | * |
| 252 | * @return WP_Smush |
| 253 | */ |
| 254 | public static function get_instance() { |
| 255 | if ( ! self::$instance ) { |
| 256 | self::$instance = new self(); |
| 257 | } |
| 258 | |
| 259 | return self::$instance; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * WP_Smush constructor. |
| 264 | */ |
| 265 | private function __construct() { |
| 266 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 267 | |
| 268 | add_action( 'admin_init', array( '\\Smush\\Core\\Installer', 'upgrade_settings' ) ); |
| 269 | add_action( 'current_screen', array( '\\Smush\\Core\\Installer', 'maybe_create_table' ) ); |
| 270 | // We use priority 9 to avoid conflict with old free-dashboard module <= 2.0.4. |
| 271 | add_action( 'admin_init', array( $this, 'register_free_modules' ), 9 ); |
| 272 | |
| 273 | // The dash-notification actions are hooked into "init" with a priority of 10. |
| 274 | add_action( 'init', array( $this, 'register_pro_modules' ), 5 ); |
| 275 | |
| 276 | add_action( 'init', array( $this, 'do_plugin_activated_action' ) ); |
| 277 | |
| 278 | $this->init(); |
| 279 | } |
| 280 | |
| 281 | public function do_plugin_activated_action() { |
| 282 | $transient_key = 'wp-smush-plugin-activated'; |
| 283 | if ( ! get_option( $transient_key ) ) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | ( new \Smush\Core\Modules\Background\Mutex( $transient_key ) ) |
| 288 | ->set_break_on_timeout( true ) |
| 289 | ->execute( function () use ( $transient_key ) { |
| 290 | // The get_option call we made above has added the "true" value to the cache, |
| 291 | // get_option is always going to return true even if the option was deleted in another thread, |
| 292 | // now we need use a thread safe method instead |
| 293 | $background_utils = new \Smush\Core\Modules\Background\Background_Utils(); |
| 294 | if ( $background_utils->get_option( $transient_key, false ) ) { |
| 295 | do_action( 'wp_smush_plugin_activated' ); |
| 296 | delete_option( $transient_key ); |
| 297 | } |
| 298 | } ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Autoload method. |
| 303 | * |
| 304 | * @since 3.1.0 |
| 305 | * @param string $class Class name to autoload. |
| 306 | */ |
| 307 | public function autoload( $class ) { |
| 308 | // Project-specific namespace prefix. |
| 309 | $prefix = 'Smush\\'; |
| 310 | |
| 311 | // Does the class use the namespace prefix? |
| 312 | $len = strlen( $prefix ); |
| 313 | if ( 0 !== strncmp( $prefix, $class, $len ) ) { |
| 314 | // Maybe require some external classes. |
| 315 | $external_libs = array( 'WDEV_Logger' ); |
| 316 | if ( in_array( $class, $external_libs, true ) ) { |
| 317 | $lib = str_replace( '_', '-', strtolower( $class ) ); |
| 318 | $file = WP_SMUSH_DIR . "core/external/{$lib}/{$lib}.php"; |
| 319 | if ( file_exists( $file ) ) { |
| 320 | require_once $file; |
| 321 | } |
| 322 | } |
| 323 | // No, move to the next registered autoloader. |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // Get the relative class name. |
| 328 | $relative_class = substr( $class, $len ); |
| 329 | |
| 330 | $path = explode( '\\', strtolower( str_replace( '_', '-', $relative_class ) ) ); |
| 331 | $file = array_pop( $path ); |
| 332 | $file = WP_SMUSH_DIR . implode( '/', $path ) . '/class-' . $file . '.php'; |
| 333 | |
| 334 | // If the file exists, require it. |
| 335 | if ( file_exists( $file ) ) { |
| 336 | /* @noinspection PhpIncludeInspection */ |
| 337 | require $file; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Init core module. |
| 343 | * |
| 344 | * @since 2.9.0 |
| 345 | */ |
| 346 | private function init() { |
| 347 | try { |
| 348 | $this->api = new Smush\Core\Api\Smush_API( Smush\Core\Helper::get_wpmudev_apikey() ); |
| 349 | } catch ( Exception $e ) { |
| 350 | $this->api = ''; |
| 351 | } |
| 352 | |
| 353 | // Handle failed items, load it before validate the install. |
| 354 | new Smush\Core\Error_Handler(); |
| 355 | |
| 356 | $this->validate_install(); |
| 357 | |
| 358 | $this->core = new Smush\Core\Core(); |
| 359 | $this->library = new Smush\App\Media_Library( $this->core() ); |
| 360 | if ( is_admin() ) { |
| 361 | $this->admin = new Smush\App\Admin( $this->library() ); |
| 362 | } |
| 363 | |
| 364 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 365 | WP_CLI::add_command( 'smush', '\\Smush\\Core\\CLI' ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Getter method for core. |
| 371 | * |
| 372 | * @since 2.9.0 |
| 373 | * |
| 374 | * @return Smush\Core\Core |
| 375 | */ |
| 376 | public function core() { |
| 377 | return $this->core; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Getter method for core. |
| 382 | * |
| 383 | * @since 2.9.0 |
| 384 | * |
| 385 | * @return Smush\App\Admin |
| 386 | */ |
| 387 | public function admin() { |
| 388 | return $this->admin; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Getter method for core. |
| 393 | * |
| 394 | * @since 3.0 |
| 395 | * |
| 396 | * @return Smush\Core\Api\Smush_API |
| 397 | */ |
| 398 | public function api() { |
| 399 | return $this->api; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Getter method for library. |
| 404 | * |
| 405 | * @since 3.4.0 |
| 406 | * |
| 407 | * @return Smush\App\Media_Library |
| 408 | */ |
| 409 | public function library() { |
| 410 | return $this->library; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Return PRO status. |
| 415 | * |
| 416 | * @since 2.9.0 |
| 417 | * |
| 418 | * @return bool |
| 419 | */ |
| 420 | public static function is_pro() { |
| 421 | return self::$is_pro; |
| 422 | } |
| 423 | |
| 424 | public static function is_expired() { |
| 425 | return ! self::is_pro() && Smush\Core\Helper::get_wpmudev_apikey(); |
| 426 | } |
| 427 | |
| 428 | public static function is_new_user() { |
| 429 | return ! self::is_pro() && ! self::is_expired(); |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Verify the site is connected to TFH. |
| 434 | * |
| 435 | * @since 3.12.0 |
| 436 | * |
| 437 | * @return boolean |
| 438 | */ |
| 439 | public static function is_site_connected_to_tfh() { |
| 440 | return isset( $_SERVER['WPMUDEV_HOSTED'] ) |
| 441 | && class_exists( '\WPMUDEV_Dashboard' ) && is_object( \WPMUDEV_Dashboard::$api ) |
| 442 | && method_exists( \WPMUDEV_Dashboard::$api, 'get_membership_status' ) |
| 443 | && 'free' === \WPMUDEV_Dashboard::$api->get_membership_status(); |
| 444 | } |
| 445 | |
| 446 | public static function is_member() { |
| 447 | return self::is_pro() || self::is_site_connected_to_tfh(); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Register submodules. |
| 452 | * Only for wordpress.org members. |
| 453 | */ |
| 454 | public function register_free_modules() { |
| 455 | if ( false === strpos( WP_SMUSH_DIR, 'wp-smushit' ) || class_exists( 'WPMUDEV_Dashboard' ) || file_exists( WP_PLUGIN_DIR . '/wpmudev-updates/update-notifications.php' ) ) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | /* @noinspection PhpIncludeInspection */ |
| 460 | require_once WP_SMUSH_DIR . 'core/external/free-dashboard/module.php'; |
| 461 | /* @noinspection PhpIncludeInspection */ |
| 462 | require_once WP_SMUSH_DIR . 'core/external/plugin-notice/notice.php'; |
| 463 | |
| 464 | // Add the Mailchimp group value. |
| 465 | add_action( |
| 466 | 'frash_subscribe_form_fields', |
| 467 | function ( $mc_list_id ) { |
| 468 | if ( '4b14b58816' === $mc_list_id ) { |
| 469 | echo '<input type="hidden" id="mce-group[53]-53-1" name="group[53][2]" value="2" />'; |
| 470 | } |
| 471 | } |
| 472 | ); |
| 473 | |
| 474 | // Register the current plugin. |
| 475 | do_action( |
| 476 | 'wpmudev_register_notices', |
| 477 | 'smush', |
| 478 | array( |
| 479 | 'basename' => WP_SMUSH_BASENAME, // Required: Plugin basename (for backward compat). |
| 480 | 'title' => 'Smush', // Required: Plugin title. |
| 481 | 'wp_slug' => 'wp-smushit', // Required: wp.org slug of the plugin. |
| 482 | 'cta_email' => __( 'Get Fast!', 'wp-smushit' ), // Email button CTA. |
| 483 | 'installed_on' => time(), // Optional: Plugin activated time. |
| 484 | 'screens' => array( // Required: Plugin screen ids. |
| 485 | 'toplevel_page_smush', |
| 486 | ), |
| 487 | ) |
| 488 | ); |
| 489 | add_filter( 'wpmudev_notices_is_disabled', array( $this, 'enable_free_tips_opt_in' ), 10, 3 ); |
| 490 | |
| 491 | // The email message contains 1 variable: plugin-name. |
| 492 | add_filter( |
| 493 | 'wdev_email_message_' . WP_SMUSH_BASENAME, |
| 494 | function () { |
| 495 | return "You're awesome for installing %s! Make sure you get the most out of it, boost your Google PageSpeed score with these tips and tricks - just for users of Smush!"; |
| 496 | } |
| 497 | ); |
| 498 | |
| 499 | // Recommended plugin notice. |
| 500 | do_action( |
| 501 | 'wpmudev-recommended-plugins-register-notice', |
| 502 | WP_SMUSH_BASENAME, |
| 503 | __( 'Smush', 'wp-smushit' ), |
| 504 | \Smush\App\Admin::$plugin_pages, |
| 505 | array( 'before', '.sui-wrap .sui-floating-notices, .sui-wrap .sui-upgrade-page' ) |
| 506 | ); |
| 507 | } |
| 508 | |
| 509 | public function enable_free_tips_opt_in( $is_disabled, $type, $plugin ) { |
| 510 | // Enable email opt-in. |
| 511 | if ( 'smush' === $plugin && 'email' === $type ) { |
| 512 | $is_disabled = false; |
| 513 | } |
| 514 | |
| 515 | return $is_disabled; |
| 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Register sub-modules. |
| 520 | * Only for WPMU DEV Members. |
| 521 | */ |
| 522 | public function register_pro_modules() { |
| 523 | if ( ! file_exists( WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php' ) ) { |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | // Register items for the dashboard plugin. |
| 528 | global $wpmudev_notices; |
| 529 | $wpmudev_notices[] = array( |
| 530 | 'id' => 912164, |
| 531 | 'name' => 'WP Smush Pro', |
| 532 | 'screens' => array( |
| 533 | 'upload', |
| 534 | ), |
| 535 | ); |
| 536 | |
| 537 | /* @noinspection PhpIncludeInspection */ |
| 538 | require_once WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php'; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * Check if user is premium member, check for API key. |
| 543 | * |
| 544 | * @param bool $manual Is it a manual check? Default: false. |
| 545 | */ |
| 546 | public function validate_install( $manual = false ) { |
| 547 | if ( isset( self::$is_pro ) && ! $manual ) { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | // No API key set, always false. |
| 552 | $api_key = Smush\Core\Helper::get_wpmudev_apikey(); |
| 553 | |
| 554 | if ( empty( $api_key ) ) { |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | // Flag to check if we need to revalidate the key. |
| 559 | $revalidate = false; |
| 560 | |
| 561 | $api_auth = get_site_option( 'wp_smush_api_auth' ); |
| 562 | |
| 563 | // Check if we need to revalidate. |
| 564 | if ( empty( $api_auth[ $api_key ] ) ) { |
| 565 | $api_auth = array(); |
| 566 | $revalidate = true; |
| 567 | } else { |
| 568 | $last_checked = $api_auth[ $api_key ]['timestamp']; |
| 569 | $valid = $api_auth[ $api_key ]['validity']; |
| 570 | |
| 571 | // Difference in hours. |
| 572 | $diff = ( time() - $last_checked ) / HOUR_IN_SECONDS; |
| 573 | |
| 574 | if ( 24 < $diff ) { |
| 575 | $revalidate = true; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // If we are supposed to validate API, update the results in options table. |
| 580 | if ( $revalidate || $manual ) { |
| 581 | if ( empty( $api_auth[ $api_key ] ) ) { |
| 582 | // For api key resets. |
| 583 | $api_auth[ $api_key ] = array(); |
| 584 | |
| 585 | // Storing it as valid, unless we really get to know from API call. |
| 586 | $valid = 'valid'; |
| 587 | $api_auth[ $api_key ]['validity'] = 'valid'; |
| 588 | } |
| 589 | |
| 590 | // This is the first check. |
| 591 | if ( ! isset( $api_auth[ $api_key ]['timestamp'] ) ) { |
| 592 | $api_auth[ $api_key ]['timestamp'] = time(); |
| 593 | } |
| 594 | |
| 595 | $request = $this->api()->check( $manual ); |
| 596 | |
| 597 | if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) { |
| 598 | // Update the timestamp only on successful attempts. |
| 599 | $api_auth[ $api_key ]['timestamp'] = time(); |
| 600 | update_site_option( 'wp_smush_api_auth', $api_auth ); |
| 601 | |
| 602 | $result = json_decode( wp_remote_retrieve_body( $request ) ); |
| 603 | if ( ! empty( $result->success ) && $result->success ) { |
| 604 | $valid = 'valid'; |
| 605 | update_site_option( 'wp-smush-cdn_status', $result->data ); |
| 606 | } else { |
| 607 | $valid = 'invalid'; |
| 608 | } |
| 609 | } elseif ( ! isset( $valid ) || 'valid' !== $valid ) { |
| 610 | // Invalidate only in case when it was not valid before. |
| 611 | $valid = 'invalid'; |
| 612 | } |
| 613 | |
| 614 | $api_auth[ $api_key ]['validity'] = $valid; |
| 615 | |
| 616 | // Update API validity. |
| 617 | update_site_option( 'wp_smush_api_auth', $api_auth ); |
| 618 | } |
| 619 | |
| 620 | self::$is_pro = isset( $valid ) && 'valid' === $valid; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 |