class-yoast-form-fieldset.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Generate the HTML for a form fieldset to wrap grouped form elements.
  9. */
  10. class Yoast_Form_Fieldset implements Yoast_Form_Element {
  11. /**
  12. * @var string The fieldset ID.
  13. */
  14. private $id;
  15. /**
  16. * @var array The fieldset HTML default attributes.
  17. */
  18. private $attributes = array(
  19. 'class' => 'yoast-form-fieldset',
  20. );
  21. /**
  22. * @var string The grouped form elements for the fieldset.
  23. */
  24. private $content;
  25. /**
  26. * @var array The fieldset legend HTML default attributes.
  27. */
  28. private $legend_attributes = array(
  29. 'class' => 'yoast-form-legend',
  30. );
  31. /**
  32. * @var string A translatable string for the fieldset legend content.
  33. */
  34. private $legend_content;
  35. /**
  36. * Constructor.
  37. *
  38. * @param string $id ID for the fieldset.
  39. * @param string $legend_content The translatable legend text.
  40. * @param string $content The grouped form elements for the fieldset.
  41. */
  42. public function __construct( $id, $legend_content, $content ) {
  43. $this->id = $id;
  44. $this->legend_content = $legend_content;
  45. $this->content = $content;
  46. }
  47. /**
  48. * Render the view.
  49. */
  50. public function render_view() {
  51. /*
  52. * Extract because we want values accessible via variables for later use
  53. * in the view instead of accessing them as an array.
  54. */
  55. extract( $this->get_parts() );
  56. require WPSEO_PATH . 'admin/views/form/fieldset.php';
  57. }
  58. /**
  59. * Start output buffering to catch the form elements to wrap in the fieldset.
  60. */
  61. public function start() {
  62. ob_start();
  63. }
  64. /**
  65. * Return output buffering with the form elements to wrap in the fieldset.
  66. */
  67. public function end() {
  68. $this->content = ob_get_clean();
  69. }
  70. /**
  71. * Return the rendered view.
  72. *
  73. * @return string
  74. */
  75. public function get_html() {
  76. ob_start();
  77. $this->render_view();
  78. return ob_get_clean();
  79. }
  80. /**
  81. * Output the rendered view.
  82. */
  83. public function html() {
  84. echo $this->get_html();
  85. }
  86. /**
  87. * Add attributes to the fieldset default attributes.
  88. *
  89. * @param array $attributes Array of attributes names and values to merge with the defaults.
  90. */
  91. public function add_attributes( $attributes ) {
  92. $this->attributes = wp_parse_args( $attributes, $this->attributes );
  93. }
  94. /**
  95. * Add attributes to the fieldset legend default attributes.
  96. *
  97. * @param array $attributes Array of attributes names and values to merge with the defaults.
  98. */
  99. public function legend_add_attributes( $attributes ) {
  100. $this->legend_attributes = wp_parse_args( $attributes, $this->legend_attributes );
  101. }
  102. /**
  103. * Visually hide the fieldset legend but keep it available to assistive technologies.
  104. */
  105. public function legend_hide() {
  106. $this->legend_attributes = wp_parse_args(
  107. array( 'class' => 'screen-reader-text' ),
  108. $this->legend_attributes
  109. );
  110. }
  111. /**
  112. * Return the set of attributes and content for the fieldset.
  113. *
  114. * @return array
  115. */
  116. private function get_parts() {
  117. return array(
  118. 'id' => $this->id,
  119. 'attributes' => $this->get_attributes_html( $this->attributes ),
  120. 'legend_content' => $this->legend_content,
  121. 'legend_attributes' => $this->get_attributes_html( $this->legend_attributes ),
  122. 'content' => $this->content,
  123. );
  124. }
  125. /**
  126. * Return HTML formatted attributes as a string, when there are attributes set.
  127. *
  128. * @param array $attributes Fieldset or legend attributes.
  129. *
  130. * @return string A space separated list of HTML formatted attributes or empty string.
  131. */
  132. private function get_attributes_html( $attributes ) {
  133. if ( ! empty( $attributes ) ) {
  134. array_walk( $attributes, array( $this, 'parse_attribute' ) );
  135. // Use an initial space as `implode()` adds a space only between array elements.
  136. return ' ' . implode( ' ', $attributes );
  137. }
  138. return '';
  139. }
  140. /**
  141. * Escape and format an attribute as an HTML attribute.
  142. *
  143. * @param string $value The value of the attribute.
  144. * @param string $attribute The attribute to look for.
  145. */
  146. private function parse_attribute( & $value, $attribute ) {
  147. $value = sprintf( '%s="%s"', esc_html( $attribute ), esc_attr( $value ) );
  148. }
  149. }