class-metabox-section-additional.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Metabox
  6. */
  7. /**
  8. * Generates and displays an additional metabox section.
  9. */
  10. class WPSEO_Metabox_Section_Additional implements WPSEO_Metabox_Section {
  11. /**
  12. * Name of the section, used as an identifier in the HTML.
  13. *
  14. * @var string
  15. */
  16. public $name;
  17. /**
  18. * Content of the tab's section.
  19. *
  20. * @var string
  21. */
  22. public $content;
  23. /**
  24. * HTML to use in the tab header.
  25. *
  26. * @var string
  27. */
  28. private $link_content;
  29. /**
  30. * Class to add to the link.
  31. *
  32. * @var string
  33. */
  34. private $link_class;
  35. /**
  36. * Aria label to use for the link.
  37. *
  38. * @var string
  39. */
  40. private $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 string $content Optional. Content to use above the React root element.
  48. * @param array $options Optional link attributes.
  49. */
  50. public function __construct( $name, $link_content, $content = '', array $options = [] ) {
  51. $this->name = $name;
  52. $this->content = $content;
  53. $default_options = [
  54. 'link_class' => '',
  55. 'link_aria_label' => '',
  56. ];
  57. $options = wp_parse_args( $options, $default_options );
  58. $this->link_content = $link_content;
  59. $this->link_class = $options['link_class'];
  60. $this->link_aria_label = $options['link_aria_label'];
  61. }
  62. /**
  63. * Outputs the section link.
  64. *
  65. * @return void
  66. */
  67. public function display_link() {
  68. printf(
  69. '<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</a></li>',
  70. esc_attr( $this->name ),
  71. esc_attr( $this->link_class ),
  72. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  73. $this->link_content
  74. );
  75. }
  76. /**
  77. * Outputs the section content.
  78. *
  79. * @return void
  80. */
  81. public function display_content() {
  82. $html = sprintf(
  83. '<div role="tabpanel" id="wpseo-meta-section-%1$s" aria-labelledby="wpseo-meta-tab-%1$s" tabindex="0" class="wpseo-meta-section wpseo-form">',
  84. esc_attr( $this->name )
  85. );
  86. $html .= $this->content;
  87. $html .= '</div>';
  88. echo $html;
  89. }
  90. }