| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Helps with creating shortlinks in the plugin. |
| 10 |
*/ |
| 11 |
class WPSEO_Shortlinker { |
| 12 |
|
| 13 |
/** |
| 14 |
* Collects the additional data necessary for the shortlink. |
| 15 |
* |
| 16 |
* @return array The shortlink data. |
| 17 |
*/ |
| 18 |
protected function collect_additional_shortlink_data() { |
| 19 |
$data = [ |
| 20 |
'php_version' => $this->get_php_version(), |
| 21 |
'platform' => 'wordpress', |
| 22 |
'platform_version' => $this->get_platform_version(), |
| 23 |
'software' => $this->get_software(), |
| 24 |
'software_version' => WPSEO_VERSION, |
| 25 |
'days_active' => $this->get_days_active(), |
| 26 |
'user_language' => $this->get_user_language(), |
| 27 |
]; |
| 28 |
|
| 29 |
$admin_page = filter_input( INPUT_GET, 'page' ); |
| 30 |
if ( ! empty( $admin_page ) ) { |
| 31 |
$data['screen'] = $admin_page; |
| 32 |
} |
| 33 |
|
| 34 |
return $data; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Builds a URL to use in the plugin as shortlink. |
| 39 |
* |
| 40 |
* @param string $url The URL to build upon. |
| 41 |
* |
| 42 |
* @return string The final URL. |
| 43 |
*/ |
| 44 |
public function build_shortlink( $url ) { |
| 45 |
return add_query_arg( $this->collect_additional_shortlink_data(), $url ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns a version of the URL with a utm_content with the current version. |
| 50 |
* |
| 51 |
* @param string $url The URL to build upon. |
| 52 |
* |
| 53 |
* @return string The final URL. |
| 54 |
*/ |
| 55 |
public static function get( $url ) { |
| 56 |
$shortlinker = new self(); |
| 57 |
|
| 58 |
return $shortlinker->build_shortlink( $url ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Echoes a version of the URL with a utm_content with the current version. |
| 63 |
* |
| 64 |
* @param string $url The URL to build upon. |
| 65 |
*/ |
| 66 |
public static function show( $url ) { |
| 67 |
echo esc_url( self::get( $url ) ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Gets the shortlink's query params. |
| 72 |
* |
| 73 |
* @return array The shortlink's query params. |
| 74 |
*/ |
| 75 |
public static function get_query_params() { |
| 76 |
$shortlinker = new self(); |
| 77 |
|
| 78 |
return $shortlinker->collect_additional_shortlink_data(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Gets the current site's PHP version, without the extra info. |
| 83 |
* |
| 84 |
* @return string The PHP version. |
| 85 |
*/ |
| 86 |
private function get_php_version() { |
| 87 |
$version = explode( '.', PHP_VERSION ); |
| 88 |
|
| 89 |
return (int) $version[0] . '.' . (int) $version[1]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Gets the current site's platform version. |
| 94 |
* |
| 95 |
* @return string The wp_version. |
| 96 |
*/ |
| 97 |
protected function get_platform_version() { |
| 98 |
return $GLOBALS['wp_version']; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get our software and whether it's active or not. |
| 103 |
* |
| 104 |
* @return string The software name + activation state. |
| 105 |
*/ |
| 106 |
private function get_software() { |
| 107 |
if ( YoastSEO()->helpers->product->is_premium() ) { |
| 108 |
return 'premium'; |
| 109 |
} |
| 110 |
|
| 111 |
return 'free'; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Gets the number of days the plugin has been active. |
| 116 |
* |
| 117 |
* @return int The number of days the plugin is active. |
| 118 |
*/ |
| 119 |
private function get_days_active() { |
| 120 |
$date_activated = WPSEO_Options::get( 'first_activated_on' ); |
| 121 |
$datediff = ( time() - $date_activated ); |
| 122 |
$days = (int) round( $datediff / DAY_IN_SECONDS ); |
| 123 |
switch ( $days ) { |
| 124 |
case 0: |
| 125 |
case 1: |
| 126 |
$cohort = '0-1'; |
| 127 |
break; |
| 128 |
case ( $days < 5 ): |
| 129 |
$cohort = '2-5'; |
| 130 |
break; |
| 131 |
case ( $days < 30 ): |
| 132 |
$cohort = '6-30'; |
| 133 |
break; |
| 134 |
case ( $days < 91 ): |
| 135 |
$cohort = '31-90'; |
| 136 |
break; |
| 137 |
case ( $days < 181 ): |
| 138 |
$cohort = '91-180'; |
| 139 |
break; |
| 140 |
case ( $days < 366 ): |
| 141 |
$cohort = '181-365'; |
| 142 |
break; |
| 143 |
default: |
| 144 |
$cohort = '365plus'; |
| 145 |
} |
| 146 |
|
| 147 |
return $cohort; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Gets the user's language. |
| 152 |
* |
| 153 |
* @return string|false The user's language or `false` when it couldn't be retrieved. |
| 154 |
*/ |
| 155 |
private function get_user_language() { |
| 156 |
if ( function_exists( 'get_user_locale' ) ) { |
| 157 |
return get_user_locale(); |
| 158 |
} |
| 159 |
|
| 160 |
return false; |
| 161 |
} |
| 162 |
} |
| 163 |
|