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
1001
1002
1003
1004
1005
|
<?php /** * Class Utils holds common functions used by the plugin. * * Class has the following structure: * I. General helper functions * II. Layout functions * III. Time and date functions * IV. Link and url functions * V. Modules functions * * @package Hummingbird\Core * @since 1.8 */
namespace Hummingbird\Core;
use Hummingbird\WP_Hummingbird; use WP_User; use WPMUDEV_Dashboard;
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * Class Utils */ class Utils {
/*************************** * * I. General helper functions * is_member() * is_free_installed() * is_dash_logged_in() * src_to_path() * enqueue_admin_scripts() * get_admin_capability() * get_current_user_name() * get_user_for_report() * calculate_sum() * format_bytes() * format_interval() * format_interval_hours() ***************************/
/** * Check if user is a paid one in WPMU DEV * * @return bool */ public static function is_member() { if ( class_exists( 'WPMUDEV_Dashboard' ) ) { if ( method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_projects' ) && method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_type' ) ) { $type = WPMUDEV_Dashboard::$api->get_membership_type(); $projects = WPMUDEV_Dashboard::$api->get_membership_projects();
if ( ( 'unit' === $type && in_array( 1081721, $projects, true ) ) || ( 'single' === $type && 1081721 === $projects ) ) { return true; }
if ( function_exists( 'is_wpmudev_member' ) ) { return is_wpmudev_member(); }
return false; } }
return false; }
/** * Check if WPMU DEV Dashboard Plugin is logged in * * @return bool */ public static function is_dash_logged_in() { if ( ! class_exists( 'WPMUDEV_Dashboard' ) ) { return false; }
if ( ! is_object( WPMUDEV_Dashboard::$api ) ) { return false; }
if ( ! method_exists( WPMUDEV_Dashboard::$api, 'has_key' ) ) { return false; }
return WPMUDEV_Dashboard::$api->has_key(); }
/** * Try to cast a source URL to a path * * @param string $src Source. * * @return string */ public static function src_to_path( $src ) { $path = wp_parse_url( $src );
// Scheme will not be set on a URL. $url = isset( $path['scheme'] );
$path = ltrim( $path['path'], '/' );
/** * DOCUMENT_ROOT does not always store the correct path. For example, Bedrock appends /wp/ to the default dir. * So if the source is a URL, we can safely use DOCUMENT_ROOT, else see if ABSPATH is defined. */ if ( $url ) { $path = path_join( $_SERVER['DOCUMENT_ROOT'], $path ); } else { $root = defined( 'ABSPATH' ) ? ABSPATH : $_SERVER['DOCUMENT_ROOT']; $path = path_join( $root, $path ); }
return apply_filters( 'wphb_src_to_path', $path, $src ); }
/** * Enqueues admin scripts * * @param int $ver Current version number of scripts. */ public static function enqueue_admin_scripts( $ver ) { wp_enqueue_script( 'wphb-admin', WPHB_DIR_URL . 'admin/assets/js/wphb-app.min.js', array( 'jquery', 'underscore' ), $ver, true );
$i10n = array( 'errorCachePurge' => __( 'There was an error during the cache purge. Check folder permissions are 755 for /wp-content/wphb-cache or delete directory manually.', 'wphb' ), 'successGravatarPurge' => __( 'Gravatar cache purged.', 'wphb' ), 'successPageCachePurge' => __( 'Page cache purged.', 'wphb' ), 'errorRecheckStatus' => __( 'There was an error re-checking the caching status, please try again later.', 'wphb' ), 'successRecheckStatus' => __( 'Browser caching status updated.', 'wphb' ), 'successCloudflarePurge' => __( 'Cloudflare cache successfully purged. Please wait 30 seconds for the purge to complete.', 'wphb' ), 'successRedisPurge' => __( 'Your cache has been cleared.', 'wphb' ), ); wp_localize_script( 'wphb-admin', 'wphbCachingStrings', $i10n );
$performance = self::get_module( 'performance' ); $last_report = $performance::get_last_report(); $mobile_score = '-'; $desktop_score = '-'; if ( is_object( $last_report ) && isset( $last_report->data ) ) { $desktop_score = is_object( $last_report->data->desktop ) ? $last_report->data->desktop->score : '-'; $mobile_score = is_object( $last_report->data->mobile ) ? $last_report->data->mobile->score : '-'; }
$i10n = array( 'previousScoreMobile' => $mobile_score, 'previousScoreDesktop' => $desktop_score, 'finishedTestURLsLink' => self::get_admin_menu_url( 'performance' ) . '&view=audits', 'removeButtonText' => __( 'Remove', 'wphb' ), 'youLabelText' => __( 'You', 'wphb' ), 'scanRunning' => __( 'Running speed test...', 'wphb' ), 'scanAnalyzing' => __( 'Analyzing data and preparing report...', 'wphb' ), 'scanWaiting' => __( 'Test is taking a little longer than expected, hang in thereā¦', 'wphb' ), 'scanComplete' => __( 'Test complete! Reloading...', 'wphb' ), ); wp_localize_script( 'wphb-admin', 'wphbPerformanceStrings', $i10n );
$i10n = array( 'finishedTestURLsLink' => self::get_admin_menu_url(), ); wp_localize_script( 'wphb-admin', 'wphbDashboardStrings', $i10n );
$url = add_query_arg( '_wpnonce', wp_create_nonce( 'wphb-toggle-uptime' ), self::get_admin_menu_url( 'uptime' ) ); $i10n = array( 'enableUptimeURL' => add_query_arg( 'action', 'enable', $url ), 'disableUptimeURL' => add_query_arg( 'action', 'disable', $url ), ); wp_localize_script( 'wphb-admin', 'wphbUptimeStrings', $i10n );
$cf = self::get_module( 'cloudflare' ); $i10n = array( 'cloudflare' => array( 'is' => array( 'connected' => $cf->is_connected() && $cf->is_zone_selected(), ), ), 'nonces' => array( 'HBFetchNonce' => wp_create_nonce( 'wphb-fetch' ), ), 'urls' => array( 'cachingEnabled' => add_query_arg( 'view', 'caching', self::get_admin_menu_url( 'caching' ) ), 'resetSettings' => add_query_arg( 'wphb-clear', 'all', self::get_admin_menu_url() ), ), 'strings' => array( 'htaccessUpdated' => __( 'Your .htaccess file has been updated', 'wphb' ), 'htaccessUpdatedFailed' => __( 'There was an error updating the .htaccess file', 'wphb' ), 'errorSettingsUpdate' => __( 'Error updating settings', 'wphb' ), 'successUpdate' => __( 'Settings updated', 'wphb' ), 'successReset' => __( 'Settings restored to defaults', 'wphb' ), 'deleteAll' => __( 'Delete All', 'wphb' ), 'db_delete' => __( 'Are you sure you wish to delete', 'wphb' ), 'db_entries' => __( 'database entries', 'wphb' ), 'db_backup' => __( 'Make sure you have a current backup just in case.', 'wphb' ), 'successRecipientAdded' => __( ' has been added as a recipient but you still need to save your changes below to set this live.', 'wphb' ), 'confirmRecipient' => __( 'Your changes have been saved successfully. Any new recipients will receive an email shortly to confirm their subscription to these emails.', 'wphb' ), 'awaitingConfirmation' => __( 'Awaiting confirmation', 'wphb' ), 'resendEmail' => __( 'Resend email', 'wphb' ), 'dismissLabel' => __( 'Dismiss', 'wphb' ), 'successAdvPurgeCache' => __( 'Preload cache purged successfully.', 'wphb' ), 'successAdvPurgeMinify' => __( 'All database data and Custom Post Type information related to Asset Optimization has been cleared successfully.', 'wphb' ), ), 'links' => array( 'audits' => self::get_admin_menu_url( 'performance' ) . '&view=audits', ), );
$minify_module = self::get_module( 'minify' );
$is_scanning = $minify_module->scanner->is_scanning();
if ( $minify_module->is_on_page() || $is_scanning ) { $i10n = array_merge_recursive( $i10n, array( 'minification' => array( 'is' => array( 'scanning' => $is_scanning, 'scanned' => $minify_module->scanner->is_files_scanned(), ), 'get' => array( 'currentScanStep' => $minify_module->scanner->get_current_scan_step(), 'totalSteps' => $minify_module->scanner->get_scan_steps(), 'showCDNModal' => ! is_multisite(), 'showSwitchModal' => (bool) get_option( 'wphb-minification-show-config_modal' ), ), ), 'strings' => array( 'discardAlert' => __( 'Are you sure? All your changes will be lost', 'wphb' ), 'queuedTooltip' => __( 'This file is queued for compression. It will get optimized when someone visits a page that requires it.', 'wphb' ), 'excludeFile' => __( "Don't load file", 'wphb' ), 'includeFile' => __( 'Re-include', 'wphb' ), 'speedySaved' => sprintf( /* translators: %1$s - opening <a> tag, %2$s - closing </a> tag */ esc_html__( 'Speedy optimization is now active. Plugins and theme files are being queued for processing and will gradually be optimized as your visitors request them. For more information on how automatic optimization works, you can check %1$sHow Does It Work%2$s section.', 'wphb' ), "<a href='#' id='wphb-basic-hdiw-link' data-modal-open='automatic-ao-hdiw-modal-content'>", '</a>' ), 'basicSaved' => sprintf( /* translators: %1$s - opening <a> tag, %2$s - closing </a> tag */ esc_html__( 'Basic optimization is now active. Plugins and theme files are now being queued for processing and will gradually be optimized as they are requested by your visitors. For more information on how automatic optimization works, you can check %1$sHow Does It Work%2$s section.', 'wphb' ), "<a href='#' id='wphb-basic-hdiw-link' data-modal-open='automatic-ao-hdiw-modal-content'>", '</a>' ), ), 'links' => array( 'minification' => self::get_admin_menu_url( 'minification' ), ), ) ); }
global $wpdb, $wp_version;
$i10n = array_merge_recursive( $i10n, array( 'mixpanel' => array( 'enabled' => Settings::get_setting( 'tracking', 'settings' ), 'plugin' => 'Hummingbird', 'plugin_type' => self::is_member() ? 'pro' : 'free', 'plugin_version' => WPHB_VERSION, 'wp_version' => $wp_version, 'wp_type' => is_multisite() ? 'multisite' : 'single', 'locale' => get_locale(), 'active_theme' => wp_get_theme()->get( 'Name' ), 'php_version' => PHP_VERSION, 'mysql_version' => $wpdb->db_version(), 'server_type' => Module_Server::get_server_type(), ), ) );
wp_localize_script( 'wphb-admin', 'wphb', $i10n ); }
/** * Return the needed capability for admin pages. * * @return string */ public static function get_admin_capability() { $cap = 'manage_options';
if ( is_multisite() && is_network_admin() ) { $cap = 'manage_network'; }
return apply_filters( 'wphb_admin_capability', $cap ); }
/** * Get Current username info */ public static function get_current_user_name() { $current_user = wp_get_current_user();
if ( ! ( $current_user instanceof WP_User ) ) { return false; }
if ( ! empty( $current_user->user_firstname ) ) { // First we try to grab user First Name. return $current_user->user_firstname; }
return $current_user->user_nicename; }
/** * Get the default user data for the report. * * @since 1.9.4 * * @return array */ public static function get_user_for_report() { /** Current user @var WP_User $user */ $user = wp_get_current_user();
if ( empty( $user->first_name ) && empty( $user->last_name ) ) { $name = $user->user_login; } else { $name = $user->first_name . ' ' . $user->last_name; }
return array( 'name' => $name, 'email' => $user->user_email, ); }
/** * This function will calculate the sum of file sizes in an array. * * We need this, because Asset Optimization module will store 'original_size' and 'compressed_size' values as * strings, and such strings will contain instead of spaces, thus making it impossible to sum all the * values with array_sum(). * * @since 1.9.2 * * @param array $arr Array of items with sizes as strings. * * @return int|mixed */ public static function calculate_sum( $arr ) { $sum = 0;
// Get separators from locale. Some Windows servers will return blank values. $locale = localeconv(); $thousands_sep = $locale['thousands_sep'] ?: ','; $decimal_point = $locale['decimal_point'] ?: '.';
foreach ( $arr as $item => $value ) { if ( is_null( $value ) ) { continue; }
// Remove spaces. $sum += (float) str_replace( array( ' ', $thousands_sep, $decimal_point ), array( '', '', '.' ), $value ); }
return $sum; }
/** * Return the file size in a humanly readable format. * * Taken from http://www.php.net/manual/en/function.filesize.php#91477 * * @since 2.0.0 * * @param int $bytes Number of bytes. * @param int $precision Precision. * * @return string */ public static function format_bytes( $bytes, $precision = 1 ) { $units = array( 'B', 'KB', 'MB', 'GB', 'TB' ); $bytes = max( $bytes, 0 ); $pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) ); $pow = min( $pow, count( $units ) - 1 ); $bytes /= pow( 1024, $pow );
return round( $bytes, $precision ) . ' ' . $units[ $pow ]; }
/** * Convert seconds to a readable value. * * @since 2.0.0 * * @param int $seconds Number of seconds. * * @return string */ public static function format_interval( $seconds ) { if ( 3600 <= $seconds && 86400 > $seconds ) { return floor( $seconds / HOUR_IN_SECONDS ) . ' h'; }
if ( 86400 <= $seconds && 2419200 > $seconds ) { return floor( $seconds / DAY_IN_SECONDS ) . ' d'; }
if ( 2419200 <= $seconds && 31536000 > $seconds ) { return floor( $seconds / MONTH_IN_SECONDS ) . ' m'; }
if ( 31536000 < $seconds && 26611200 >= $seconds ) { return floor( $seconds / YEAR_IN_SECONDS ) . ' y'; }
return '-'; }
/** * Format hours into days. * * @since 2.1.0 * * @param int $hours Number of hours. * * @return array */ public static function format_interval_hours( $hours ) { if ( $hours <= 24 ) { return array( $hours, 'hours' ); }
$days = floor( $hours / 24 ); return array( $days, 'days' ); }
/*************************** * * II. Layout functions * get_servers_dropdown() * get_caching_frequencies_dropdown() ***************************/
/** * Get servers dropdown. * * @param bool|string $selected Selected server. */ public static function get_servers_dropdown( $selected = false ) { $selected = $selected ? $selected : Module_Server::get_server_type(); ?> <select name="wphb-server-type" id="wphb-server-type" class="server-type"> <?php foreach ( Module_Server::get_servers() as $server => $server_name ) : ?> <option value="<?php echo esc_attr( $server ); ?>" <?php selected( $server, $selected ); ?>> <?php echo esc_html( $server_name ); ?> </option> <?php endforeach; ?> </select> <?php }
/** * Prepare dropdown select with caching expiry settings. * * @param array $args Arguments list. * @param bool $cloudflare Get Cloudflare frequencies. */ public static function get_caching_frequencies_dropdown( $args = array(), $cloudflare = false ) { $defaults = array( 'selected' => false, 'name' => 'expiry-select', 'id' => false, 'class' => '', 'data-type' => '', );
$args = wp_parse_args( $args, $defaults );
if ( ! $args['id'] ) { $args['id'] = $args['name']; }
if ( $cloudflare ) { $frequencies = Modules\Cloudflare::get_frequencies(); } else { $frequencies = Modules\Caching::get_frequencies(); }
?> <select id="<?php echo esc_attr( $args['id'] ); ?>" name="<?php echo esc_attr( $args['name'] ); ?>" class="<?php echo esc_attr( $args['class'] ); ?>" data-type="<?php echo esc_attr( $args['data-type'] ); ?>"> <?php foreach ( $frequencies as $key => $value ) : ?> <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $args['selected'], $key ); ?>><?php echo $value; ?></option> <?php endforeach; ?> </select> <?php }
/*************************** * * III. Time and date functions * human_read_time_diff() * get_days_of_week() * get_times() ***************************/
/** * Credits to: http://stackoverflow.com/a/11389893/1502521 * * @param int $seconds Seconds. * * @return string */ public static function human_read_time_diff( $seconds ) { if ( ! $seconds ) { return false; }
$minutes = 0; $hours = 0; $days = 0; $months = 0; $years = 0;
while ( $seconds >= YEAR_IN_SECONDS ) { $years ++; $seconds = $seconds - YEAR_IN_SECONDS; }
while ( $seconds >= MONTH_IN_SECONDS ) { $months ++; $seconds = $seconds - MONTH_IN_SECONDS; }
while ( $seconds >= DAY_IN_SECONDS ) { $days ++; $seconds = $seconds - DAY_IN_SECONDS; }
while ( $seconds >= HOUR_IN_SECONDS ) { $hours++; $seconds = $seconds - HOUR_IN_SECONDS; }
while ( $seconds >= MINUTE_IN_SECONDS ) { $minutes++; $seconds = $seconds - MINUTE_IN_SECONDS; }
$diff = new \stdClass();
$diff->y = $years; $diff->m = $months; $diff->d = $days; $diff->h = $hours; $diff->i = $minutes; $diff->s = $seconds;
if ( $diff->y || ( 11 === $diff->m && 30 <= $diff->d ) ) { $years = $diff->y; if ( 11 === $diff->m && 30 <= $diff->d ) { $years++; } /* translators: %d: year */ $diff_time = sprintf( _n( '%d year', '%d years', $years, 'wphb' ), $years ); } elseif ( $diff->m ) { /* translators: %d: month */ $diff_time = sprintf( _n( '%d month', '%d months', $diff->m, 'wphb' ), $diff->m ); } elseif ( $diff->d ) { /* translators: %d: day */ $diff_time = sprintf( _n( '%d day', '%d days', $diff->d, 'wphb' ), $diff->d ); } elseif ( $diff->h ) { /* translators: %d: hour */ $diff_time = sprintf( _n( '%d hour', '%d hours', $diff->h, 'wphb' ), $diff->h ); } elseif ( $diff->i ) { /* translators: %d: minute */ $diff_time = sprintf( _n( '%d minute', '%d minutes', $diff->i, 'wphb' ), $diff->i ); } else { /* translators: %d: second */ $diff_time = sprintf( _n( '%d second', '%d seconds', $diff->s, 'wphb' ), $diff->s ); }
return $diff_time; }
/** * Get days of the week. * * @since 1.4.5 * * @return mixed */ public static function get_days_of_week() { $timestamp = strtotime( 'next Monday' ); if ( 7 === get_option( 'start_of_week' ) ) { $timestamp = strtotime( 'next Sunday' ); } $days = array(); for ( $i = 0; $i < 7; $i ++ ) { $days[] = strftime( '%A', $timestamp ); $timestamp = strtotime( '+1 day', $timestamp ); }
return apply_filters( 'wphb_scan_get_days_of_week', $days ); }
/** * Return times frame for select box * * @since 1.4.5 * * @return mixed */ public static function get_times() { $data = array(); for ( $i = 0; $i < 24; $i ++ ) { foreach ( apply_filters( 'wphb_scan_get_times_interval', array( '00' ) ) as $min ) { $time = $i . ':' . $min; $data[ $time ] = apply_filters( 'wphb_scan_get_times_hour_min', $time ); } }
return apply_filters( 'wphb_scan_get_times', $data ); }
/*************************** * * IV. Link and url functions * get_link() * get_documentation_url() * still_having_trouble_link() * get_admin_menu_url() * get_avatar_url() ***************************/
/** * Return URL link. * * @param string $link_for Accepts: 'chat', 'plugin', 'support', 'smush', 'docs'. * @param string $campaign Utm campaign tag to be used in link. Default: 'hummingbird_pro_modal_upgrade'. * * @return string */ public static function get_link( $link_for, $campaign = 'hummingbird_pro_modal_upgrade' ) { $domain = 'https://premium.wpmudev.org'; $wp_org = 'https://wordpress.org'; $utm_tags = "?utm_source=hummingbird&utm_medium=plugin&utm_campaign={$campaign}";
switch ( $link_for ) { case 'chat': $link = "{$domain}/live-support/{$utm_tags}"; break; case 'plugin': $link = "{$domain}/project/wp-hummingbird/{$utm_tags}"; break; case 'support': if ( self::is_member() ) { $link = "{$domain}/hub/support/#get-support"; } else { $link = "{$wp_org}/support/plugin/hummingbird-performance"; } break; case 'docs': $link = "{$domain}/docs/wpmu-dev-plugins/hummingbird/{$utm_tags}"; break; case 'smush': if ( self::is_member() ) { // Return the pro plugin URL. $url = WPMUDEV_Dashboard::$ui->page_urls->plugins_url; $link = $url . '#pid=912164'; } else { // Return the free URL. $link = wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=wp-smushit' ), 'install-plugin_wp-smushit' ); } break; case 'smush-plugin': $link = "{$domain}/project/wp-smush-pro/{$utm_tags}"; break; case 'hosting': $link = "{$domain}/hosting/{$utm_tags}"; break; case 'wpmudev': $link = "{$domain}/{$utm_tags}"; break; default: $link = ''; break; }
return $link; }
/** * Get documentation URL. * * @since 1.7.0 * * @param string $page Page slug. * @param string $view View slug. * * @return string */ public static function get_documentation_url( $page, $view = '' ) { switch ( $page ) { case 'wphb-performance': if ( 'reports' === $view ) { $anchor = '#reports-pro'; } else { $anchor = '#performance-report'; } break; case 'wphb-caching': $anchor = '#caching'; break; case 'wphb-gzip': $anchor = '#gzip-compression'; break; case 'wphb-minification': $anchor = '#asset-optimization'; break; case 'wphb-advanced': $anchor = '#advanced-tools'; break; case 'wphb-uptime': $anchor = '#uptime-monitoring-pro'; break; case 'wphb-settings': $anchor = '#settings'; break; default: $anchor = ''; }
return 'https://premium.wpmudev.org/docs/wpmu-dev-plugins/hummingbird/' . $anchor; }
/** * Display start a live chat link for pro user or open support ticket for non-pro user. */ public static function still_having_trouble_link() { esc_html_e( 'Still having trouble? ', 'wphb' ); if ( self::is_member() && ! apply_filters( 'wpmudev_branding_hide_branding', false ) ) : ?> <a target="_blank" href="<?php echo esc_url( self::get_link( 'chat' ) ); ?>"> <?php esc_html_e( 'Start a live chat.', 'wphb' ); ?> </a> <?php else : ?> <a target="_blank" href="<?php echo esc_url( self::get_link( 'support' ) ); ?>"> <?php esc_html_e( 'Open a support ticket.', 'wphb' ); ?> </a> <?php endif; }
/** * Get url for plugin module page. * * @param string $page Page. * * @return string */ public static function get_admin_menu_url( $page = '' ) { $hummingbird = WP_Hummingbird::get_instance();
if ( is_object( $hummingbird->admin ) ) { $page_slug = empty( $page ) ? 'wphb' : 'wphb-' . $page; $page = $hummingbird->admin->get_admin_page( $page_slug ); if ( $page ) { return $page->get_page_url(); } }
return ''; }
/** * Get avatar URL. * * @since 1.4.5 * * @param string $get_avatar User email. * * @return mixed */ public static function get_avatar_url( $get_avatar ) { preg_match( "/src='(.*?)'/i", $get_avatar, $matches );
return $matches[1]; }
/*************************** * * V. Modules functions * get_api() * get_modules() * get_module() * get_active_cache_modules() * get_number_of_issues() * minified_files_count() * remove_quick_setup() ***************************/
/** * Get API. * * @return Api\API */ public static function get_api() { $hummingbird = WP_Hummingbird::get_instance(); return $hummingbird->core->api; }
/** * Return the list of modules and their object instances * * Do not try to load before 'wp_hummingbird_loaded' action has been executed * * @return mixed */ private static function get_modules() { $hummingbird = WP_Hummingbird::get_instance(); return $hummingbird->core->modules; }
/** * Get a module instance * * @param string $module Module slug. * * @return bool|Module|Modules\Page_Cache|Modules\GZip|Modules\Minify|Modules\Cloudflare|Modules\Uptime|Modules\Performance|Modules\Advanced|Modules\Redis */ public static function get_module( $module ) { $modules = self::get_modules(); return isset( $modules[ $module ] ) ? $modules[ $module ] : false; }
/** * Return human readable names of active modules that have a cache. * * Checks Page, Gravatar & Asset Optimization. * * @return array */ public static function get_active_cache_modules() { $modules = array( 'page_cache' => __( 'Page', 'wphb' ), 'cloudflare' => __( 'CloudFlare', 'wphb' ), 'gravatar' => __( 'Gravatar', 'wphb' ), 'minify' => __( 'Asset Optimization', 'wphb' ), );
$hb_modules = self::get_modules();
foreach ( $modules as $module => $module_name ) { // If inactive, skip to next step. if ( 'cloudflare' !== $module && isset( $hb_modules[ $module ] ) && ! $hb_modules[ $module ]->is_active() ) { unset( $modules[ $module ] ); }
// Fix CloudFlare clear cache appearing on dashboard if it had been previously enabled but then uninstalled and reinstalled HB. // TODO: do we need this? if ( 'cloudflare' === $module && isset( $hb_modules[ $module ] ) && ! $hb_modules[ $module ]->is_connected() && ! $hb_modules[ $module ]->is_zone_selected() ) { unset( $modules[ $module ] ); } }
return $modules; }
/** * Get the number of issues for selected module * * @since 1.8.1 Added $report parameter. * * @param string $module Module name. * @param bool|array $report Current report. * * @return int */ public static function get_number_of_issues( $module, $report = false ) { $issues = 0;
switch ( $module ) { case 'caching': $mod = self::get_module( $module );
if ( ! $report ) { $mod->get_analysis_data(); $report = $mod->status; }
// No report - break. if ( ! $report ) { break; }
$recommended = $mod->get_recommended_caching_values();
foreach ( $report as $type => $value ) { if ( empty( $value ) || ( $recommended[ $type ]['value'] > $value ) ) { $issues++; } } break; case 'gzip': if ( ! $report ) { $mod = self::get_module( $module ); $mod->get_analysis_data(); $report = $mod->status; }
// No report - break. if ( ! $report ) { break; }
$invalid = 0; foreach ( $report as $item => $type ) { if ( ! $type || 'privacy' === $type ) { $invalid++; } }
$issues = $invalid; break; }
return $issues; }
/** * Return the number of files used by minification. * * @since 1.4.5 * * @param bool $only_minified Only minified files. * * @return int */ public static function minified_files_count( $only_minified = false ) { $minify_module = self::get_module( 'minify' );
// Get files count. $collection = $minify_module->get_resources_collection(); // Remove those assets that we don't want to display. foreach ( $collection['styles'] as $key => $item ) { if ( ! apply_filters( 'wphb_minification_display_enqueued_file', true, $item, 'styles' ) ) { unset( $collection['styles'][ $key ] ); }
// Keep only minified files. if ( $only_minified && ! preg_match( '/\.min\.(css|js)/', basename( $item['src'] ) ) ) { unset( $collection['styles'][ $key ] ); } } foreach ( $collection['scripts'] as $key => $item ) { if ( ! apply_filters( 'wphb_minification_display_enqueued_file', true, $item, 'scripts' ) ) { unset( $collection['scripts'][ $key ] ); }
// Kepp only minified files. if ( $only_minified && ! preg_match( '/\.min\.(css|js)/', basename( $item['src'] ) ) ) { unset( $collection['scripts'][ $key ] ); } }
return ( count( $collection['scripts'] ) + count( $collection['styles'] ) ); }
/** * Returns a list of incompatible plugins if any * * @return array */ public static function get_incompat_plugin_list() { $plugins = array(); $caching_plugins = array( 'autoptimize/autoptimize.php' => 'Autoptimize', 'litespeed-cache/litespeed-cache.php' => 'LiteSpeed Cache', 'speed-booster-pack/speed-booster-pack.php' => 'Speed Booster Pack', 'swift-performance-lite/performance.php' => 'Swift Performance Lite', 'w3-total-cache/w3-total-cache.php' => 'W3 Total Cache', 'wp-fastest-cache/wpFastestCache.php' => 'WP Fastest Cache', 'wp-optimize/wp-optimize.php' => 'WP-Optimize', 'wp-performance-score-booster/wp-performance-score-booster.php' => 'WP Performance Score Booster', 'wp-performance/wp-performance.php' => 'WP Performance', 'wp-super-cache/wp-cache.php' => 'WP Super Cache', );
foreach ( $caching_plugins as $plugin => $plugin_name ) { if ( is_plugin_active( $plugin ) ) { $plugins[ $plugin ] = $plugin_name; } }
return $plugins; }
}
|