PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
← All changes | inc/classes/class-imagify-views.php +177 -180 1.10 → trunk View file →
@@ -1,19 +1,22 @@
1 1 <?php
2 -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
3 2
3 +use Imagify\User\User;
4 +use Imagify\Dependencies\WPMedia\PluginFamily\Model\PluginFamily;
5 +use Imagify\Traits\InstanceGetterTrait;
6 +
4 7 /**
5 8 * Class that handles templates and menus.
6 9 *
7 - * @since 1.7
8 - * @author Grégory Viguier
10 + * @since 1.7
9 11 */
10 12 class Imagify_Views {
13 + use InstanceGetterTrait;
11 14
12 15 /**
13 16 * Class version.
14 17 *
15 - * @var string
18 + * @var string
16 19 * @since 1.7
17 20 */
18 21 const VERSION = '1.1';
19 22
@@ -19,11 +22,10 @@
19 22
20 23 /**
21 24 * Slug used for the settings page URL.
22 25 *
23 - * @var string
24 - * @since 1.7
25 - * @access protected
26 + * @var string
27 + * @since 1.7
26 28 */
27 29 protected $slug_settings;
28 30
29 31 /**
@@ -28,11 +30,10 @@
28 30
29 31 /**
30 32 * Slug used for the bulk optimization page URL.
31 33 *
32 - * @var string
33 - * @since 1.7
34 - * @access protected
34 + * @var string
35 + * @since 1.7
35 36 */
36 37 protected $slug_bulk;
37 38
38 39 /**
@@ -37,11 +38,10 @@
37 38
38 39 /**
39 40 * Slug used for the "custom folders" page URL.
40 41 *
41 - * @var string
42 - * @since 1.7
43 - * @access protected
42 + * @var string
43 + * @since 1.7
44 44 */
45 45 protected $slug_files;
46 46
47 47 /**
@@ -46,11 +46,10 @@
46 46
47 47 /**
48 48 * A list of JS templates to print at the end of the page.
49 49 *
50 - * @var array
51 - * @since 1.9
52 - * @access protected
50 + * @var array
51 + * @since 1.9
53 52 */
54 53 protected $templates_in_footer = [];
55 54
56 55 /**
@@ -55,11 +54,10 @@
55 54
56 55 /**
57 56 * Stores the "custom folders" files list instance.
58 57 *
59 - * @var object Imagify_Files_List_Table
60 - * @since 1.7
61 - * @access protected
58 + * @var Imagify_Files_List_Table
59 + * @since 1.7
62 60 */
63 61 protected $list_table;
64 62
65 63 /**
@@ -64,23 +62,19 @@
64 62
65 63 /**
66 64 * Filesystem object.
67 65 *
68 - * @var object Imagify_Filesystem
69 - * @since 1.7.1
70 - * @access protected
71 - * @author Grégory Viguier
66 + * @var Imagify_Filesystem
67 + * @since 1.7.1
72 68 */
73 69 protected $filesystem;
74 70
75 71 /**
76 - * The single instance of the class.
72 + * Imagify admin bar menu.
77 73 *
78 - * @var object
79 - * @since 1.7
80 - * @access protected
74 + * @var bool
81 75 */
82 - protected static $_instance;
76 + private $admin_menu_is_present = false;
83 77
84 78
85 79 /** ----------------------------------------------------------------------------------------- */
86 80 /** INSTANCE/INIT =========================================================================== */
@@ -88,11 +82,9 @@
88 82
89 83 /**
90 84 * The constructor.
91 85 *
92 - * @since 1.7
93 - * @author Grégory Viguier
94 - * @access protected
86 + * @since 1.7
95 87 */
96 88 protected function __construct() {
97 89 $this->slug_settings = IMAGIFY_SLUG;
98 90 $this->slug_bulk = IMAGIFY_SLUG . '-bulk-optimization';
@@ -100,30 +92,11 @@
100 92 $this->filesystem = Imagify_Filesystem::get_instance();
101 93 }
102 94
103 95 /**
104 - * Get the main Instance.
105 - *
106 - * @since 1.7
107 - * @author Grégory Viguier
108 - * @access public
109 - *
110 - * @return object Main instance.
111 - */
112 - public static function get_instance() {
113 - if ( ! isset( self::$_instance ) ) {
114 - self::$_instance = new self();
115 - }
116 -
117 - return self::$_instance;
118 - }
119 -
120 - /**
121 96 * Launch the hooks.
122 97 *
123 - * @since 1.7
124 - * @author Grégory Viguier
125 - * @access public
98 + * @since 1.7
126 99 */
127 100 public function init() {
128 101 // Menu items.
129 102 add_action( 'admin_menu', [ $this, 'add_site_menus' ] );
@@ -131,18 +104,15 @@
131 104 if ( imagify_is_active_for_network() ) {
132 105 add_action( 'network_admin_menu', [ $this, 'add_network_menus' ] );
133 106 }
134 107
135 - // Action links in plugins list.
136 - $basename = plugin_basename( IMAGIFY_FILE );
137 - add_filter( 'plugin_action_links_' . $basename, [ $this, 'plugin_action_links' ] );
138 - add_filter( 'network_admin_plugin_action_links_' . $basename, [ $this, 'plugin_action_links' ] );
139 -
140 108 // Save the "per page" option value from the files list screen.
141 109 add_filter( 'set-screen-option', [ 'Imagify_Files_List_Table', 'save_screen_options' ], 10, 3 );
142 110
143 111 // JS templates in footer.
144 112 add_action( 'admin_print_footer_scripts', [ $this, 'print_js_templates' ] );
113 + add_action( 'admin_footer', [ $this, 'print_modal_payment' ] );
114 + add_action( 'wp_before_admin_bar_render', [ $this, 'maybe_print_modal_payment' ] );
145 115 }
146 116
147 117
148 118 /** ----------------------------------------------------------------------------------------- */
@@ -151,11 +121,9 @@
151 121
152 122 /**
153 123 * Add sub-menus for all sites.
154 124 *
155 - * @since 1.7
156 - * @author Grégory Viguier
157 - * @access public
125 + * @since 1.7
158 126 */
159 127 public function add_site_menus() {
160 128 $wp_context = imagify_get_context( 'wp' );
161 129
@@ -186,11 +154,9 @@
186 154
187 155 /**
188 156 * Add menu and sub-menus in the network admin when Imagify is network-activated.
189 157 *
190 - * @since 1.7
191 - * @author Grégory Viguier
192 - * @access public
158 + * @since 1.7
193 159 */
194 160 public function add_network_menus() {
195 161 global $submenu;
196 162
@@ -197,9 +163,9 @@
197 163 $wp_context = imagify_get_context( 'wp' );
198 164
199 165 if ( ! imagify_can_optimize_custom_folders() ) {
200 166 // Main item: settings (edge case).
201 - add_menu_page( 'Imagify', 'Imagify', $wp_context->get_capacity( 'manage' ), $this->get_settings_page_slug(), array( $this, 'display_settings_page' ) );
167 + add_menu_page( 'Imagify', 'Imagify', $wp_context->get_capacity( 'manage' ), $this->get_settings_page_slug(), [ $this, 'display_settings_page' ] );
202 168 return;
203 169 }
204 170
205 171 $cf_context = imagify_get_context( 'custom-folders' );
@@ -204,51 +170,28 @@
204 170
205 171 $cf_context = imagify_get_context( 'custom-folders' );
206 172
207 173 // Main item: bulk optimization (custom folders).
208 - add_menu_page( __( 'Bulk Optimization', 'imagify' ), 'Imagify', $cf_context->current_user_can( 'bulk-optimize' ), $this->get_bulk_page_slug(), array( $this, 'display_bulk_page' ) );
174 + add_menu_page( __( 'Bulk Optimization', 'imagify' ), 'Imagify', $cf_context->get_capacity( 'bulk-optimize' ), $this->get_bulk_page_slug(), [ $this, 'display_bulk_page' ] );
209 175
210 176 // Sub-menu item: custom folders list.
211 - $screen_id = add_submenu_page( $this->get_bulk_page_slug(), __( 'Other Media optimized by Imagify', 'imagify' ), __( 'Other Media', 'imagify' ), $cf_context->current_user_can( 'bulk-optimize' ), $this->get_files_page_slug(), array( $this, 'display_files_list' ) );
177 + $screen_id = add_submenu_page( $this->get_bulk_page_slug(), __( 'Other Media optimized by Imagify', 'imagify' ), __( 'Other Media', 'imagify' ), $cf_context->get_capacity( 'bulk-optimize' ), $this->get_files_page_slug(), [ $this, 'display_files_list' ] );
212 178
213 179 // Sub-menu item: settings.
214 - add_submenu_page( $this->get_bulk_page_slug(), 'Imagify', __( 'Settings', 'imagify' ), $wp_context->get_capacity( 'manage' ), $this->get_settings_page_slug(), array( $this, 'display_settings_page' ) );
180 + add_submenu_page( $this->get_bulk_page_slug(), 'Imagify', __( 'Settings', 'imagify' ), $wp_context->get_capacity( 'manage' ), $this->get_settings_page_slug(), [ $this, 'display_settings_page' ] );
215 181
216 182 // Change the sub-menu label.
217 183 if ( ! empty( $submenu[ $this->get_bulk_page_slug() ] ) ) {
218 - $submenu[ $this->get_bulk_page_slug() ][0][0] = __( 'Bulk Optimization', 'imagify' ); // WPCS: override ok.
184 + $submenu[ $this->get_bulk_page_slug() ][0][0] = __( 'Bulk Optimization', 'imagify' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
219 185 }
220 186
221 187 if ( $screen_id ) {
222 188 // On the "Other Media optimized by Imagify" page, load the data.
223 - add_action( 'load-' . $screen_id, array( $this, 'load_files_list' ) );
189 + add_action( 'load-' . $screen_id, [ $this, 'load_files_list' ] );
224 190 }
225 191 }
226 192
227 -
228 193 /** ----------------------------------------------------------------------------------------- */
229 - /** PLUGIN ACTION LINKS ===================================================================== */
230 - /** ----------------------------------------------------------------------------------------- */
231 -
232 - /**
233 - * Add links to the plugin row in the plugins list.
234 - *
235 - * @since 1.7
236 - * @author Grégory Viguier
237 - * @access public
238 - *
239 - * @param array $actions An array of action links.
240 - * @return array
241 - */
242 - public function plugin_action_links( $actions ) {
243 - array_unshift( $actions, sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( imagify_get_external_url( 'documentation' ) ), __( 'Documentation', 'imagify' ) ) );
244 - array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url( 'bulk-optimization' ) ), __( 'Bulk Optimization', 'imagify' ) ) );
245 - array_unshift( $actions, sprintf( '<a href="%s">%s</a>', esc_url( get_imagify_admin_url() ), __( 'Settings', 'imagify' ) ) );
246 - return $actions;
247 - }
248 -
249 -
250 - /** ----------------------------------------------------------------------------------------- */
251 194 /** MAIN PAGE TEMPLATES ===================================================================== */
252 195 /** ----------------------------------------------------------------------------------------- */
253 196
254 197 /**
@@ -253,33 +196,37 @@
253 196
254 197 /**
255 198 * The main settings page.
256 199 *
257 - * @since 1.7
258 - * @author Grégory Viguier
259 - * @access public
200 + * @since 1.7
260 201 */
261 202 public function display_settings_page() {
262 - $this->print_template( 'page-settings' );
203 + $plugin_family = new PluginFamily();
204 + $plugins_array = $plugin_family->get_filtered_plugins( 'imagify/imagify' );
205 +
206 + $data = [
207 + 'hide_plugin_family' => wpm_apply_filters_typed( 'boolean', 'imagify_hide_plugin_family', false ),
208 + 'plugin_family' => $plugins_array['uncategorized'],
209 + ];
210 +
211 + $this->print_template( 'page-settings', $data );
263 212 }
264 213
265 214 /**
266 215 * The bulk optimization page.
267 216 *
268 - * @since 1.7
269 - * @author Grégory Viguier
270 - * @access public
217 + * @since 1.7
271 218 */
272 219 public function display_bulk_page() {
273 - $types = array();
274 - $data = array(
220 + $types = [];
221 + $data = [
275 222 // Limits.
276 223 'unoptimized_attachment_limit' => 0,
277 224 // What to optimize.
278 225 'icon' => 'images-alt2',
279 226 'title' => __( 'Optimize your media files', 'imagify' ),
280 - 'groups' => array(),
281 - );
227 + 'groups' => [],
228 + ];
282 229
283 230 if ( imagify_is_screen( 'bulk' ) ) {
284 231 if ( ! is_network_admin() ) {
285 232 /**
@@ -287,9 +234,17 @@
287 234 */
288 235 $types['library|wp'] = 1;
289 236 }
290 237
291 - if ( imagify_can_optimize_custom_folders() && ( imagify_is_active_for_network() && is_network_admin() || ! imagify_is_active_for_network() ) ) {
238 + if (
239 + imagify_can_optimize_custom_folders()
240 + &&
241 + (
242 + ( imagify_is_active_for_network() && is_network_admin() )
243 + ||
244 + ! imagify_is_active_for_network()
245 + )
246 + ) {
292 247 /**
293 248 * Custom folders: in network admin only if network activated, in each site otherwise.
294 249 */
295 250 $types['custom-folders|custom-folders'] = 1;
@@ -310,9 +265,9 @@
310 265 if ( isset( $types['library|wp'] ) ) {
311 266 // Limits.
312 267 $data['unoptimized_attachment_limit'] += imagify_get_unoptimized_attachment_limit();
313 268 // Group.
314 - $data['groups']['library'] = array(
269 + $data['groups']['library'] = [
315 270 /**
316 271 * The group_id corresponds to the file names like 'part-bulk-optimization-results-row-{$group_id}'.
317 272 * It is also used in get_imagify_localize_script_translations().
318 273 */
@@ -320,31 +275,36 @@
320 275 'context' => 'wp',
321 276 'title' => __( 'Media Library', 'imagify' ),
322 277 /* translators: 1 is the opening of a link, 2 is the closing of this link. */
323 278 'footer' => sprintf( __( 'You can also re-optimize your media files from your %1$sMedia Library%2$s screen.', 'imagify' ), '<a href="' . esc_url( admin_url( 'upload.php' ) ) . '">', '</a>' ),
324 - );
279 + ];
325 280 }
326 281
327 282 if ( isset( $types['custom-folders|custom-folders'] ) ) {
328 283 if ( ! Imagify_Folders_DB::get_instance()->has_items() ) {
329 - // New Feature!
330 284 $data['no-custom-folders'] = true;
331 285 } elseif ( Imagify_Folders_DB::get_instance()->has_active_folders() ) {
332 286 // Group.
333 - $data['groups']['custom-folders'] = array(
287 + $data['groups']['custom-folders'] = [
334 288 'group_id' => 'custom-folders',
335 289 'context' => 'custom-folders',
336 290 'title' => __( 'Custom folders', 'imagify' ),
337 291 /* translators: 1 is the opening of a link, 2 is the closing of this link. */
338 292 'footer' => sprintf( __( 'You can re-optimize your media files more finely directly in the %1$smedia management%2$s.', 'imagify' ), '<a href="' . esc_url( get_imagify_admin_url( 'files-list' ) ) . '">', '</a>' ),
339 - );
293 + ];
340 294 }
341 295 }
342 296
343 297 // Add generic stats.
344 - $data = array_merge( $data, imagify_get_bulk_stats( $types, array(
345 - 'fullset' => true,
346 - ) ) );
298 + $data = array_merge(
299 + $data,
300 + imagify_get_bulk_stats(
301 + $types,
302 + [
303 + 'fullset' => true,
304 + ]
305 + )
306 + );
347 307
348 308 /**
349 309 * Filter the data to use on the bulk optimization page.
350 310 *
@@ -362,11 +322,9 @@
362 322
363 323 /**
364 324 * The page displaying the "custom folders" files.
365 325 *
366 - * @since 1.7
367 - * @author Grégory Viguier
368 - * @access public
326 + * @since 1.7
369 327 */
370 328 public function display_files_list() {
371 329 $this->print_template( 'page-files-list' );
372 330 }
@@ -373,17 +331,17 @@
373 331
374 332 /**
375 333 * Initiate the "custom folders" list table data.
376 334 *
377 - * @since 1.7
378 - * @author Grégory Viguier
379 - * @access public
335 + * @since 1.7
380 336 */
381 337 public function load_files_list() {
382 338 // Instantiate the list.
383 - $this->list_table = new Imagify_Files_List_Table( array(
384 - 'screen' => 'imagify-files',
385 - ) );
339 + $this->list_table = new Imagify_Files_List_Table(
340 + [
341 + 'screen' => 'imagify-files',
342 + ]
343 + );
386 344
387 345 // Query the Items.
388 346 $this->list_table->prepare_items();
389 347 }
@@ -395,11 +353,9 @@
395 353
396 354 /**
397 355 * Get the settings page slug.
398 356 *
399 - * @since 1.7
400 - * @author Grégory Viguier
401 - * @access public
357 + * @since 1.7
402 358 *
403 359 * @return string
404 360 */
405 361 public function get_settings_page_slug() {
@@ -408,11 +364,9 @@
408 364
409 365 /**
410 366 * Get the bulk optimization page slug.
411 367 *
412 - * @since 1.7
413 - * @author Grégory Viguier
414 - * @access public
368 + * @since 1.7
415 369 *
416 370 * @return string
417 371 */
418 372 public function get_bulk_page_slug() {
@@ -421,11 +375,9 @@
421 375
422 376 /**
423 377 * Get the "custom folders" files page slug.
424 378 *
425 - * @since 1.7
426 - * @author Grégory Viguier
427 - * @access public
379 + * @since 1.7
428 380 *
429 381 * @return string
430 382 */
431 383 public function get_files_page_slug() {
@@ -439,11 +391,9 @@
439 391
440 392 /**
441 393 * Tell if we’re displaying the settings page.
442 394 *
443 - * @since 1.9
444 - * @author Grégory Viguier
445 - * @access public
395 + * @since 1.9
446 396 *
447 397 * @return bool
448 398 */
449 399 public function is_settings_page() {
@@ -448,10 +398,14 @@
448 398 */
449 399 public function is_settings_page() {
450 400 global $pagenow;
451 401
452 - $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
402 + if ( ! isset( $_GET['page'] ) ) {
403 + return false;
404 + }
453 405
406 + $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
407 +
454 408 if ( $this->get_settings_page_slug() !== $page ) {
455 409 return false;
456 410 }
457 411
@@ -464,11 +418,9 @@
464 418
465 419 /**
466 420 * Tell if we’re displaying the bulk optimization page.
467 421 *
468 - * @since 1.9
469 - * @author Grégory Viguier
470 - * @access public
422 + * @since 1.9
471 423 *
472 424 * @return bool
473 425 */
474 426 public function is_bulk_page() {
@@ -473,10 +425,14 @@
473 425 */
474 426 public function is_bulk_page() {
475 427 global $pagenow;
476 428
477 - $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
429 + if ( ! isset( $_GET['page'] ) ) {
430 + return false;
431 + }
478 432
433 + $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
434 +
479 435 return 'upload.php' === $pagenow && $this->get_bulk_page_slug() === $page;
480 436 }
481 437
482 438 /**
@@ -481,11 +437,9 @@
481 437
482 438 /**
483 439 * Tell if we’re displaying the custom files list page.
484 440 *
485 - * @since 1.9
486 - * @author Grégory Viguier
487 - * @access public
441 + * @since 1.9
488 442 *
489 443 * @return bool
490 444 */
491 445 public function is_files_page() {
@@ -490,10 +444,14 @@
490 444 */
491 445 public function is_files_page() {
492 446 global $pagenow;
493 447
494 - $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
448 + if ( ! isset( $_GET['page'] ) ) {
449 + return false;
450 + }
495 451
452 + $page = sanitize_text_field( wp_unslash( $_GET['page'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
453 +
496 454 return 'upload.php' === $pagenow && $this->get_files_page_slug() === $page;
497 455 }
498 456
499 457 /**
@@ -498,11 +456,9 @@
498 456
499 457 /**
500 458 * Tell if we’re displaying the WP media library page.
501 459 *
502 - * @since 1.9
503 - * @author Grégory Viguier
504 - * @access public
460 + * @since 1.9
505 461 *
506 462 * @return bool
507 463 */
508 464 public function is_wp_library_page() {
@@ -507,19 +463,15 @@
507 463 */
508 464 public function is_wp_library_page() {
509 465 global $pagenow;
510 466
511 - $page = filter_input( INPUT_GET, 'page', FILTER_SANITIZE_STRING );
512 -
513 - return 'upload.php' === $pagenow && ! $page;
467 + return 'upload.php' === $pagenow && ! isset( $_GET['page'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
514 468 }
515 469
516 470 /**
517 471 * Tell if we’re displaying a media page.
518 472 *
519 - * @since 1.9
520 - * @author Grégory Viguier
521 - * @access public
473 + * @since 1.9
522 474 *
523 475 * @return bool
524 476 */
525 477 public function is_media_page() {
@@ -535,11 +487,9 @@
535 487
536 488 /**
537 489 * Get the remaining quota in percent.
538 490 *
539 - * @since 1.8.1
540 - * @author Grégory Viguier
541 - * @access public
491 + * @since 1.8.1
542 492 *
543 493 * @return int
544 494 */
545 495 public function get_quota_percent() {
@@ -548,9 +498,9 @@
548 498 if ( isset( $quota ) ) {
549 499 return $quota;
550 500 }
551 501
552 - $user = new Imagify_User();
502 + $user = new User();
553 503 $quota = $user->get_percent_unconsumed_quota();
554 504
555 505 return $quota;
556 506 }
@@ -557,11 +507,9 @@
557 507
558 508 /**
559 509 * Get the HTML class used for the quota (to change the color when out of quota for example).
560 510 *
561 - * @since 1.8.1
562 - * @author Grégory Viguier
563 - * @access public
511 + * @since 1.8.1
564 512 *
565 513 * @return string
566 514 */
567 515 public function get_quota_class() {
@@ -587,11 +535,9 @@
587 535
588 536 /**
589 537 * Get the HTML tag used for the quota (the weather-like icon).
590 538 *
591 - * @since 1.8.1
592 - * @author Grégory Viguier
593 - * @access public
539 + * @since 1.8.1
594 540 *
595 541 * @return string
596 542 */
597 543 public function get_quota_icon() {
@@ -603,9 +549,9 @@
603 549
604 550 $quota = $this->get_quota_percent();
605 551
606 552 if ( $quota <= 20 ) {
607 - $icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="64" height="63" alt="" />';
553 + $icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'stormy.svg" width="40" height="63" alt="" />';
608 554 } elseif ( $quota <= 50 ) {
609 555 $icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'cloudy-sun.svg" width="63" height="64" alt="" />';
610 556 } else {
611 557 $icon = '<img src="' . IMAGIFY_ASSETS_IMG_URL . 'sun.svg" width="63" height="64" alt="" />';
@@ -621,17 +567,15 @@
621 567
622 568 /**
623 569 * Get a template contents.
624 570 *
625 - * @since 1.7
626 - * @author Grégory Viguier
627 - * @access public
571 + * @since 1.7
628 572 *
629 573 * @param string $template The template name.
630 574 * @param mixed $data Some data to pass to the template.
631 575 * @return string|bool The page contents. False if the template doesn't exist.
632 576 */
633 - public function get_template( $template, $data = array() ) {
577 + public function get_template( $template, $data = [] ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
634 578 $path = str_replace( '_', '-', $template );
635 579 $path = IMAGIFY_PATH . 'views/' . $template . '.php';
636 580
637 581 if ( ! $this->filesystem->exists( $path ) ) {
@@ -647,25 +591,21 @@
647 591
648 592 /**
649 593 * Print a template.
650 594 *
651 - * @since 1.7
652 - * @author Grégory Viguier
653 - * @access public
595 + * @since 1.7
654 596 *
655 597 * @param string $template The template name.
656 598 * @param mixed $data Some data to pass to the template.
657 599 */
658 - public function print_template( $template, $data = array() ) {
659 - echo $this->get_template( $template, $data );
600 + public function print_template( $template, $data = [] ) {
601 + echo $this->get_template( $template, $data ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
660 602 }
661 603
662 604 /**
663 605 * Add a template to the list of JS templates to print at the end of the page.
664 606 *
665 - * @since 1.7
666 - * @author Grégory Viguier
667 - * @access public
607 + * @since 1.7
668 608 *
669 609 * @param string $template The template name.
670 610 */
671 611 public function print_js_template_in_footer( $template ) {
@@ -690,11 +630,9 @@
690 630
691 631 /**
692 632 * Print the JS templates that have been added to the "queue".
693 633 *
694 - * @since 1.9
695 - * @author Grégory Viguier
696 - * @access public
634 + * @since 1.9
697 635 */
698 636 public function print_js_templates() {
699 637 if ( ! $this->templates_in_footer ) {
700 638 return;
@@ -702,15 +640,76 @@
702 640
703 641 foreach ( $this->templates_in_footer as $template => $data ) {
704 642 $template_id = str_replace( [ '/', '_' ], '-', $template );
705 643
706 - echo '<script type="text/html" id="tmpl-imagify-' . $template_id . '">';
644 + echo '<script type="text/html" id="tmpl-imagify-' . esc_attr( $template_id ) . '">';
707 645 $this->print_template( $template, $data );
708 646 echo '</script>';
709 647 }
710 648 }
711 649
650 + /**
651 + * Get imagify user info
652 + *
653 + * @return bool
654 + */
655 + private function get_user_info(): bool {
656 + $user = new User();
657 + $unconsumed_quota = $user->get_percent_unconsumed_quota();
712 658
659 + return ( ! $user->is_infinite() && $unconsumed_quota <= 20 )
660 + || ( $user->is_free() && $unconsumed_quota > 20 );
661 + }
662 +
663 + /**
664 + * Start print the payment modal process.
665 + */
666 + public function maybe_print_modal_payment() {
667 + if ( $this->get_user_info() ) {
668 + global $wp_admin_bar;
669 + $this->admin_menu_is_present = $wp_admin_bar && $wp_admin_bar->get_node( 'imagify' );
670 +
671 + return;
672 + }
673 +
674 + $this->admin_menu_is_present = false;
675 + }
676 +
677 + /**
678 + * Print the payment modal.
679 + *
680 + * @return void
681 + */
682 + public function print_modal_payment() {
683 + if ( is_admin_bar_showing() && $this->admin_menu_is_present ) {
684 + $this->print_template(
685 + 'modal-payment',
686 + [
687 + 'attachments_number' => $this->get_attachments_number_modal(),
688 + ]
689 + );
690 + }
691 + }
692 +
693 + /**
694 + * Get the number of attachments to display in the payment modal.
695 + *
696 + * @return int
697 + */
698 + private function get_attachments_number_modal() {
699 + $transient = get_transient( 'imagify_attachments_number_modal' );
700 +
701 + if ( false !== $transient ) {
702 + return $transient;
703 + }
704 +
705 + $attachments_number = imagify_count_attachments() + Imagify_Files_Stats::count_all_files();
706 +
707 + set_transient( 'imagify_attachments_number_modal', $attachments_number, 1 * DAY_IN_SECONDS );
708 +
709 + return $attachments_number;
710 + }
711 +
713 712 /** ----------------------------------------------------------------------------------------- */
714 713 /** TOOLS =================================================================================== */
715 714 /** ----------------------------------------------------------------------------------------- */
716 715
@@ -716,11 +715,9 @@
716 715
717 716 /**
718 717 * Create HTML attributes from an array.
719 718 *
720 - * @since 1.9
721 - * @access public
722 - * @author Grégory Viguier
719 + * @since 1.9
723 720 *
724 721 * @param array $attributes A list of attribute pairs.
725 722 * @return string HTML attributes.
726 723 */