' . wp_kses_post( $result->get_error_message() ) . '

'; } // Show admin interface. if ( ! empty( $_GET['edit'] ) ) { self::edit_attribute(); } else { self::add_attribute(); } } /** * Get and sanitize posted attribute data. * * @return array */ private static function get_posted_attribute() { $attribute = array( 'attribute_label' => isset( $_POST['attribute_label'] ) ? wc_clean( wp_unslash( $_POST['attribute_label'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Action helpers verify nonces; fields are normalized before CRUD. 'attribute_name' => isset( $_POST['attribute_name'] ) ? wc_sanitize_taxonomy_name( wp_unslash( $_POST['attribute_name'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Action helpers verify nonces; fields are normalized before CRUD. 'attribute_type' => isset( $_POST['attribute_type'] ) ? wc_clean( wp_unslash( $_POST['attribute_type'] ) ) : 'select', // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Action helpers verify nonces; fields are normalized before CRUD. 'attribute_orderby' => isset( $_POST['attribute_orderby'] ) ? wc_clean( wp_unslash( $_POST['attribute_orderby'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Action helpers verify nonces; fields are normalized before CRUD. 'attribute_public' => isset( $_POST['attribute_public'] ) ? 1 : 0, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Action helpers verify nonces; fields are normalized before CRUD. ); if ( empty( $attribute['attribute_type'] ) ) { $attribute['attribute_type'] = 'select'; } if ( empty( $attribute['attribute_label'] ) ) { $attribute['attribute_label'] = ucfirst( $attribute['attribute_name'] ); } if ( empty( $attribute['attribute_name'] ) ) { $attribute['attribute_name'] = wc_sanitize_taxonomy_name( $attribute['attribute_label'] ); } return $attribute; } /** * Add an attribute. * * @return bool|WP_Error */ private static function process_add_attribute() { check_admin_referer( 'woocommerce-add-new_attribute' ); $attribute = self::get_posted_attribute(); $args = array( 'name' => $attribute['attribute_label'], 'slug' => $attribute['attribute_name'], 'type' => $attribute['attribute_type'], 'order_by' => $attribute['attribute_orderby'], 'has_archives' => $attribute['attribute_public'], ); $id = wc_create_attribute( $args ); if ( is_wp_error( $id ) ) { return $id; } VisualAttributeTermAdmin::seed_visual_attribute_terms( $id ); return true; } /** * Edit an attribute. * * @return bool|WP_Error */ private static function process_edit_attribute() { $attribute_id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0; check_admin_referer( 'woocommerce-save-attribute_' . $attribute_id ); $attribute = self::get_posted_attribute(); $args = array( 'name' => $attribute['attribute_label'], 'slug' => $attribute['attribute_name'], 'type' => $attribute['attribute_type'], 'order_by' => $attribute['attribute_orderby'], 'has_archives' => $attribute['attribute_public'], ); $id = wc_update_attribute( $attribute_id, $args ); if ( is_wp_error( $id ) ) { return $id; } self::$edited_attribute_id = $id; return true; } /** * Delete an attribute. * * @return bool */ private static function process_delete_attribute() { $attribute_id = isset( $_GET['delete'] ) ? absint( $_GET['delete'] ) : 0; check_admin_referer( 'woocommerce-delete-attribute_' . $attribute_id ); return wc_delete_attribute( $attribute_id ); } /** * Build the slug field description shown to users (and served to search engines and * no-JavaScript clients as the static copy). * * States the authoritative byte limit alongside a character estimate tailored to the * script of the user's language, so users on non-Latin scripts get a meaningful * figure rather than a raw byte count they must translate into characters themselves. * * @return string */ private static function slug_field_description(): string { return sprintf( /* translators: 1: maximum slug length in bytes, 2: approximate maximum number of characters for the user's language. */ __( 'Unique slug/reference for the attribute. Limited to %1$d bytes — roughly %2$d characters in your language; non-ASCII characters use 2–4 bytes each.', 'woocommerce' ), wc_get_attribute_slug_max_byte_length(), AttributeSlugLength::get_character_estimate() ); } /** * Output an inline script that shows a live UTF-8 byte count next to the * slug input and visually warns when the value approaches or exceeds the * server-side byte limit. * * The HTML `maxlength` attribute counts characters, not bytes, so a * multibyte slug (e.g. Cyrillic, Chinese) can pass the browser's guard * and still be rejected by `register_taxonomy()`'s 32-byte name limit. * This counter closes that feedback gap before submission. * * When the live counter activates, it also swaps the server-rendered field * description for a leaner note: the counter now carries the concrete byte * numbers, so the locale-tailored character estimate becomes redundant. */ private static function slug_byte_counter_script(): void { $max_bytes = wc_get_attribute_slug_max_byte_length(); /* translators: 1: current byte count, 2: maximum allowed bytes. */ $template = wp_json_encode( __( '%1$d / %2$d bytes', 'woocommerce' ), JSON_HEX_TAG | JSON_HEX_AMP ); if ( false === $template ) { // Encoding the counter template failed (e.g. invalid UTF-8 in the translated // string); skip the counter rather than emit broken inline JavaScript. return; } // Leaner description for JS clients: the live counter below carries the concrete // numbers, so the locale-tailored character estimate becomes redundant noise. $js_description = wp_json_encode( /* translators: %d: maximum slug length in bytes. */ sprintf( __( 'Unique slug/reference for the attribute. Non-ASCII characters use 2–4 bytes of the %d-byte limit.', 'woocommerce' ), $max_bytes ), JSON_HEX_TAG | JSON_HEX_AMP ); if ( false === $js_description ) { // Keep the server-rendered description if encoding fails. $js_description = 'null'; } ?> get_row( $wpdb->prepare( " SELECT attribute_type, attribute_label, attribute_name, attribute_orderby, attribute_public FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_id = %d ", $edit ) ); ?>

' . esc_html__( 'Error: non-existing attribute ID.', 'woocommerce' ) . '

'; } else { if ( self::$edited_attribute_id > 0 ) { echo '

' . esc_html__( 'Attribute updated successfully', 'woocommerce' ) . '

' . esc_html__( 'Back to Attributes', 'woocommerce' ) . '

'; self::$edited_attribute_id = null; } $att_type = $attribute_to_edit->attribute_type; $att_label = format_to_edit( $attribute_to_edit->attribute_label ); $att_name = $attribute_to_edit->attribute_name; $att_orderby = $attribute_to_edit->attribute_orderby; $att_public = $attribute_to_edit->attribute_public; ?>

/>


attribute_name ); $actions = array( 'edit' => '' . esc_html__( 'Edit', 'woocommerce' ) . '', 'delete' => '' . esc_html__( 'Delete', 'woocommerce' ) . '', ); /** * Filters the row actions for a global product attribute taxonomy on the Products > Attributes screen. * * The action values are HTML links. Default actions are escaped before this filter runs, and callbacks * should return safe, escaped HTML. * * @param array $actions Action link HTML keyed by action name. * @param object $tax Attribute taxonomy object. * @param string $taxonomy Full taxonomy name, including the `pa_` prefix. * * @since 11.1.0 */ $actions = (array) apply_filters( 'woocommerce_attribute_taxonomy_row_actions', $actions, $tax, $taxonomy ); $row_actions = array(); foreach ( $actions as $action => $link ) { $row_actions[] = '' . $link . ''; } ?>
attribute_label ); ?> ' . implode( ' | ', $row_actions ) . ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Row action values are link HTML. Default values are escaped above; filtered callbacks should return safe HTML. ?> attribute_name ); ?> attribute_type ) ); ?> attribute_public ? esc_html__( '(Public)', 'woocommerce' ) : ''; ?> attribute_orderby ) { case 'name': esc_html_e( 'Name', 'woocommerce' ); break; case 'name_num': esc_html_e( 'Name (numeric)', 'woocommerce' ); break; case 'id': esc_html_e( 'Term ID', 'woocommerce' ); break; default: esc_html_e( 'Custom ordering', 'woocommerce' ); break; } ?> $taxonomy, 'fields' => 'count', 'hide_empty' => false, ) ); if ( 0 === $total_count ) { echo '–'; } elseif ( $max_terms_to_display > 0 ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'number' => $max_terms_to_display, 'fields' => 'names', 'hide_empty' => false, ) ); $terms_string = implode( ', ', $terms ); if ( $total_count > $max_terms_to_display ) { $remaining = $total_count - $max_terms_to_display; /* translators: 1: Comma-separated terms list, 2: how many terms are hidden */ $terms_string = sprintf( __( '%1$s... (%2$s more)', 'woocommerce' ), $terms_string, $remaining ); } echo esc_html( $terms_string ); } elseif ( 1 === $total_count ) { echo esc_html( __( '1 term', 'woocommerce' ) ); } else { /* translators: %s: Total count of terms available for the attribute */ echo esc_html( sprintf( __( '%s terms', 'woocommerce' ), $total_count ) ); }//end if } else { echo '–
'; }//end if ?>