PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.1.1
ShareThis Dashboard for Google Analytics v3.1.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 3 years ago class-ga-autoloader.php 4 years ago class-ga-frontend.php 4 years ago class-ga-helper.php 3 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 3 years ago class-ga-template.php 4 years ago
class-ga-helper.php
890 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 $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 $ga4_property = get_option('googleanalytics-ga4-property');
343
344 if ( (true === self::is_authorized() && false === self::is_code_manually_enabled() && false === self::is_all_feature_disabled()) || false === empty($ga4_property) ) {
345 if ( true === self::is_account_selected() || true === isset($ga4_property)) {
346 if ( false === empty( $params ) ) {
347 return Ga_View_Core::load( $view, $params, true );
348 } else {
349 return self::ga_oauth_notice( sprintf( 'Please configure your <a href="%s">Google Analytics settings</a>.', admin_url( self::GA_SETTINGS_PAGE_URL ) ) );
350 }
351 } else {
352 return self::ga_oauth_notice( $message );
353 }
354 } else {
355 return self::ga_oauth_notice( $message );
356 }
357 }
358
359 /**
360 * Checks whether users is authorized with Google.
361 *
362 * @return boolean
363 */
364 public static function is_authorized() {
365 return Ga_Admin::api_client()->get_instance()->is_authorized();
366 }
367
368 /**
369 * Wrapper for WordPress method get_option
370 *
371 * @param string $name Option name.
372 * @param mixed|null $default Default value if fetched option is null.
373 *
374 * @return mixed|null
375 */
376 public static function get_option( $name, $default = null ) {
377 $opt = get_option( $name, $default );
378
379 return false === empty( $opt ) ? $opt : $default;
380 }
381
382 /**
383 * Wrapper for WordPress method update_option
384 *
385 * @param string $name Option name.
386 * @param mixed $value Option value.
387 *
388 * @return null|boolean
389 */
390 public static function update_option( $name, $value ) {
391 $opt = update_option( $name, $value );
392
393 return ! empty( $opt ) ? $opt : null;
394 }
395
396 /**
397 * Loads ga notice HTML code with given message included.
398 *
399 * @param string $message Message.
400 *
401 * @return string
402 */
403 public static function ga_oauth_notice( $message ) {
404 return Ga_View_Core::load(
405 'ga-oauth-notice',
406 array(
407 'msg' => $message,
408 ),
409 true
410 );
411 }
412
413 /**
414 * Displays notice following the WP style.
415 *
416 * @param string $message Message string.
417 * @param string $type Type string.
418 * @param bool $is_dismissable Whether the notice is dismissable.
419 * @param string $action Action type.
420 *
421 * @return string
422 */
423 public static function ga_wp_notice( $message, $type = '', $is_dismissable = false, $action = array() ) {
424 return Ga_View_Core::load(
425 'ga-wp-notice',
426 array(
427 'notice_type' => empty( $type ) ? Ga_Admin::NOTICE_WARNING : $type,
428 'msg' => $message,
429 'is_dismissable' => $is_dismissable,
430 'action' => $action,
431 ),
432 true
433 );
434 }
435
436 /**
437 * Gets data according to selected GA account.
438 *
439 * @param boolean $assoc Whether the return should be an associative array.
440 *
441 * @return mixed
442 */
443 public static function get_selected_account_data( $assoc = false ) {
444 $data = json_decode( self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
445 $data = ( false === empty( $data ) && 3 === count( $data ) ) ? $data : false;
446
447 if ( $data ) {
448 if ( $assoc ) {
449 return array(
450 'account_id' => $data[0],
451 'web_property_id' => $data[1],
452 'view_id' => $data[2],
453 );
454 } else {
455 return $data;
456 }
457 }
458
459 return false;
460 }
461
462 /**
463 * Checks whether option for manually UA-code.
464 *
465 * @return boolean
466 */
467 public static function is_code_manually_enabled() {
468 return boolval( self::get_option( Ga_Admin::GA_WEB_PROPERTY_ID_MANUALLY_OPTION_NAME, false ) );
469 }
470
471 /**
472 * Adds percent sign to the given text.
473 *
474 * @param string $text Text string to format as a percentage.
475 *
476 * @return string
477 */
478 public static function format_percent( $text ) {
479 $text = self::add_plus( $text );
480
481 return $text . '%';
482 }
483
484 /**
485 * Adds plus sign before number.
486 *
487 * @param string $number Number string.
488 *
489 * @return string
490 */
491 public static function add_plus( $number ) {
492 if ( $number > 0 ) {
493 return '+' . $number;
494 }
495
496 return $number;
497 }
498
499 /**
500 * Check whether current user has administrator privileges.
501 *
502 * @return bool
503 */
504 public static function is_administrator() {
505 if ( current_user_can( 'administrator' ) ) {
506 return true;
507 }
508
509 return false;
510 }
511
512 /**
513 * Is this WordPress version valid?
514 *
515 * @return bool|int
516 */
517 public static function is_wp_version_valid() {
518 $wp_version = get_bloginfo( 'version' );
519
520 return version_compare( $wp_version, Ga_Admin::MIN_WP_VERSION, 'ge' );
521 }
522
523 /**
524 * Check if terms are accepted.
525 *
526 * @return bool
527 */
528 public static function are_terms_accepted() {
529 return boolval( self::get_option( Ga_Admin::GA_SHARETHIS_TERMS_OPTION_NAME ) );
530 }
531
532 /**
533 * Check if sharethis scripts enabled.
534 *
535 * @return bool
536 */
537 public static function is_sharethis_included() {
538 return boolval( GA_SHARETHIS_SCRIPTS_INCLUDED );
539 }
540
541 /**
542 * Is this PHP version valid?
543 *
544 * @return mixed
545 */
546 public static function is_php_version_valid() {
547 $p = '#(\.0+)+($|-)#';
548 $ver1 = preg_replace( $p, '', phpversion() );
549 $ver2 = preg_replace( $p, '', self::PHP_VERSION_REQUIRED );
550 $operator = 'ge';
551
552 return version_compare( $ver1, $ver2, $operator );
553 }
554
555 /**
556 * Get current URL.
557 *
558 * @return mixed
559 */
560 public static function get_current_url() {
561 return filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
562 }
563
564 /**
565 * Create URL.
566 *
567 * @param string $url URL.
568 * @param array $data Data array.
569 *
570 * @return mixed|string
571 */
572 public static function create_url( $url, $data = array() ) {
573 return false === empty( $data ) ?
574 ( strstr( $url, '?' ) ?
575 ( $url . '&' ) :
576 ( $url . '?' ) ) . http_build_query( $data ) :
577 $url;
578 }
579
580 /**
581 * Create base64 url message
582 *
583 * @param string $msg Message.
584 * @param string $status Status.
585 *
586 * @return string
587 */
588 public static function create_url_msg( $msg, $status ) {
589 $msg = array(
590 'status' => $status,
591 'message' => $msg,
592 );
593
594 return base64_encode( wp_json_encode( $msg ) ); // phpcs:ignore
595 }
596
597 /**
598 * Are all features disabled?
599 *
600 * @return bool
601 */
602 public static function is_all_feature_disabled() {
603 return boolval( self::get_option( Ga_Admin::GA_DISABLE_ALL_FEATURES, false ) );
604 }
605
606 /**
607 * Are features enabled?
608 *
609 * @return bool
610 */
611 public static function are_features_enabled() {
612 return true === self::are_terms_accepted() && false === self::is_all_feature_disabled();
613 }
614
615 /**
616 * Are ShareThis properties verified?
617 *
618 * @return bool
619 */
620 public static function are_sharethis_properties_verified() {
621 return (
622 false !== get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT )
623 && true === self::are_sharethis_properties_set()
624 );
625 }
626
627 /**
628 * Are ShareThis properties ready to verify?
629 *
630 * @return bool
631 */
632 public static function are_sharethis_properties_ready_to_verify() {
633 return (
634 true === self::are_sharethis_properties_set()
635 && false === get_option( Ga_Admin::GA_SHARETHIS_VERIFICATION_RESULT )
636 );
637 }
638
639 /**
640 * Are ShareThis properties set?
641 *
642 * @return bool
643 */
644 public static function are_sharethis_properties_set() {
645 return (
646 false !== get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_ID ) &&
647 false !== get_option( Ga_Admin::GA_SHARETHIS_PROPERTY_SECRET )
648 );
649 }
650
651 /**
652 * Should we create the ShareThis property?
653 *
654 * @return bool
655 */
656 public static function should_create_sharethis_property() {
657 return true === self::are_features_enabled() && false === self::are_sharethis_properties_set();
658 }
659
660 /**
661 * Should we verify the ShareThis installation?
662 *
663 * @return bool
664 */
665 public static function should_verify_sharethis_installation() {
666 return true === self::are_features_enabled() && true === self::are_sharethis_properties_ready_to_verify();
667 }
668
669 /**
670 * Get tooltip.
671 *
672 * @return string
673 */
674 public static function get_tooltip() {
675 if ( false === self::are_terms_accepted() ) {
676 return self::GA_TOOLTIP_TERMS_NOT_ACCEPTED;
677 } elseif ( false === self::are_features_enabled() ) {
678 return self::GA_TOOLTIP_FEATURES_DISABLED;
679 } else {
680 return '';
681 }
682 }
683
684 /**
685 * Is this version of WordPress considered old (< 4.1)?
686 *
687 * @return bool True if old, False if not.
688 */
689 public static function is_wp_old() {
690 return version_compare( get_bloginfo( 'version' ), self::GA_WP_MODERN_VERSION, 'lt' );
691 }
692
693 /**
694 * Should we load GA JavaScript on this property?
695 *
696 * @param string $web_property_id Web property ID.
697 *
698 * @return bool
699 */
700 public static function should_load_ga_javascript( $web_property_id ) {
701 return true === self::is_configured( $web_property_id )
702 && (
703 true === self::can_add_ga_code()
704 || true === self::is_all_feature_disabled()
705 );
706 }
707
708 /**
709 * Get account ID.
710 *
711 * @return string
712 */
713 public static function get_account_id() {
714 $account_id = json_decode( self::get_option( Ga_Admin::GA_SELECTED_ACCOUNT ) );
715
716 return ! empty( $account_id[0] ) ? $account_id[0] : '';
717 }
718
719 /**
720 * Is curl disabled?
721 *
722 * @return bool True if disabled, false if enabled.
723 */
724 public static function is_curl_disabled() {
725 return ! function_exists( 'curl_version' );
726 }
727
728 /**
729 * Get URL with correct protocol.
730 *
731 * @return string URL with correct protocol.
732 */
733 public static function get_plugin_url_with_correct_protocol() {
734 return GA_PLUGIN_URL;
735 }
736
737 /**
738 * Get code to manually label classes.
739 *
740 * @return string
741 */
742 public static function get_code_manually_label_classes() {
743 $classes = '';
744 if ( ! self::are_features_enabled() ) {
745 $classes = 'label-grey ga-tooltip';
746 } elseif ( self::is_account_selected() ) {
747 $classes = 'label-grey';
748 }
749 return $classes;
750 }
751
752 /**
753 * Get Previous Period for Dates (date start and date end).
754 *
755 * @param string $date_start Date string.
756 * @param string $date_end Date string.
757 *
758 * @return array Array of start and end dates in Y-m-d format.
759 * @since 2.5.2
760 */
761 public static function get_previous_period_for_dates( $date_start = '', $date_end = '' ) {
762 try {
763 // Get distance between dates in days.
764 $start = new DateTime( $date_start );
765 $end = new DateTime( $date_end );
766 } catch ( \Exception $e ) {
767 return array(
768 'start' => gmdate( 'Y-m-d', strtotime( '-1 week' ) ),
769 'end' => gmdate( 'Y-m-d' ),
770 );
771 }
772
773 // Clone $start date into end_previous so we don't modify $start.
774 $end_previous = clone $start;
775
776 // Set the period to the difference between the start/end dates in days.
777 $period = $end->diff( $start )->days;
778
779 // Subtract 1 day from $end_previous so it's one day before $start.
780 $end_previous->modify( '-1 day' );
781
782 // Clone $end_previous so we can subtract $period from it in days.
783 $start_previous = clone $end_previous;
784 $start_previous->modify( sprintf( '-%d day', $period ) );
785
786 return array(
787 'start' => $start_previous->format( 'Y-m-d' ),
788 'end' => $end_previous->format( 'Y-m-d' ),
789 );
790 }
791
792 /**
793 * Get period between dates in days.
794 *
795 * @param string $date_start Start date string.
796 * @param string $date_end End date string.
797 *
798 * @return int
799 * @since 2.5.2
800 */
801 public static function get_period_in_days( $date_start = '', $date_end = '' ) {
802 $date_start = empty( $date_start ) ? gmdate( 'Y-m-d', strtotime( '-1 week' ) ) : $date_start;
803 $date_end = empty( $date_end ) ? gmdate( 'Y-m-d' ) : $date_end;
804
805 try {
806 // Get distance between dates in days.
807 $start = new DateTime( $date_start );
808 $end = new DateTime( $date_end );
809 } catch ( \Exception $e ) {
810 return 0;
811 }
812
813 // Set the period to the difference between the start/end dates in days.
814 return intval( $start->diff( $end )->format( '%r%a' ) );
815 }
816
817 /**
818 * Get period in Days as words.
819 *
820 * @param string $date_start Start date string.
821 * @param string $date_end End date string.
822 *
823 * @return string Words to indicate days.
824 * @since 2.5.2
825 */
826 public static function get_period_in_days_words( $date_start = '', $date_end = '' ) {
827 $days = self::get_period_in_days( $date_start, $date_end );
828
829 $date_end = empty( $date_end ) ? strtotime( 'now' ) : strtotime( $date_end );
830
831 // If today is the same as the end date.
832 if ( gmdate( 'Y-m-d', $date_end ) === gmdate( 'Y-m-d' ) ) {
833 if ( 0 === $days ) {
834 return __( 'Today', 'googleanalytics' );
835 }
836
837 if ( 7 === $days ) {
838 return __( 'This Week', 'googleanalytics' );
839 }
840
841 return sprintf(
842 /* translators: %d stands for the Day or Days. */
843 _n( 'Last %d Day', 'Last %d Days', $days, 'googleanalytics' ),
844 $days
845 );
846 }
847
848 return sprintf(
849 /* translators: %d stands for the Day or Days. */
850 _n( '%d Day', '%d Days', $days, 'googleanalytics' ),
851 $days
852 );
853 }
854
855 /**
856 * Get date range from GET request.
857 *
858 * @return array
859 * @since 2.5.2
860 */
861 public static function get_date_range_from_request() {
862 $date_range = filter_input_array(
863 INPUT_GET,
864 array(
865 'date_from' => FILTER_SANITIZE_STRING,
866 'date_to' => FILTER_SANITIZE_STRING,
867 )
868 );
869
870 // If date_from is after date_to, let's reset 'from' to a week before 'to'.
871 if ( 0 > self::get_period_in_days( $date_range['date_from'], $date_range['date_to'] ) ) {
872 try {
873 $date = new DateTime( $date_range['date_to'] );
874 $date->modify( '-1 week' );
875
876 $date_from = $date->format( 'Y-m-d' );
877 } catch ( \Exception $e ) {
878 $date_from = gmdate( 'Y-m-d', strtotime( '-1 week' ) );
879 }
880
881 $date_range['date_from'] = $date_from;
882 }
883
884 return array(
885 'from' => $date_range['date_from'],
886 'to' => $date_range['date_to'],
887 );
888 }
889 }
890