class-metabox-section-react.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. * Constructor.
  43. *
  44. * @param string $name The name of the section, used as an identifier in the html. Can only contain URL safe characters.
  45. * @param string $link_content The text content of the section link.
  46. * @param string $content Optional. Content to use above the React root element.
  47. * @param array $options Optional link attributes.
  48. */
  49. public function __construct( $name, $link_content, $content = '', array $options = array() ) {
  50. $this->name = $name;
  51. $this->content = $content;
  52. $default_options = array(
  53. 'link_class' => '',
  54. 'link_aria_label' => '',
  55. );
  56. $options = wp_parse_args( $options, $default_options );
  57. $this->link_content = $link_content;
  58. $this->link_class = $options['link_class'];
  59. $this->link_aria_label = $options['link_aria_label'];
  60. }
  61. /**
  62. * Outputs the section link.
  63. *
  64. * @return void
  65. */
  66. public function display_link() {
  67. printf(
  68. '<li><a href="#wpseo-meta-section-%1$s" class="wpseo-meta-section-link %2$s"%3$s>%4$s</a></li>',
  69. esc_attr( $this->name ),
  70. esc_attr( $this->link_class ),
  71. ( '' !== $this->link_aria_label ) ? ' aria-label="' . esc_attr( $this->link_aria_label ) . '"' : '',
  72. $this->link_content
  73. );
  74. }
  75. /**
  76. * Outputs the section content.
  77. *
  78. * @return void
  79. */
  80. public function display_content() {
  81. $html = sprintf( '<div id="%1$s" class="wpseo-meta-section">', esc_attr( 'wpseo-meta-section-' . $this->name ) );
  82. $html .= $this->content;
  83. $html .= '<div id="wpseo-metabox-root" class="wpseo-metabox-root"></div>';
  84. $html .= '</div>';
  85. echo $html;
  86. }
  87. }