class-abstract-sectioned-metabox-tab.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Base class for metabox that consist of multiple sections.
  9. */
  10. abstract class WPSEO_Abstract_Metabox_Tab_With_Sections implements WPSEO_Metabox_Section {
  11. /**
  12. * Holds the name of the tab.
  13. *
  14. * @var string
  15. */
  16. public $name;
  17. /**
  18. * Holds the HTML of the tab header.
  19. *
  20. * @var string
  21. */
  22. protected $link_content;
  23. /**
  24. * Holds the name of the tab header.
  25. *
  26. * @var string
  27. */
  28. protected $link_title;
  29. /**
  30. * Holds the classname of the tab header.
  31. *
  32. * @var string
  33. */
  34. protected $link_class;
  35. /**
  36. * Holds the aria label of the tab header.
  37. *
  38. * @var string
  39. */
  40. protected $link_aria_label;
  41. /**
  42. * Constructor.
  43. *
  44. * @param string $name The name of the section, used as an identifier in the html.
  45. * Can only contain URL safe characters.
  46. * @param string $link_content The text content of the section link.
  47. * @param array $options Optional link attributes.
  48. */
  49. public function __construct( $name, $link_content, array $options = [] ) {
  50. $default_options = [
  51. 'link_title' => '',
  52. 'link_class' => '',
  53. 'link_aria_label' => '',
  54. ];
  55. $options = array_merge( $default_options, $options );
  56. $this->name = $name;
  57. $this->link_content = $link_content;
  58. $this->link_title = $options['link_title'];
  59. $this->link_class = $options['link_class'];
  60. $this->link_aria_label = $options['link_aria_label'];
  61. }
  62. /**
  63. * Outputs the section link if any section has been added.
  64. */
  65. public function display_link() {
  66. if ( $this->has_sections() ) {
  67. printf(
  68. '<li role="presentation"><a role="tab" href="#wpseo-meta-section-%1$s" id="wpseo-meta-tab-%1$s" aria-controls="wpseo-meta-section-%1$s" class="wpseo-meta-section-link %2$s"%3$s%4$s>%5$s</a></li>',
  69. esc_attr( $this->name ),
  70. esc_attr( $this->link_class ),
  71. ( '' !== $this->link_title ) ? ' title="' . esc_attr( $this->link_title ) . '"' : '',
  72. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  73. $this->link_content
  74. );
  75. }
  76. }
  77. /**
  78. * Checks whether the tab has any sections.
  79. *
  80. * @return bool Whether the tab has any sections
  81. */
  82. abstract protected function has_sections();
  83. }