PluginProbe ʕ •ᴥ•ʔ
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN / 3.14.1
Smush – Image Optimization, Compression, Lazy Load, WebP & CDN v3.14.1
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 / core / modules / class-resize-detection.php
wp-smushit / core / modules Last commit date
async 2 years ago background 2 years ago bulk 2 years ago helpers 2 years ago class-abstract-module.php 2 years ago class-backup.php 2 years ago class-cdn.php 2 years ago class-dir.php 2 years ago class-lazy.php 2 years ago class-png2jpg.php 2 years ago class-product-analytics.php 2 years ago class-resize-detection.php 2 years ago class-resize.php 2 years ago class-smush.php 2 years ago class-webp.php 2 years ago
class-resize-detection.php
171 lines
1 <?php
2 /**
3 * Auto resize functionality: Resize_Detection class
4 *
5 * @package Smush\Core\Modules
6 * @version 2.8.0
7 *
8 * @author Joel James <joel@incsub.com>
9 *
10 * @copyright (c) 2018, Incsub (http://incsub.com)
11 */
12
13 namespace Smush\Core\Modules;
14
15 if ( ! defined( 'WPINC' ) ) {
16 die;
17 }
18
19 /**
20 * Class Resize_Detection
21 */
22 class Resize_Detection extends Abstract_Module {
23
24 /**
25 * Is auto detection enabled.
26 *
27 * @var bool
28 */
29 private $can_auto_detect = false;
30
31 /**
32 * Resize_Detection constructor.
33 */
34 public function init() {
35 // Set auto resize flag.
36 add_action( 'wp', array( $this, 'init_flags' ) );
37
38 // Load js file that is required in public facing pages.
39 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_resize_assets' ) );
40
41 // Set a flag to media library images.
42 add_filter( 'smush_cdn_image_tag', array( $this, 'skip_image_resize_detection' ) );
43
44 // Generate markup for the template engine.
45 add_action( 'wp_footer', array( $this, 'generate_markup' ) );
46 }
47
48 /**
49 * Check if auto resize can be performed.
50 *
51 * Allow only if current user is admin and auto resize
52 * detection is enabled in settings.
53 */
54 public function init_flags() {
55 // Only required for admin users.
56 if ( $this->settings->get( 'detection' ) && current_user_can( 'manage_options' ) ) {
57 $this->can_auto_detect = true;
58 }
59 }
60
61 /**
62 * Enqueue JS files required in public pages.
63 *
64 * Enqueue resize detection js and css files to public
65 * facing side of the site. Load only if auto detect
66 * is enabled.
67 *
68 * @return void
69 */
70 public function enqueue_resize_assets() {
71 // Required only if auto detection is required.
72 if ( ! $this->can_auto_detect ) {
73 return;
74 }
75
76 // Required scripts for front end.
77 wp_enqueue_script(
78 'smush-resize-detection',
79 WP_SMUSH_URL . 'app/assets/js/smush-rd.min.js',
80 array( 'jquery' ),
81 WP_SMUSH_VERSION,
82 true
83 );
84
85 // Required styles for front end.
86 wp_enqueue_style(
87 'smush-resize-detection',
88 WP_SMUSH_URL . 'app/assets/css/smush-rd.min.css',
89 array(),
90 WP_SMUSH_VERSION
91 );
92
93 // Define ajaxurl var.
94 wp_localize_script(
95 'smush-resize-detection',
96 'wp_smush_resize_vars',
97 array(
98 'ajaxurl' => admin_url( 'admin-ajax.php' ),
99 'ajax_nonce' => wp_create_nonce( 'smush_resize_nonce' ),
100 // translators: %s - width, %s - height.
101 'large_image' => sprintf( __( 'This image is too large for its container. Adjust the image dimensions to %1$s x %2$spx for optimal results.', 'wp-smushit' ), 'width', 'height' ),
102 // translators: %s - width, %s - height.
103 'small_image' => sprintf( __( 'This image is too small for its container. Adjust the image dimensions to %1$s x %2$spx for optimal results.', 'wp-smushit' ), 'width', 'height' ),
104 )
105 );
106 }
107
108 /**
109 * Generate markup for the template engine.
110 *
111 * @since 2.9
112 */
113 public function generate_markup() {
114 // Required only if auto detection is required.
115 if ( ! $this->can_auto_detect ) {
116 return;
117 }
118 ?>
119 <div id="smush-image-bar-toggle" class="closed">
120 <i class="sui-icon-loader" aria-hidden="true"> </i>
121 </div>
122 <div id="smush-image-bar" class="closed">
123 <h3><?php esc_html_e( 'Image Issues', 'wp-smushit' ); ?></h3>
124 <p>
125 <?php esc_html_e( 'The images listed below are being resized to fit a container. To avoid serving oversized or blurry images, try to match the images to their container sizes.', 'wp-smushit' ); ?>
126 </p>
127
128 <div id="smush-image-bar-items-bigger">
129 <strong><?php esc_html_e( 'Oversized', 'wp-smushit' ); ?></strong>
130 </div>
131 <div id="smush-image-bar-items-smaller">
132 <strong><?php esc_html_e( 'Undersized', 'wp-smushit' ); ?></strong>
133 </div>
134
135 <div id="smush-image-bar-notice">
136 <p><?php esc_html_e( 'All images are properly sized', 'wp-smushit' ); ?></p>
137 </div>
138 <p id="smush-image-bar-notice-desc">
139 <?php esc_html_e( 'Note: It’s not always easy to make this happen, fix up what you can.', 'wp-smushit' ); ?>
140 </p>
141 </div>
142 <?php
143 }
144
145 /**
146 * Exclude images that are hosted on CDN and have auto resize enabled.
147 *
148 * @since 3.2.0
149 *
150 * @param string $image Image tag.
151 *
152 * @return string
153 */
154 public function skip_image_resize_detection( $image ) {
155 // No need to add attachment id if auto detection is not enabled.
156 if ( ! $this->can_auto_detect ) {
157 return $image;
158 }
159
160 // CDN with auto resize need to be enabled.
161 if ( ! $this->settings->get( 'cdn' ) || ! $this->settings->get( 'auto_resize' ) ) {
162 return $image;
163 }
164
165 Helpers\Parser::add_attribute( $image, 'no-resize-detection' );
166
167 return $image;
168 }
169
170 }
171