From b0f2ca5631c1d1cb0461725713edba593380c5d8 Mon Sep 17 00:00:00 2001 From: Manish Menaria Date: Fri, 14 Apr 2023 15:10:06 +0530 Subject: [PATCH] Fix: tax_query should be calculated only if __woocommerceAttributes is valid This commit refactors the update_rest_query method in the ProductQuery.php file to improve code readability. It simplifies conditional expressions by introducing variables for clarity, and uses ternary operators to streamline the logic. --- src/BlockTypes/ProductQuery.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 7013de7b61e..6420019e97e 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -135,16 +135,19 @@ private function merge_tax_queries( ...$queries ) { * @param array $args Query args. * @param WP_REST_Request $request Request. */ - public function update_rest_query( $args, $request ) { - $orderby = $request->get_param( 'orderby' ); - $woo_attributes = $request->get_param( '__woocommerceAttributes' ); - $woo_stock_status = $request->get_param( '__woocommerceStockStatus' ); - $on_sale_query = $request->get_param( '__woocommerceOnSale' ) === 'true' ? $this->get_on_sale_products_query() : array(); - $orderby_query = isset( $orderby ) ? $this->get_custom_orderby_query( $orderby ) : array(); - $attributes_query = is_array( $woo_attributes ) ? $this->get_product_attributes_query( $woo_attributes ) : array(); - $stock_query = is_array( $woo_stock_status ) ? $this->get_stock_status_query( $woo_stock_status ) : array(); - $visibility_query = $this->get_product_visibility_query( $stock_query ); - $tax_query = $this->merge_tax_queries( $attributes_query, $visibility_query ); + public function update_rest_query( $args, $request ): array { + $woo_attributes = $request->get_param( '__woocommerceAttributes' ); + $is_valid_attributes = is_array( $woo_attributes ); + $orderby = $request->get_param( 'orderby' ); + $woo_stock_status = $request->get_param( '__woocommerceStockStatus' ); + $on_sale = $request->get_param( '__woocommerceOnSale' ) === 'true'; + + $on_sale_query = $on_sale ? $this->get_on_sale_products_query() : []; + $orderby_query = $orderby ? $this->get_custom_orderby_query( $orderby ) : []; + $attributes_query = $is_valid_attributes ? $this->get_product_attributes_query( $woo_attributes ) : []; + $stock_query = is_array( $woo_stock_status ) ? $this->get_stock_status_query( $woo_stock_status ) : []; + $visibility_query = is_array( $woo_stock_status ) ? $this->get_product_visibility_query( $stock_query ) : []; + $tax_query = $is_valid_attributes ? $this->merge_tax_queries( $attributes_query, $visibility_query ) : []; return array_merge( $args, $on_sale_query, $orderby_query, $stock_query, $tax_query ); }