class-taxonomy-fields-presenter.php 6.2 KB

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