class-option-tabs.php 2.0 KB

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