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