From 8da93d36f17362ae4671e22808991356a008f31f Mon Sep 17 00:00:00 2001 From: Kelly Dwan Date: Wed, 6 Feb 2019 17:59:34 -0500 Subject: [PATCH] API: Remove custom `attributes` handling from API (https://github.com/woocommerce/woocommerce-blocks/pull/401) --- .../class-wgpb-products-controller.php | 57 ++------------ .../tests/api/products-attributes.php | 75 +------------------ 2 files changed, 10 insertions(+), 122 deletions(-) diff --git a/plugins/woocommerce-blocks/includes/class-wgpb-products-controller.php b/plugins/woocommerce-blocks/includes/class-wgpb-products-controller.php index eea7cb846df..1f08cc84826 100644 --- a/plugins/woocommerce-blocks/includes/class-wgpb-products-controller.php +++ b/plugins/woocommerce-blocks/includes/class-wgpb-products-controller.php @@ -233,9 +233,7 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller { $orderby = $request->get_param( 'orderby' ); $order = $request->get_param( 'order' ); $cat_operator = $request->get_param( 'cat_operator' ); - $attributes = $request->get_param( 'attributes' ); $attr_operator = $request->get_param( 'attr_operator' ); - $tax_relation = $request->get_param( 'tax_relation' ); $ordering_args = WC()->query->get_catalog_ordering_args( $orderby, $order ); $args['orderby'] = $ordering_args['orderby']; @@ -253,32 +251,14 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller { } } - $tax_query = array(); - if ( $attributes ) { - foreach ( $attributes as $attribute => $attribute_terms ) { - if ( in_array( $attribute, wc_get_attribute_taxonomy_names(), true ) ) { - $tax_query[] = array( - 'taxonomy' => $attribute, - 'field' => 'term_id', - 'terms' => $attribute_terms, - 'operator' => ! $attr_operator ? 'IN' : $attr_operator, - ); + if ( $attr_operator && isset( $args['tax_query'] ) ) { + foreach ( $args['tax_query'] as $i => $tax_query ) { + if ( in_array( $tax_query['taxonomy'], wc_get_attribute_taxonomy_names(), true ) ) { + $args['tax_query'][ $i ]['operator'] = $attr_operator; } } } - // Merge attribute `$tax_query`s into the request's WP_Query args. - if ( ! empty( $tax_query ) ) { - if ( ! empty( $args['tax_query'] ) ) { - $args['tax_query'] = array_merge( $tax_query, $args['tax_query'] ); // WPCS: slow query ok. - } else { - $args['tax_query'] = $tax_query; // WPCS: slow query ok. - } - if ( ! empty( $tax_relation ) && count( $tax_query ) > 1 ) { - $args['tax_query']['relation'] = 'AND' === $tax_relation ? 'AND' : 'OR'; - } - } - return $args; } @@ -309,8 +289,7 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller { /** * Update the collection params. * - * Adds new options for 'orderby', and new parameters 'cat_operator', 'attributes', - * 'attr_operator', and 'tax_relation'. + * Adds new options for 'orderby', and new parameters 'cat_operator', 'attr_operator'. * * @return array */ @@ -327,34 +306,10 @@ class WGPB_Products_Controller extends WC_REST_Products_Controller { $params['attr_operator'] = array( 'description' => __( 'Operator to compare product attribute terms.', 'woo-gutenberg-products-block' ), 'type' => 'string', - 'enum' => array( 'IN', 'AND' ), + 'enum' => array( 'IN', 'NOT IN', 'AND' ), 'sanitize_callback' => 'sanitize_text_field', 'validate_callback' => 'rest_validate_request_arg', ); - $params['tax_relation'] = array( - 'description' => __( 'The logical relationship between each inner taxonomy array when there is more than one.', 'woo-gutenberg-products-block' ), - 'type' => 'string', - 'enum' => array( 'AND', 'OR' ), - 'sanitize_callback' => 'sanitize_text_field', - 'validate_callback' => 'rest_validate_request_arg', - ); - - $attr_properties = array(); - foreach ( wc_get_attribute_taxonomy_names() as $name ) { - $attr_properties[ $name ] = array( - 'type' => 'array', - 'items' => array( 'type' => 'string' ), - ); - } - $params['attributes'] = array( - 'description' => __( 'Map of attributes to selected terms.', 'woo-gutenberg-products-block' ), - 'type' => 'object', - 'validate_callback' => 'rest_validate_request_arg', - ); - if ( ! empty( $attr_properties ) ) { - $params['attributes']['properties'] = $attr_properties; - $params['attributes']['additionalProperties'] = false; - } return $params; } diff --git a/plugins/woocommerce-blocks/tests/api/products-attributes.php b/plugins/woocommerce-blocks/tests/api/products-attributes.php index 3031b5cf59c..af5ea21a28a 100644 --- a/plugins/woocommerce-blocks/tests/api/products-attributes.php +++ b/plugins/woocommerce-blocks/tests/api/products-attributes.php @@ -130,14 +130,11 @@ class WC_Tests_API_Products_By_Attributes_Controller extends WC_REST_Unit_Test_C public function test_get_products_by_multiple_terms_all() { wp_set_current_user( $this->user ); $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); + $request->set_param( 'attribute', 'pa_size' ); $request->set_param( - 'attributes', - array( - 'pa_color' => array( - $this->attr_term_ids['red'], - $this->attr_term_ids['blue'], - ), - ) + 'attribute_term', + // Terms list needs to be a string. + $this->attr_term_ids['medium'] . ',' . $this->attr_term_ids['large'] ); $request->set_param( 'attr_operator', 'AND' ); @@ -146,68 +143,4 @@ class WC_Tests_API_Products_By_Attributes_Controller extends WC_REST_Unit_Test_C $this->assertEquals( 200, $response->get_status() ); $this->assertEquals( 1, count( $response_products ) ); } - - /** - * Test getting products by multiple terms in multiple attributes, matching any. - * - * @since 1.2.0 - */ - public function test_get_products_by_multiple_terms_multiple_attrs_any() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( - 'attributes', - array( - 'pa_color' => array( $this->attr_term_ids['red'] ), - 'pa_size' => array( $this->attr_term_ids['large'] ), - ) - ); - $request->set_param( 'attr_operator', 'IN' ); - $request->set_param( 'tax_relation', 'OR' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $response_products ) ); - } - - /** - * Test getting products by multiple terms in multiple attributes, matching all. - * - * @since 1.2.0 - */ - public function test_get_products_by_multiple_terms_multiple_attrs_all() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( - 'attributes', - array( - 'pa_color' => array( $this->attr_term_ids['blue'] ), - 'pa_size' => array( $this->attr_term_ids['medium'] ), - ) - ); - $request->set_param( 'attr_operator', 'AND' ); - $request->set_param( 'tax_relation', 'AND' ); - - $response = $this->server->dispatch( $request ); - $response_products = $response->get_data(); - $this->assertEquals( 200, $response->get_status() ); - $this->assertEquals( 2, count( $response_products ) ); - } - - /** - * Test getting products by attributes that don't exist. - * - * Note: This test is currently skipped because the API isn't registering the attribute - * properties correctly, and therefor not validating attribute names against "real" attributes. - * - * @since 1.2.0 - */ - public function xtest_get_products_by_fake_attrs() { - wp_set_current_user( $this->user ); - $request = new WP_REST_Request( 'GET', $this->endpoint . '/products' ); - $request->set_param( 'attributes', array( 'pa_fake' => array( 21 ) ) ); - $response = $this->server->dispatch( $request ); - $this->assertEquals( 400, $response->get_status() ); - } }