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