From 978fa976f79c1df86e51933d6509b10463283044 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 12 Sep 2016 14:37:44 +0100 Subject: [PATCH] [2.6][api] Convert posted names to the API to slugs before save. Fixes #11871 @claudiosmweb can you review the API changes made today - there are a few like this - for inclusion in 2.6 asap. Let me know on slack. --- .../api/class-wc-rest-products-controller.php | 31 ++++++++++++------- includes/wc-formatting-functions.php | 2 +- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/includes/api/class-wc-rest-products-controller.php b/includes/api/class-wc-rest-products-controller.php index 4d626464c69..ddd0ee90edf 100644 --- a/includes/api/class-wc-rest-products-controller.php +++ b/includes/api/class-wc-rest-products-controller.php @@ -1556,15 +1556,20 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller { if ( isset( $_attribute['is_variation'] ) && $_attribute['is_variation'] ) { $_attribute_key = 'attribute_' . sanitize_title( $_attribute['name'] ); $updated_attribute_keys[] = $_attribute_key; + $attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; - if ( isset( $_attribute['is_taxonomy'] ) && $_attribute['is_taxonomy'] ) { - // Don't use wc_clean as it destroys sanitized characters - $_attribute_value = isset( $attribute['option'] ) ? sanitize_title( stripslashes( $attribute['option'] ) ) : ''; - } else { - $_attribute_value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; + if ( ! empty( $_attribute['is_taxonomy'] ) ) { + // If dealing with a taxonomy, we need to get the slug from the name posted to the API. + $term = get_term_by( 'name', $attribute_value, $attribute_name ); + + if ( $term && ! is_wp_error( $term ) ) { + $attribute_value = $term->slug; + } else { + $attribute_value = sanitize_title( $attribute_value ); + } } - update_post_meta( $variation_id, $_attribute_key, $_attribute_value ); + update_post_meta( $variation_id, $_attribute_key, $attribute_value ); } } @@ -1610,14 +1615,16 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller { $_attribute = $attributes[ $attribute_name ]; if ( $_attribute['is_variation'] ) { - $value = ''; + $value = isset( $attribute['option'] ) ? wc_clean( stripslashes( $attribute['option'] ) ) : ''; - if ( isset( $attribute['option'] ) ) { - if ( $_attribute['is_taxonomy'] ) { - // Don't use wc_clean as it destroys sanitized characters. - $value = sanitize_title( trim( stripslashes( $attribute['option'] ) ) ); + if ( ! empty( $_attribute['is_taxonomy'] ) ) { + // If dealing with a taxonomy, we need to get the slug from the name posted to the API. + $term = get_term_by( 'name', $attribute_value, $attribute_name ); + + if ( $term && ! is_wp_error( $term ) ) { + $value = $term->slug; } else { - $value = wc_clean( trim( stripslashes( $attribute['option'] ) ) ); + $value = sanitize_title( $attribute_value ); } } diff --git a/includes/wc-formatting-functions.php b/includes/wc-formatting-functions.php index ef5515ff2a2..1030b961d3f 100644 --- a/includes/wc-formatting-functions.php +++ b/includes/wc-formatting-functions.php @@ -44,7 +44,7 @@ function wc_string_to_array( $string, $delimiter = ',' ) { * @return string */ function wc_sanitize_taxonomy_name( $taxonomy ) { - return apply_filters( 'sanitize_taxonomy_name', sanitize_title( urldecode( $taxonomy ) ), $taxonomy ); + return apply_filters( 'sanitize_taxonomy_name', urldecode( sanitize_title( $taxonomy ) ), $taxonomy ); } /**