| 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( Options_Helper $options_helper, Product_Helper $product_helper ) { |
| 31 |
$this->options_helper = $options_helper; |
| 32 |
$this->product_helper = $product_helper; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Builds a URL to use in the plugin as shortlink. |
| 37 |
* |
| 38 |
* @param string $url The URL to build upon. |
| 39 |
* |
| 40 |
* @return string The final URL. |
| 41 |
*/ |
| 42 |
public function build( $url ) { |
| 43 |
return \add_query_arg( $this->collect_additional_shortlink_data(), $url ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns a version of the URL with a utm_content with the current version. |
| 48 |
* |
| 49 |
* @param string $url The URL to build upon. |
| 50 |
* |
| 51 |
* @return string The final URL. |
| 52 |
*/ |
| 53 |
public function get( $url ) { |
| 54 |
return $this->build( $url ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Echoes a version of the URL with a utm_content with the current version. |
| 59 |
* |
| 60 |
* @param string $url The URL to build upon. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
public function show( $url ) { |
| 65 |
echo \esc_url( $this->get( $url ) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Gets the shortlink's query params. |
| 70 |
* |
| 71 |
* @return array The shortlink's query params. |
| 72 |
*/ |
| 73 |
public function get_query_params() { |
| 74 |
return $this->collect_additional_shortlink_data(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Gets the current site's PHP version, without the extra info. |
| 79 |
* |
| 80 |
* @return string The PHP version. |
| 81 |
*/ |
| 82 |
private function get_php_version() { |
| 83 |
$version = \explode( '.', \PHP_VERSION ); |
| 84 |
|
| 85 |
return (int) $version[0] . '.' . (int) $version[1]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets the current site's platform version. |
| 90 |
* |
| 91 |
* @return string The wp_version. |
| 92 |
*/ |
| 93 |
protected function get_platform_version() { |
| 94 |
return $GLOBALS['wp_version']; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Collects the additional data necessary for the shortlink. |
| 99 |
* |
| 100 |
* @return array The shortlink data. |
| 101 |
*/ |
| 102 |
protected function collect_additional_shortlink_data() { |
| 103 |
$data = [ |
| 104 |
'php_version' => $this->get_php_version(), |
| 105 |
'platform' => 'wordpress', |
| 106 |
'platform_version' => $this->get_platform_version(), |
| 107 |
'software' => $this->get_software(), |
| 108 |
'software_version' => \WPSEO_VERSION, |
| 109 |
'days_active' => $this->get_days_active(), |
| 110 |
'user_language' => \get_user_locale(), |
| 111 |
]; |
| 112 |
|
| 113 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 114 |
if ( isset( $_GET['page'] ) && \is_string( $_GET['page'] ) ) { |
| 115 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: We are not processing form information. |
| 116 |
$admin_page = \sanitize_text_field( \wp_unslash( $_GET['page'] ) ); |
| 117 |
if ( ! empty( $admin_page ) ) { |
| 118 |
$data['screen'] = $admin_page; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $data; |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get our software and whether it's active or not. |
| 127 |
* |
| 128 |
* @return string The software name. |
| 129 |
*/ |
| 130 |
protected function get_software() { |
| 131 |
if ( $this->product_helper->is_premium() ) { |
| 132 |
return 'premium'; |
| 133 |
} |
| 134 |
|
| 135 |
return 'free'; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Gets the number of days the plugin has been active. |
| 140 |
* |
| 141 |
* @return int The number of days the plugin is active. |
| 142 |
*/ |
| 143 |
protected function get_days_active() { |
| 144 |
$date_activated = $this->options_helper->get( 'first_activated_on' ); |
| 145 |
$datediff = ( \time() - $date_activated ); |
| 146 |
|
| 147 |
return (int) \round( $datediff / \DAY_IN_SECONDS ); |
| 148 |
} |
| 149 |
} |
| 150 |
|