/wp-content/plugins/wordpress-seo/admin/metabox/class-metabox-tab-section.php

https://bitbucket.org/carloskikea/helpet · PHP · 168 lines · 79 code · 21 blank · 68 comment · 4 complexity · ad29df0a076a861488144c8a2e53407a MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generates and displays the HTML for a metabox section.
  9. */
  10. class WPSEO_Metabox_Tab_Section implements WPSEO_Metabox_Section {
  11. /**
  12. * @var WPSEO_Metabox_Tab[]
  13. */
  14. public $tabs = array();
  15. /**
  16. * @var string
  17. */
  18. public $name;
  19. /**
  20. * @var string
  21. */
  22. private $link_content;
  23. /**
  24. * @var string
  25. */
  26. private $link_title;
  27. /**
  28. * @var string
  29. */
  30. private $link_class;
  31. /**
  32. * @var string
  33. */
  34. private $link_aria_label;
  35. /**
  36. * Constructor.
  37. *
  38. * @param string $name The name of the section, used as an identifier in the html. Can only contain URL safe characters.
  39. * @param string $link_content The text content of the section link.
  40. * @param array $tabs The metabox tabs (`WPSEO_Metabox_Tabs[]`) to be included in the section.
  41. * @param array $options Optional link attributes.
  42. */
  43. public function __construct( $name, $link_content, array $tabs = array(), array $options = array() ) {
  44. $default_options = array(
  45. 'link_title' => '',
  46. 'link_class' => '',
  47. 'link_aria_label' => '',
  48. );
  49. $options = array_merge( $default_options, $options );
  50. $this->name = $name;
  51. // Filter out invalid tab instances.
  52. $valid_tabs = array_filter( $tabs, array( $this, 'is_valid_tab' ) );
  53. foreach ( $valid_tabs as $tab ) {
  54. $this->add_tab( $tab );
  55. }
  56. $this->link_content = $link_content;
  57. $this->link_title = $options['link_title'];
  58. $this->link_class = $options['link_class'];
  59. $this->link_aria_label = $options['link_aria_label'];
  60. }
  61. /**
  62. * Determines whether the passed tab is considered valid.
  63. *
  64. * @param mixed $tab The potential tab that needs to be validated.
  65. *
  66. * @return bool Whether or not the tab is valid.
  67. */
  68. protected function is_valid_tab( $tab ) {
  69. if ( $tab instanceof WPSEO_Metabox_Tab && ! $tab instanceof WPSEO_Metabox_Null_Tab ) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * Outputs the section link if any tab has been added.
  76. */
  77. public function display_link() {
  78. if ( $this->has_tabs() ) {
  79. printf(
  80. '<li><a href="#wpseo-meta-section-%1$s" class="wpseo-meta-section-link %2$s"%3$s%4$s>%5$s</a></li>',
  81. esc_attr( $this->name ),
  82. esc_attr( $this->link_class ),
  83. ( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',
  84. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  85. $this->link_content
  86. );
  87. }
  88. }
  89. /**
  90. * Outputs the section content if any tab has been added.
  91. */
  92. public function display_content() {
  93. if ( $this->has_tabs() ) {
  94. $html = '<div id="%1$s" class="wpseo-meta-section">';
  95. $html .= '<div class="wpseo-metabox-tabs-div">';
  96. $html .= '<ul class="wpseo-metabox-tabs %2$s">%3$s</ul>%4$s';
  97. $html .= '</div></div>';
  98. printf(
  99. $html,
  100. esc_attr( 'wpseo-meta-section-' . $this->name ),
  101. esc_attr( 'wpseo-metabox-tab-' . $this->name ),
  102. $this->tab_links(),
  103. $this->tab_content()
  104. );
  105. }
  106. }
  107. /**
  108. * Add a `WPSEO_Metabox_Tab` object to the tabs.
  109. *
  110. * @param WPSEO_Metabox_Tab $tab Tab to add.
  111. */
  112. public function add_tab( WPSEO_Metabox_Tab $tab ) {
  113. $this->tabs[] = $tab;
  114. }
  115. /**
  116. * Checks if any tabs have been added to the section.
  117. *
  118. * @return bool
  119. */
  120. protected function has_tabs() {
  121. return ! empty( $this->tabs );
  122. }
  123. /**
  124. * Concatenates all tabs' links into one html string.
  125. *
  126. * @return string
  127. */
  128. private function tab_links() {
  129. $links = '';
  130. foreach ( $this->tabs as $tab ) {
  131. $links .= $tab->link();
  132. }
  133. return $links;
  134. }
  135. /**
  136. * Concatenates all tabs' content into one html string.
  137. *
  138. * @return string
  139. */
  140. private function tab_content() {
  141. $content = '';
  142. foreach ( $this->tabs as $tab ) {
  143. $content .= $tab->content();
  144. }
  145. return $content;
  146. }
  147. }