wp-smushit
Last commit date
_src
3 years ago
app
3 years ago
core
3 years ago
languages
3 years ago
vendor
3 years ago
readme.txt
3 years ago
uninstall.php
3 years ago
wp-smush.php
3 years ago
wp-smush.php
516 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.12.4 |
| 17 | * Author: WPMU DEV |
| 18 | * Author URI: https://profiles.wordpress.org/wpmudev/ |
| 19 | * License: GPLv2 |
| 20 | * License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 21 | * Text Domain: wp-smushit |
| 22 | * Domain Path: /languages/ |
| 23 | * Network: true |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | This plugin was originally developed by Alex Dunae (http://dialect.ca/). |
| 28 | |
| 29 | Copyright 2007-2022 Incsub (http://incsub.com) |
| 30 | |
| 31 | This program is free software; you can redistribute it and/or modify |
| 32 | it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by |
| 33 | the Free Software Foundation. |
| 34 | |
| 35 | This program is distributed in the hope that it will be useful, |
| 36 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | GNU General Public License for more details. |
| 39 | |
| 40 | You should have received a copy of the GNU General Public License |
| 41 | along with this program; if not, write to the Free Software |
| 42 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 43 | */ |
| 44 | |
| 45 | // If this file is called directly, abort. |
| 46 | if ( ! defined( 'WPINC' ) ) { |
| 47 | die; |
| 48 | } |
| 49 | |
| 50 | if ( ! defined( 'WP_SMUSH_VERSION' ) ) { |
| 51 | define( 'WP_SMUSH_VERSION', '3.12.4' ); |
| 52 | } |
| 53 | // Used to define body class. |
| 54 | if ( ! defined( 'WP_SHARED_UI_VERSION' ) ) { |
| 55 | define( 'WP_SHARED_UI_VERSION', 'sui-2-12-10' ); |
| 56 | } |
| 57 | if ( ! defined( 'WP_SMUSH_BASENAME' ) ) { |
| 58 | define( 'WP_SMUSH_BASENAME', plugin_basename( __FILE__ ) ); |
| 59 | } |
| 60 | if ( ! defined( 'WP_SMUSH_API' ) ) { |
| 61 | define( 'WP_SMUSH_API', 'https://smushpro.wpmudev.com/1.0/' ); |
| 62 | } |
| 63 | if ( ! defined( 'WP_SMUSH_UA' ) ) { |
| 64 | define( 'WP_SMUSH_UA', 'WP Smush/' . WP_SMUSH_VERSION . '; ' . network_home_url() ); |
| 65 | } |
| 66 | if ( ! defined( 'WP_SMUSH_DIR' ) ) { |
| 67 | define( 'WP_SMUSH_DIR', plugin_dir_path( __FILE__ ) ); |
| 68 | } |
| 69 | if ( ! defined( 'WP_SMUSH_URL' ) ) { |
| 70 | define( 'WP_SMUSH_URL', plugin_dir_url( __FILE__ ) ); |
| 71 | } |
| 72 | if ( ! defined( 'WP_SMUSH_MAX_BYTES' ) ) { |
| 73 | define( 'WP_SMUSH_MAX_BYTES', 5000000 ); |
| 74 | } |
| 75 | if ( ! defined( 'WP_SMUSH_PREMIUM_MAX_BYTES' ) ) { |
| 76 | define( 'WP_SMUSH_PREMIUM_MAX_BYTES', 32000000 ); |
| 77 | } |
| 78 | if ( ! defined( 'WP_SMUSH_TIMEOUT' ) ) { |
| 79 | define( 'WP_SMUSH_TIMEOUT', 150 ); |
| 80 | } |
| 81 | if ( ! defined( 'WP_SMUSH_RETRY_ATTEMPTS' ) ) { |
| 82 | define( 'WP_SMUSH_RETRY_ATTEMPTS', 3 ); |
| 83 | } |
| 84 | if ( ! defined( 'WP_SMUSH_RETRY_WAIT' ) ) { |
| 85 | define( 'WP_SMUSH_RETRY_WAIT', 1 ); |
| 86 | } |
| 87 | if ( ! defined( 'WP_SMUSH_PARALLEL' ) ) { |
| 88 | define( 'WP_SMUSH_PARALLEL', true ); |
| 89 | } |
| 90 | if ( ! defined( 'WP_SMUSH_BACKGROUND' ) ) { |
| 91 | define( 'WP_SMUSH_BACKGROUND', true ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * To support Smushing on staging sites like SiteGround staging where staging site urls are different |
| 96 | * but redirects to main site url. Remove the protocols and www, and get the domain name.* |
| 97 | * If Set to false, WP Smush switch backs to the Old Sync Optimisation. |
| 98 | */ |
| 99 | $site_url = str_replace( array( 'http://', 'https://', 'www.' ), '', site_url() ); |
| 100 | // Compat with WPMU DEV staging. |
| 101 | $wpmu_host = isset( $_SERVER['WPMUDEV_HOSTING_ENV'] ) && 'staging' === sanitize_text_field( wp_unslash( $_SERVER['WPMUDEV_HOSTING_ENV'] ) ); |
| 102 | if ( ! defined( 'WP_SMUSH_ASYNC' ) ) { |
| 103 | if ( ( ! empty( $_SERVER['SERVER_NAME'] ) && 0 !== strpos( $site_url, sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) ) ) ) || $wpmu_host ) { |
| 104 | define( 'WP_SMUSH_ASYNC', false ); |
| 105 | } else { |
| 106 | define( 'WP_SMUSH_ASYNC', true ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * If we are activating a version, while having another present and activated. |
| 112 | * Leave in the Pro version, if it is available. |
| 113 | * |
| 114 | * @since 2.9.1 |
| 115 | */ |
| 116 | if ( WP_SMUSH_BASENAME !== plugin_basename( __FILE__ ) ) { |
| 117 | $pro_installed = false; |
| 118 | if ( file_exists( WP_PLUGIN_DIR . '/wp-smush-pro/wp-smush.php' ) ) { |
| 119 | $pro_installed = true; |
| 120 | } |
| 121 | |
| 122 | if ( ! function_exists( 'is_plugin_active' ) ) { |
| 123 | include_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 124 | } |
| 125 | |
| 126 | if ( is_plugin_active( 'wp-smush-pro/wp-smush.php' ) ) { |
| 127 | deactivate_plugins( plugin_basename( __FILE__ ) ); |
| 128 | update_site_option( 'smush_deactivated', 1 ); |
| 129 | return; // Return to avoid errors with free-dashboard module. |
| 130 | } elseif ( $pro_installed && is_plugin_active( WP_SMUSH_BASENAME ) ) { |
| 131 | deactivate_plugins( WP_SMUSH_BASENAME ); |
| 132 | // If WordPress is already in the process of activating - return. |
| 133 | if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) { |
| 134 | return; |
| 135 | } |
| 136 | activate_plugin( plugin_basename( __FILE__ ) ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /* @noinspection PhpIncludeInspection */ |
| 141 | require_once WP_SMUSH_DIR . 'core/class-installer.php'; |
| 142 | register_activation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_activated' ) ); |
| 143 | register_deactivation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_deactivated' ) ); |
| 144 | |
| 145 | // Init the plugin and load the plugin instance for the first time. |
| 146 | add_action( 'plugins_loaded', array( 'WP_Smush', 'get_instance' ) ); |
| 147 | |
| 148 | if ( ! class_exists( 'WP_Smush' ) ) { |
| 149 | /** |
| 150 | * Class WP_Smush |
| 151 | */ |
| 152 | final class WP_Smush { |
| 153 | |
| 154 | /** |
| 155 | * Plugin instance. |
| 156 | * |
| 157 | * @since 2.9.0 |
| 158 | * @var null|WP_Smush |
| 159 | */ |
| 160 | private static $instance = null; |
| 161 | |
| 162 | /** |
| 163 | * Plugin core. |
| 164 | * |
| 165 | * @since 2.9.0 |
| 166 | * @var Smush\Core\Core |
| 167 | */ |
| 168 | private $core; |
| 169 | |
| 170 | /** |
| 171 | * Plugin admin. |
| 172 | * |
| 173 | * @since 2.9.0 |
| 174 | * @var Smush\App\Admin |
| 175 | */ |
| 176 | private $admin; |
| 177 | |
| 178 | /** |
| 179 | * Plugin API. |
| 180 | * |
| 181 | * @since 3.0 |
| 182 | * @var Smush\Core\Api\Smush_API |
| 183 | */ |
| 184 | private $api = ''; |
| 185 | |
| 186 | /** |
| 187 | * Media library UI. |
| 188 | * |
| 189 | * @since 3.4.0 |
| 190 | * @var Smush\App\Media_Library |
| 191 | */ |
| 192 | private $library; |
| 193 | |
| 194 | /** |
| 195 | * Stores the value of validate_install function. |
| 196 | * |
| 197 | * @var bool $is_pro |
| 198 | */ |
| 199 | private static $is_pro; |
| 200 | |
| 201 | /** |
| 202 | * Return the plugin instance. |
| 203 | * |
| 204 | * @return WP_Smush |
| 205 | */ |
| 206 | public static function get_instance() { |
| 207 | if ( ! self::$instance ) { |
| 208 | self::$instance = new self(); |
| 209 | } |
| 210 | |
| 211 | return self::$instance; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * WP_Smush constructor. |
| 216 | */ |
| 217 | private function __construct() { |
| 218 | spl_autoload_register( array( $this, 'autoload' ) ); |
| 219 | |
| 220 | /** |
| 221 | * Include vendor dependencies |
| 222 | */ |
| 223 | require_once __DIR__ . '/vendor/autoload.php'; |
| 224 | |
| 225 | add_action( 'admin_init', array( '\\Smush\\Core\\Installer', 'upgrade_settings' ) ); |
| 226 | add_action( 'current_screen', array( '\\Smush\\Core\\Installer', 'maybe_create_table' ) ); |
| 227 | add_action( 'admin_init', array( $this, 'register_free_modules' ) ); |
| 228 | |
| 229 | // The dash-notification actions are hooked into "init" with a priority of 10. |
| 230 | add_action( 'init', array( $this, 'register_pro_modules' ), 5 ); |
| 231 | |
| 232 | $this->init(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Autoload method. |
| 237 | * |
| 238 | * @since 3.1.0 |
| 239 | * @param string $class Class name to autoload. |
| 240 | */ |
| 241 | public function autoload( $class ) { |
| 242 | // Project-specific namespace prefix. |
| 243 | $prefix = 'Smush\\'; |
| 244 | |
| 245 | // Does the class use the namespace prefix? |
| 246 | $len = strlen( $prefix ); |
| 247 | if ( 0 !== strncmp( $prefix, $class, $len ) ) { |
| 248 | // Maybe require some external classes. |
| 249 | $external_libs = array( 'WDEV_Logger' ); |
| 250 | if ( in_array( $class, $external_libs, true ) ) { |
| 251 | $lib = str_replace( '_', '-', strtolower( $class ) ); |
| 252 | $file = WP_SMUSH_DIR . "core/external/{$lib}/{$lib}.php"; |
| 253 | if ( file_exists( $file ) ) { |
| 254 | require_once $file; |
| 255 | } |
| 256 | } |
| 257 | // No, move to the next registered autoloader. |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // Get the relative class name. |
| 262 | $relative_class = substr( $class, $len ); |
| 263 | |
| 264 | $path = explode( '\\', strtolower( str_replace( '_', '-', $relative_class ) ) ); |
| 265 | $file = array_pop( $path ); |
| 266 | $file = WP_SMUSH_DIR . implode( '/', $path ) . '/class-' . $file . '.php'; |
| 267 | |
| 268 | // If the file exists, require it. |
| 269 | if ( file_exists( $file ) ) { |
| 270 | /* @noinspection PhpIncludeInspection */ |
| 271 | require $file; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Init core module. |
| 277 | * |
| 278 | * @since 2.9.0 |
| 279 | */ |
| 280 | private function init() { |
| 281 | try { |
| 282 | $this->api = new Smush\Core\Api\Smush_API( Smush\Core\Helper::get_wpmudev_apikey() ); |
| 283 | } catch ( Exception $e ) { |
| 284 | $this->api = ''; |
| 285 | } |
| 286 | |
| 287 | // Handle failed items, load it before validate the install. |
| 288 | new Smush\Core\Error_Handler(); |
| 289 | |
| 290 | $this->validate_install(); |
| 291 | |
| 292 | $this->core = new Smush\Core\Core(); |
| 293 | $this->library = new Smush\App\Media_Library( $this->core() ); |
| 294 | if ( is_admin() ) { |
| 295 | $this->admin = new Smush\App\Admin( $this->library() ); |
| 296 | } |
| 297 | |
| 298 | if ( defined( 'WP_CLI' ) && WP_CLI ) { |
| 299 | WP_CLI::add_command( 'smush', '\\Smush\\Core\\CLI' ); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Getter method for core. |
| 305 | * |
| 306 | * @since 2.9.0 |
| 307 | * |
| 308 | * @return Smush\Core\Core |
| 309 | */ |
| 310 | public function core() { |
| 311 | return $this->core; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Getter method for core. |
| 316 | * |
| 317 | * @since 2.9.0 |
| 318 | * |
| 319 | * @return Smush\App\Admin |
| 320 | */ |
| 321 | public function admin() { |
| 322 | return $this->admin; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Getter method for core. |
| 327 | * |
| 328 | * @since 3.0 |
| 329 | * |
| 330 | * @return Smush\Core\Api\Smush_API |
| 331 | */ |
| 332 | public function api() { |
| 333 | return $this->api; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Getter method for library. |
| 338 | * |
| 339 | * @since 3.4.0 |
| 340 | * |
| 341 | * @return Smush\App\Media_Library |
| 342 | */ |
| 343 | public function library() { |
| 344 | return $this->library; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Return PRO status. |
| 349 | * |
| 350 | * @since 2.9.0 |
| 351 | * |
| 352 | * @return bool |
| 353 | */ |
| 354 | public static function is_pro() { |
| 355 | return self::$is_pro; |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Register submodules. |
| 360 | * Only for wordpress.org members. |
| 361 | */ |
| 362 | public function register_free_modules() { |
| 363 | if ( false === strpos( WP_SMUSH_DIR, 'wp-smushit' ) || class_exists( 'WPMUDEV_Dashboard' ) || file_exists( WP_PLUGIN_DIR . '/wpmudev-updates/update-notifications.php' ) ) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | /* @noinspection PhpIncludeInspection */ |
| 368 | require_once WP_SMUSH_DIR . 'core/external/free-dashboard/module.php'; |
| 369 | /* @noinspection PhpIncludeInspection */ |
| 370 | require_once WP_SMUSH_DIR . 'core/external/plugin-notice/notice.php'; |
| 371 | |
| 372 | // Add the Mailchimp group value. |
| 373 | add_action( |
| 374 | 'frash_subscribe_form_fields', |
| 375 | function ( $mc_list_id ) { |
| 376 | if ( '4b14b58816' === $mc_list_id ) { |
| 377 | echo '<input type="hidden" id="mce-group[53]-53-1" name="group[53][2]" value="2" />'; |
| 378 | } |
| 379 | } |
| 380 | ); |
| 381 | |
| 382 | // Register the current plugin. |
| 383 | do_action( |
| 384 | 'wdev_register_plugin', |
| 385 | /* 1 Plugin ID */ WP_SMUSH_BASENAME, |
| 386 | /* 2 Plugin Title */ 'Smush', |
| 387 | /* 3 https://wordpress.org */ '/plugins/wp-smushit/', |
| 388 | /* 4 Email Button CTA */ __( 'Get Fast!', 'wp-smushit' ), |
| 389 | /* 5 Mailchimp List id for the plugin - e.g. 4b14b58816 is list id for Smush */ '4b14b58816' |
| 390 | ); |
| 391 | |
| 392 | // The email message contains 1 variable: plugin-name. |
| 393 | add_filter( |
| 394 | 'wdev_email_message_' . WP_SMUSH_BASENAME, |
| 395 | function () { |
| 396 | 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!"; |
| 397 | } |
| 398 | ); |
| 399 | |
| 400 | // Recommended plugin notice. |
| 401 | do_action( |
| 402 | 'wpmudev-recommended-plugins-register-notice', |
| 403 | WP_SMUSH_BASENAME, |
| 404 | __( 'Smush', 'wp-smushit' ), |
| 405 | \Smush\App\Admin::$plugin_pages, |
| 406 | array( 'before', '.sui-wrap .sui-floating-notices, .sui-wrap .sui-upgrade-page' ) |
| 407 | ); |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Register sub-modules. |
| 412 | * Only for WPMU DEV Members. |
| 413 | */ |
| 414 | public function register_pro_modules() { |
| 415 | if ( ! file_exists( WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php' ) ) { |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | // Register items for the dashboard plugin. |
| 420 | global $wpmudev_notices; |
| 421 | $wpmudev_notices[] = array( |
| 422 | 'id' => 912164, |
| 423 | 'name' => 'WP Smush Pro', |
| 424 | 'screens' => array( |
| 425 | 'upload', |
| 426 | ), |
| 427 | ); |
| 428 | |
| 429 | /* @noinspection PhpIncludeInspection */ |
| 430 | require_once WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php'; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Check if user is premium member, check for API key. |
| 435 | * |
| 436 | * @param bool $manual Is it a manual check? Default: false. |
| 437 | */ |
| 438 | public function validate_install( $manual = false ) { |
| 439 | if ( isset( self::$is_pro ) && ! $manual ) { |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | // No API key set, always false. |
| 444 | $api_key = Smush\Core\Helper::get_wpmudev_apikey(); |
| 445 | |
| 446 | if ( empty( $api_key ) ) { |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | // Flag to check if we need to revalidate the key. |
| 451 | $revalidate = false; |
| 452 | |
| 453 | $api_auth = get_site_option( 'wp_smush_api_auth' ); |
| 454 | |
| 455 | // Check if we need to revalidate. |
| 456 | if ( empty( $api_auth[ $api_key ] ) ) { |
| 457 | $api_auth = array(); |
| 458 | $revalidate = true; |
| 459 | } else { |
| 460 | $last_checked = $api_auth[ $api_key ]['timestamp']; |
| 461 | $valid = $api_auth[ $api_key ]['validity']; |
| 462 | |
| 463 | // Difference in hours. |
| 464 | $diff = ( time() - $last_checked ) / HOUR_IN_SECONDS; |
| 465 | |
| 466 | if ( 24 < $diff ) { |
| 467 | $revalidate = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // If we are supposed to validate API, update the results in options table. |
| 472 | if ( $revalidate || $manual ) { |
| 473 | if ( empty( $api_auth[ $api_key ] ) ) { |
| 474 | // For api key resets. |
| 475 | $api_auth[ $api_key ] = array(); |
| 476 | |
| 477 | // Storing it as valid, unless we really get to know from API call. |
| 478 | $valid = 'valid'; |
| 479 | $api_auth[ $api_key ]['validity'] = 'valid'; |
| 480 | } |
| 481 | |
| 482 | // This is the first check. |
| 483 | if ( ! isset( $api_auth[ $api_key ]['timestamp'] ) ) { |
| 484 | $api_auth[ $api_key ]['timestamp'] = time(); |
| 485 | } |
| 486 | |
| 487 | $request = $this->api()->check( $manual ); |
| 488 | |
| 489 | if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) { |
| 490 | // Update the timestamp only on successful attempts. |
| 491 | $api_auth[ $api_key ]['timestamp'] = time(); |
| 492 | update_site_option( 'wp_smush_api_auth', $api_auth ); |
| 493 | |
| 494 | $result = json_decode( wp_remote_retrieve_body( $request ) ); |
| 495 | if ( ! empty( $result->success ) && $result->success ) { |
| 496 | $valid = 'valid'; |
| 497 | update_site_option( 'wp-smush-cdn_status', $result->data ); |
| 498 | } else { |
| 499 | $valid = 'invalid'; |
| 500 | } |
| 501 | } elseif ( ! isset( $valid ) || 'valid' !== $valid ) { |
| 502 | // Invalidate only in case when it was not valid before. |
| 503 | $valid = 'invalid'; |
| 504 | } |
| 505 | |
| 506 | $api_auth[ $api_key ]['validity'] = $valid; |
| 507 | |
| 508 | // Update API validity. |
| 509 | update_site_option( 'wp_smush_api_auth', $api_auth ); |
| 510 | } |
| 511 | |
| 512 | self::$is_pro = isset( $valid ) && 'valid' === $valid; |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 |