PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.2.1
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.2.1
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / modules / core / components / user-feedback.php

user-feedback.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.2.1, at modules/core/components/user-feedback.php

279 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Core\Components;
4
5 use ImageOptimization\Classes\Image\Image_Query_Builder;
6 use ImageOptimization\Classes\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 class User_Feedback {
13 public const FEEDBACK_LINK = 'https://wordpress.org/support/plugin/image-optimization/reviews/?filter=5#new-post';
14 private const FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-first-image-optimized-feedback';
15 private const THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG = 'image-optimizer-third-images-optimized-feedback';
16
17 /**
18 * Checks if the notice after the first optimization should be rendered.
19 *
20 * @param bool $check_location
21 *
22 * @return bool
23 */
24 public function should_render_first_optimized_notice( bool $check_location = true ): bool {
25 if ( Pointers::is_dismissed( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ) ) {
26 return false;
27 }
28
29 $is_wrong_page = ( ! Utils::is_media_page() && ! Utils::is_plugin_page() && ! Utils::is_single_attachment_page() ) ||
30 Utils::is_plugin_settings_page();
31
32 if ( $check_location && $is_wrong_page ) {
33 return false;
34 }
35
36 $optimized_image_query = ( new Image_Query_Builder() )
37 ->return_optimized_images()
38 ->set_paging_size( 1 )
39 ->execute();
40
41 if ( ! $optimized_image_query->post_count ) {
42 return false;
43 }
44
45 return true;
46 }
47
48 /**
49 * Checks if the notice should be rendered after the one third of images were optimized.
50 *
51 * @param bool $check_location
52 *
53 * @return bool
54 */
55 public function should_render_third_optimized_notice( bool $check_location = true ): bool {
56 if ( Pointers::is_dismissed( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ) ) {
57 return false;
58 }
59
60 if ( $check_location && ! Utils::is_plugin_settings_page() ) {
61 return false;
62 }
63
64 $images_query = ( new Image_Query_Builder() )
65 ->execute();
66
67 $optimized_images_query = ( new Image_Query_Builder() )
68 ->return_optimized_images()
69 ->execute();
70
71 if ( ! $optimized_images_query->post_count ) {
72 return false;
73 }
74
75 $optimized_percentage = round( $optimized_images_query->post_count / $images_query->post_count * 100 );
76
77 if ( $optimized_percentage < 33.33 ) {
78 return false;
79 }
80
81 return true;
82 }
83
84 /**
85 * Renders the notice after the first optimization.
86 *
87 * @return void
88 */
89 public function render_first_optimized_notice() {
90 ?>
91 <div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback"
92 data-notice-slug="<?php echo esc_attr( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>">
93 <p>
94 <b>
95 <?php esc_html_e(
96 'Your image has been optimized!',
97 'image-optimization'
98 ); ?>
99 </b>
100
101 <span>
102 <?php printf(
103 __(
104 'If you enjoyed using Image Optimizer, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank"
105 rel="noopener noreferrer">�
106 �
107 �
108 �
109 �
110 </a> review to spread the word.',
111 'image-optimization'
112 ),
113 esc_url( self::FEEDBACK_LINK ),
114 esc_attr__( 'Five stars', 'image-optimization' )
115 ); ?>
116 </span>
117 </p>
118 </div>
119
120 <script>
121 const onClose = () => {
122 const pointer = '<?php
123 echo $this->should_render_third_optimized_notice( false ) ?
124 esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) :
125 esc_js( static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG )
126 ?>';
127
128 return wp.ajax.post( 'image_optimizer_pointer_dismissed', {
129 data: {
130 pointer,
131 },
132 nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>',
133 } );
134 }
135
136 jQuery( document ).ready( function( $ ) {
137 setTimeout(() => {
138 const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' )
139
140 $closeButton
141 .first()
142 .on( 'click', onClose )
143
144 $( '[data-notice-slug="<?php echo esc_js( self::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' )
145 .first()
146 .on( 'click', function ( e ) {
147 e.preventDefault();
148
149 onClose().promise().done(() => {
150 window.open( $( this ).attr( 'href' ), '_blank' ).focus();
151
152 $closeButton.click();
153 });
154 })
155 }, 0);
156 } );
157 </script>
158 <?php
159 }
160
161 /**
162 * Renders the notice after the one third of images were optimized.
163 *
164 * @return void
165 */
166 public function render_third_optimized_notice() {
167 ?>
168 <div class="notice is-dismissible notice-success notice image-optimizer__notice image-optimizer__notice--success image-optimizer__notice--feedback"
169 data-notice-slug="<?php echo esc_attr( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>">
170 <p>
171 <b>
172 <?php esc_html_e(
173 'Thanks for using Image Optimizer!',
174 'image-optimization'
175 ); ?>
176 </b>
177
178 <span>
179 <?php printf(
180 __(
181 'If you\'ve enjoyed it, consider leaving a <a href="%1$s" aria-label="%2$s" target="_blank"
182 rel="noopener noreferrer">�
183 �
184 �
185 �
186 �
187 </a> review to spread the word.',
188 'image-optimization'
189 ),
190 esc_url( self::FEEDBACK_LINK ),
191 esc_attr__( 'Five stars', 'image-optimization' )
192 ); ?>
193 </span>
194 </p>
195 </div>
196
197 <script>
198 const onClose = () => {
199 const pointer = '<?php
200 echo $this->should_render_first_optimized_notice( false ) ?
201 esc_js( join( ',', [ static::FIRST_IMAGE_OPTIMIZED_FEEDBACK_SLUG, static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ] ) ) :
202 esc_js( static::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG )
203 ?>';
204
205 return wp.ajax.post( 'image_optimizer_pointer_dismissed', {
206 data: {
207 pointer,
208 },
209 nonce: '<?php echo esc_js( wp_create_nonce( 'image-optimization-pointer-dismissed' ) ); ?>',
210 } );
211 }
212
213 jQuery( document ).ready( function( $ ) {
214 setTimeout(() => {
215 const $closeButton = $( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] .notice-dismiss' );
216
217 $closeButton
218 .first()
219 .on( 'click', onClose)
220
221 $( '[data-notice-slug="<?php echo esc_js( self::THIRD_OF_IMAGES_OPTIMIZED_FEEDBACK_SLUG ); ?>"] a' )
222 .first()
223 .on( 'click', function ( e ) {
224 e.preventDefault();
225
226 onClose().promise().done(() => {
227 window.open( $( this ).attr( 'href' ), '_blank' ).focus();
228
229 $closeButton.click();
230 });
231 })
232 }, 0);
233 } );
234 </script>
235 <?php
236 }
237
238 /**
239 * Renders the admin footer feedback notice.
240 *
241 * @return void
242 */
243 public function add_leave_feedback_footer_text(): void {
244 printf(
245 __( '<b>Found Image Optimizer helpful?</b> Leave us a <a href="%1$s" aria-label="%2$s" target="_blank"
246 rel="noopener noreferrer">�
247 �
248 �
249 �
250 �
251 </a> rating!',
252 'image-optimization'
253 ),
254 esc_url( self::FEEDBACK_LINK ),
255 esc_attr__( 'Five stars', 'image-optimization' )
256 );
257 }
258
259 public function __construct() {
260 add_action('current_screen', function () {
261 if ( ! Utils::user_is_admin() ) {
262 return;
263 }
264
265 if ( $this->should_render_first_optimized_notice() ) {
266 add_action( 'admin_notices', [ $this, 'render_first_optimized_notice' ] );
267 }
268
269 if ( $this->should_render_third_optimized_notice() ) {
270 add_action( 'admin_notices', [ $this, 'render_third_optimized_notice' ] );
271 }
272
273 if ( Utils::is_media_page() || Utils::is_plugin_page() || Utils::is_single_attachment_page() ) {
274 add_filter( 'admin_footer_text', [ $this, 'add_leave_feedback_footer_text' ] );
275 }
276 });
277 }
278 }
279