PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.7
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.7
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
imagify / inc / classes / class-imagify-assets.php

class-imagify-assets.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 1.7, at inc/classes/class-imagify-assets.php

850 lines 23.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) || die( 'Cheatin\' uh?' );
3
4 /**
5 * Class that handles stylesheets and JavaScripts.
6 *
7 * @since 1.6.10
8 * @author Grégory Viguier
9 */
10 class Imagify_Assets {
11
12 /**
13 * Class version.
14 *
15 * @var string
16 */
17 const VERSION = '1.0.2';
18
19 /**
20 * Prefix used for stylesheet handles.
21 *
22 * @var string
23 */
24 const CSS_PREFIX = 'imagify-';
25
26 /**
27 * Prefix used for script handles.
28 *
29 * @var string
30 */
31 const JS_PREFIX = 'imagify-';
32
33 /**
34 * An array containing our registered styles.
35 *
36 * @var array
37 */
38 protected $styles = array();
39
40 /**
41 * An array containing our registered scripts.
42 *
43 * @var array
44 */
45 protected $scripts = array();
46
47 /**
48 * Current handle.
49 *
50 * @var string
51 */
52 protected $current_handle;
53
54 /**
55 * Current handle type.
56 *
57 * @var string 'css' or 'js'.
58 */
59 protected $current_handle_type;
60
61 /**
62 * Array of scripts that should be localized when they are enqueued.
63 *
64 * @var array
65 */
66 protected $deferred_localizations = array();
67
68 /**
69 * A "random" script version to use when debug is on.
70 *
71 * @var int
72 */
73 protected static $version;
74
75 /**
76 * The single instance of the class.
77 *
78 * @var object
79 */
80 protected static $_instance;
81
82 /**
83 * The constructor.
84 *
85 * @return void
86 */
87 protected function __construct() {
88 if ( ! isset( self::$version ) ) {
89 self::$version = time();
90 }
91 }
92
93
94 /** ----------------------------------------------------------------------------------------- */
95 /** PUBLIC METHODS ========================================================================== */
96 /** ----------------------------------------------------------------------------------------- */
97
98 /**
99 * Get the main Instance.
100 *
101 * @since 1.6.10
102 * @author Grégory Viguier
103 *
104 * @return object Main instance.
105 */
106 public static function get_instance() {
107 if ( ! isset( self::$_instance ) ) {
108 self::$_instance = new self();
109 }
110
111 return self::$_instance;
112 }
113
114 /**
115 * Launch the hooks.
116 *
117 * @since 1.6.10
118 * @author Grégory Viguier
119 */
120 public function init() {
121 if ( ! is_admin() ) {
122 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts_frontend' ) );
123 return;
124 }
125
126 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles_and_scripts' ), IMAGIFY_INT_MAX );
127 add_action( 'wp_enqueue_media', array( $this, 'enqueue_media_modal' ) );
128
129 add_action( 'admin_footer-media_page_imagify-bulk-optimization', array( $this, 'print_support_script' ) );
130 add_action( 'admin_footer-settings_page_imagify', array( $this, 'print_support_script' ) );
131 }
132
133 /**
134 * Enqueue stylesheets and scripts for the frontend.
135 *
136 * @since 1.6.10
137 * @author Grégory Viguier
138 */
139 public function enqueue_styles_and_scripts_frontend() {
140 if ( ! $this->is_admin_bar_item_showing() ) {
141 return;
142 }
143
144 $this->register_style( 'admin-bar' );
145 $this->register_script( 'admin-bar', 'admin-bar', array( 'jquery' ) );
146
147 $this->enqueue_assets( 'admin-bar' )->localize( 'imagifyAdminBar' );
148 }
149
150 /**
151 * Register stylesheets and scripts for the administration area.
152 *
153 * @since 1.6.10
154 * @author Grégory Viguier
155 */
156 public function register_styles_and_scripts() {
157 static $done = false;
158
159 if ( $done ) {
160 return;
161 }
162 $done = true;
163
164 /**
165 * 3rd Party Styles.
166 */
167 $this->register_style( 'sweetalert-core', 'sweetalert2', array(), '4.6.6' );
168
169 /**
170 * Imagify Styles.
171 */
172 $this->register_style( 'sweetalert', 'sweetalert-custom', array( 'sweetalert-core' ) );
173
174 $this->register_style( 'admin-bar' );
175
176 $this->register_style( 'admin' );
177
178 $this->register_style( 'notices', 'notices', array( 'admin' ) ); // Needs SweetAlert on some cases.
179
180 $this->register_style( 'twentytwenty', 'twentytwenty', array( 'admin' ) );
181
182 $this->register_style( 'pricing-modal', 'pricing-modal', array( 'admin' ) );
183
184 $this->register_style( 'bulk', 'bulk', array( 'sweetalert', 'admin' ) );
185
186 $this->register_style( 'options', 'options', array( 'sweetalert', 'admin' ) );
187
188 $this->register_style( 'files-list', 'files-list', array( 'admin' ) );
189
190 /**
191 * 3rd Party Scripts.
192 */
193 $this->register_script( 'promise-polyfill', 'es6-promise.auto', array(), '4.1.1' );
194
195 $this->register_script( 'sweetalert', 'sweetalert2', array( 'promise-polyfill' ), '4.6.6' )->localize( 'imagifySwal' );
196
197 $this->register_script( 'chart', 'chart', array(), '2.7.1.0' );
198
199 $this->register_script( 'event-move', 'jquery.event.move', array( 'jquery' ), '2.0.1' );
200
201 /**
202 * Imagify Scripts.
203 */
204 $this->register_script( 'admin-bar', 'admin-bar', array( 'jquery' ) )->defer_localization( 'imagifyAdminBar' );
205
206 $this->register_script( 'admin', 'admin', array( 'jquery' ) );
207
208 $this->register_script( 'notices', 'notices', array( 'jquery', 'admin' ) )->defer_localization( 'imagifyNotices' ); // Needs SweetAlert on some cases.
209
210 $this->register_script( 'twentytwenty', 'jquery.twentytwenty', array( 'jquery', 'event-move', 'chart', 'admin' ) )->defer_localization( 'imagifyTTT' );
211
212 $this->register_script( 'media-modal', 'media-modal', array( 'jquery', 'chart', 'admin' ) );
213
214 $this->register_script( 'pricing-modal', 'pricing-modal', array( 'jquery', 'admin' ) )->defer_localization( 'imagifyPricingModal' );
215
216 $this->register_script( 'library', 'library', array( 'jquery', 'media-modal' ) )->defer_localization( 'imagifyLibrary' );
217
218 $this->register_script( 'async', 'imagify-gulp', array(), '2017-07-28' );
219
220 $this->register_script( 'bulk', 'bulk', array( 'jquery', 'heartbeat', 'underscore', 'chart', 'sweetalert', 'async', 'admin' ) )->defer_localization( 'imagifyBulk' );
221
222 $this->register_script( 'options', 'options', array( 'jquery', 'sweetalert', 'underscore', 'admin' ) )->defer_localization( 'imagifyOptions' );
223
224 $this->register_script( 'files-list', 'files-list', array( 'jquery', 'chart', 'admin' ) )->defer_localization( 'imagifyFiles' );
225 }
226
227 /**
228 * Enqueue stylesheets and scripts for the administration area.
229 *
230 * @since 1.6.10
231 * @author Grégory Viguier
232 */
233 public function enqueue_styles_and_scripts() {
234 static $done = false;
235
236 if ( $done ) {
237 return;
238 }
239 $done = true;
240
241 /*
242 * Register stylesheets and scripts.
243 */
244 $this->register_styles_and_scripts();
245
246 /**
247 * Admin bar.
248 */
249 if ( $this->is_admin_bar_item_showing() ) {
250 $this->enqueue_assets( 'admin-bar' );
251 }
252
253 /**
254 * Notices.
255 */
256 $notices = Imagify_Notices::get_instance();
257
258 if ( $notices->has_notices() ) {
259 if ( $notices->display_welcome_steps() || $notices->display_wrong_api_key() ) {
260 // This is where we display things about the API key.
261 $this->enqueue_assets( 'sweetalert' );
262 }
263
264 $this->enqueue_assets( 'notices' );
265 }
266
267 /**
268 * Loaded in the library and attachment edition.
269 */
270 if ( imagify_is_screen( 'library' ) || imagify_is_screen( 'attachment' ) ) {
271 $this->enqueue_assets( 'twentytwenty' );
272 }
273
274 /**
275 * Loaded in the library.
276 */
277 if ( imagify_is_screen( 'library' ) ) {
278 $this->enqueue_style( 'admin' )->enqueue_script( 'library' );
279 }
280
281 /**
282 * Loaded in the bulk optimization page.
283 */
284 if ( imagify_is_screen( 'bulk' ) ) {
285 $this->enqueue_assets( array( 'pricing-modal', 'bulk' ) );
286 }
287
288 /*
289 * Loaded in the settings page.
290 */
291 if ( imagify_is_screen( 'imagify-settings' ) ) {
292 $this->enqueue_assets( array( 'sweetalert', 'notices', 'twentytwenty', 'pricing-modal', 'options' ) );
293 }
294
295 /*
296 * Loaded in the files list page.
297 */
298 if ( imagify_is_screen( 'files-list' ) ) {
299 $this->enqueue_assets( array( 'files-list', 'twentytwenty' ) );
300 }
301
302 /**
303 * Triggered after Imagify CSS and JS have been enqueued.
304 *
305 * @since 1.6.10
306 * @author Grégory Viguier
307 */
308 do_action( 'imagify_assets_enqueued' );
309 }
310
311 /**
312 * Enqueue stylesheets and scripts for the media modal.
313 *
314 * @since 1.6.10
315 * @author Grégory Viguier
316 */
317 public function enqueue_media_modal() {
318 static $done = false;
319
320 if ( $done ) {
321 return;
322 }
323 $done = true;
324
325 /*
326 * Register stylesheets and scripts.
327 */
328 $this->register_styles_and_scripts();
329
330 $this->enqueue_style( 'admin' )->enqueue_script( 'media-modal' );
331
332 /**
333 * Triggered after Imagify CSS and JS have been enqueued for the media modal.
334 *
335 * @since 1.6.10
336 * @author Grégory Viguier
337 */
338 do_action( 'imagify_media_modal_assets_enqueued' );
339 }
340
341 /**
342 * Add Intercom on Options page an Bulk Optimization.
343 * Previously was _imagify_admin_print_intercom()
344 *
345 * @since 1.6.10
346 * @author Grégory Viguier
347 */
348 public function print_support_script() {
349 if ( ! imagify_valid_key() ) {
350 return;
351 }
352
353 $user = get_imagify_user();
354
355 if ( empty( $user->is_intercom ) || empty( $user->display_support ) ) {
356 return;
357 }
358 ?>
359 <script>
360 window.intercomSettings = {
361 app_id: 'cd6nxj3z',
362 user_id: <?php echo (int) $user->id; ?>
363 };
364 (function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/cd6nxj3z';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})()
365 </script>
366 <?php
367 }
368
369
370 /** ----------------------------------------------------------------------------------------- */
371 /** PUBLIC TOOLS ============================================================================ */
372 /** ----------------------------------------------------------------------------------------- */
373
374 /**
375 * Register a style.
376 *
377 * @since 1.6.10
378 * @author Grégory Viguier
379 *
380 * @param string $handle Name of the stylesheet. Should be unique.
381 * @param string|null $file_name The file name, without the extension. If null, $handle is used.
382 * @param array $dependencies An array of registered stylesheet handles this stylesheet depends on.
383 * @param string|null $version String specifying stylesheet version number. If set to null, the plugin version is used. If SCRIPT_DEBUG is true, a random string is used.
384 * @return object This class instance.
385 */
386 public function register_style( $handle, $file_name = null, $dependencies = array(), $version = null ) {
387 // If we register it, it's one of our styles.
388 $this->styles[ $handle ] = 1;
389 $this->current_handle = $handle;
390 $this->current_handle_type = 'css';
391
392 $file_name = $file_name ? $file_name : $handle;
393 $version = $version ? $version : IMAGIFY_VERSION;
394 $version = $this->is_debug() ? self::$version : $version;
395 $extension = $this->is_debug() ? '.css' : '.min.css';
396 $handle = self::CSS_PREFIX . $handle;
397 $dependencies = $this->prefix_dependencies( $dependencies, 'css' );
398
399 wp_register_style(
400 $handle,
401 IMAGIFY_ASSETS_CSS_URL . $file_name . $extension,
402 $dependencies,
403 $version
404 );
405
406 return $this;
407 }
408
409 /**
410 * Enqueue a style.
411 *
412 * @since 1.6.10
413 * @author Grégory Viguier
414 *
415 * @param string|array $handles Name of the stylesheet. Should be unique. Can be an array to enqueue several stylesheets.
416 * @return object This class instance.
417 */
418 public function enqueue_style( $handles ) {
419 $handles = (array) $handles;
420
421 foreach ( $handles as $handle ) {
422 $this->current_handle = $handle;
423 $this->current_handle_type = 'css';
424
425 if ( ! empty( $this->styles[ $handle ] ) ) {
426 // If we registered it, it's one of our styles.
427 $handle = self::CSS_PREFIX . $handle;
428 }
429
430 wp_enqueue_style( $handle );
431 }
432
433 return $this;
434 }
435
436 /**
437 * Dequeue a style.
438 *
439 * @since 1.6.10
440 * @author Grégory Viguier
441 *
442 * @param string|array $handles Name of the stylesheet. Should be unique. Can be an array to dequeue several stylesheets.
443 * @return object This class instance.
444 */
445 public function dequeue_style( $handles ) {
446 $handles = (array) $handles;
447
448 foreach ( $handles as $handle ) {
449 $this->current_handle = $handle;
450 $this->current_handle_type = 'css';
451
452 if ( ! empty( $this->styles[ $handle ] ) ) {
453 // If we registered it, it's one of our styles.
454 $handle = self::CSS_PREFIX . $handle;
455 }
456
457 wp_dequeue_style( $handle );
458 }
459
460 return $this;
461 }
462
463 /**
464 * Register a script.
465 *
466 * @since 1.6.10
467 * @author Grégory Viguier
468 *
469 * @param string $handle Name of the script. Should be unique.
470 * @param string|null $file_name The file name, without the extension. If null, $handle is used.
471 * @param array $dependencies An array of registered script handles this script depends on.
472 * @param string|null $version String specifying script version number. If set to null, the plugin version is used. If SCRIPT_DEBUG is true, a random string is used.
473 * @return object This class instance.
474 */
475 public function register_script( $handle, $file_name = null, $dependencies = array(), $version = null ) {
476 // If we register it, it's one of our scripts.
477 $this->scripts[ $handle ] = 1;
478 // Set the current handler and handler type.
479 $this->current_handle = $handle;
480 $this->current_handle_type = 'js';
481
482 $file_name = $file_name ? $file_name : $handle;
483 $version = $version ? $version : IMAGIFY_VERSION;
484 $version = $this->is_debug() ? self::$version : $version;
485 $extension = $this->is_debug() ? '.js' : '.min.js';
486 $handle = self::JS_PREFIX . $handle;
487 $dependencies = $this->prefix_dependencies( $dependencies );
488
489 wp_register_script(
490 $handle,
491 IMAGIFY_ASSETS_JS_URL . $file_name . $extension,
492 $dependencies,
493 $version,
494 true
495 );
496
497 return $this;
498 }
499
500 /**
501 * Enqueue a script.
502 *
503 * @since 1.6.10
504 * @author Grégory Viguier
505 *
506 * @param string|array $handles Name of the script. Should be unique. Can be an array to enqueue several scripts.
507 * @return object This class instance.
508 */
509 public function enqueue_script( $handles ) {
510 $handles = (array) $handles;
511
512 foreach ( $handles as $handle ) {
513 // Enqueue the corresponding style.
514 if ( ! empty( $this->styles[ $handle ] ) ) {
515 $this->enqueue_style( $handle );
516 }
517
518 $this->current_handle = $handle;
519 $this->current_handle_type = 'js';
520
521 $this->maybe_register_heartbeat( $handle );
522
523 if ( ! empty( $this->scripts[ $handle ] ) ) {
524 // If we registered it, it's one of our scripts.
525 $handle = self::JS_PREFIX . $handle;
526 }
527
528 wp_enqueue_script( $handle );
529
530 // Deferred localization.
531 if ( ! empty( $this->deferred_localizations[ $this->current_handle ] ) ) {
532 array_map( array( $this, 'localize' ), $this->deferred_localizations[ $this->current_handle ] );
533 unset( $this->deferred_localizations[ $this->current_handle ] );
534 }
535 }
536
537 return $this;
538 }
539
540 /**
541 * Dequeue a script.
542 *
543 * @since 1.6.10
544 * @author Grégory Viguier
545 *
546 * @param string|array $handles Name of the script. Should be unique. Can be an array to dequeue several scripts.
547 * @return object This class instance.
548 */
549 public function dequeue_script( $handles ) {
550 $handles = (array) $handles;
551
552 foreach ( $handles as $handle ) {
553 // Enqueue the corresponding style.
554 if ( ! empty( $this->styles[ $handle ] ) ) {
555 $this->dequeue_style( $handle );
556 }
557
558 $this->current_handle = $handle;
559 $this->current_handle_type = 'js';
560
561 if ( ! empty( $this->scripts[ $handle ] ) ) {
562 // If we registered it, it's one of our scripts.
563 $handle = self::JS_PREFIX . $handle;
564 }
565
566 wp_dequeue_script( $handle );
567 }
568
569 return $this;
570 }
571
572 /**
573 * Localize a script.
574 *
575 * @since 1.6.10
576 * @author Grégory Viguier
577 *
578 * @param string $handle Name of the script. Should be unique.
579 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.
580 * @param string|array|null $l10n The data itself. The data can be either a single or multi-dimensional array. If null, $handle is used.
581 * @return object This class instance.
582 */
583 public function localize_script( $handle, $object_name, $l10n = null ) {
584 $this->current_handle = $handle;
585 $this->current_handle_type = 'js';
586
587 if ( ! isset( $l10n ) ) {
588 $l10n = $handle;
589 }
590
591 if ( is_string( $l10n ) ) {
592 $l10n = $this->get_localization_data( $l10n );
593 }
594
595 if ( ! $l10n ) {
596 return $this;
597 }
598
599 if ( ! empty( $this->scripts[ $handle ] ) ) {
600 // If we registered it, it's one of our scripts.
601 $handle = self::JS_PREFIX . $handle;
602 }
603
604 wp_localize_script( $handle, $object_name, $l10n );
605
606 return $this;
607 }
608
609 /**
610 * Enqueue a style and a script that have the same handle.
611 *
612 * @since 1.6.10
613 * @author Grégory Viguier
614 *
615 * @param string|array $handles Name of the script. Should be unique. Can be an array to enqueue several scripts.
616 * @return object This class instance.
617 */
618 public function enqueue_assets( $handles ) {
619 $handles = (array) $handles;
620
621 foreach ( $handles as $handle ) {
622 $this->enqueue_style( $handle );
623 $this->enqueue_script( $handle );
624 }
625
626 return $this;
627 }
628
629 /**
630 * Dequeue a style and a script that have the same handle.
631 *
632 * @since 1.6.10
633 * @author Grégory Viguier
634 *
635 * @param string|array $handles Name of the script. Should be unique. Can be an array to dequeue several scripts.
636 * @return object This class instance.
637 */
638 public function dequeue_assets( $handles ) {
639 $handles = (array) $handles;
640
641 foreach ( $handles as $handle ) {
642 $this->dequeue_style( $handle );
643 $this->dequeue_script( $handle );
644 }
645
646 return $this;
647 }
648
649 /**
650 * Enqueue the current script or style.
651 *
652 * @since 1.6.10
653 * @author Grégory Viguier
654 *
655 * @return object This class instance.
656 */
657 public function enqueue() {
658 if ( 'js' === $this->current_handle_type ) {
659 $this->enqueue_script( $this->current_handle );
660 } elseif ( 'css' === $this->current_handle_type ) {
661 $this->enqueue_style( $this->current_handle );
662 }
663
664 return $this;
665 }
666
667 /**
668 * Localize the current script.
669 *
670 * @since 1.6.10
671 * @author Grégory Viguier
672 *
673 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.
674 * @param string|array|null $l10n The data itself. The data can be either a single or multi-dimensional array. If null, $handle is used.
675 * @return object This class instance.
676 */
677 public function localize( $object_name, $l10n = null ) {
678 return $this->localize_script( $this->current_handle, $object_name, $l10n );
679 }
680
681 /**
682 * Localize the current script when it is enqueued with `$this->enqueue()` or `$this->enqueue_script()`. This should be used right after `$this->register_script()`.
683 * Be careful, it won't work if the script is enqueued because it's a dependency.
684 * This is handy to not forget to localize the script later. It also prevents to localize the script right away, and maybe execute all localizations while the script is not enqueued (so we localize for nothing).
685 *
686 * @since 1.6.10
687 * @author Grégory Viguier
688 *
689 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.
690 * @return object This class instance.
691 */
692 public function defer_localization( $object_name ) {
693 if ( ! isset( $this->deferred_localizations[ $this->current_handle ] ) ) {
694 $this->deferred_localizations[ $this->current_handle ] = array();
695 }
696
697 $this->deferred_localizations[ $this->current_handle ][ $object_name ] = $object_name;
698
699 return $this;
700 }
701
702 /**
703 * Remove a deferred localization.
704 *
705 * @since 1.6.10
706 * @author Grégory Viguier
707 *
708 * @param string $handle Name of the script. Should be unique.
709 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable. Example: '/[a-zA-Z0-9_]+/'.
710 * @return object This class instance.
711 */
712 public function remove_deferred_localization( $handle, $object_name = null ) {
713 if ( empty( $this->deferred_localizations[ $handle ] ) ) {
714 return $this;
715 }
716
717 if ( $object_name ) {
718 unset( $this->deferred_localizations[ $handle ][ $object_name ] );
719 } else {
720 unset( $this->deferred_localizations[ $handle ] );
721 }
722
723 return $this;
724 }
725
726 /**
727 * Get all translations we can use with wp_localize_script().
728 *
729 * @since 1.6.10
730 * @author Grégory Viguier
731 *
732 * @param string $context The translation context.
733 * @param array $more_data More data to merge.
734 * @return array $translations The translations.
735 */
736 public function get_localization_data( $context, $more_data = array() ) {
737 $data = get_imagify_localize_script_translations( $context );
738
739 if ( $more_data ) {
740 return array_merge( $data, $more_data );
741 }
742
743 return $data;
744 }
745
746
747 /** ----------------------------------------------------------------------------------------- */
748 /** INTERNAL TOOLS ========================================================================== */
749 /** ----------------------------------------------------------------------------------------- */
750
751 /**
752 * Prefix the dependencies if they are ours.
753 *
754 * @since 1.6.10
755 * @author Grégory Viguier
756 *
757 * @param array $dependencies An array of registered script handles this script depends on.
758 * @param string $type Type of dependency: css or js.
759 * @return array
760 */
761 protected function prefix_dependencies( $dependencies, $type = 'js' ) {
762 if ( ! $dependencies ) {
763 return array();
764 }
765
766 if ( 'js' === $type ) {
767 $prefix = self::JS_PREFIX;
768 $scripts = $this->scripts;
769 } else {
770 $prefix = self::CSS_PREFIX;
771 $scripts = $this->styles;
772 }
773
774 $depts = array();
775
776 foreach ( $dependencies as $dept ) {
777 if ( ! empty( $scripts[ $dept ] ) ) {
778 $depts[] = $prefix . $dept;
779 } else {
780 $depts[] = $dept;
781 }
782 }
783
784 return $depts;
785 }
786
787 /**
788 * Make sure Heartbeat is registered if the given script requires it.
789 * Lots of people love deregister Heartbeat.
790 *
791 * @since 1.6.11
792 * @author Grégory Viguier
793 *
794 * @param string $handle Name of the script. Should be unique.
795 */
796 protected function maybe_register_heartbeat( $handle ) {
797 if ( wp_script_is( 'heartbeat', 'registered' ) ) {
798 return;
799 }
800
801 if ( ! empty( $this->scripts[ $handle ] ) ) {
802 // If we registered it, it's one of our scripts.
803 $handle = self::JS_PREFIX . $handle;
804 }
805
806 $dependencies = wp_scripts()->query( $handle );
807
808 if ( ! $dependencies || ! $dependencies->deps ) {
809 return;
810 }
811
812 $dependencies = array_flip( $dependencies->deps );
813
814 if ( ! isset( $dependencies['heartbeat'] ) ) {
815 return;
816 }
817
818 $suffix = SCRIPT_DEBUG ? '' : '.min';
819 wp_register_script( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array( 'jquery' ), false, true );
820 }
821
822 /**
823 * Tell if debug is on.
824 *
825 * @since 1.6.10
826 * @author Grégory Viguier
827 *
828 * @return bool
829 */
830 protected function is_debug() {
831 return defined( 'IMAGIFY_DEBUG' ) && IMAGIFY_DEBUG || defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
832 }
833
834 /**
835 * Tell if the admin bar item is displaying.
836 *
837 * @since 1.6.10
838 * @author Grégory Viguier
839 *
840 * @return bool
841 */
842 protected function is_admin_bar_item_showing() {
843 if ( defined( 'IMAGIFY_HIDDEN_ACCOUNT' ) && IMAGIFY_HIDDEN_ACCOUNT ) {
844 return false;
845 }
846
847 return get_imagify_option( 'api_key' ) && is_admin_bar_showing() && imagify_current_user_can() && get_imagify_option( 'admin_bar_menu' );
848 }
849 }
850