* Reduce image file sizes, improve performance and boost your SEO using the free
* <a href="https://premium.wpmudev.org/">WPMU DEV</a> WordPress Smush API.
* @link http://premium.wpmudev.org/project/wp-smush-pro/
* Plugin URI: http://wordpress.org/plugins/wp-smushit/
* Description: Reduce image file sizes, improve performance and boost your SEO using the free <a href="https://premium.wpmudev.org/">WPMU DEV</a> WordPress Smush API.
* Author URI: https://profiles.wordpress.org/wpmudev/
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-smushit
* Domain Path: /languages/
This plugin was originally developed by Alex Dunae (http://dialect.ca/).
Copyright 2007-2020 Incsub (http://incsub.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
if ( ! defined( 'WP_SMUSH_VERSION' ) ) {
define( 'WP_SMUSH_VERSION', '3.8.2' );
// Used to define body class.
if ( ! defined( 'WP_SHARED_UI_VERSION' ) ) {
define( 'WP_SHARED_UI_VERSION', 'sui-2-10-1' );
if ( ! defined( 'WP_SMUSH_BASENAME' ) ) {
define( 'WP_SMUSH_BASENAME', plugin_basename( __FILE__ ) );
if ( ! defined( 'WP_SMUSH_API' ) ) {
define( 'WP_SMUSH_API', 'https://smushpro.wpmudev.org/1.0/' );
if ( ! defined( 'WP_SMUSH_UA' ) ) {
define( 'WP_SMUSH_UA', 'WP Smush/' . WP_SMUSH_VERSION . '; ' . network_home_url() );
if ( ! defined( 'WP_SMUSH_DIR' ) ) {
define( 'WP_SMUSH_DIR', plugin_dir_path( __FILE__ ) );
if ( ! defined( 'WP_SMUSH_URL' ) ) {
define( 'WP_SMUSH_URL', plugin_dir_url( __FILE__ ) );
if ( ! defined( 'WP_SMUSH_MAX_BYTES' ) ) {
define( 'WP_SMUSH_MAX_BYTES', 5000000 );
if ( ! defined( 'WP_SMUSH_PREMIUM_MAX_BYTES' ) ) {
define( 'WP_SMUSH_PREMIUM_MAX_BYTES', 32000000 );
if ( ! defined( 'WP_SMUSH_PREFIX' ) ) {
define( 'WP_SMUSH_PREFIX', 'wp-smush-' );
if ( ! defined( 'WP_SMUSH_TIMEOUT' ) ) {
define( 'WP_SMUSH_TIMEOUT', 150 );
* To support Smushing on staging sites like SiteGround staging where staging site urls are different
* but redirects to main site url. Remove the protocols and www, and get the domain name.*
* If Set to false, WP Smush switch backs to the Old Sync Optimisation.
$site_url = str_replace( array( 'http://', 'https://', 'www.' ), '', site_url() );
// Compat with WPMU DEV staging.
$wpmu_host = isset( $_SERVER['WPMUDEV_HOSTING_ENV'] ) && 'staging' === sanitize_text_field( wp_unslash( $_SERVER['WPMUDEV_HOSTING_ENV'] ) );
if ( ! defined( 'WP_SMUSH_ASYNC' ) ) {
if ( ( ! empty( $_SERVER['SERVER_NAME'] ) && 0 !== strpos( $site_url, sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) ) ) ) || $wpmu_host ) {
define( 'WP_SMUSH_ASYNC', false );
define( 'WP_SMUSH_ASYNC', true );
* If we are activating a version, while having another present and activated.
* Leave in the Pro version, if it is available.
if ( WP_SMUSH_BASENAME !== plugin_basename( __FILE__ ) ) {
if ( file_exists( WP_PLUGIN_DIR . '/wp-smush-pro/wp-smush.php' ) ) {
if ( ! function_exists( 'is_plugin_active' ) ) {
include_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'wp-smush-pro/wp-smush.php' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
update_site_option( 'smush_deactivated', 1 );
return; // Return to avoid errors with free-dashboard module.
} elseif ( $pro_installed && is_plugin_active( WP_SMUSH_BASENAME ) ) {
deactivate_plugins( WP_SMUSH_BASENAME );
// If WordPress is already in the process of activating - return.
if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) {
activate_plugin( plugin_basename( __FILE__ ) );
/* @noinspection PhpIncludeInspection */
require_once WP_SMUSH_DIR . 'core/class-installer.php';
register_activation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_activated' ) );
register_deactivation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_deactivated' ) );
// Init the plugin and load the plugin instance for the first time.
add_action( 'plugins_loaded', array( 'WP_Smush', 'get_instance' ) );
if ( ! class_exists( 'WP_Smush' ) ) {
private static $instance = null;
* @var Smush\Core\Api\API
* @var Smush\App\Media_Library
* Stores the value of validate_install function.
* Return the plugin instance.
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
private function __construct() {
spl_autoload_register( array( $this, 'autoload' ) );
add_action( 'admin_init', array( '\\Smush\\Core\\Installer', 'upgrade_settings' ) );
add_action( 'admin_init', array( $this, 'register_free_modules' ) );
add_action( 'admin_init', array( $this, 'register_pro_modules' ), 5 );
* @param string $class Class name to autoload.
public function autoload( $class ) {
// Project-specific namespace prefix.
// Does the class use the namespace prefix?
$len = strlen( $prefix );
if ( 0 !== strncmp( $prefix, $class, $len ) ) {
// No, move to the next registered autoloader.
// Get the relative class name.
$relative_class = substr( $class, $len );
$path = explode( '\\', strtolower( str_replace( '_', '-', $relative_class ) ) );
$file = array_pop( $path );
$file = WP_SMUSH_DIR . implode( '/', $path ) . '/class-' . $file . '.php';
// If the file exists, require it.
if ( file_exists( $file ) ) {
/* @noinspection PhpIncludeInspection */
private function init() {
$this->api = new Smush\Core\Api\API( self::get_api_key() );
} catch ( Exception $e ) {
$this->validate_install();
$this->core = new Smush\Core\Core();
$this->library = new Smush\App\Media_Library( $this->core() );
$this->admin = new Smush\App\Admin( $this->library() );
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'smush', '\\Smush\\Core\\CLI' );
* Getter method for core.
* @return Smush\Core\Core
* Getter method for core.
* @return Smush\App\Admin
public function admin() {
* Getter method for core.
* @return Smush\Core\Api\API
* Getter method for library.
* @return Smush\App\Media_Library
public function library() {
public static function is_pro() {
* Filters the rating message, include stats if greater than 1Mb
* @param string $message Message text.
public function wp_smush_rating_message( $message ) {
if ( empty( $this->core()->stats ) ) {
$this->core()->setup_global_stats();
$savings = $this->core()->stats;
// If there is any saving, greater than 1Mb, show stats.
if ( ! empty( $savings ) && ! empty( $savings['bytes'] ) && $savings['bytes'] > 1048576 ) {
$message = "Hey %s, you've been using %s for a while now, and we hope you're happy with it.";
// Conditionally Show stats in rating message.
$message .= sprintf( " You've smushed <strong>%s</strong> from %d images already, improving the speed and SEO ranking of this site!", $savings['human'], $savings['total_images'] );
$message .= " We've spent countless hours developing this free plugin for you, and we would really appreciate it if you dropped us a quick rating!";
* Only for wordpress.org members.
public function register_free_modules() {
if ( false === strpos( WP_SMUSH_DIR, 'wp-smushit' ) ) {
/* @noinspection PhpIncludeInspection */
require_once WP_SMUSH_DIR . 'core/external/free-dashboard/module.php';
/* @noinspection PhpIncludeInspection */
require_once WP_SMUSH_DIR . 'core/external/plugin-notice/notice.php';
// Add the Mailchimp group value.
'frash_subscribe_form_fields',
function ( $mc_list_id ) {
if ( '4b14b58816' === $mc_list_id ) {
echo '<input type="hidden" id="mce-group[53]-53-1" name="group[53][2]" value="2" />';
// Register the current plugin.
/* 1 Plugin ID */ WP_SMUSH_BASENAME,
/* 2 Plugin Title */ 'Smush',
/* 3 https://wordpress.org */ '/plugins/wp-smushit/',
/* 4 Email Button CTA */ __( 'Get Fast!', 'wp-smushit' ),
/* 5 Mailchimp List id for the plugin - e.g. 4b14b58816 is list id for Smush */ '4b14b58816'
// The rating message contains 2 variables: user-name, plugin-name.
add_filter( 'wdev-rating-message-' . WP_SMUSH_BASENAME, array( $this, 'wp_smush_rating_message' ) );
// The email message contains 1 variable: plugin-name.
'wdev-email-message-' . WP_SMUSH_BASENAME,
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!";
// Recommended plugin notice.
'wpmudev-recommended-plugins-register-notice',
__( 'Smush', 'wp-smushit' ),
\Smush\App\Admin::$plugin_pages,
array( 'after', '.sui-wrap .sui-header' )
* Only for WPMU DEV Members.
public function register_pro_modules() {
if ( ! file_exists( WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php' ) ) {
// Register items for the dashboard plugin.
$wpmudev_notices[] = array(
'name' => 'WP Smush Pro',
/* @noinspection PhpIncludeInspection */
require_once WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php';
* Check if user is premium member, check for API key.
* @param bool $manual Is it a manual check? Default: false.
public function validate_install( $manual = false ) {
if ( isset( self::$is_pro ) && ! $manual ) {
// No API key set, always false.
$api_key = self::get_api_key();
if ( empty( $api_key ) ) {
// Flag to check if we need to revalidate the key.
$api_auth = get_site_option( 'wp_smush_api_auth' );
// Check if need to revalidate.
if ( ! $api_auth || empty( $api_auth ) || empty( $api_auth[ $api_key ] ) ) {
$last_checked = $api_auth[ $api_key ]['timestamp'];
$valid = $api_auth[ $api_key ]['validity'];
$diff = ( current_time( 'timestamp' ) - $last_checked ) / HOUR_IN_SECONDS;
// If we are suppose to validate API, update the results in options table.
if ( $revalidate || $manual ) {
if ( empty( $api_auth[ $api_key ] ) ) {
$api_auth[ $api_key ] = array();
// Storing it as valid, unless we really get to know from API call.
$api_auth[ $api_key ]['validity'] = 'valid';
// This is the first check.
if ( ! isset( $api_auth[ $api_key ]['timestamp'] ) ) {
$api_auth[ $api_key ]['timestamp'] = current_time( 'timestamp' );
$request = $this->api()->check( $manual );
if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
// Update the timestamp only on successful attempts.
$api_auth[ $api_key ]['timestamp'] = current_time( 'timestamp' );
update_site_option( 'wp_smush_api_auth', $api_auth );
$result = json_decode( wp_remote_retrieve_body( $request ) );
if ( ! empty( $result->success ) && $result->success ) {
update_site_option( WP_SMUSH_PREFIX . 'cdn_status', $result->data );