PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / admin / taxonomy / class-taxonomy-fields-presenter.php

class-taxonomy-fields-presenter.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at admin/taxonomy/class-taxonomy-fields-presenter.php

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