class-wpseo-shortlinker.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO
  6. */
  7. /**
  8. * Helps with creating shortlinks in the plugin.
  9. */
  10. class WPSEO_Shortlinker {
  11. /**
  12. * Collects the additional data necessary for the shortlink.
  13. *
  14. * @return array The shortlink data.
  15. */
  16. protected function collect_additional_shortlink_data() {
  17. return [
  18. 'php_version' => $this->get_php_version(),
  19. 'platform' => 'wordpress',
  20. 'platform_version' => $GLOBALS['wp_version'],
  21. 'software' => $this->get_software(),
  22. 'software_version' => WPSEO_VERSION,
  23. 'days_active' => $this->get_days_active(),
  24. 'user_language' => $this->get_user_language(),
  25. ];
  26. }
  27. /**
  28. * Builds a URL to use in the plugin as shortlink.
  29. *
  30. * @param string $url The URL to build upon.
  31. *
  32. * @return string The final URL.
  33. */
  34. public function build_shortlink( $url ) {
  35. return add_query_arg( $this->collect_additional_shortlink_data(), $url );
  36. }
  37. /**
  38. * Returns a version of the URL with a utm_content with the current version.
  39. *
  40. * @param string $url The URL to build upon.
  41. *
  42. * @return string The final URL.
  43. */
  44. public static function get( $url ) {
  45. $shortlinker = new self();
  46. return $shortlinker->build_shortlink( $url );
  47. }
  48. /**
  49. * Echoes 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. public static function show( $url ) {
  54. echo esc_url( self::get( $url ) );
  55. }
  56. /**
  57. * Gets the shortlink's query params.
  58. *
  59. * @return array The shortlink's query params.
  60. */
  61. public static function get_query_params() {
  62. $shortlinker = new self();
  63. return $shortlinker->collect_additional_shortlink_data();
  64. }
  65. /**
  66. * Gets the current site's PHP version, without the extra info.
  67. *
  68. * @return string The PHP version.
  69. */
  70. private function get_php_version() {
  71. $version = explode( '.', PHP_VERSION );
  72. return (int) $version[0] . '.' . (int) $version[1];
  73. }
  74. /**
  75. * Get our software and whether it's active or not.
  76. *
  77. * @return string The software name + activation state.
  78. */
  79. private function get_software() {
  80. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  81. return 'premium';
  82. }
  83. return 'free';
  84. }
  85. /**
  86. * Gets the number of days the plugin has been active.
  87. *
  88. * @return int The number of days the plugin is active.
  89. */
  90. private function get_days_active() {
  91. $date_activated = WPSEO_Options::get( 'first_activated_on' );
  92. $datediff = ( time() - $date_activated );
  93. $days = (int) round( $datediff / DAY_IN_SECONDS );
  94. switch ( $days ) {
  95. case 0:
  96. case 1:
  97. $cohort = '0-1';
  98. break;
  99. case ( $days < 5 ):
  100. $cohort = '2-5';
  101. break;
  102. case ( $days < 30 ):
  103. $cohort = '6-30';
  104. break;
  105. default:
  106. $cohort = '30plus';
  107. }
  108. return $cohort;
  109. }
  110. /**
  111. * Gets the user's language.
  112. *
  113. * @return string The user's language.
  114. */
  115. private function get_user_language() {
  116. if ( function_exists( 'get_user_locale' ) ) {
  117. return get_user_locale();
  118. }
  119. return false;
  120. }
  121. }