class-gsc-platform-tabs.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Google_Search_Console
  6. */
  7. /**
  8. * Class WPSEO_GSC_Platform_Tabs
  9. */
  10. class WPSEO_GSC_Platform_Tabs {
  11. /**
  12. * @var string
  13. */
  14. private $current_tab;
  15. /**
  16. * Return the tabs as a string
  17. *
  18. * @return string
  19. */
  20. public function __toString() {
  21. return $this->platform_tabs();
  22. }
  23. /**
  24. * Getting the current_tab
  25. *
  26. * @return string
  27. */
  28. public function current_tab() {
  29. return $this->current_tab;
  30. }
  31. /**
  32. * Loops through the array with all the platforms and convert it into an array
  33. *
  34. * @return string
  35. */
  36. private function platform_tabs() {
  37. $tabs = array( 'settings' => __( 'Settings', 'wordpress-seo' ) );
  38. $platforms = array(
  39. 'web' => __( 'Desktop', 'wordpress-seo' ),
  40. 'smartphone_only' => __( 'Smartphone', 'wordpress-seo' ),
  41. 'mobile' => __( 'Feature phone', 'wordpress-seo' ),
  42. );
  43. if ( WPSEO_GSC_Settings::get_profile() !== '' ) {
  44. $tabs = array_merge( $platforms, $tabs );
  45. }
  46. $admin_link = admin_url( 'admin.php?page=wpseo_search_console&tab=' );
  47. $this->set_current_tab( $tabs );
  48. $return = '';
  49. foreach ( $tabs as $platform_target => $platform_value ) {
  50. $return .= $this->platform_tab( $platform_target, $platform_value, $admin_link );
  51. }
  52. return $return;
  53. }
  54. /**
  55. * Setting the current tab
  56. *
  57. * @param array $platforms Set of platforms (desktop, mobile, feature phone).
  58. */
  59. private function set_current_tab( array $platforms ) {
  60. $this->current_tab = key( $platforms );
  61. $current_platform = filter_input( INPUT_GET, 'tab' );
  62. if ( ! empty( $current_platform ) && isset( $platforms[ $current_platform ] ) ) {
  63. $this->current_tab = $current_platform;
  64. }
  65. }
  66. /**
  67. * Parses the tab
  68. *
  69. * @param string $platform_target Platform (desktop, mobile, feature phone).
  70. * @param string $platform_value Link anchor.
  71. * @param string $admin_link Link URL admin base.
  72. *
  73. * @return string
  74. */
  75. private function platform_tab( $platform_target, $platform_value, $admin_link ) {
  76. $active = '';
  77. if ( $this->current_tab === $platform_target ) {
  78. $active = ' nav-tab-active';
  79. }
  80. return '<a class="nav-tab' . esc_attr( $active ) . '" id="' . esc_attr( $platform_target . '-tab' ) . '" href="' . esc_url( $admin_link . $platform_target ) . '">' . esc_html( $platform_value ) . '</a>';
  81. }
  82. }