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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductAttributes / VisualAttributeTermAdmin.php
woocommerce / src / Internal / ProductAttributes Last commit date
VisualAttributeTermAdmin.php 4 days ago VisualAttributeTermMeta.php 1 month ago
VisualAttributeTermAdmin.php
442 lines
1 <?php
2 /**
3 * Visual attribute term admin fields.
4 *
5 * @package WooCommerce\Classes
6 */
7
8 declare( strict_types = 1 );
9
10 namespace Automattic\WooCommerce\Internal\ProductAttributes;
11
12 use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
13 use Automattic\WooCommerce\Internal\RegisterHooksInterface;
14
15 /**
16 * Admin UI for wc-visual attribute term metadata.
17 *
18 * @internal
19 *
20 * @since 10.9.0
21 */
22 class VisualAttributeTermAdmin implements RegisterHooksInterface {
23
24 /**
25 * Register hooks.
26 *
27 * @return void
28 */
29 public function register(): void {
30 if ( ! is_admin() ) {
31 return;
32 }
33 add_action( 'created_term', array( $this, 'save_product_attribute_term_fields' ), 10, 3 );
34 add_action( 'edit_term', array( $this, 'save_product_attribute_term_fields' ), 10, 3 );
35
36 foreach ( wc_get_attribute_taxonomies() as $attribute ) {
37 $taxonomy = 'pa_' . $attribute->attribute_name;
38
39 add_action( $taxonomy . '_add_form_fields', array( $this, 'add_product_attribute_term_fields' ) );
40 add_action( $taxonomy . '_edit_form_fields', array( $this, 'edit_product_attribute_term_fields' ), 10, 1 );
41 add_filter(
42 "manage_edit-{$taxonomy}_columns",
43 function ( $columns ) use ( $taxonomy ) {
44 return $this->add_term_visual_column( $columns, $taxonomy );
45 }
46 );
47 add_filter(
48 "manage_{$taxonomy}_custom_column",
49 function ( $content, $column, $term_id ) use ( $taxonomy ) {
50 return $this->render_term_visual_column( $content, $column, $term_id, $taxonomy );
51 },
52 10,
53 3
54 );
55 }
56
57 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_visual_attribute_script' ) );
58 add_filter( 'woocommerce_json_search_found_product_attribute_terms', array( $this, 'add_visual_data_to_attribute_terms' ), 10, 2 );
59 }
60
61 /**
62 * Add visual swatch data to attribute term search results.
63 *
64 * @internal
65 *
66 * @param array|\WP_Error $terms The list of matched terms.
67 * @param string $taxonomy The terms taxonomy.
68 * @return array|\WP_Error
69 */
70 public function add_visual_data_to_attribute_terms( $terms, string $taxonomy ) {
71 if ( is_wp_error( $terms ) || empty( $terms ) || ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
72 return $terms;
73 }
74
75 // Because `$terms` might be filtered by a plugin, make sure we only operate on valid terms.
76 $valid_terms = array_filter(
77 $terms,
78 static function ( $term ) {
79 return is_object( $term ) && isset( $term->term_id );
80 }
81 );
82
83 $term_ids = array_map( 'intval', wp_list_pluck( $valid_terms, 'term_id' ) );
84 $visuals = VisualAttributeTermMeta::get_term_visuals( $term_ids );
85
86 foreach ( $valid_terms as $term ) {
87 /**
88 * Term object with dynamic visual property.
89 *
90 * @var \stdClass $term
91 */
92 $term->visual = $visuals[ $term->term_id ] ?? VisualAttributeTermMeta::get_empty_visual();
93 }
94
95 return $terms;
96 }
97
98 /**
99 * Add custom fields for product attribute terms.
100 *
101 * @internal
102 *
103 * @param string $taxonomy Taxonomy slug.
104 * @return void
105 */
106 public function add_product_attribute_term_fields( $taxonomy ): void {
107 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
108 return;
109 }
110
111 self::render_div_visual_attribute_fields( 'term-' );
112 }
113
114 /**
115 * Edit custom fields for product attribute terms.
116 *
117 * @internal
118 *
119 * @param \WP_Term $term Current term.
120 * @return void
121 */
122 public function edit_product_attribute_term_fields( $term ): void {
123 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $term->taxonomy ) ) {
124 return;
125 }
126
127 self::render_table_visual_attribute_fields( $term );
128 }
129
130 /**
131 * Render visual fields for the add attribute term modal.
132 *
133 * @internal
134 *
135 * @return void
136 */
137 public static function render_add_attribute_term_modal_fields(): void {
138 self::render_div_visual_attribute_fields( 'wc-modal-add-attribute-term-' );
139 }
140
141 /**
142 * Render visual attribute fields for add forms.
143 *
144 * @param string $field_id_prefix Field ID prefix.
145 * @return void
146 */
147 private static function render_div_visual_attribute_fields( string $field_id_prefix ): void {
148 ?>
149 <div class="form-field wc-admin-visual-attribute-type">
150 <label><?php esc_html_e( 'Swatch type', 'woocommerce' ); ?></label>
151 <?php self::render_visual_type_inputs( $field_id_prefix, VisualAttributeTermMeta::TYPE_COLOR ); ?>
152 </div>
153 <div class="form-field wc-admin-visual-attribute-color">
154 <?php self::render_color_input( $field_id_prefix, '' ); ?>
155 </div>
156 <div class="form-field wc-admin-visual-attribute-image">
157 <?php self::render_image_input( $field_id_prefix, 0 ); ?>
158 </div>
159 <?php
160 }
161
162 /**
163 * Render visual attribute fields for edit forms.
164 *
165 * @param \WP_Term $term Current term.
166 * @return void
167 */
168 private static function render_table_visual_attribute_fields( \WP_Term $term ): void {
169 $color_value = get_term_meta( $term->term_id, 'color', true );
170 $color_value = is_string( $color_value ) ? $color_value : '';
171 $image_value = absint( get_term_meta( $term->term_id, 'image', true ) );
172 $visual_type = $image_value > 0 ? VisualAttributeTermMeta::TYPE_IMAGE : VisualAttributeTermMeta::TYPE_COLOR;
173 $field_prefix = 'term-';
174 ?>
175 <tr class="form-field wc-admin-visual-attribute-type">
176 <th scope="row" valign="top">
177 <label><?php esc_html_e( 'Swatch type', 'woocommerce' ); ?></label>
178 </th>
179 <td><?php self::render_visual_type_inputs( $field_prefix, $visual_type ); ?></td>
180 </tr>
181 <tr class="form-field wc-admin-visual-attribute-color">
182 <th scope="row" valign="top">
183 <label for="<?php echo esc_attr( self::get_color_input_id( $field_prefix ) ); ?>"><?php esc_html_e( 'Color value', 'woocommerce' ); ?></label>
184 </th>
185 <td><?php self::render_color_input_control( $field_prefix, $color_value ); ?></td>
186 </tr>
187 <tr class="form-field wc-admin-visual-attribute-image">
188 <th scope="row" valign="top">
189 <label for="<?php echo esc_attr( self::get_image_input_id( $field_prefix ) ); ?>"><?php esc_html_e( 'Image value', 'woocommerce' ); ?></label>
190 </th>
191 <td><?php self::render_image_input_control( $field_prefix, $image_value ); ?></td>
192 </tr>
193 <?php
194 }
195
196 /**
197 * Render visual type radio inputs.
198 *
199 * @param string $field_id_prefix Field ID prefix.
200 * @param string $selected_type Selected visual type.
201 * @return void
202 */
203 private static function render_visual_type_inputs( string $field_id_prefix, string $selected_type ): void {
204 $color_id = $field_id_prefix . 'visual-type-color';
205 $image_id = $field_id_prefix . 'visual-type-image';
206 ?>
207 <fieldset>
208 <label for="<?php echo esc_attr( $color_id ); ?>">
209 <input
210 type="radio"
211 id="<?php echo esc_attr( $color_id ); ?>"
212 name="wc_visual_attribute_type"
213 value="<?php echo esc_attr( VisualAttributeTermMeta::TYPE_COLOR ); ?>"
214 <?php checked( VisualAttributeTermMeta::TYPE_COLOR, $selected_type ); ?>
215 />
216 <?php esc_html_e( 'Color', 'woocommerce' ); ?>
217 </label>
218 <label for="<?php echo esc_attr( $image_id ); ?>">
219 <input
220 type="radio"
221 id="<?php echo esc_attr( $image_id ); ?>"
222 name="wc_visual_attribute_type"
223 value="<?php echo esc_attr( VisualAttributeTermMeta::TYPE_IMAGE ); ?>"
224 <?php checked( VisualAttributeTermMeta::TYPE_IMAGE, $selected_type ); ?>
225 />
226 <?php esc_html_e( 'Image', 'woocommerce' ); ?>
227 </label>
228 </fieldset>
229 <?php
230 }
231
232 /**
233 * Render the color input and label.
234 *
235 * @param string $field_id_prefix Field ID prefix.
236 * @param string $color_value Color value.
237 * @return void
238 */
239 private static function render_color_input( string $field_id_prefix, string $color_value ): void {
240 ?>
241 <label for="<?php echo esc_attr( self::get_color_input_id( $field_id_prefix ) ); ?>"><?php esc_html_e( 'Color value', 'woocommerce' ); ?></label>
242 <?php self::render_color_input_control( $field_id_prefix, $color_value ); ?>
243 <?php
244 }
245
246 /**
247 * Render the color input control.
248 *
249 * @param string $field_id_prefix Field ID prefix.
250 * @param string $color_value Color value.
251 * @return void
252 */
253 private static function render_color_input_control( string $field_id_prefix, string $color_value ): void {
254 ?>
255 <input name="term_color" id="<?php echo esc_attr( self::get_color_input_id( $field_id_prefix ) ); ?>" class="wc-admin-visual-attribute-color-input" type="text" value="<?php echo esc_attr( $color_value ); ?>" />
256 <?php
257 }
258
259 /**
260 * Render the image input and label.
261 *
262 * @param string $field_id_prefix Field ID prefix.
263 * @param int $image_value Image attachment ID.
264 * @return void
265 */
266 private static function render_image_input( string $field_id_prefix, int $image_value ): void {
267 ?>
268 <label for="<?php echo esc_attr( self::get_image_input_id( $field_id_prefix ) ); ?>"><?php esc_html_e( 'Image value', 'woocommerce' ); ?></label>
269 <?php self::render_image_input_control( $field_id_prefix, $image_value ); ?>
270 <?php
271 }
272
273 /**
274 * Render the image input control.
275 *
276 * @param string $field_id_prefix Field ID prefix.
277 * @param int $image_value Image attachment ID.
278 * @return void
279 */
280 private static function render_image_input_control( string $field_id_prefix, int $image_value ): void {
281 ?>
282 <input name="term_image" id="<?php echo esc_attr( self::get_image_input_id( $field_id_prefix ) ); ?>" class="wc-admin-visual-attribute-image-input" type="hidden" value="<?php echo absint( $image_value ); ?>" />
283 <?php
284 }
285
286 /**
287 * Get color input ID.
288 *
289 * @param string $field_id_prefix Field ID prefix.
290 * @return string
291 */
292 private static function get_color_input_id( string $field_id_prefix ): string {
293 return $field_id_prefix . 'color';
294 }
295
296 /**
297 * Get image input ID.
298 *
299 * @param string $field_id_prefix Field ID prefix.
300 * @return string
301 */
302 private static function get_image_input_id( string $field_id_prefix ): string {
303 return $field_id_prefix . 'image';
304 }
305
306 /**
307 * Enqueue the visual attribute script.
308 *
309 * @internal
310 *
311 * @return void
312 */
313 public function enqueue_visual_attribute_script(): void {
314 $screen = get_current_screen();
315
316 if ( ! $screen ) {
317 return;
318 }
319
320 $is_product_editor_screen = 'product' === $screen->id;
321
322 if ( $is_product_editor_screen && array_key_exists( 'wc-visual', wc_get_attribute_types() ) ) {
323 wp_enqueue_media();
324 WCAdminAssets::register_script( 'wp-admin-scripts', 'visual-attribute-picker', true, array( 'wp-components' ) );
325 return;
326 }
327
328 $is_attribute_term_screen = 0 === strpos( $screen->id, 'edit-pa_' );
329 $taxonomy = $this->get_current_taxonomy();
330
331 if ( $is_attribute_term_screen && VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
332 wp_enqueue_media();
333 WCAdminAssets::register_script( 'wp-admin-scripts', 'visual-attribute-picker', true, array( 'wp-components' ) );
334 }
335 }
336
337 /**
338 * Save product attribute term fields.
339 *
340 * @internal
341 *
342 * @param mixed $term_id Term ID being saved.
343 * @param mixed $tt_id Term taxonomy ID.
344 * @param string $taxonomy Taxonomy slug.
345 * @return void
346 */
347 public function save_product_attribute_term_fields( $term_id, $tt_id = '', $taxonomy = '' ): void {
348 if ( $this->is_ajax_add_attribute_request() ) {
349 return;
350 }
351
352 VisualAttributeTermMeta::save_term_visual_from_request( (int) $term_id, $taxonomy, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
353 }
354
355 /**
356 * Add visual column for product attribute terms.
357 *
358 * @internal
359 *
360 * @param array $columns Existing columns.
361 * @param string $taxonomy Taxonomy slug.
362 * @return array
363 */
364 public function add_term_visual_column( $columns, $taxonomy ): array {
365 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
366 return $columns;
367 }
368
369 $new_columns = array();
370 foreach ( $columns as $key => $label ) {
371 if ( 'slug' === $key ) {
372 $new_columns['visual'] = __( 'Visual', 'woocommerce' );
373 }
374 $new_columns[ $key ] = $label;
375 }
376
377 if ( ! isset( $new_columns['visual'] ) ) {
378 $new_columns['visual'] = __( 'Visual', 'woocommerce' );
379 }
380
381 return $new_columns;
382 }
383
384 /**
385 * Render visual column for product attribute terms.
386 *
387 * @internal
388 *
389 * @param string $content Column output so far.
390 * @param string $column Current column key.
391 * @param int $term_id Term ID.
392 * @param string $taxonomy Taxonomy slug.
393 * @return string
394 */
395 public function render_term_visual_column( $content, $column, $term_id, $taxonomy ): string {
396 if ( 'visual' !== $column || ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
397 return $content;
398 }
399
400 $image_id = absint( get_term_meta( $term_id, 'image', true ) );
401
402 if ( $image_id && wp_attachment_is_image( $image_id ) ) {
403 $thumbnail = wp_get_attachment_image( $image_id, array( 32, 32 ) );
404
405 return $thumbnail ? $thumbnail : '&ndash;';
406 }
407
408 $color_value = sanitize_hex_color( get_term_meta( $term_id, 'color', true ) );
409
410 if ( ! $color_value ) {
411 return '&ndash;';
412 }
413
414 $swatch = sprintf(
415 '<span class="wc-admin-color-swatch" style="background-color:%s;" aria-hidden="true"></span>',
416 esc_attr( $color_value )
417 );
418
419 return $swatch . esc_html( strtoupper( $color_value ) );
420 }
421
422 /**
423 * Check whether the current request is the add attribute AJAX action.
424 *
425 * @return bool
426 */
427 private function is_ajax_add_attribute_request(): bool {
428 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
429
430 return wp_doing_ajax() && 'woocommerce_add_new_attribute' === $action;
431 }
432
433 /**
434 * Get current taxonomy from request.
435 *
436 * @return string
437 */
438 private function get_current_taxonomy(): string {
439 return isset( $_GET['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
440 }
441 }
442