Yoast_Form_Fieldset

Generate the HTML for a form fieldset to wrap grouped form elements.

Defined (1)

The class is defined in the following location(s).

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