class-taxonomy-fields-presenter.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Class WPSEO_Taxonomy_Presenter.
  9. */
  10. class WPSEO_Taxonomy_Fields_Presenter {
  11. /**
  12. * The taxonomy meta data for the current term.
  13. *
  14. * @var array
  15. */
  16. private $tax_meta;
  17. /**
  18. * Constructs the WPSEO_Taxonomy_Fields_Presenter class.
  19. *
  20. * @param stdClass $term The current term.
  21. */
  22. public function __construct( $term ) {
  23. $this->tax_meta = WPSEO_Taxonomy_Meta::get_term_meta( (int) $term->term_id, $term->taxonomy );
  24. }
  25. /**
  26. * Displaying the form fields.
  27. *
  28. * @param array $fields Array with the fields that will be displayed.
  29. */
  30. public function html( array $fields ) {
  31. $content = '';
  32. foreach ( $fields as $field_name => $field_configuration ) {
  33. $content .= $this->form_row( 'wpseo_' . $field_name, $field_configuration );
  34. }
  35. return $content;
  36. }
  37. /**
  38. * Create a row in the form table.
  39. *
  40. * @param string $field_name Variable the row controls.
  41. * @param array $field_configuration Array with the field configuration.
  42. */
  43. private function form_row( $field_name, array $field_configuration ) {
  44. $esc_field_name = esc_attr( $field_name );
  45. $options = (array) $field_configuration['options'];
  46. if ( ! empty( $field_configuration['description'] ) ) {
  47. $options['description'] = $field_configuration['description'];
  48. }
  49. $label = $this->get_label( $field_configuration['label'], $esc_field_name );
  50. $field = $this->get_field( $field_configuration['type'], $esc_field_name, $this->get_field_value( $field_name ), $options );
  51. $help_content = isset( $field_configuration['options']['help'] ) ? $field_configuration['options']['help'] : '';
  52. $help_button_text = isset( $field_configuration['options']['help-button'] ) ? $field_configuration['options']['help-button'] : '';
  53. $help = new WPSEO_Admin_Help_Panel( $field_name, $help_button_text, $help_content );
  54. return $this->parse_row( $label, $help, $field );
  55. }
  56. /**
  57. * Generates the html for the given field config.
  58. *
  59. * @param string $field_type The fieldtype, e.g: text, checkbox, etc.
  60. * @param string $field_name The name of the field.
  61. * @param string $field_value The value of the field.
  62. * @param array $options Array with additional options.
  63. *
  64. * @return string
  65. */
  66. private function get_field( $field_type, $field_name, $field_value, array $options ) {
  67. $class = $this->get_class( $options );
  68. $field = '';
  69. $description = '';
  70. $aria_describedby = '';
  71. if ( ! empty( $options['description'] ) ) {
  72. $aria_describedby = ' aria-describedby="' . $field_name . '-desc"';
  73. $description = '<p id="' . $field_name . '-desc" class="yoast-metabox__description">' . $options['description'] . '</p>';
  74. }
  75. switch ( $field_type ) {
  76. case 'div':
  77. $field .= '<div id="' . $field_name . '"></div>';
  78. break;
  79. case 'text':
  80. $field .= '<input name="' . $field_name . '" id="' . $field_name . '" ' . $class . ' type="text" value="' . esc_attr( $field_value ) . '" size="40"' . $aria_describedby . '/>';
  81. break;
  82. case 'checkbox':
  83. $field .= '<input name="' . $field_name . '" id="' . $field_name . '" type="checkbox" ' . checked( $field_value ) . $aria_describedby . '/>';
  84. break;
  85. case 'textarea':
  86. $rows = 3;
  87. if ( ! empty( $options['rows'] ) ) {
  88. $rows = $options['rows'];
  89. }
  90. $field .= '<textarea class="large-text" rows="' . esc_attr( $rows ) . '" id="' . $field_name . '" name="' . $field_name . '"' . $aria_describedby . '>' . esc_textarea( $field_value ) . '</textarea>';
  91. break;
  92. case 'upload':
  93. $field .= '<input' .
  94. ' id="' . $field_name . '"' .
  95. ' type="text"' .
  96. ' size="36"' .
  97. ' name="' . $field_name . '"' .
  98. ' value="' . esc_attr( $field_value ) . '"' . $aria_describedby . '' .
  99. ' readonly="readonly"' .
  100. ' /> ';
  101. $field .= '<input' .
  102. ' id="' . esc_attr( $field_name ) . '_button"' .
  103. ' class="wpseo_image_upload_button button"' .
  104. ' data-target="' . esc_attr( $field_name ) . '"' .
  105. ' data-target-id="hidden_' . esc_attr( $field_name ) . '-id"' .
  106. ' type="button"' .
  107. ' value="' . esc_attr__( 'Upload Image', 'wordpress-seo' ) . '"' .
  108. ' /> ';
  109. $field .= '<input' .
  110. ' id="' . esc_attr( $field_name ) . '_button"' .
  111. ' class="wpseo_image_remove_button button"' .
  112. ' type="button"' .
  113. ' value="' . esc_attr__( 'Clear Image', 'wordpress-seo' ) . '"' .
  114. ' />';
  115. break;
  116. case 'select':
  117. if ( is_array( $options ) && $options !== [] ) {
  118. $field .= '<select name="' . $field_name . '" id="' . $field_name . '"' . $aria_describedby . '>';
  119. $select_options = ( array_key_exists( 'options', $options ) ) ? $options['options'] : $options;
  120. foreach ( $select_options as $option => $option_label ) {
  121. $selected = selected( $option, $field_value, false );
  122. $field .= '<option ' . $selected . ' value="' . esc_attr( $option ) . '">' . esc_html( $option_label ) . '</option>';
  123. }
  124. unset( $option, $option_label, $selected );
  125. $field .= '</select>';
  126. }
  127. break;
  128. case 'hidden':
  129. $field .= '<input name="' . $field_name . '" id="hidden_' . $field_name . '" type="hidden" value="' . esc_attr( $field_value ) . '" />';
  130. break;
  131. }
  132. return $field . $description;
  133. }
  134. /**
  135. * Getting the value for given field_name.
  136. *
  137. * @param string $field_name The fieldname to get the value for.
  138. *
  139. * @return string
  140. */
  141. private function get_field_value( $field_name ) {
  142. if ( isset( $this->tax_meta[ $field_name ] ) && $this->tax_meta[ $field_name ] !== '' ) {
  143. return $this->tax_meta[ $field_name ];
  144. }
  145. return '';
  146. }
  147. /**
  148. * Getting the class attributes if $options contains a class key.
  149. *
  150. * @param array $options The array with field options.
  151. *
  152. * @return string
  153. */
  154. private function get_class( array $options ) {
  155. if ( ! empty( $options['class'] ) ) {
  156. return ' class="' . esc_attr( $options['class'] ) . '"';
  157. }
  158. return '';
  159. }
  160. /**
  161. * Getting the label HTML.
  162. *
  163. * @param string $label The label value.
  164. * @param string $field_name The target field.
  165. *
  166. * @return string
  167. */
  168. private function get_label( $label, $field_name ) {
  169. if ( $label !== '' ) {
  170. return '<label for="' . $field_name . '">' . esc_html( $label ) . '</label>';
  171. }
  172. return '';
  173. }
  174. /**
  175. * Returns the HTML for the row which contains label, help and the field.
  176. *
  177. * @param string $label The html for the label if there was a label set.
  178. * @param WPSEO_Admin_Help_Panel $help The help panel to render in this row.
  179. * @param string $field The html for the field.
  180. *
  181. * @return string
  182. */
  183. private function parse_row( $label, WPSEO_Admin_Help_Panel $help, $field ) {
  184. if ( $label !== '' || $help !== '' ) {
  185. return $label . $help->get_button_html() . $help->get_panel_html() . $field;
  186. }
  187. return $field;
  188. }
  189. }