PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 3.8.2
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v3.8.2
4.1.0 4.0.3 4.0.2 2.8.1 2.9.1 3.0.0 3.0.1 3.0.2 3.1.1 3.10.1 3.10.2 3.10.3 3.11.1 3.12.3 3.12.4 3.12.5 3.12.6 3.13.0 3.13.1 3.13.2 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.2 3.16.4 3.16.5 3.16.6 3.17.0 3.17.1 3.18.0 3.18.1 3.2.0.1 3.2.1 3.2.2.1 3.2.4 3.20.0 3.21.1 3.22.1 3.22.3 3.23.0 3.23.1 3.23.2 3.23.3 3.23.4 3.24.0 3.24.0-beta.2 3.3.0 3.3.1 3.3.2 3.4.1 3.4.2 3.6.1 3.6.3 3.7.0 3.7.1 3.7.2 3.7.3 3.8.2 3.8.3 3.8.4 3.8.5 3.8.7 3.8.8 3.9.0 3.9.1 3.9.11 3.9.2 3.9.4 3.9.5 3.9.8 3.9.9 trunk 1.0.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.5.3 1.6.5.4 1.7 1.7.1 1.7.1.1 2.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.6.2 2.0.6.3 2.0.6.5 2.0.7 2.0.7.1 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.2 2.3 2.3.1 2.4 2.4.2 2.4.3 2.4.4 2.4.5 2.5.2 2.5.3 2.6.1 2.6.2 2.6.3 2.7 2.7.1 2.7.4 2.7.4.1 2.7.5 2.7.6 2.7.8 2.7.8.1 2.7.9.1 2.8.0 2.8.0.1
wp-smushit / wp-smush.php
wp-smushit Last commit date
_src 5 years ago app 5 years ago core 5 years ago languages 5 years ago readme.txt 5 years ago uninstall.php 5 years ago wp-smush.php 5 years ago
wp-smush.php
538 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://premium.wpmudev.org/">WPMU DEV</a> WordPress Smush API.
7 *
8 * @link http://premium.wpmudev.org/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://premium.wpmudev.org/">WPMU DEV</a> WordPress Smush API.
16 * Version: 3.8.2
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 */
24
25 /*
26 This plugin was originally developed by Alex Dunae (http://dialect.ca/).
27
28 Copyright 2007-2020 Incsub (http://incsub.com)
29
30 This program is free software; you can redistribute it and/or modify
31 it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
32 the Free Software Foundation.
33
34 This program is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
38
39 You should have received a copy of the GNU General Public License
40 along with this program; if not, write to the Free Software
41 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 */
43
44 // If this file is called directly, abort.
45 if ( ! defined( 'WPINC' ) ) {
46 die;
47 }
48
49 if ( ! defined( 'WP_SMUSH_VERSION' ) ) {
50 define( 'WP_SMUSH_VERSION', '3.8.2' );
51 }
52 // Used to define body class.
53 if ( ! defined( 'WP_SHARED_UI_VERSION' ) ) {
54 define( 'WP_SHARED_UI_VERSION', 'sui-2-10-1' );
55 }
56 if ( ! defined( 'WP_SMUSH_BASENAME' ) ) {
57 define( 'WP_SMUSH_BASENAME', plugin_basename( __FILE__ ) );
58 }
59 if ( ! defined( 'WP_SMUSH_API' ) ) {
60 define( 'WP_SMUSH_API', 'https://smushpro.wpmudev.org/1.0/' );
61 }
62 if ( ! defined( 'WP_SMUSH_UA' ) ) {
63 define( 'WP_SMUSH_UA', 'WP Smush/' . WP_SMUSH_VERSION . '; ' . network_home_url() );
64 }
65 if ( ! defined( 'WP_SMUSH_DIR' ) ) {
66 define( 'WP_SMUSH_DIR', plugin_dir_path( __FILE__ ) );
67 }
68 if ( ! defined( 'WP_SMUSH_URL' ) ) {
69 define( 'WP_SMUSH_URL', plugin_dir_url( __FILE__ ) );
70 }
71 if ( ! defined( 'WP_SMUSH_MAX_BYTES' ) ) {
72 define( 'WP_SMUSH_MAX_BYTES', 5000000 );
73 }
74 if ( ! defined( 'WP_SMUSH_PREMIUM_MAX_BYTES' ) ) {
75 define( 'WP_SMUSH_PREMIUM_MAX_BYTES', 32000000 );
76 }
77 if ( ! defined( 'WP_SMUSH_PREFIX' ) ) {
78 define( 'WP_SMUSH_PREFIX', 'wp-smush-' );
79 }
80 if ( ! defined( 'WP_SMUSH_TIMEOUT' ) ) {
81 define( 'WP_SMUSH_TIMEOUT', 150 );
82 }
83
84 /**
85 * To support Smushing on staging sites like SiteGround staging where staging site urls are different
86 * but redirects to main site url. Remove the protocols and www, and get the domain name.*
87 * If Set to false, WP Smush switch backs to the Old Sync Optimisation.
88 */
89 $site_url = str_replace( array( 'http://', 'https://', 'www.' ), '', site_url() );
90 // Compat with WPMU DEV staging.
91 $wpmu_host = isset( $_SERVER['WPMUDEV_HOSTING_ENV'] ) && 'staging' === sanitize_text_field( wp_unslash( $_SERVER['WPMUDEV_HOSTING_ENV'] ) );
92 if ( ! defined( 'WP_SMUSH_ASYNC' ) ) {
93 if ( ( ! empty( $_SERVER['SERVER_NAME'] ) && 0 !== strpos( $site_url, sanitize_text_field( wp_unslash( $_SERVER['SERVER_NAME'] ) ) ) ) || $wpmu_host ) {
94 define( 'WP_SMUSH_ASYNC', false );
95 } else {
96 define( 'WP_SMUSH_ASYNC', true );
97 }
98 }
99
100 /**
101 * If we are activating a version, while having another present and activated.
102 * Leave in the Pro version, if it is available.
103 *
104 * @since 2.9.1
105 */
106 if ( WP_SMUSH_BASENAME !== plugin_basename( __FILE__ ) ) {
107 $pro_installed = false;
108 if ( file_exists( WP_PLUGIN_DIR . '/wp-smush-pro/wp-smush.php' ) ) {
109 $pro_installed = true;
110 }
111
112 if ( ! function_exists( 'is_plugin_active' ) ) {
113 include_once ABSPATH . 'wp-admin/includes/plugin.php';
114 }
115
116 if ( is_plugin_active( 'wp-smush-pro/wp-smush.php' ) ) {
117 deactivate_plugins( plugin_basename( __FILE__ ) );
118 update_site_option( 'smush_deactivated', 1 );
119 return; // Return to avoid errors with free-dashboard module.
120 } elseif ( $pro_installed && is_plugin_active( WP_SMUSH_BASENAME ) ) {
121 deactivate_plugins( WP_SMUSH_BASENAME );
122 // If WordPress is already in the process of activating - return.
123 if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) {
124 return;
125 }
126 activate_plugin( plugin_basename( __FILE__ ) );
127 }
128 }
129
130 /* @noinspection PhpIncludeInspection */
131 require_once WP_SMUSH_DIR . 'core/class-installer.php';
132 register_activation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_activated' ) );
133 register_deactivation_hook( __FILE__, array( 'Smush\\Core\\Installer', 'smush_deactivated' ) );
134
135 // Init the plugin and load the plugin instance for the first time.
136 add_action( 'plugins_loaded', array( 'WP_Smush', 'get_instance' ) );
137
138 if ( ! class_exists( 'WP_Smush' ) ) {
139 /**
140 * Class WP_Smush
141 */
142 class WP_Smush {
143
144 /**
145 * Plugin instance.
146 *
147 * @since 2.9.0
148 * @var null|WP_Smush
149 */
150 private static $instance = null;
151
152 /**
153 * Plugin core.
154 *
155 * @since 2.9.0
156 * @var Smush\Core\Core
157 */
158 private $core;
159
160 /**
161 * Plugin admin.
162 *
163 * @since 2.9.0
164 * @var Smush\App\Admin
165 */
166 private $admin;
167
168 /**
169 * Plugin API.
170 *
171 * @since 3.0
172 * @var Smush\Core\Api\API
173 */
174 private $api = '';
175
176 /**
177 * Media library UI.
178 *
179 * @since 3.4.0
180 * @var Smush\App\Media_Library
181 */
182 private $library;
183
184 /**
185 * Stores the value of validate_install function.
186 *
187 * @var bool $is_pro
188 */
189 private static $is_pro;
190
191 /**
192 * Return the plugin instance.
193 *
194 * @return WP_Smush
195 */
196 public static function get_instance() {
197 if ( ! self::$instance ) {
198 self::$instance = new self();
199 }
200
201 return self::$instance;
202 }
203
204 /**
205 * WP_Smush constructor.
206 */
207 private function __construct() {
208 spl_autoload_register( array( $this, 'autoload' ) );
209
210 add_action( 'admin_init', array( '\\Smush\\Core\\Installer', 'upgrade_settings' ) );
211 add_action( 'admin_init', array( $this, 'register_free_modules' ) );
212 add_action( 'admin_init', array( $this, 'register_pro_modules' ), 5 );
213
214 $this->init();
215 }
216
217 /**
218 * Autoload method.
219 *
220 * @since 3.1.0
221 * @param string $class Class name to autoload.
222 */
223 public function autoload( $class ) {
224 // Project-specific namespace prefix.
225 $prefix = 'Smush\\';
226
227 // Does the class use the namespace prefix?
228 $len = strlen( $prefix );
229 if ( 0 !== strncmp( $prefix, $class, $len ) ) {
230 // No, move to the next registered autoloader.
231 return;
232 }
233
234 // Get the relative class name.
235 $relative_class = substr( $class, $len );
236
237 $path = explode( '\\', strtolower( str_replace( '_', '-', $relative_class ) ) );
238 $file = array_pop( $path );
239 $file = WP_SMUSH_DIR . implode( '/', $path ) . '/class-' . $file . '.php';
240
241 // If the file exists, require it.
242 if ( file_exists( $file ) ) {
243 /* @noinspection PhpIncludeInspection */
244 require $file;
245 }
246 }
247
248 /**
249 * Init core module.
250 *
251 * @since 2.9.0
252 */
253 private function init() {
254 try {
255 $this->api = new Smush\Core\Api\API( self::get_api_key() );
256 } catch ( Exception $e ) {
257 $this->api = '';
258 }
259
260 $this->validate_install();
261
262 $this->core = new Smush\Core\Core();
263 $this->library = new Smush\App\Media_Library( $this->core() );
264 if ( is_admin() ) {
265 $this->admin = new Smush\App\Admin( $this->library() );
266 }
267
268 if ( defined( 'WP_CLI' ) && WP_CLI ) {
269 WP_CLI::add_command( 'smush', '\\Smush\\Core\\CLI' );
270 }
271 }
272
273 /**
274 * Getter method for core.
275 *
276 * @since 2.9.0
277 *
278 * @return Smush\Core\Core
279 */
280 public function core() {
281 return $this->core;
282 }
283
284 /**
285 * Getter method for core.
286 *
287 * @since 2.9.0
288 *
289 * @return Smush\App\Admin
290 */
291 public function admin() {
292 return $this->admin;
293 }
294
295 /**
296 * Getter method for core.
297 *
298 * @since 3.0
299 *
300 * @return Smush\Core\Api\API
301 */
302 public function api() {
303 return $this->api;
304 }
305
306 /**
307 * Getter method for library.
308 *
309 * @since 3.4.0
310 *
311 * @return Smush\App\Media_Library
312 */
313 public function library() {
314 return $this->library;
315 }
316
317 /**
318 * Return PRO status.
319 *
320 * @since 2.9.0
321 *
322 * @return bool
323 */
324 public static function is_pro() {
325 return self::$is_pro;
326 }
327
328 /**
329 * Filters the rating message, include stats if greater than 1Mb
330 *
331 * @param string $message Message text.
332 *
333 * @return string
334 */
335 public function wp_smush_rating_message( $message ) {
336 if ( empty( $this->core()->stats ) ) {
337 $this->core()->setup_global_stats();
338 }
339
340 $savings = $this->core()->stats;
341 $show_stats = false;
342
343 // If there is any saving, greater than 1Mb, show stats.
344 if ( ! empty( $savings ) && ! empty( $savings['bytes'] ) && $savings['bytes'] > 1048576 ) {
345 $show_stats = true;
346 }
347
348 $message = "Hey %s, you've been using %s for a while now, and we hope you're happy with it.";
349
350 // Conditionally Show stats in rating message.
351 if ( $show_stats ) {
352 $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'] );
353 }
354 $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!";
355
356 return $message;
357 }
358
359 /**
360 * Register sub-modules.
361 * Only for wordpress.org members.
362 */
363 public function register_free_modules() {
364 if ( false === strpos( WP_SMUSH_DIR, 'wp-smushit' ) ) {
365 return;
366 }
367
368 /* @noinspection PhpIncludeInspection */
369 require_once WP_SMUSH_DIR . 'core/external/free-dashboard/module.php';
370 /* @noinspection PhpIncludeInspection */
371 require_once WP_SMUSH_DIR . 'core/external/plugin-notice/notice.php';
372
373 // Add the Mailchimp group value.
374 add_action(
375 'frash_subscribe_form_fields',
376 function ( $mc_list_id ) {
377 if ( '4b14b58816' === $mc_list_id ) {
378 echo '<input type="hidden" id="mce-group[53]-53-1" name="group[53][2]" value="2" />';
379 }
380 }
381 );
382
383 // Register the current plugin.
384 do_action(
385 'wdev-register-plugin',
386 /* 1 Plugin ID */ WP_SMUSH_BASENAME,
387 /* 2 Plugin Title */ 'Smush',
388 /* 3 https://wordpress.org */ '/plugins/wp-smushit/',
389 /* 4 Email Button CTA */ __( 'Get Fast!', 'wp-smushit' ),
390 /* 5 Mailchimp List id for the plugin - e.g. 4b14b58816 is list id for Smush */ '4b14b58816'
391 );
392
393 // The rating message contains 2 variables: user-name, plugin-name.
394 add_filter( 'wdev-rating-message-' . WP_SMUSH_BASENAME, array( $this, 'wp_smush_rating_message' ) );
395 // The email message contains 1 variable: plugin-name.
396 add_filter(
397 'wdev-email-message-' . WP_SMUSH_BASENAME,
398 function () {
399 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!";
400 }
401 );
402
403 // Recommended plugin notice.
404 do_action(
405 'wpmudev-recommended-plugins-register-notice',
406 WP_SMUSH_BASENAME,
407 __( 'Smush', 'wp-smushit' ),
408 \Smush\App\Admin::$plugin_pages,
409 array( 'after', '.sui-wrap .sui-header' )
410 );
411 }
412
413 /**
414 * Register sub-modules.
415 * Only for WPMU DEV Members.
416 */
417 public function register_pro_modules() {
418 if ( ! file_exists( WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php' ) ) {
419 return;
420 }
421
422 // Register items for the dashboard plugin.
423 global $wpmudev_notices;
424 $wpmudev_notices[] = array(
425 'id' => 912164,
426 'name' => 'WP Smush Pro',
427 'screens' => array(
428 'upload',
429 ),
430 );
431
432 /* @noinspection PhpIncludeInspection */
433 require_once WP_SMUSH_DIR . 'core/external/dash-notice/wpmudev-dash-notification.php';
434 }
435
436 /**
437 * Check if user is premium member, check for API key.
438 *
439 * @param bool $manual Is it a manual check? Default: false.
440 */
441 public function validate_install( $manual = false ) {
442 if ( isset( self::$is_pro ) && ! $manual ) {
443 return;
444 }
445
446 // No API key set, always false.
447 $api_key = self::get_api_key();
448
449 if ( empty( $api_key ) ) {
450 return;
451 }
452
453 // Flag to check if we need to revalidate the key.
454 $revalidate = false;
455
456 $api_auth = get_site_option( 'wp_smush_api_auth' );
457
458 // Check if need to revalidate.
459 if ( ! $api_auth || empty( $api_auth ) || empty( $api_auth[ $api_key ] ) ) {
460 $revalidate = true;
461 } else {
462 $last_checked = $api_auth[ $api_key ]['timestamp'];
463 $valid = $api_auth[ $api_key ]['validity'];
464
465 // Difference in hours.
466 $diff = ( current_time( 'timestamp' ) - $last_checked ) / HOUR_IN_SECONDS;
467
468 if ( 24 < $diff ) {
469 $revalidate = true;
470 }
471 }
472
473 // If we are suppose to validate API, update the results in options table.
474 if ( $revalidate || $manual ) {
475 if ( empty( $api_auth[ $api_key ] ) ) {
476 // For api key resets.
477 $api_auth[ $api_key ] = array();
478
479 // Storing it as valid, unless we really get to know from API call.
480 $valid = 'valid';
481 $api_auth[ $api_key ]['validity'] = 'valid';
482 }
483
484 // This is the first check.
485 if ( ! isset( $api_auth[ $api_key ]['timestamp'] ) ) {
486 $api_auth[ $api_key ]['timestamp'] = current_time( 'timestamp' );
487 }
488
489 $request = $this->api()->check( $manual );
490
491 if ( ! is_wp_error( $request ) && 200 === wp_remote_retrieve_response_code( $request ) ) {
492 // Update the timestamp only on successful attempts.
493 $api_auth[ $api_key ]['timestamp'] = current_time( 'timestamp' );
494 update_site_option( 'wp_smush_api_auth', $api_auth );
495
496 $result = json_decode( wp_remote_retrieve_body( $request ) );
497 if ( ! empty( $result->success ) && $result->success ) {
498 $valid = 'valid';
499 update_site_option( WP_SMUSH_PREFIX . 'cdn_status', $result->data );
500 } else {
501 $valid = 'invalid';
502 }
503 } elseif ( ! isset( $valid ) || 'valid' !== $valid ) {
504 // Invalidate only in case when it was not valid before.
505 $valid = 'invalid';
506 }
507
508 $api_auth[ $api_key ]['validity'] = $valid;
509
510 // Update API validity.
511 update_site_option( 'wp_smush_api_auth', $api_auth );
512 }
513
514 self::$is_pro = isset( $valid ) && 'valid' === $valid;
515 }
516
517 /**
518 * Returns api key.
519 *
520 * @return mixed
521 */
522 private static function get_api_key() {
523 $api_key = false;
524
525 // If API key defined manually, get that.
526 if ( defined( 'WPMUDEV_APIKEY' ) && WPMUDEV_APIKEY ) {
527 $api_key = WPMUDEV_APIKEY;
528 } elseif ( class_exists( 'WPMUDEV_Dashboard' ) ) {
529 // If dashboard plugin is active, get API key from db.
530 $api_key = get_site_option( 'wpmudev_apikey' );
531 }
532
533 return $api_key;
534 }
535
536 }
537 }
538