WPSEO_Metabox_Tab_Section

Generates and displays the HTML for a metabox section.

Defined (1)

The class is defined in the following location(s).

/admin/metabox/class-metabox-tab-section.php  
  1. class WPSEO_Metabox_Tab_Section implements WPSEO_Metabox_Section { 
  2.  
  3. /** 
  4. * @var WPSEO_Metabox_Tab[] 
  5. */ 
  6. public $tabs = array(); 
  7.  
  8. /** 
  9. * @var string 
  10. */ 
  11. public $name; 
  12.  
  13. /** 
  14. * @var string 
  15. */ 
  16. private $link_content; 
  17.  
  18. /** 
  19. * @var string 
  20. */ 
  21. private $link_title; 
  22.  
  23. /** 
  24. * @var string 
  25. */ 
  26. private $link_class; 
  27.  
  28. /** 
  29. * @var string 
  30. */ 
  31. private $link_aria_label; 
  32.  
  33. /** 
  34. * Constructor. 
  35. * @param string $name The name of the section, used as an identifier in the html. Can only contain URL safe characters. 
  36. * @param string $link_content The text content of the section link. 
  37. * @param array $tabs The metabox tabs (`WPSEO_Metabox_Tabs[]`) to be included in the section. 
  38. * @param array $options Optional link attributes. 
  39. */ 
  40. public function __construct( $name, $link_content, array $tabs = array(), array $options = array() ) { 
  41. $default_options = array( 
  42. 'link_title' => '',  
  43. 'link_class' => '',  
  44. 'link_aria_label' => '',  
  45. ); 
  46. $options = array_merge( $default_options, $options ); 
  47.  
  48. $this->name = $name; 
  49. foreach ( $tabs as $tab ) { 
  50. $this->add_tab( $tab ); 
  51. $this->link_content = $link_content; 
  52. $this->link_title = $options['link_title']; 
  53. $this->link_class = $options['link_class']; 
  54. $this->link_aria_label = $options['link_aria_label']; 
  55.  
  56. /** 
  57. * Outputs the section link if any tab has been added. 
  58. */ 
  59. public function display_link() { 
  60. if ( $this->has_tabs() ) { 
  61. printf( 
  62. '<li><a href="#wpseo-meta-section-%1$s" class="wpseo-meta-section-link %2$s"%3$s%4$s>%5$s</a></li>',  
  63. esc_attr( $this->name ),  
  64. esc_attr( $this->link_class ),  
  65. ( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',  
  66. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',  
  67. $this->link_content 
  68. ); 
  69.  
  70. /** 
  71. * Outputs the section content if any tab has been added. 
  72. */ 
  73. public function display_content() { 
  74. if ( $this->has_tabs() ) { 
  75. $html = '<div id="wpseo-meta-section-%1$s" class="wpseo-meta-section">'; 
  76. $html .= '<div class="wpseo-metabox-tabs-div">'; 
  77. $html .= '<ul class="wpseo-metabox-tabs wpseo-metabox-tab-%1$s">%2$s</ul>%3$s'; 
  78. $html .= '</div></div>'; 
  79.  
  80. printf( $html, esc_attr( $this->name ), $this->tab_links(), $this->tab_content() ); 
  81.  
  82. /** 
  83. * Add a `WPSEO_Metabox_Tab` object to the tabs. 
  84. * @param WPSEO_Metabox_Tab $tab Tab to add. 
  85. */ 
  86. public function add_tab( WPSEO_Metabox_Tab $tab ) { 
  87. $this->tabs[] = $tab; 
  88.  
  89. /** 
  90. * Checks if any tabs have been added to the section. 
  91. * @return bool 
  92. */ 
  93. protected function has_tabs() { 
  94. return ! empty( $this->tabs ); 
  95.  
  96. /** 
  97. * Concatenates all tabs' links into one html string. 
  98. * @return string 
  99. */ 
  100. private function tab_links() { 
  101. $links = ''; 
  102. foreach ( $this->tabs as $tab ) { 
  103. $links .= $tab->link(); 
  104. return $links; 
  105.  
  106. /** 
  107. * Concatenates all tabs' content into one html string. 
  108. * @return string 
  109. */ 
  110. private function tab_content() { 
  111. $content = ''; 
  112. foreach ( $this->tabs as $tab ) { 
  113. $content .= $tab->content(); 
  114. return $content;