class-option-tabs.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Options\Tabs
  6. */
  7. /**
  8. * Class WPSEO_Option_Tabs.
  9. */
  10. class WPSEO_Option_Tabs {
  11. /**
  12. * Tabs base.
  13. *
  14. * @var string
  15. */
  16. private $base;
  17. /**
  18. * The tabs in this group.
  19. *
  20. * @var array
  21. */
  22. private $tabs = [];
  23. /**
  24. * Name of the active tab.
  25. *
  26. * @var string
  27. */
  28. private $active_tab = '';
  29. /**
  30. * WPSEO_Option_Tabs constructor.
  31. *
  32. * @codeCoverageIgnore
  33. *
  34. * @param string $base Base of the tabs.
  35. * @param string $active_tab Currently active tab.
  36. */
  37. public function __construct( $base, $active_tab = '' ) {
  38. $this->base = sanitize_title( $base );
  39. $tab = filter_input( INPUT_GET, 'tab' );
  40. $this->active_tab = empty( $tab ) ? $active_tab : $tab;
  41. }
  42. /**
  43. * Get the base.
  44. *
  45. * @return string
  46. */
  47. public function get_base() {
  48. return $this->base;
  49. }
  50. /**
  51. * Add a tab.
  52. *
  53. * @param WPSEO_Option_Tab $tab Tab to add.
  54. *
  55. * @return $this
  56. */
  57. public function add_tab( WPSEO_Option_Tab $tab ) {
  58. $this->tabs[] = $tab;
  59. return $this;
  60. }
  61. /**
  62. * Get active tab.
  63. *
  64. * @return null|WPSEO_Option_Tab Get the active tab.
  65. */
  66. public function get_active_tab() {
  67. if ( empty( $this->active_tab ) ) {
  68. return null;
  69. }
  70. $active_tabs = array_filter( $this->tabs, [ $this, 'is_active_tab' ] );
  71. if ( ! empty( $active_tabs ) ) {
  72. $active_tabs = array_values( $active_tabs );
  73. if ( count( $active_tabs ) === 1 ) {
  74. return $active_tabs[0];
  75. }
  76. }
  77. return null;
  78. }
  79. /**
  80. * Is the tab the active tab.
  81. *
  82. * @param WPSEO_Option_Tab $tab Tab to check for active tab.
  83. *
  84. * @return bool
  85. */
  86. public function is_active_tab( WPSEO_Option_Tab $tab ) {
  87. return ( $tab->get_name() === $this->active_tab );
  88. }
  89. /**
  90. * Get all tabs.
  91. *
  92. * @return WPSEO_Option_Tab[]
  93. */
  94. public function get_tabs() {
  95. return $this->tabs;
  96. }
  97. /**
  98. * Display the tabs.
  99. *
  100. * @param Yoast_Form $yform Yoast Form needed in the views.
  101. */
  102. public function display( Yoast_Form $yform ) {
  103. $formatter = new WPSEO_Option_Tabs_Formatter();
  104. $formatter->run( $this, $yform );
  105. }
  106. }