PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.2.1
ShareThis Dashboard for Google Analytics v3.2.1
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / class / class-ga-helper.php
googleanalytics / class Last commit date
controller 4 years ago core 4 years ago class-ga-admin.php 2 years ago class-ga-autoloader.php 4 years ago class-ga-frontend.php 3 years ago class-ga-helper.php 2 years ago class-ga-hook.php 4 years ago class-ga-notice.php 4 years ago class-ga-sharethis.php 4 years ago class-ga-stats.php 2 years ago class-ga-template.php 4 years ago
class-ga-helper.php
892 lines
1 <?php
2 /**
3 * GoogleAnalytics Helper.
4 *
5 * @package GoogleAnalytics
6 */
7
8 /**
9 * Helper class.
10 */
11 class Ga_Helper {
12
13 const ROLE_ID_PREFIX = 'role-id-';
14 const GA_DEFAULT_WEB_ID = 'UA-0000000-0';
15 const GA_STATISTICS_PAGE_URL = 'admin.php?page=googleanalytics';
16 const GA_SETTINGS_PAGE_URL = 'admin.php?page=googleanalytics/settings';
17 const DASHBOARD_PAGE_NAME = 'admin.php?page=googleanalytics';
18 const PHP_VERSION_REQUIRED = '7.4';
19 const GA_WP_MODERN_VERSION = '4.1';
20 const GA_TOOLTIP_TERMS_NOT_ACCEPTED = 'Please accept the terms to use this feature.';
21 const GA_TOOLTIP_FEATURES_DISABLED = 'Click the Enable button at the top to start using this feature.';
22 const GA_DEBUG_MODE = false;
23
24 /**
25 * Init plugin actions.
26 */
27 public static function init() {
28
29 // Displays errors related to required PHP version.
30 if ( false === self::is_php_version_valid() ) {
31 add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_php_version' );
32
33 return false;
34 }
35
36 // Displays errors related to required WP version.
37 if ( false === self::is_wp_version_valid() ) {
38 add_action( 'admin_notices', 'Ga_Admin::admin_notice_googleanalytics_wp_version' );
39
40 return false;
41 }
42
43 if ( ! is_admin() ) {
44 Ga_Frontend::add_actions();
45 }
46
47 if ( is_admin() ) {
48 Ga_Admin::add_filters();
49 Ga_Admin::add_actions();
50 Ga_Admin::init_oauth();
51
52 $admin_controller = new Ga_Admin_Controller();
53 $admin_controller->handle_actions();
54 }
55 }
56
57 /**
58 * Checks if current page is a WordPress dashboard.
59 *
60 * @return integer
61 */
62 public static function is_plugin_page() {
63 $page = filter_input( INPUT_GET, 'page', FILTER_UNSAFE_RAW );
64 $page_split = explode( '/', sanitize_text_field( wp_unslash( $page ) ) );
65
66 if ( false === empty( $page_split ) && true === isset( $page_split[0] ) ) {
67 return GA_NAME === $page_split[0];
68 }
69
70 return false;
71 }
72
73 /**
74 * Checks if current page is a WordPress dashboard.
75 *
76 * @return number
77 */
78 public static function is_dashboard_page() {
79 $site = get_current_screen();
80
81 return preg_match( '/' . self::DASHBOARD_PAGE_NAME . '/', $site->base );
82 }
83
84 /**
85 * Check whether the plugin is configured.
86 *
87 * @param string $web_id Web ID string.
88 *
89 * @return boolean
90 */
91 public static function is_configured( $web_id ) {
92 return self::GA_DEFAULT_WEB_ID !== $web_id && false === empty( $web_id );
93 }
94
95 /**
96 * Prepare an array of current site's user roles
97 *
98 * @return array
99 */
100 public static function get_user_roles() {
101 global $wp_roles;
102 if ( false === isset( $wp_roles ) ) {
103 $wp_roles = new WP_Roles(); // phpcs:ignore
104 }
105
106 return $wp_roles->get_names();
107 }
108
109 /**
110 * Prepare a role ID.
111 *
112 * The role ID is derived from the role's name and will be used
113 * in its setting name in the additional settings.
114 *
115 * @param string $role_name Role name.
116 *
117 * @return string
118 */
119 public static function prepare_role_id( $role_name ) {
120 return self::ROLE_ID_PREFIX . strtolower( preg_replace( '/[\W]/', '-', before_last_bar( $role_name ) ) );
121 }
122
123 /**
124 * Prepares role id.
125 *
126 * @param string $v Value string.
127 * @param string $k Key string.
128 */
129 public static function prepare_role( &$v, $k ) {
130 $v = self::prepare_role_id( $v );
131 }
132
133 /**
134 * Checks whether user role is excluded from adding UA code.
135 *
136 * @return boolean
137 */
138 public static function can_add_ga_code() {
139 $current_user = wp_get_current_user();
140 $user_roles = ! empty( $current_user->roles ) ? $current_user->roles : array();
141 $exclude_roles = json_decode( get_option( Ga_Admin::GA_EXCLUDE_ROLES_OPTION_NAME ), true );
142
143 array_walk( $user_roles, 'Ga_Helper::prepare_role' );
144
145 $return = true;
146 foreach ( $user_roles as $role ) {
147 if ( ! empty( $exclude_roles[ $role ] ) ) {
148 $return = false;
149 break;
150 }
151 }
152
153 return $return;
154 }
155
156 /**
157 * Adds ga dashboard widget HTML code for a WordPress Dashboard widget hook.
158 */
159 public static function add_ga_dashboard_widget() {
160 $widget = self::get_ga_dashboard_widget(
161 null,
162 false,
163 false,
164 true
165 );
166
167 echo wp_kses(
168 $widget,
169 array(
170 'a' => array(
171 'href' => array(),
172 ),
173 'button' => array(
174 'class' => array(),
175 'id' => array(),
176 'style' => array(),
177 ),
178 'div' => array(
179 'class' => array(),
180 'id' => array(),
181 'style' => array(),
182 ),
183 'select' => array(
184 'id' => array(),
185 'autocomplete' => array(),
186 ),
187 'script' => array(
188 'type' => array(),
189 ),
190 'option' => array(
191 'value' => array(),
192 'selected' => array(),
193 ),
194 )
195 );
196 }
197
198 /**
199 * Generates dashboard widget HTML code.
200 *
201 * @param string $date_range Google Analytics specific date range string.
202 * @param boolean $text_mode Text mode.
203 * @param boolean $ajax Ajax.
204 * @param boolean $trigger_request Trigger request.
205 *
206 * @return null | string HTML dashboard widget code.
207 */
208 public static function get_ga_dashboard_widget(
209 $date_range = null,
210 $text_mode = false,
211 $ajax = false,
212 $trigger_request = false
213 ) {
214 if ( empty( $date_range ) ) {
215 $date_range = '30daysAgo';
216 }
217
218 if ( false === $trigger_request ) {
219 // Get chart and boxes data.
220 $data = self::get_dashboard_widget_data( $date_range );
221
222 if ( $text_mode ) {
223 return self::get_chart_page(
224 'ga-dashboard-widget' . ( $ajax ? '_ajax' : '' ),
225 array(
226 'chart' => $data['chart'],
227 'boxes' => $data['boxes'],
228 )
229 );
230 } else {
231 return self::get_chart_page(
232 'ga-dashboard-widget' . ( $ajax ? '_ajax' : '' ),
233 array(
234 'chart' => $data['chart'],
235 'boxes' => $data['boxes'],
236 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
237 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
238 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME,
239 )
240 );
241 }
242 } else {
243 return self::get_chart_page(
244 'ga-dashboard-widget' . ( $ajax ? '_ajax' : '' ),
245 array(
246 'chart' => array(),
247 'boxes' => Ga_Stats::get_empty_boxes_structure(),
248 'more_details_url' => admin_url( self::GA_STATISTICS_PAGE_URL ),
249 'show_trigger_button' => true,
250 'ga_nonce' => wp_create_nonce( 'ga_ajax_data_change' ),
251 'ga_nonce_name' => Ga_Admin_Controller::GA_NONCE_FIELD_NAME,
252 )
253 );
254 }
255
256 return null;
257 }
258
259 /**
260 * Generates JSON data string for AJAX calls.
261 *
262 * @param string $date_range Date range.
263 * @param string $metric Metric string.
264 * @param boolean $text_mode Text mode.
265 * @param boolean $ajax Ajax.
266 *
267 * @return string|false Returns JSON data string
268 */
269 public static function get_ga_dashboard_widget_data_json(
270 $date_range = null, $metric = null, $text_mode = false, $ajax = false
271 ) {
272 if ( empty( $date_range ) ) {
273 $date_range = '30daysAgo';
274 }
275
276 if ( empty( $metric ) ) {
277 $metric = 'pageviews';
278 }
279
280 $data = self::get_dashboard_widget_data( $date_range, $metric );
281
282 return wp_json_encode( $data );
283 }
284
285 /**
286 * Gets dashboard widget data.
287 *
288 * @param string $date_range Date range string.
289 * @param string $metric Metric.
290 *
291 * @return array Return chart and boxes data
292 */
293 private static function get_dashboard_widget_data( $date_range, $metric = null ) {
294 $selected = self::get_selected_account_data( true );
295 if ( self::is_authorized() && self::is_account_selected() ) {
296 $query_params = Ga_Stats::get_query( 'main_chart', $selected['view_id'], $date_range, $metric, true );
297 $stats_data = Ga_Admin::api_client()->call(
298 'ga_api_data',
299 array(
300 $query_params,
301 )
302 );
303
304 $boxes_query = Ga_Stats::get_query( 'dashboard_boxes', $selected['view_id'], $date_range, null, true );
305 $boxes_data = Ga_Admin::api_client()->call(
306 'ga_api_data',
307 array(
308 $boxes_query,
309 )
310 );
311 }
312 $chart = ! empty( $stats_data ) ? Ga_Stats::get_dashboard_chart( $stats_data->get_data() ) : array();
313 $boxes = ! empty( $boxes_data ) ? Ga_Stats::get_dashboard_boxes_data( $boxes_data->get_data() ) : array();
314
315 return array(
316 'chart' => $chart,
317 'boxes' => $boxes,
318 );
319 }
320
321 /**
322 * Is account selected?
323 *
324 * @return bool
325 */
326 public static function is_account_selected() {
327 return false === empty( self::get_selected_account_data() );
328 }
329
330 /**
331 * Returns HTML code of the chart page or a notice.
332 *
333 * @param string $view View string.
334 * @param array $params Params array.
335 *
336 * @return string Returns HTML code
337 */
338 public static function get_chart_page( $view, $params ) {
339 $message = sprintf(
340 /* translators: %s is the settings page URL. */
341 __( 'Statistics can only be seen after you authenticate with your Google account on the <a href="%s">Settings page</a>.' ),
342 admin_url( self::GA_SETTINGS_PAGE_URL )
343 );
344 $ga4_property = get_option('googleanalytics-ga4-property');
345
346 if ( (true === self::is_authorized() && false === self::is_code_manually_enabled() && false === self::is_all_feature_disabled()) || false === empty($ga4_property) ) {
347 if ( true === self::is_account_selected() || true === isset($ga4_property)) {
348 if ( false === empty( $params ) ) {
349 return Ga_View_Core::load( $view, $params, true );
350 } else {
351 return self::ga_oauth_notice( sprintf( 'Please configure your <a href="%s">Google Analytics settings</a>.', admin_url( self::GA_SETTINGS_PAGE_URL ) ) );
352 }
353 } else {
354 return self::ga_oauth_notice( $message );
355 }
356 } else {
357 return self::ga_oauth_notice( $message );
358 }
359 }
360
361 /**
362 * Checks whether users is authorized with Google.
363 *
364 * @return boolean
365 */
366 public static function is_authorized() {
367 return Ga_Admin::api_client()->get_instance()->is_authorized();
368 }
369
370 /**
371 * Wrapper for WordPress method get_option
372 *
373 * @param string $name Option name.
374 * @param mixed|null $default Default value if fetched option is null.
375 *
376 * @return mixed|null
377 */
378 public static function get_option( $name, $default = null ) {
379 $opt = get_option( $name, $default );
380
381 return false === empty( $opt ) ? $opt : $default;
382 }
383
384 /**
385 * Wrapper for WordPress method update_option
386 *
387 * @param string $name Option name.
388 * @param mixed $value Option value.
389 *
390 * @return null|boolean
391 */
392 public static function update_option( $name, $value ) {
393 $opt = update_option( $name, $value );
394
395 return ! empty( $opt ) ? $opt : null;
396 }
397
398 /**
399 * Loads ga notice HTML code with given message included.
400 *
401 * @param string $message Message.
402 *
403 * @return string
404 */
405 public static function ga_oauth_notice( $message ) {
406 return Ga_View_Core::load(
407 'ga-oauth-notice',
408 array(
409 'msg' => $message,
410 ),
411 true
412 );
413 }
414
415 /**
416 * Displays notice following the WP style.
417 *
418 * @param string $message Message string.
419 * @param string $type Type string.
420 * @param bool $is_dismissable Whether the notice is dismissable.
421 * @param string $action Action type.
422 *
423 * @return string
424 */
425 public static function ga_wp_notice( $message, $type = '', $is_dismissable = false, $action = array() ) {
426 return Ga_View_Core::load(
427 'ga-wp-notice',
428 array(
429 'notice_type' => empty( $type ) ? Ga_Admin::NOTICE_WARNING : $type,
430 'msg' => $message,
431 'is_dismissable' => $is_dismissable,
432 'action' => $action,
433 ),
434 true
435 );
436 }
437
438 /**
439 * Gets data according to selected GA account.
440 *
441 * @param boolean $assoc Whether the return should be an associative array.
442 *
443 * @return mixed
444 */
445 public static function get_selected_account_data( $assoc = false ) {
446 $data = self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT );
447 $data = ( false === empty( $data ) && 3 === count( $data ) ) ? json_decode( $data ) : false;
448
449 if ( $data ) {
450 if ( $assoc ) {
451 return array(
452 'account_id' => $data[0],
453 'web_property_id' => $data[1],
454 'view_id' => $data[2],
455 );
456 } else {
457 return $data;
458 }
459 }
460
461 return false;
462 }
463
464 /**
465 * Checks whether option for manually UA-code.
466 *
467 * @return boolean
468 */
469 public static function is_code_manually_enabled() {
470 return boolval( self::get_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false ) );
471 }
472
473 /**
474 * Adds percent sign to the given text.
475 *
476 * @param string $text Text string to format as a percentage.
477 *
478 * @return string
479 */
480 public static function format_percent( $text ) {
481 $text = self::add_plus( $text );
482
483 return $text . '%';
484 }
485
486 /**
487 * Adds plus sign before number.
488 *
489 * @param string $number Number string.
490 *
491 * @return string
492 */
493 public static function add_plus( $number ) {
494 if ( $number > 0 ) {
495 return '+' . $number;
496 }
497
498 return $number;
499 }
500
501 /**
502 * Check whether current user has administrator privileges.
503 *
504 * @return bool
505 */
506 public static function is_administrator() {
507 if ( current_user_can( 'administrator' ) ) {
508 return true;
509 }
510
511 return false;
512 }
513
514 /**
515 * Is this WordPress version valid?
516 *
517 * @return bool|int
518 */
519 public static function is_wp_version_valid() {
520 $wp_version = get_bloginfo( 'version' );
521
522 return version_compare( $wp_version, Ga_Admin::MIN_WP_VERSION, 'ge' );
523 }
524
525 /**
526 * Check if terms are accepted.
527 *
528 * @return bool
529 */
530 public static function are_terms_accepted() {
531 return boolval( self::get_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME ) );
532 }
533
534 /**
535 * Check if sharethis scripts enabled.
536 *
537 * @return bool
538 */
539 public static function is_sharethis_included() {
540 return boolval( GA_SHARETHIS_SCRIPTS_INCLUDED );
541 }
542
543 /**
544 * Is this PHP version valid?
545 *
546 * @return mixed
547 */
548 public static function is_php_version_valid() {
549 $p = '#(\.0+)+($|-)#';
550 $ver1 = preg_replace( $p, '', phpversion() );
551 $ver2 = preg_replace( $p, '', self::PHP_VERSION_REQUIRED );
552 $operator = 'ge';
553
554 return version_compare( $ver1, $ver2, $operator );
555 }
556
557 /**
558 * Get current URL.
559 *
560 * @return mixed
561 */
562 public static function get_current_url() {
563 return filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
564 }
565
566 /**
567 * Create URL.
568 *
569 * @param string $url URL.
570 * @param array $data Data array.
571 *
572 * @return mixed|string
573 */
574 public static function create_url( $url, $data = array() ) {
575 return false === empty( $data ) ?
576 ( strstr( $url, '?' ) ?
577 ( $url . '&' ) :
578 ( $url . '?' ) ) . http_build_query( $data ) :
579 $url;
580 }
581
582 /**
583 * Create base64 url message
584 *
585 * @param string $msg Message.
586 * @param string $status Status.
587 *
588 * @return string
589 */
590 public static function create_url_msg( $msg, $status ) {
591 $msg = array(
592 'status' => $status,
593 'message' => $msg,
594 );
595
596 return base64_encode( wp_json_encode( $msg ) ); // phpcs:ignore
597 }
598
599 /**
600 * Are all features disabled?
601 *
602 * @return bool
603 */
604 public static function is_all_feature_disabled() {
605 return boolval( self::get_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, false ) );
606 }
607
608 /**
609 * Are features enabled?
610 *
611 * @return bool
612 */
613 public static function are_features_enabled() {
614 return true === self::are_terms_accepted() && false === self::is_all_feature_disabled();
615 }
616
617 /**
618 * Are ShareThis properties verified?
619 *
620 * @return bool
621 */
622 public static function are_sharethis_properties_verified() {
623 return (
624 false !== get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT )
625 && true === self::are_sharethis_properties_set()
626 );
627 }
628
629 /**
630 * Are ShareThis properties ready to verify?
631 *
632 * @return bool
633 */
634 public static function are_sharethis_properties_ready_to_verify() {
635 return (
636 true === self::are_sharethis_properties_set()
637 && false === get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT )
638 );
639 }
640
641 /**
642 * Are ShareThis properties set?
643 *
644 * @return bool
645 */
646 public static function are_sharethis_properties_set() {
647 return (
648 false !== get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ) &&
649 false !== get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET )
650 );
651 }
652
653 /**
654 * Should we create the ShareThis property?
655 *
656 * @return bool
657 */
658 public static function should_create_sharethis_property() {
659 return true === self::are_features_enabled() && false === self::are_sharethis_properties_set();
660 }
661
662 /**
663 * Should we verify the ShareThis installation?
664 *
665 * @return bool
666 */
667 public static function should_verify_sharethis_installation() {
668 return true === self::are_features_enabled() && true === self::are_sharethis_properties_ready_to_verify();
669 }
670
671 /**
672 * Get tooltip.
673 *
674 * @return string
675 */
676 public static function get_tooltip() {
677 if ( false === self::are_terms_accepted() ) {
678 return self::GA_TOOLTIP_TERMS_NOT_ACCEPTED;
679 } elseif ( false === self::are_features_enabled() ) {
680 return self::GA_TOOLTIP_FEATURES_DISABLED;
681 } else {
682 return '';
683 }
684 }
685
686 /**
687 * Is this version of WordPress considered old (< 4.1)?
688 *
689 * @return bool True if old, False if not.
690 */
691 public static function is_wp_old() {
692 return version_compare( get_bloginfo( 'version' ), self::GA_WP_MODERN_VERSION, 'lt' );
693 }
694
695 /**
696 * Should we load GA JavaScript on this property?
697 *
698 * @param string $web_property_id Web property ID.
699 *
700 * @return bool
701 */
702 public static function should_load_ga_javascript( $web_property_id ) {
703 return true === self::is_configured( $web_property_id )
704 && (
705 true === self::can_add_ga_code()
706 || true === self::is_all_feature_disabled()
707 );
708 }
709
710 /**
711 * Get account ID.
712 *
713 * @return string
714 */
715 public static function get_account_id() {
716 $account_id = self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT );
717
718 return false === empty( $account_id ) ? json_decode( $account_id )[0] : '';
719 }
720
721 /**
722 * Is curl disabled?
723 *
724 * @return bool True if disabled, false if enabled.
725 */
726 public static function is_curl_disabled() {
727 return ! function_exists( 'curl_version' );
728 }
729
730 /**
731 * Get URL with correct protocol.
732 *
733 * @return string URL with correct protocol.
734 */
735 public static function get_plugin_url_with_correct_protocol() {
736 return GA_PLUGIN_URL;
737 }
738
739 /**
740 * Get code to manually label classes.
741 *
742 * @return string
743 */
744 public static function get_code_manually_label_classes() {
745 $classes = '';
746 if ( ! self::are_features_enabled() ) {
747 $classes = 'label-grey ga-tooltip';
748 } elseif ( self::is_account_selected() ) {
749 $classes = 'label-grey';
750 }
751 return $classes;
752 }
753
754 /**
755 * Get Previous Period for Dates (date start and date end).
756 *
757 * @param string $date_start Date string.
758 * @param string $date_end Date string.
759 *
760 * @return array Array of start and end dates in Y-m-d format.
761 * @since 2.5.2
762 */
763 public static function get_previous_period_for_dates( $date_start = '', $date_end = '' ) {
764 try {
765 // Get distance between dates in days.
766 $start = new DateTime( $date_start );
767 $end = new DateTime( $date_end );
768 } catch ( \Exception $e ) {
769 return array(
770 'start' => gmdate( 'Y-m-d', strtotime( '-1 week' ) ),
771 'end' => gmdate( 'Y-m-d' ),
772 );
773 }
774
775 // Clone $start date into end_previous so we don't modify $start.
776 $end_previous = clone $start;
777
778 // Set the period to the difference between the start/end dates in days.
779 $period = $end->diff( $start )->days;
780
781 // Subtract 1 day from $end_previous so it's one day before $start.
782 $end_previous->modify( '-1 day' );
783
784 // Clone $end_previous so we can subtract $period from it in days.
785 $start_previous = clone $end_previous;
786 $start_previous->modify( sprintf( '-%d day', $period ) );
787
788 return array(
789 'start' => $start_previous->format( 'Y-m-d' ),
790 'end' => $end_previous->format( 'Y-m-d' ),
791 );
792 }
793
794 /**
795 * Get period between dates in days.
796 *
797 * @param string $date_start Start date string.
798 * @param string $date_end End date string.
799 *
800 * @return int
801 * @since 2.5.2
802 */
803 public static function get_period_in_days( $date_start = '', $date_end = '' ) {
804 $date_start = empty( $date_start ) ? gmdate( 'Y-m-d', strtotime( '-1 week' ) ) : $date_start;
805 $date_end = empty( $date_end ) ? gmdate( 'Y-m-d' ) : $date_end;
806
807 try {
808 // Get distance between dates in days.
809 $start = new DateTime( $date_start );
810 $end = new DateTime( $date_end );
811 } catch ( \Exception $e ) {
812 return 0;
813 }
814
815 // Set the period to the difference between the start/end dates in days.
816 return intval( $start->diff( $end )->format( '%r%a' ) );
817 }
818
819 /**
820 * Get period in Days as words.
821 *
822 * @param string $date_start Start date string.
823 * @param string $date_end End date string.
824 *
825 * @return string Words to indicate days.
826 * @since 2.5.2
827 */
828 public static function get_period_in_days_words( $date_start = '', $date_end = '' ) {
829 $days = self::get_period_in_days( $date_start, $date_end );
830
831 $date_end = empty( $date_end ) ? strtotime( 'now' ) : strtotime( $date_end );
832
833 // If today is the same as the end date.
834 if ( gmdate( 'Y-m-d', $date_end ) === gmdate( 'Y-m-d' ) ) {
835 if ( 0 === $days ) {
836 return __( 'Today', 'googleanalytics' );
837 }
838
839 if ( 7 === $days ) {
840 return __( 'This Week', 'googleanalytics' );
841 }
842
843 return sprintf(
844 /* translators: %d stands for the Day or Days. */
845 _n( 'Last %d Day', 'Last %d Days', $days, 'googleanalytics' ),
846 $days
847 );
848 }
849
850 return sprintf(
851 /* translators: %d stands for the Day or Days. */
852 _n( '%d Day', '%d Days', $days, 'googleanalytics' ),
853 $days
854 );
855 }
856
857 /**
858 * Get date range from GET request.
859 *
860 * @return array
861 * @since 2.5.2
862 */
863 public static function get_date_range_from_request() {
864 $date_range = filter_input_array(
865 INPUT_GET,
866 array(
867 'date_from' => FILTER_UNSAFE_RAW,
868 'date_to' => FILTER_UNSAFE_RAW,
869 )
870 );
871
872 // If date_from is after date_to, let's reset 'from' to a week before 'to'.
873 if ( 0 > self::get_period_in_days( $date_range['date_from'], $date_range['date_to'] ) ) {
874 try {
875 $date = new DateTime( $date_range['date_to'] );
876 $date->modify( '-1 week' );
877
878 $date_from = $date->format( 'Y-m-d' );
879 } catch ( \Exception $e ) {
880 $date_from = gmdate( 'Y-m-d', strtotime( '-1 week' ) );
881 }
882
883 $date_range['date_from'] = $date_from;
884 }
885
886 return array(
887 'from' => $date_range['date_from'],
888 'to' => $date_range['date_to'],
889 );
890 }
891 }
892