class-metabox-section-react.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generates and displays the React root element for a metabox section.
  9. */
  10. class WPSEO_Metabox_Section_React 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 to use before the React root node.
  19. *
  20. * @var string
  21. */
  22. public $content;
  23. /**
  24. * Content to use to display the button to open this content block.
  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. * Additional html content to be displayed within the section.
  43. *
  44. * @var string
  45. */
  46. private $html_after;
  47. /**
  48. * Constructor.
  49. *
  50. * @param string $name The name of the section, used as an identifier in the html.
  51. * Can only contain URL safe characters.
  52. * @param string $link_content The text content of the section link.
  53. * @param string $content Optional. Content to use above the React root element.
  54. * @param array $options Optional link attributes.
  55. */
  56. public function __construct( $name, $link_content, $content = '', array $options = [] ) {
  57. $this->name = $name;
  58. $this->content = $content;
  59. $default_options = [
  60. 'link_class' => '',
  61. 'link_aria_label' => '',
  62. 'html_after' => '',
  63. ];
  64. $options = wp_parse_args( $options, $default_options );
  65. $this->link_content = $link_content;
  66. $this->link_class = $options['link_class'];
  67. $this->link_aria_label = $options['link_aria_label'];
  68. $this->html_after = $options['html_after'];
  69. }
  70. /**
  71. * Outputs the section link.
  72. *
  73. * @return void
  74. */
  75. public function display_link() {
  76. printf(
  77. '<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>',
  78. esc_attr( $this->name ),
  79. esc_attr( $this->link_class ),
  80. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  81. wp_kses_post( $this->link_content )
  82. );
  83. }
  84. /**
  85. * Outputs the section content.
  86. *
  87. * @return void
  88. */
  89. public function display_content() {
  90. add_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_forms' ] );
  91. add_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_a11y' ] );
  92. printf(
  93. '<div role="tabpanel" id="wpseo-meta-section-%1$s" aria-labelledby="wpseo-meta-tab-%1$s" tabindex="0" class="wpseo-meta-section">',
  94. esc_attr( $this->name )
  95. );
  96. echo wp_kses_post( $this->content );
  97. echo '<div id="wpseo-metabox-root" class="wpseo-metabox-root"></div>';
  98. echo wp_kses_post( $this->html_after );
  99. echo '</div>';
  100. remove_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_forms' ] );
  101. remove_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_a11y' ] );
  102. }
  103. }