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