class-wpseo-shortlinker.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * Builds a URL to use in the plugin as shortlink.
  13. *
  14. * @param string $url The URL to build upon.
  15. *
  16. * @return string The final URL.
  17. */
  18. public function build_shortlink( $url ) {
  19. return add_query_arg(
  20. array(
  21. 'php_version' => $this->get_php_version(),
  22. 'platform' => 'wordpress',
  23. 'platform_version' => $GLOBALS['wp_version'],
  24. 'software' => $this->get_software(),
  25. 'software_version' => WPSEO_VERSION,
  26. 'role' => $this->get_filtered_user_role(),
  27. 'days_active' => $this->get_days_active(),
  28. ),
  29. $url
  30. );
  31. }
  32. /**
  33. * Returns a version of the URL with a utm_content with the current version.
  34. *
  35. * @param string $url The URL to build upon.
  36. *
  37. * @return string The final URL.
  38. */
  39. public static function get( $url ) {
  40. $shortlinker = new WPSEO_Shortlinker();
  41. return $shortlinker->build_shortlink( $url );
  42. }
  43. /**
  44. * Echoes a version of the URL with a utm_content with the current version.
  45. *
  46. * @param string $url The URL to build upon.
  47. */
  48. public static function show( $url ) {
  49. echo esc_url( self::get( $url ) );
  50. }
  51. /**
  52. * Gets the current site's PHP version, without the extra info.
  53. *
  54. * @return string The PHP version.
  55. */
  56. private function get_php_version() {
  57. $version = explode( '.', PHP_VERSION );
  58. return (int) $version[0] . '.' . (int) $version[1] . '.' . (int) $version[2];
  59. }
  60. /**
  61. * Get our software and whether it's active or not.
  62. *
  63. * @return string The software name + activation state.
  64. */
  65. private function get_software() {
  66. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  67. return 'premium';
  68. }
  69. return 'free';
  70. }
  71. /**
  72. * Gets the current user's role without leaking roles that shouldn't be public.
  73. *
  74. * @return string The filtered user role.
  75. */
  76. private function get_filtered_user_role() {
  77. $user = wp_get_current_user();
  78. $built_in_roles = array(
  79. 'administrator',
  80. 'wpseo_manager',
  81. 'wpseo_editor',
  82. 'editor',
  83. 'author',
  84. 'contributor',
  85. 'subscriber',
  86. );
  87. $filtered_roles = array_intersect( $built_in_roles, $user->roles );
  88. $role = current( $filtered_roles );
  89. if ( ! $role ) {
  90. $role = 'unknown';
  91. }
  92. return $role;
  93. }
  94. /**
  95. * Gets the number of days the plugin has been active.
  96. *
  97. * @return int The number of days the plugin is active.
  98. */
  99. private function get_days_active() {
  100. $date_activated = WPSEO_Options::get( 'first_activated_on' );
  101. $datediff = ( time() - $date_activated );
  102. return (int) round( $datediff / DAY_IN_SECONDS );
  103. }
  104. }