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