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