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 : '–'; |
| 406 | } |
| 407 | |
| 408 | $color_value = sanitize_hex_color( get_term_meta( $term_id, 'color', true ) ); |
| 409 | |
| 410 | if ( ! $color_value ) { |
| 411 | return '–'; |
| 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 |