class-yoast-form-fieldset.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * The fieldset ID.
  13. *
  14. * @var string
  15. */
  16. private $id;
  17. /**
  18. * The fieldset HTML default attributes.
  19. *
  20. * @var array
  21. */
  22. private $attributes = array(
  23. 'class' => 'yoast-form-fieldset',
  24. );
  25. /**
  26. * The grouped form elements for the fieldset.
  27. *
  28. * @var string
  29. */
  30. private $content;
  31. /**
  32. * The fieldset legend HTML default attributes.
  33. *
  34. * @var array
  35. */
  36. private $legend_attributes = array(
  37. 'class' => 'yoast-form-legend',
  38. );
  39. /**
  40. * A translatable string for the fieldset legend content.
  41. *
  42. * @var string
  43. */
  44. private $legend_content;
  45. /**
  46. * Constructor.
  47. *
  48. * @deprecated 11.9
  49. * @codeCoverageIgnore
  50. *
  51. * @param string $id ID for the fieldset.
  52. * @param string $legend_content The translatable legend text.
  53. * @param string $content The grouped form elements for the fieldset.
  54. */
  55. public function __construct( $id, $legend_content, $content ) {
  56. _deprecated_function( __METHOD__, '11.9' );
  57. $this->id = $id;
  58. $this->legend_content = $legend_content;
  59. $this->content = $content;
  60. }
  61. /**
  62. * Render the view.
  63. *
  64. * @deprecated 11.9
  65. * @codeCoverageIgnore
  66. */
  67. public function render_view() {
  68. _deprecated_function( __METHOD__, '11.9' );
  69. /*
  70. * Extract because we want values accessible via variables for later use
  71. * in the view instead of accessing them as an array.
  72. */
  73. extract( $this->get_parts() );
  74. require WPSEO_PATH . 'admin/views/form/fieldset.php';
  75. }
  76. /**
  77. * Start output buffering to catch the form elements to wrap in the fieldset.
  78. *
  79. * @deprecated 11.9
  80. * @codeCoverageIgnore
  81. */
  82. public function start() {
  83. _deprecated_function( __METHOD__, '11.9' );
  84. ob_start();
  85. }
  86. /**
  87. * Return output buffering with the form elements to wrap in the fieldset.
  88. *
  89. * @deprecated 11.9
  90. * @codeCoverageIgnore
  91. */
  92. public function end() {
  93. _deprecated_function( __METHOD__, '11.9' );
  94. $this->content = ob_get_clean();
  95. }
  96. /**
  97. * Return the rendered view.
  98. *
  99. * @deprecated 11.9
  100. * @codeCoverageIgnore
  101. *
  102. * @return string
  103. */
  104. public function get_html() {
  105. _deprecated_function( __METHOD__, '11.9' );
  106. ob_start();
  107. $this->render_view();
  108. return ob_get_clean();
  109. }
  110. /**
  111. * Output the rendered view.
  112. *
  113. * @deprecated 11.9
  114. * @codeCoverageIgnore
  115. */
  116. public function html() {
  117. _deprecated_function( __METHOD__, '11.9' );
  118. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Reason: This is deprecated.
  119. echo $this->get_html();
  120. }
  121. /**
  122. * Add attributes to the fieldset default attributes.
  123. *
  124. * @deprecated 11.9
  125. * @codeCoverageIgnore
  126. *
  127. * @param array $attributes Array of attributes names and values to merge with the defaults.
  128. */
  129. public function add_attributes( $attributes ) {
  130. _deprecated_function( __METHOD__, '11.9' );
  131. $this->attributes = wp_parse_args( $attributes, $this->attributes );
  132. }
  133. /**
  134. * Add attributes to the fieldset legend default attributes.
  135. *
  136. * @deprecated 11.9
  137. * @codeCoverageIgnore
  138. *
  139. * @param array $attributes Array of attributes names and values to merge with the defaults.
  140. */
  141. public function legend_add_attributes( $attributes ) {
  142. _deprecated_function( __METHOD__, '11.9' );
  143. $this->legend_attributes = wp_parse_args( $attributes, $this->legend_attributes );
  144. }
  145. /**
  146. * Visually hide the fieldset legend but keep it available to assistive technologies.
  147. *
  148. * @deprecated 11.9
  149. * @codeCoverageIgnore
  150. */
  151. public function legend_hide() {
  152. _deprecated_function( __METHOD__, '11.9' );
  153. $this->legend_attributes = wp_parse_args(
  154. array( 'class' => 'screen-reader-text' ),
  155. $this->legend_attributes
  156. );
  157. }
  158. /**
  159. * Return the set of attributes and content for the fieldset.
  160. *
  161. * @deprecated 11.9
  162. * @codeCoverageIgnore
  163. *
  164. * @return array
  165. */
  166. private function get_parts() {
  167. return array(
  168. 'id' => $this->id,
  169. 'attributes' => $this->get_attributes_html( $this->attributes ),
  170. 'legend_content' => $this->legend_content,
  171. 'legend_attributes' => $this->get_attributes_html( $this->legend_attributes ),
  172. 'content' => $this->content,
  173. );
  174. }
  175. /**
  176. * Return HTML formatted attributes as a string, when there are attributes set.
  177. *
  178. * @deprecated 11.9
  179. * @codeCoverageIgnore
  180. *
  181. * @param array $attributes Fieldset or legend attributes.
  182. *
  183. * @return string A space separated list of HTML formatted attributes or empty string.
  184. */
  185. private function get_attributes_html( $attributes ) {
  186. if ( ! empty( $attributes ) ) {
  187. array_walk( $attributes, array( $this, 'parse_attribute' ) );
  188. // Use an initial space as `implode()` adds a space only between array elements.
  189. return ' ' . implode( ' ', $attributes );
  190. }
  191. return '';
  192. }
  193. /**
  194. * Escape and format an attribute as an HTML attribute.
  195. *
  196. * @deprecated 11.9
  197. * @codeCoverageIgnore
  198. *
  199. * @param string $value The value of the attribute.
  200. * @param string $attribute The attribute to look for.
  201. */
  202. private function parse_attribute( &$value, $attribute ) {
  203. $value = sprintf( '%s="%s"', sanitize_key( $attribute ), esc_attr( $value ) );
  204. }
  205. }