class-metabox-collapsibles-section.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generates and displays a metabox tab that consists of collapsible sections.
  9. */
  10. class WPSEO_Metabox_Collapsibles_Sections extends WPSEO_Abstract_Metabox_Tab_With_Sections {
  11. /**
  12. * Holds the tab's collapsibles.
  13. *
  14. * @var WPSEO_Metabox_Collapsible[]
  15. */
  16. private $collapsibles = [];
  17. /**
  18. * Constructor.
  19. *
  20. * @param string $name The name of the section, used as an identifier in the html.
  21. * Can only contain URL safe characters.
  22. * @param string $link_content The text content of the section link.
  23. * @param array $collapsibles The metabox collapsibles (`WPSEO_Metabox_Collapsible[]`) to be included in the section.
  24. * @param array $options Optional link attributes.
  25. */
  26. public function __construct( $name, $link_content, array $collapsibles = [], array $options = [] ) {
  27. parent::__construct( $name, $link_content, $options );
  28. $this->collapsibles = $collapsibles;
  29. }
  30. /**
  31. * Outputs the section content if any tab has been added.
  32. */
  33. public function display_content() {
  34. if ( $this->has_sections() ) {
  35. printf( '<div id="%1$s" class="wpseo-meta-section">', esc_attr( 'wpseo-meta-section-' . $this->name ) );
  36. echo '<div class="wpseo_content_wrapper">';
  37. add_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_forms' ] );
  38. add_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_a11y' ] );
  39. foreach ( $this->collapsibles as $collapsible ) {
  40. echo wp_kses_post( $collapsible->content() );
  41. }
  42. remove_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_forms' ] );
  43. remove_filter( 'wp_kses_allowed_html', [ 'WPSEO_Utils', 'extend_kses_post_with_a11y' ] );
  44. echo '</div></div>';
  45. }
  46. }
  47. /**
  48. * Checks whether the tab has any sections.
  49. *
  50. * @return bool Whether the tab has any sections
  51. */
  52. protected function has_sections() {
  53. return ! empty( $this->collapsibles );
  54. }
  55. }