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