/wp-content/plugins/wordpress-seo/inc/class-wpseo-shortlinker.php

https://bitbucket.org/carloskikea/helpet · PHP · 103 lines · 51 code · 10 blank · 42 comment · 2 complexity · 50936943cec0b3bc74df07aa21e21386 MD5 · raw file

  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. ),
  28. $url
  29. );
  30. }
  31. /**
  32. * Returns a version of the URL with a utm_content with the current version.
  33. *
  34. * @param string $url The URL to build upon.
  35. *
  36. * @return string The final URL.
  37. */
  38. public static function get( $url ) {
  39. $shortlinker = new WPSEO_Shortlinker();
  40. return $shortlinker->build_shortlink( $url );
  41. }
  42. /**
  43. * Echoes a version of the URL with a utm_content with the current version.
  44. *
  45. * @param string $url The URL to build upon.
  46. */
  47. public static function show( $url ) {
  48. echo esc_url( self::get( $url ) );
  49. }
  50. /**
  51. * Gets the current site's PHP version, without the extra info.
  52. *
  53. * @return string The PHP version.
  54. */
  55. private function get_php_version() {
  56. $version = explode( '.', PHP_VERSION );
  57. return (int) $version[0] . '.' . (int) $version[1] . '.' . (int) $version[2];
  58. }
  59. /**
  60. * Get our software and whether it's active or not.
  61. *
  62. * @return string The software name + activation state.
  63. */
  64. private function get_software() {
  65. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  66. return 'premium';
  67. }
  68. return 'free';
  69. }
  70. /**
  71. * Gets the current user's role without leaking roles that shouldn't be public.
  72. *
  73. * @return string The filtered user role.
  74. */
  75. private function get_filtered_user_role() {
  76. $user = wp_get_current_user();
  77. $built_in_roles = array(
  78. 'administrator',
  79. 'wpseo_manager',
  80. 'wpseo_editor',
  81. 'editor',
  82. 'author',
  83. 'contributor',
  84. 'subscriber',
  85. );
  86. $filtered_roles = array_intersect( $built_in_roles, $user->roles );
  87. $role = current( $filtered_roles );
  88. if ( ! $role ) {
  89. $role = 'unknown';
  90. }
  91. return $role;
  92. }
  93. }