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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 1 month ago VisualAttributeTermMeta.php 1 month ago
VisualAttributeTermAdmin.php
404 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 }
59
60 /**
61 * Add custom fields for product attribute terms.
62 *
63 * @internal
64 *
65 * @param string $taxonomy Taxonomy slug.
66 * @return void
67 */
68 public function add_product_attribute_term_fields( $taxonomy ): void {
69 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
70 return;
71 }
72
73 self::render_div_visual_attribute_fields( 'term-' );
74 }
75
76 /**
77 * Edit custom fields for product attribute terms.
78 *
79 * @internal
80 *
81 * @param \WP_Term $term Current term.
82 * @return void
83 */
84 public function edit_product_attribute_term_fields( $term ): void {
85 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $term->taxonomy ) ) {
86 return;
87 }
88
89 self::render_table_visual_attribute_fields( $term );
90 }
91
92 /**
93 * Render visual fields for the add attribute term modal.
94 *
95 * @internal
96 *
97 * @return void
98 */
99 public static function render_add_attribute_term_modal_fields(): void {
100 self::render_div_visual_attribute_fields( 'wc-modal-add-attribute-term-' );
101 }
102
103 /**
104 * Render visual attribute fields for add forms.
105 *
106 * @param string $field_id_prefix Field ID prefix.
107 * @return void
108 */
109 private static function render_div_visual_attribute_fields( string $field_id_prefix ): void {
110 ?>
111 <div class="form-field wc-admin-visual-attribute-type">
112 <label><?php esc_html_e( 'Swatch type', 'woocommerce' ); ?></label>
113 <?php self::render_visual_type_inputs( $field_id_prefix, VisualAttributeTermMeta::TYPE_COLOR ); ?>
114 </div>
115 <div class="form-field wc-admin-visual-attribute-color">
116 <?php self::render_color_input( $field_id_prefix, '' ); ?>
117 </div>
118 <div class="form-field wc-admin-visual-attribute-image">
119 <?php self::render_image_input( $field_id_prefix, 0 ); ?>
120 </div>
121 <?php
122 }
123
124 /**
125 * Render visual attribute fields for edit forms.
126 *
127 * @param \WP_Term $term Current term.
128 * @return void
129 */
130 private static function render_table_visual_attribute_fields( \WP_Term $term ): void {
131 $color_value = get_term_meta( $term->term_id, 'color', true );
132 $color_value = is_string( $color_value ) ? $color_value : '';
133 $image_value = absint( get_term_meta( $term->term_id, 'image', true ) );
134 $visual_type = $image_value > 0 ? VisualAttributeTermMeta::TYPE_IMAGE : VisualAttributeTermMeta::TYPE_COLOR;
135 $field_prefix = 'term-';
136 ?>
137 <tr class="form-field wc-admin-visual-attribute-type">
138 <th scope="row" valign="top">
139 <label><?php esc_html_e( 'Swatch type', 'woocommerce' ); ?></label>
140 </th>
141 <td><?php self::render_visual_type_inputs( $field_prefix, $visual_type ); ?></td>
142 </tr>
143 <tr class="form-field wc-admin-visual-attribute-color">
144 <th scope="row" valign="top">
145 <label for="<?php echo esc_attr( self::get_color_input_id( $field_prefix ) ); ?>"><?php esc_html_e( 'Color value', 'woocommerce' ); ?></label>
146 </th>
147 <td><?php self::render_color_input_control( $field_prefix, $color_value ); ?></td>
148 </tr>
149 <tr class="form-field wc-admin-visual-attribute-image">
150 <th scope="row" valign="top">
151 <label for="<?php echo esc_attr( self::get_image_input_id( $field_prefix ) ); ?>"><?php esc_html_e( 'Image value', 'woocommerce' ); ?></label>
152 </th>
153 <td><?php self::render_image_input_control( $field_prefix, $image_value ); ?></td>
154 </tr>
155 <?php
156 }
157
158 /**
159 * Render visual type radio inputs.
160 *
161 * @param string $field_id_prefix Field ID prefix.
162 * @param string $selected_type Selected visual type.
163 * @return void
164 */
165 private static function render_visual_type_inputs( string $field_id_prefix, string $selected_type ): void {
166 $color_id = $field_id_prefix . 'visual-type-color';
167 $image_id = $field_id_prefix . 'visual-type-image';
168 ?>
169 <fieldset>
170 <label for="<?php echo esc_attr( $color_id ); ?>">
171 <input
172 type="radio"
173 id="<?php echo esc_attr( $color_id ); ?>"
174 name="wc_visual_attribute_type"
175 value="<?php echo esc_attr( VisualAttributeTermMeta::TYPE_COLOR ); ?>"
176 <?php checked( VisualAttributeTermMeta::TYPE_COLOR, $selected_type ); ?>
177 />
178 <?php esc_html_e( 'Color', 'woocommerce' ); ?>
179 </label>
180 <label for="<?php echo esc_attr( $image_id ); ?>">
181 <input
182 type="radio"
183 id="<?php echo esc_attr( $image_id ); ?>"
184 name="wc_visual_attribute_type"
185 value="<?php echo esc_attr( VisualAttributeTermMeta::TYPE_IMAGE ); ?>"
186 <?php checked( VisualAttributeTermMeta::TYPE_IMAGE, $selected_type ); ?>
187 />
188 <?php esc_html_e( 'Image', 'woocommerce' ); ?>
189 </label>
190 </fieldset>
191 <?php
192 }
193
194 /**
195 * Render the color input and label.
196 *
197 * @param string $field_id_prefix Field ID prefix.
198 * @param string $color_value Color value.
199 * @return void
200 */
201 private static function render_color_input( string $field_id_prefix, string $color_value ): void {
202 ?>
203 <label for="<?php echo esc_attr( self::get_color_input_id( $field_id_prefix ) ); ?>"><?php esc_html_e( 'Color value', 'woocommerce' ); ?></label>
204 <?php self::render_color_input_control( $field_id_prefix, $color_value ); ?>
205 <?php
206 }
207
208 /**
209 * Render the color input control.
210 *
211 * @param string $field_id_prefix Field ID prefix.
212 * @param string $color_value Color value.
213 * @return void
214 */
215 private static function render_color_input_control( string $field_id_prefix, string $color_value ): void {
216 ?>
217 <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 ); ?>" />
218 <?php
219 }
220
221 /**
222 * Render the image input and label.
223 *
224 * @param string $field_id_prefix Field ID prefix.
225 * @param int $image_value Image attachment ID.
226 * @return void
227 */
228 private static function render_image_input( string $field_id_prefix, int $image_value ): void {
229 ?>
230 <label for="<?php echo esc_attr( self::get_image_input_id( $field_id_prefix ) ); ?>"><?php esc_html_e( 'Image value', 'woocommerce' ); ?></label>
231 <?php self::render_image_input_control( $field_id_prefix, $image_value ); ?>
232 <?php
233 }
234
235 /**
236 * Render the image input control.
237 *
238 * @param string $field_id_prefix Field ID prefix.
239 * @param int $image_value Image attachment ID.
240 * @return void
241 */
242 private static function render_image_input_control( string $field_id_prefix, int $image_value ): void {
243 ?>
244 <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 ); ?>" />
245 <?php
246 }
247
248 /**
249 * Get color input ID.
250 *
251 * @param string $field_id_prefix Field ID prefix.
252 * @return string
253 */
254 private static function get_color_input_id( string $field_id_prefix ): string {
255 return $field_id_prefix . 'color';
256 }
257
258 /**
259 * Get image input ID.
260 *
261 * @param string $field_id_prefix Field ID prefix.
262 * @return string
263 */
264 private static function get_image_input_id( string $field_id_prefix ): string {
265 return $field_id_prefix . 'image';
266 }
267
268 /**
269 * Enqueue the visual attribute script.
270 *
271 * @internal
272 *
273 * @return void
274 */
275 public function enqueue_visual_attribute_script(): void {
276 $screen = get_current_screen();
277
278 if ( ! $screen ) {
279 return;
280 }
281
282 $is_product_editor_screen = 'product' === $screen->id;
283
284 if ( $is_product_editor_screen && array_key_exists( 'wc-visual', wc_get_attribute_types() ) ) {
285 wp_enqueue_media();
286 WCAdminAssets::register_script( 'wp-admin-scripts', 'visual-attribute-color-picker', true, array( 'wp-components' ) );
287 return;
288 }
289
290 $is_attribute_term_screen = 0 === strpos( $screen->id, 'edit-pa_' );
291 $taxonomy = $this->get_current_taxonomy();
292
293 if ( $is_attribute_term_screen && VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
294 wp_enqueue_media();
295 WCAdminAssets::register_script( 'wp-admin-scripts', 'visual-attribute-color-picker', true, array( 'wp-components' ) );
296 }
297 }
298
299 /**
300 * Save product attribute term fields.
301 *
302 * @internal
303 *
304 * @param mixed $term_id Term ID being saved.
305 * @param mixed $tt_id Term taxonomy ID.
306 * @param string $taxonomy Taxonomy slug.
307 * @return void
308 */
309 public function save_product_attribute_term_fields( $term_id, $tt_id = '', $taxonomy = '' ): void {
310 if ( $this->is_ajax_add_attribute_request() ) {
311 return;
312 }
313
314 VisualAttributeTermMeta::save_term_visual_from_request( (int) $term_id, $taxonomy, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
315 }
316
317 /**
318 * Add visual column for product attribute terms.
319 *
320 * @internal
321 *
322 * @param array $columns Existing columns.
323 * @param string $taxonomy Taxonomy slug.
324 * @return array
325 */
326 public function add_term_visual_column( $columns, $taxonomy ): array {
327 if ( ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
328 return $columns;
329 }
330
331 $new_columns = array();
332 foreach ( $columns as $key => $label ) {
333 if ( 'slug' === $key ) {
334 $new_columns['visual'] = __( 'Visual', 'woocommerce' );
335 }
336 $new_columns[ $key ] = $label;
337 }
338
339 if ( ! isset( $new_columns['visual'] ) ) {
340 $new_columns['visual'] = __( 'Visual', 'woocommerce' );
341 }
342
343 return $new_columns;
344 }
345
346 /**
347 * Render visual column for product attribute terms.
348 *
349 * @internal
350 *
351 * @param string $content Column output so far.
352 * @param string $column Current column key.
353 * @param int $term_id Term ID.
354 * @param string $taxonomy Taxonomy slug.
355 * @return string
356 */
357 public function render_term_visual_column( $content, $column, $term_id, $taxonomy ): string {
358 if ( 'visual' !== $column || ! VisualAttributeTermMeta::is_visual_attribute_taxonomy( $taxonomy ) ) {
359 return $content;
360 }
361
362 $image_id = absint( get_term_meta( $term_id, 'image', true ) );
363
364 if ( $image_id && wp_attachment_is_image( $image_id ) ) {
365 $thumbnail = wp_get_attachment_image( $image_id, array( 32, 32 ) );
366
367 return $thumbnail ? $thumbnail : '&ndash;';
368 }
369
370 $color_value = sanitize_hex_color( get_term_meta( $term_id, 'color', true ) );
371
372 if ( ! $color_value ) {
373 return '&ndash;';
374 }
375
376 $swatch = sprintf(
377 '<span class="wc-admin-color-swatch" style="background-color:%s;" aria-hidden="true"></span>',
378 esc_attr( $color_value )
379 );
380
381 return $swatch . esc_html( strtoupper( $color_value ) );
382 }
383
384 /**
385 * Check whether the current request is the add attribute AJAX action.
386 *
387 * @return bool
388 */
389 private function is_ajax_add_attribute_request(): bool {
390 $action = isset( $_REQUEST['action'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
391
392 return wp_doing_ajax() && 'woocommerce_add_new_attribute' === $action;
393 }
394
395 /**
396 * Get current taxonomy from request.
397 *
398 * @return string
399 */
400 private function get_current_taxonomy(): string {
401 return isset( $_GET['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
402 }
403 }
404