class-gsc-mapper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Mapper
  9. */
  10. class WPSEO_GSC_Mapper {
  11. /**
  12. * The platforms which can be mapped.
  13. *
  14. * @var array
  15. */
  16. private static $platforms = array(
  17. 'web' => 'web',
  18. 'mobile' => 'mobile',
  19. 'smartphone_only' => 'smartphoneOnly',
  20. 'settings' => 'settings', // This one is basicly not a platform, but a tab.
  21. );
  22. /**
  23. * The categories which can be mapped
  24. *
  25. * @var array
  26. */
  27. private static $categories = array(
  28. 'access_denied' => 'authPermissions',
  29. 'faulty_redirects' => 'manyToOneRedirect',
  30. 'not_followed' => 'notFollowed',
  31. 'not_found' => 'notFound',
  32. 'other' => 'other',
  33. 'roboted' => 'roboted',
  34. 'server_error' => 'serverError',
  35. 'soft_404' => 'soft404',
  36. );
  37. /**
  38. * If there is no platform, just get the first key out of the array and redirect to it.
  39. *
  40. * @param string $platform Platform (desktop, mobile, feature phone).
  41. *
  42. * @return mixed
  43. */
  44. public static function get_current_platform( $platform ) {
  45. $current_platform = filter_input( INPUT_GET, $platform );
  46. if ( ! empty( $current_platform ) ) {
  47. return $current_platform;
  48. }
  49. wp_redirect( add_query_arg( $platform, key( self::$platforms ) ) );
  50. exit;
  51. }
  52. /**
  53. * Mapping the platform
  54. *
  55. * @param string $platform Platform (desktop, mobile, feature phone).
  56. *
  57. * @return mixed
  58. */
  59. public static function platform_to_api( $platform ) {
  60. if ( ! empty( $platform ) && array_key_exists( $platform, self::$platforms ) ) {
  61. return self::$platforms[ $platform ];
  62. }
  63. }
  64. /**
  65. * Mapping the given platform by value and return its key
  66. *
  67. * @param string $platform Platform (desktop, mobile, feature phone).
  68. *
  69. * @return string
  70. */
  71. public static function platform_from_api( $platform ) {
  72. if ( ! empty( $platform ) ) {
  73. $platform = array_search( $platform, self::$platforms, true );
  74. if ( $platform !== false ) {
  75. return $platform;
  76. }
  77. }
  78. return $platform;
  79. }
  80. /**
  81. * Mapping the given category by searching for its key.
  82. *
  83. * @param string $category Issue type.
  84. *
  85. * @return mixed
  86. */
  87. public static function category_to_api( $category ) {
  88. if ( ! empty( $category ) && array_key_exists( $category, self::$categories ) ) {
  89. return self::$categories[ $category ];
  90. }
  91. return $category;
  92. }
  93. /**
  94. * Mapping the given category by value and return its key
  95. *
  96. * @param string $category Issue type.
  97. *
  98. * @return string
  99. */
  100. public static function category_from_api( $category ) {
  101. if ( ! empty( $category ) ) {
  102. $category = array_search( $category, self::$categories, true );
  103. if ( $category !== false ) {
  104. return $category;
  105. }
  106. }
  107. return $category;
  108. }
  109. }