-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathadmin.php
executable file
·2129 lines (1932 loc) · 101 KB
/
admin.php
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Admin class.
*
* Author: Andrei Baicus <[email protected]>
* Created on: 19/07/2018
*
* @soundtrack Somewhere Else - Marillion
* @package \Optimole\Inc
* @author Optimole <[email protected]>
*/
use enshrined\svgSanitize\Sanitizer;
/**
* Class Optml_Admin
*/
class Optml_Admin {
use Optml_Normalizer;
const IMAGE_DATA_COLLECTED = 'optml_image_data_collected';
const IMAGE_DATA_COLLECTED_BATCH = 100;
const BF_PROMO_DISMISS_KEY = 'optml_bf24_notice_dismiss';
const SPC_BANNER_DISMISS_KEY = 'optml_spc_banner_dismiss';
/**
* Hold the settings object.
*
* @var Optml_Settings Settings object.
*/
public $settings;
/**
* Hold the plugin conflict object.
*
* @var Optml_Conflicting_Plugins Settings object.
*/
public $conflicting_plugins;
const NEW_USER_DEFAULTS_UPDATED = 'optml_defaults_updated';
const OLD_USER_ENABLED_LD = 'optml_enabled_limit_dimensions';
const OLD_USER_ENABLED_CL = 'optml_enabled_cloud_sites';
const SYNC_CRON = 'optml_daily_sync';
const ENRICH_CRON = 'optml_pull_image_data';
/**
* Optml_Admin constructor.
*/
public function __construct() {
$this->settings = new Optml_Settings();
$this->conflicting_plugins = new Optml_Conflicting_Plugins();
$dashboard_widget = new Optml_Dashboard_Widget();
$dashboard_widget->init();
add_filter( 'plugin_action_links_' . plugin_basename( OPTML_BASEFILE ), [ $this, 'add_action_links' ] );
add_action( 'admin_menu', [ $this, 'add_dashboard_page' ] );
add_action( 'admin_menu', [ $this, 'add_settings_subpage' ], 99 );
add_action( 'admin_head', [ $this, 'menu_icon_style' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue' ], PHP_INT_MIN );
add_action( 'admin_notices', [ $this, 'add_notice' ] );
add_action( 'admin_notices', [ $this, 'add_notice_upgrade' ] );
add_action( 'admin_notices', [ $this, 'add_notice_conflicts' ] );
add_action( self::SYNC_CRON, [ $this, 'daily_sync' ] );
add_action( 'admin_init', [ $this, 'redirect_old_dashboard' ] );
if ( $this->settings->is_connected() ) {
add_action( 'init', [ $this, 'check_domain_change' ] );
add_action( self::ENRICH_CRON, [ $this, 'pull_image_data' ] );
// Backwards compatibility for older versions of WordPress < 6.0.0 requiring 3 parameters for this specific filter.
$below_6_0_0 = version_compare( get_bloginfo( 'version' ), '6.0.0', '<' );
if ( $below_6_0_0 ) {
add_filter( 'wp_insert_attachment_data', [ $this, 'legacy_detect_image_title_changes' ], 10, 3 );
} else {
add_filter( 'wp_insert_attachment_data', [ $this, 'detect_image_title_changes' ], 10, 4 );
}
add_action( 'updated_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
add_action( 'added_post_meta', [ $this, 'detect_image_alt_change' ], 10, 4 );
if ( ! wp_next_scheduled( self::ENRICH_CRON ) ) {
wp_schedule_event( time() + 10, 'hourly', self::ENRICH_CRON );
}
}
add_action( 'init', [ $this, 'update_default_settings' ] );
add_action( 'init', [ $this, 'update_limit_dimensions' ] );
add_action( 'init', [ $this, 'update_cloud_sites_default' ] );
add_action( 'admin_init', [ $this, 'maybe_redirect' ] );
add_action( 'admin_init', [ $this, 'init_no_script' ] );
if ( ! is_admin() && $this->settings->is_connected() && ! wp_next_scheduled( self::SYNC_CRON ) ) {
wp_schedule_event( time() + 10, 'twicedaily', self::SYNC_CRON, [] );
}
add_action( 'optml_after_setup', [ $this, 'register_public_actions' ], 999999 );
if ( ! function_exists( 'is_wpcom_vip' ) ) {
add_filter(
'upload_mimes',
[
$this,
'allow_svg',
]
); // phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.upload_mimes
add_filter( 'wp_handle_upload_prefilter', [ $this, 'check_svg_and_sanitize' ] );
}
add_filter( 'themeisle-sdk/survey/' . OPTML_PRODUCT_SLUG, [ $this, 'get_survey_metadata' ], 10, 2 );
}
/**
* Check if the file is an SVG, if so handle appropriately
*
* @param array $file An array of data for a single file.
*
* @return mixed
*/
public function check_svg_and_sanitize( $file ) {
// Ensure we have a proper file path before processing.
if ( ! isset( $file['tmp_name'] ) ) {
return $file;
}
$file_name = isset( $file['name'] ) ? $file['name'] : '';
$wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file_name );
$type = ! empty( $wp_filetype['type'] ) ? $wp_filetype['type'] : '';
if ( 'image/svg+xml' === $type ) {
if ( ! current_user_can( 'upload_files' ) ) {
$file['error'] = 'Invalid';
return $file;
}
if ( ! $this->sanitize_svg( $file['tmp_name'] ) ) {
$file['error'] = 'Invalid';
}
}
return $file;
}
/**
* Sanitize the SVG
*
* @param string $file Temp file path.
*
* @return bool|int
*/
protected function sanitize_svg( $file ) {
// We can ignore the phpcs warning here as we're reading and writing to the Temp file.
$dirty = file_get_contents( $file ); // phpcs:ignore
// Is the SVG gzipped? If so we try and decode the string.
$is_zipped = $this->is_gzipped( $dirty );
if ( $is_zipped && ( ! function_exists( 'gzdecode' ) || ! function_exists( 'gzencode' ) ) ) {
return false;
}
if ( $is_zipped ) {
$dirty = gzdecode( $dirty );
// If decoding fails, bail as we're not secure.
if ( false === $dirty ) {
return false;
}
}
$sanitizer = new Sanitizer();
$clean = $sanitizer->sanitize( $dirty );
if ( false === $clean ) {
return false;
}
// If we were gzipped, we need to re-zip.
if ( $is_zipped ) {
$clean = gzencode( $clean );
}
// We can ignore the phpcs warning here as we're reading and writing to the Temp file.
file_put_contents( $file, $clean ); // phpcs:ignore
return true;
}
/**
* Check if the contents are gzipped
*
* @see http://www.gzip.org/zlib/rfc-gzip.html#member-format
*
* @param string $contents Content to check.
*
* @return bool
*/
protected function is_gzipped( $contents ) {
// phpcs:disable Generic.Strings.UnnecessaryStringConcat.Found
if ( function_exists( 'mb_strpos' ) ) {
return 0 === mb_strpos( $contents, "\x1f" . "\x8b" . "\x08" );
} else {
return 0 === strpos( $contents, "\x1f" . "\x8b" . "\x08" );
}
// phpcs:enable
}
/**
* Query the database for images and extract the alt/title to send them to the API
*
* @uses action: optml_pull_image_data
*/
public function pull_image_data() {
// Get all image attachments that are not processed
$args = [
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => self::IMAGE_DATA_COLLECTED_BATCH,
'meta_query' => [
'relation' => 'AND',
[
'key' => self::IMAGE_DATA_COLLECTED,
'compare' => 'NOT EXISTS',
],
],
];
$attachments = get_posts( $args );
$image_data = [];
foreach ( $attachments as $attachment ) {
// Get image URL, alt, and title
$image_url = wp_get_attachment_url( $attachment->ID );
$image_alt = get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true );
$image_title = $attachment->post_title;
$image_data[ $image_url ] = [];
if ( ! empty( $image_alt ) ) {
$image_data[ $image_url ]['alt'] = $image_alt;
}
if ( ! empty( $image_title ) ) {
$image_data[ $image_url ]['title'] = $image_title;
}
if ( empty( $image_data[ $image_url ] ) ) {
unset( $image_data[ $image_url ] );
}
// Mark the image as processed
update_post_meta( $attachment->ID, self::IMAGE_DATA_COLLECTED, 'yes' );
}
if ( ! empty( $image_data ) ) {
$api = new Optml_Api();
$api->call_data_enrich_api( $image_data );
}
}
/**
* Delete the processed meta from an image when the alt text is changed
*
* @uses action: updated_post_meta, added_post_meta
*/
public function detect_image_alt_change( $meta_id, $post_id, $meta_key, $meta_value ) {
// Check if the updated metadata is for alt text and it's an attachment
if ( $meta_key === '_wp_attachment_image_alt' && get_post_type( $post_id ) === 'attachment' ) {
delete_post_meta( $post_id, self::IMAGE_DATA_COLLECTED );
}
}
/**
* Delete the processed meta from an image when the title is changed
*
* @uses filter: wp_insert_attachment_data
*/
public function detect_image_title_changes( $data, $postarr, $unsanitized_postarr, $update ) {
// Check if it's an attachment being updated
if ( $data['post_type'] !== 'attachment' ) {
return $data;
}
if ( ! isset( $postarr['ID'] ) ) {
return $data;
}
if ( isset( $postarr['post_title'] ) && isset( $postarr['original_post_title'] ) && $postarr['post_title'] !== $postarr['original_post_title'] ) {
delete_post_meta( $postarr['ID'], self::IMAGE_DATA_COLLECTED );
}
return $data;
}
/**
* Delete the processed meta from an image when the title is changed
*
* @uses filter: wp_insert_attachment_data
*/
public function legacy_detect_image_title_changes( $data, $postarr, $unsanitized_postarr ) {
return $this->detect_image_title_changes( $data, $postarr, $unsanitized_postarr, true );
}
/**
* Init no_script setup value based on whether the user is connected or not.
*/
public function init_no_script() {
if ( $this->settings->is_connected() ) {
$raw_settings = $this->settings->get_raw_settings();
if ( ! isset( $raw_settings['no_script'] ) ) {
$this->settings->update( 'no_script', 'enabled' );
}
}
}
/**
* Checks if domain has changed
*/
public function check_domain_change() {
$previous_domain = get_option( 'optml_current_domain', 0 );
$site_url = $this->to_domain_hash( get_home_url() );
if ( $site_url !== $previous_domain ) {
update_option( 'optml_current_domain', $site_url );
if ( $previous_domain !== 0 ) {
$this->daily_sync();
}
}
}
/**
* Update the limit dimensions setting to enabled if the user is new.
*/
public function update_default_settings() {
if ( get_option( self::NEW_USER_DEFAULTS_UPDATED ) === 'yes' ) {
return;
}
if ( $this->settings->is_connected() ) {
update_option( self::NEW_USER_DEFAULTS_UPDATED, 'yes' );
return;
}
$this->settings->update( 'limit_dimensions', 'enabled' );
$this->settings->update( 'lazyload', 'enabled' );
$this->settings->update( 'best_format', 'disabled' );
$this->settings->update( 'skip_lazyload_images', '2' );
$this->settings->update( 'avif', 'disabled' );
update_option( self::NEW_USER_DEFAULTS_UPDATED, 'yes' );
}
/**
* Enable limit dimensions for old users after removing the Resize option.
*
* @return void
*/
public function update_limit_dimensions() {
if ( get_option( self::OLD_USER_ENABLED_LD ) === 'yes' ) {
return;
}
// New users already have this enabled as we changed defaults.
if ( ! $this->settings->is_connected() ) {
return;
}
$this->settings->update( 'limit_dimensions', 'enabled' );
update_option( self::OLD_USER_ENABLED_LD, 'yes' );
}
/**
* Enable cloud sites and add the current site to the whitelist.
*
* @return void
*/
public function update_cloud_sites_default() {
if ( get_option( self::OLD_USER_ENABLED_CL ) === 'yes' ) {
return;
}
if ( ! $this->settings->is_connected() ) {
return;
}
// This is already enabled. Don't alter it.
if ( $this->settings->get( 'cloud_images' ) === 'enabled' ) {
update_option( self::OLD_USER_ENABLED_CL, 'yes' );
return;
}
$this->settings->update( 'cloud_images', 'enabled' );
$this->settings->update( 'cloud_sites', $this->settings->get_cloud_sites_whitelist_default() );
update_option( self::OLD_USER_ENABLED_CL, 'yes' );
}
/**
* Register public actions.
*/
public function register_public_actions() {
add_action( 'wp_head', [ $this, 'generator' ] );
add_filter( 'wp_resource_hints', [ $this, 'add_dns_prefetch' ], 10, 2 );
if ( Optml_Manager::should_ignore_image_tags() ) {
return;
}
if ( ! $this->settings->use_lazyload()
|| ( $this->settings->get( 'native_lazyload' ) === 'enabled'
&& $this->settings->get( 'video_lazyload' ) === 'disabled'
&& $this->settings->get( 'bg_replacer' ) === 'disabled' ) ) {
return;
}
add_action( 'wp_enqueue_scripts', [ $this, 'frontend_scripts' ] );
add_action( 'wp_head', [ $this, 'inline_bootstrap_script' ] );
add_filter( 'optml_additional_html_classes', [ $this, 'add_no_js_class_to_html_tag' ], 10 );
}
/**
* Use filter to add additional class to html tag.
*
* @param array $classes The classes to be added.
*
* @return array
*/
public function add_no_js_class_to_html_tag( $classes ) {
// we need the space padding since some plugins might not target correctly with js there own classes
// this causes some issues if they concat directly, this way we can protect against that since no matter what
// there will be an extra space padding so that classes can be easily identified
return array_merge( $classes, [ ' optml_no_js ' ] );
}
/**
* Adds script for lazyload/js replacement.
*/
public function inline_bootstrap_script() {
$domain = 'https://' . $this->settings->get_cdn_url() . '/js-lib';
if ( defined( 'OPTML_JS_CDN' ) && constant( 'OPTML_JS_CDN' ) ) {
$domain = 'https://' . constant( 'OPTML_JS_CDN' ) . '/js-lib';
}
$min = ! OPTML_DEBUG ? '.min' : '';
$bgclasses = Optml_Lazyload_Replacer::get_lazyload_bg_classes();
$watcher_classes = Optml_Lazyload_Replacer::get_watcher_lz_classes();
$lazyload_bg_selectors = Optml_Lazyload_Replacer::get_background_lazyload_selectors();
foreach ( $bgclasses as $key ) {
$lazyload_bg_selectors[] = '.' . $key;
}
$lazyload_bg_selectors = empty( $lazyload_bg_selectors ) ? '' : sprintf( '%s', implode( ', ', (array) $lazyload_bg_selectors ) );
$bgclasses = empty( $bgclasses ) ? '' : sprintf( '"%s"', implode( '","', (array) $bgclasses ) );
$watcher_classes = empty( $watcher_classes ) ? '' : sprintf( '"%s"', implode( '","', (array) $watcher_classes ) );
$default_network = ( $this->settings->get( 'network_optimization' ) === 'enabled' );
$limit_dimensions = $this->settings->get( 'limit_dimensions' ) === 'enabled';
$limit_width = $limit_dimensions ? $this->settings->get( 'limit_width' ) : 0;
$limit_height = $limit_dimensions ? $this->settings->get( 'limit_height' ) : 0;
$retina_ready = $limit_dimensions ||
! ( $this->settings->get( 'retina_images' ) === 'enabled' );
$scale_is_disabled = ( $this->settings->get( 'scale' ) === 'enabled' );
$native_lazy_enabled = ( $this->settings->get( 'native_lazyload' ) === 'enabled' );
$output = sprintf(
'
<style type="text/css">
img[data-opt-src]:not([data-opt-lazy-loaded]) {
transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
-webkit-transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
-moz-transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
-o-transition: .2s filter linear, .2s opacity linear, .2s border-radius linear;
}
img[data-opt-src]:not([data-opt-lazy-loaded]) {
opacity: .75;
-webkit-filter: blur(8px);
-moz-filter: blur(8px);
-o-filter: blur(8px);
-ms-filter: blur(8px);
filter: blur(8px);
transform: scale(1.04);
animation: 0.1s ease-in;
-webkit-transform: translate3d(0, 0, 0);
}
%s
</style>
<script type="application/javascript">
document.documentElement.className = document.documentElement.className.replace(/\boptml_no_js\b/g, "");
(function(w, d){
var b = d.getElementsByTagName("head")[0];
var s = d.createElement("script");
var v = ("IntersectionObserver" in w && "isIntersecting" in w.IntersectionObserverEntry.prototype) ? "_no_poly" : "";
s.async = true;
s.src = "%s/v2/latest/optimole_lib" + v + "%s.js";
b.appendChild(s);
w.optimoleData = {
lazyloadOnly: "optimole-lazy-only",
backgroundReplaceClasses: [%s],
nativeLazyload : %s,
scalingDisabled: %s,
watchClasses: [%s],
backgroundLazySelectors: "%s",
network_optimizations: %s,
ignoreDpr: %s,
quality: %d,
maxWidth: %d,
maxHeight: %d,
}
}(window, document));
</script>',
Optml_Lazyload_Replacer::IFRAME_TEMP_COMMENT,
esc_url( $domain ),
$min,
wp_strip_all_tags( $bgclasses ),
$native_lazy_enabled ? 'true' : 'false',
$scale_is_disabled ? 'true' : 'false',
wp_strip_all_tags( $watcher_classes ),
addcslashes( wp_strip_all_tags( $lazyload_bg_selectors ), '"' ),
defined( 'OPTML_NETWORK_ON' ) && constant( 'OPTML_NETWORK_ON' ) ? ( OPTML_NETWORK_ON ? 'true' : 'false' ) : ( $default_network ? 'true' : 'false' ),
$retina_ready ? 'true' : 'false',
$this->settings->get_numeric_quality(),
$limit_width,
$limit_height
);
echo $output;
}
/**
* Add settings links in the plugin listing page.
*
* @param array $links Old plugin links.
*
* @return array Altered links.
*/
public function add_action_links( $links ) {
if ( ! is_array( $links ) ) {
return $links;
}
return array_merge(
$links,
[
'<a href="' . admin_url( 'admin.php?page=optimole' ) . '">' . __( 'Settings', 'optimole-wp' ) . '</a>',
]
);
}
/**
* Check if we should show the notice.
*
* @return bool Should show?
*/
public function should_show_notice() {
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
return false;
}
if ( is_network_admin() ) {
return false;
}
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
if ( $this->settings->is_connected() ) {
return false;
}
$current_screen = get_current_screen();
if ( empty( $current_screen ) ) {
return false;
}
if ( ( get_option( 'optml_notice_optin', 'no' ) === 'yes' ) ) {
return false;
}
return true;
}
/**
* Show upgrade notice.
*/
public function add_notice_upgrade() {
if ( ! $this->should_show_upgrade() ) {
return;
}
?>
<div class="notice optml-notice-optin"
style="background-color: #577BF9; color:white; border: none !important; display: flex;">
<div style="margin: 1% 2%;">
<img src='<?php echo OPTML_URL . 'assets/img/upgrade_icon.png'; ?>'>
</div>
<div style="margin-top: 0.7%;">
<p style="font-size: 16px !important;">
<?php
printf(
/* translators: 1 - opening strong tag, 2 - visits limit, 3 - closing strong tag, 4 - opening strong tag, 5 - closing strong tag, 6 - br tag */
__( '%1$sIt seems you are close to the %2$s visits limit with %3$sOptimole%4$s for this month.%5$s %6$s For a larger quota you may want to check the upgrade plans. If you exceed the quota we will need to deliver back your original, un-optimized images, which might decrease your site speed performance.', 'optimole-wp' ),
'<strong>',
number_format_i18n( 1000 ),
'</strong>',
'<strong>',
'</strong>',
'<br/><br/>'
);
?>
</p>
<p style="margin: 1.5% 0;">
<a href="<?php echo esc_url( tsdk_translate_link( 'https://optimole.com/pricing' ) ); ?>"
target="_blank"
style="border-radius: 4px;padding: 9px 10px;border: 2px solid #FFF;color: white;text-decoration: none;"><?php _e( 'Check upgrade plans', 'optimole-wp' ); ?>
</a>
<a style="padding: 2%; color: white;"
href="<?php echo wp_nonce_url( add_query_arg( [ 'optml_hide_upg' => 'yes' ] ), 'hide_nonce', 'optml_nonce' ); ?>"><?php _e( 'I have already done this', 'optimole-wp' ); ?></a>
</p>
</div>
</div>
<?php
}
/**
* Check if we should show the upgrade notice to users.
*
* @return bool Should we show it?
*/
public function should_show_upgrade() {
$current_screen = get_current_screen();
if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ||
is_network_admin() ||
! current_user_can( 'manage_options' ) ||
! $this->settings->is_connected() ||
empty( $current_screen )
) {
return false;
}
if ( get_option( 'optml_notice_hide_upg', 'no' ) === 'yes' ) {
return false;
}
if ( $current_screen->base !== 'upload' ) {
return false;
}
$service_data = $this->settings->get( 'service_data' );
if ( ! isset( $service_data['plan'] ) ) {
return false;
}
if ( $service_data['plan'] !== 'free' ) {
return false;
}
$visitors_limit = isset( $service_data['visitors_limit'] ) ? (int) $service_data['visitors_limit'] : 0;
$visitors_left = isset( $service_data['visitors_left'] ) ? (int) $service_data['visitors_left'] : 0;
if ( $visitors_limit === 0 ) {
return false;
}
if ( $visitors_left > 300 ) {
return false;
}
return true;
}
/**
* CSS styles for Notice.
*/
public static function notice_styles() {
?>
<style>
.optml-notice-optin:not(.has-dismiss) {
background: url(" <?php echo esc_attr( OPTML_URL . '/assets/img/disconnected.svg' ); ?> ") #fff 100% 0 no-repeat;
position: relative;
padding: 0;
}
.optml-notice-optin.has-dismiss {
position: relative;
}
.optml-notice-optin .content {
background: rgba(255, 255, 255, 0.75);
display: flex;
align-items: center;
padding: 20px;
}
.optml-notice-optin img {
max-width: 100px;
margin-right: 20px;
display: none;
}
.optml-notice-optin .description {
font-size: 14px;
margin-bottom: 20px;
color: #000;
}
.optml-notice-optin .actions {
margin-top: auto;
display: flex;
gap: 20px;
}
@media screen and (min-width: 768px) {
.optml-notice-optin img {
display: block;
}
}
</style>
<?php
}
/**
* JS for Notice.
*/
public static function notice_js( $action ) {
?>
<script>
jQuery(document).ready(function ($) {
// AJAX request to update the option value
$('.optml-notice-optin button.notice-dismiss').click(function (e) {
e.preventDefault();
var notice = $(this).closest('.optml-notice-optin');
var nonce = '<?php echo esc_attr( wp_create_nonce( $action ) ); ?>';
$.ajax({
url: window.ajaxurl,
type: 'POST',
data: {
action: '<?php echo esc_attr( $action ); ?>',
nonce
},
complete() {
notice.remove();
}
});
});
});
</script>
<?php
}
/**
* Adds opt in notice.
*/
public function add_notice() {
if ( ! $this->should_show_notice() ) {
return;
}
self::notice_styles();
?>
<div class="notice notice-info optml-notice-optin">
<div class="content">
<img src="<?php echo OPTML_URL . '/assets/img/logo.svg'; ?>"
alt="<?php echo esc_attr__( 'Logo', 'optimole-wp' ); ?>"/>
<div>
<p class="notice-title"> <?php echo esc_html__( 'Finish setting up!', 'optimole-wp' ); ?></p>
<p class="description">
<?php
printf(
/* translators: 1 - opening strong tag, 2 - closing strong tag, 3 - opening strong tag, 4 - closing strong tag */
__( 'Welcome to %1$sOptimole%2$s, the easiest way to optimize your website images. Your users will enjoy a %3$sfaster%4$s website after you connect it with our service.', 'optimole-wp' ),
'<strong>',
'</strong>',
'<strong>',
'</strong>'
);
?>
</p>
<div class="actions">
<a href="<?php echo esc_url( admin_url( 'admin.php?page=optimole' ) ); ?>"
class="button button-primary button-hero"><?php _e( 'Connect to OptiMole', 'optimole-wp' ); ?>
</a>
<a class="button button-secondary button-hero"
href="<?php echo wp_nonce_url( add_query_arg( [ 'optml_hide_optin' => 'yes' ] ), 'hide_nonce', 'optml_nonce' ); ?>"><?php _e( 'I will do it later', 'optimole-wp' ); ?>
</a>
</div>
</div>
</div>
</div>
<?php
}
/**
* Adds conflicts notice.
*/
public function add_notice_conflicts() {
if ( ! $this->conflicting_plugins->should_show_notice() ) {
return;
}
$plugins = $this->conflicting_plugins->get_conflicting_plugins();
$names = [];
foreach ( $plugins as $plugin ) {
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin );
$names[] = $plugin_data['Name'];
}
$names = implode( ', ', $names );
self::notice_styles();
self::notice_js( 'optml_dismiss_conflict_notice' );
?>
<div class="notice notice-info optml-notice-optin has-dismiss">
<div class="content">
<img src="<?php echo OPTML_URL . '/assets/img/logo.svg'; ?>"
alt="<?php echo esc_attr__( 'Logo', 'optimole-wp' ); ?>"/>
<div>
<p class="notice-title">
<strong><?php echo esc_html__( 'Oops... Multiple image optimization plugins active', 'optimole-wp' ); ?></strong>
</p>
<p class="description">
<?php
printf(
/* translators: 1 - plugin name, 2 - opening bold tag, 3 - closing bold tag */
__( 'We noticed multiple image optimization plugins active on your site, which may cause issues in Optimole. We recommend using only one image optimization plugin on your site for the best results. The following plugins may cause issues in Optimole: %2$s%1$s%3$s.', 'optimole-wp' ),
$names,
'<strong>',
'</strong>'
);
?>
</p>
<div class="actions">
<a href="<?php echo esc_url( admin_url( 'plugins.php?optimole_conflicts' ) ); ?>"
class="button button-primary button-hero"><?php _e( 'Manage Plugins', 'optimole-wp' ); ?>
</a>
</div>
</div>
</div>
<button type="button" class="notice-dismiss">
<span class="screen-reader-text"><?php _e( 'Dismiss this notice.', 'optimole-wp' ); ?></span>
</button>
</div>
<?php
}
/**
* Add style classes for lazy loading background images.
*/
protected function get_background_lazy_css() {
$watchers = Optml_Lazyload_Replacer::get_background_lazyload_selectors();
$css = [];
foreach ( $watchers as $selector ) {
$css[] = 'html ' . $selector . ':not(.optml-bg-lazyloaded)';
}
if ( empty( $css ) ) {
return '';
}
$css = implode( ",\n", $css ) . ' { background-image: none !important; } ';
return strip_tags( $css );
}
/**
* Enqueue frontend scripts.
*/
public function frontend_scripts() {
$bg_css = $this->get_background_lazy_css();
wp_register_style( 'optm_lazyload_noscript_style', false );
wp_enqueue_style( 'optm_lazyload_noscript_style' );
wp_add_inline_style( 'optm_lazyload_noscript_style', "html.optml_no_js img[data-opt-src] { display: none !important; } \n " . $bg_css );
if ( $this->settings->use_lazyload() === true ) {
wp_register_script( 'optml-print', false );
wp_enqueue_script( 'optml-print' );
$script = '
(function(w, d){
w.addEventListener("beforeprint", function(){
let images = d.getElementsByTagName( "img" );
for (let img of images) {
if ( !img.dataset.optSrc) {
continue;
}
img.src = img.dataset.optSrc;
delete img.dataset.optSrc;
}
});
}(window, document));
';
wp_add_inline_script( 'optml-print', $script );
}
}
/**
* Maybe redirect to dashboard page.
*/
public function maybe_redirect() {
if ( isset( $_GET['optml_nonce'] ) && isset( $_GET['optml_hide_optin'] ) && $_GET['optml_hide_optin'] === 'yes' && wp_verify_nonce( $_GET['optml_nonce'], 'hide_nonce' ) ) {
update_option( 'optml_notice_optin', 'yes' );
}
if ( isset( $_GET['optml_nonce'] ) && isset( $_GET['optml_hide_upg'] ) && $_GET['optml_hide_upg'] === 'yes' && wp_verify_nonce( $_GET['optml_nonce'], 'hide_nonce' ) ) {
update_option( 'optml_notice_hide_upg', 'yes' );
}
if ( ! get_transient( 'optml_fresh_install' ) ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
delete_transient( 'optml_fresh_install' );
if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) {
return;
}
if ( $this->settings->is_connected() ) {
return;
}
wp_safe_redirect( admin_url( 'admin.php?page=optimole' ) );
exit;
}
/**
* Output Generator tag.
*/
public function generator() {
if ( ! $this->settings->is_connected() ) {
return;
}
if ( ! $this->settings->is_enabled() ) {
return;
}
echo '<meta name="generator" content="Optimole ' . esc_attr( OPTML_VERSION ) . '">';
}
/**
* Update daily the quota routine.
*/
public function daily_sync() {
$api_key = $this->settings->get( 'api_key' );
$service_data = $this->settings->get( 'service_data' );
$application = '';
if ( isset( $service_data['cdn_key'] ) ) {
$application = $service_data['cdn_key'];
}
if ( empty( $api_key ) ) {
return;
}
$request = new Optml_Api();
$data = $request->get_user_data( $api_key, $application );
if ( $data === false || is_wp_error( $data ) ) {
return;
}
if ( $data === 'disconnect' ) {
$settings = new Optml_Settings();
$settings->reset();
return;
}
add_filter( 'optml_dont_trigger_settings_updated', '__return_true' );
$this->settings->update( 'service_data', $data );
if ( isset( $data['extra_visits'] ) ) {
$this->settings->update_frontend_banner_from_remote( $data['extra_visits'] );
}
// Here the account got deactivated, in this case we check if the user is using offloaded images and we roll them back.
if ( isset( $data['status'] ) && $data['status'] === 'inactive' ) {
// We check if the user has images offloaded.
if ( $this->settings->get( 'offload_media' ) === 'disabled' ) {
return;
}
$in_progress = $this->settings->get( 'offloading_status' ) !== 'disabled';
// We check if there is an in progress transfer, we stop it.
if ( $in_progress ) {
$this->settings->update( 'offloading_status', 'disabled' );
}
$this->settings->update( 'rollback_status', 'enabled' );
// We start the rollback process.
Optml_Logger::instance()->add_log( 'rollback_images', 'Account deactivated, starting rollback.' );
Optml_Media_Offload::get_image_count( 'rollback_images', false );
$this->settings->update( 'offload_media', 'disabled' );
}
remove_filter( 'optml_dont_trigger_settings_updated', '__return_true' );
}
/**
* Adds cdn url for prefetch.
*
* @param array $hints Hints array.
* @param string $relation_type Type of relation.
*
* @return array Altered hints array.
*/