From 35d5bf31dda1675983918d727b3127e69abd88db Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 15 Jun 2018 14:26:47 -0300 Subject: [PATCH 1/2] phpcs fixes --- includes/class-wc-query.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/includes/class-wc-query.php b/includes/class-wc-query.php index 35c00aa4a2b..44d52421471 100644 --- a/includes/class-wc-query.php +++ b/includes/class-wc-query.php @@ -176,8 +176,6 @@ class WC_Query { /** * Add query vars. * - * @access public - * * @param array $vars Query vars. * @return array */ @@ -371,7 +369,6 @@ class WC_Query { * * Hooked into wpseo_ hook already, so no need for function_exist. * - * @access public * @return string */ public function wpseo_metakey() { From 7a7130c9d04ed359e85c94d4d5d603714fca678f Mon Sep 17 00:00:00 2001 From: Rodrigo Primo Date: Fri, 15 Jun 2018 14:26:57 -0300 Subject: [PATCH 2/2] Improve WC_Query::get_layered_nav_chosen_attributes() performance This commits improves the performance of the method WC_Query::get_layered_nav_chosen_attributes() when a site has a significant number of product attributes. Instead of looping over all existent product attributes to find which have been passed in the request, the new code loops over all the request parameters and checks which are valid product attributes. On a test site with 2000 product attributes, the old version of WC_Query::get_layered_nav_chosen_attributes() was responsible for 25% of the shop page generation time. With the new version, the amount that this method contributes to the page generation time is negligible. Related #20262 --- includes/class-wc-query.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/includes/class-wc-query.php b/includes/class-wc-query.php index 44d52421471..510fbb90a4f 100644 --- a/includes/class-wc-query.php +++ b/includes/class-wc-query.php @@ -755,21 +755,22 @@ class WC_Query { public static function get_layered_nav_chosen_attributes() { if ( ! is_array( self::$_chosen_attributes ) ) { self::$_chosen_attributes = array(); - $attribute_taxonomies = wc_get_attribute_taxonomies(); - if ( ! empty( $attribute_taxonomies ) ) { - foreach ( $attribute_taxonomies as $tax ) { - $attribute = wc_sanitize_taxonomy_name( $tax->attribute_name ); - $taxonomy = wc_attribute_taxonomy_name( $attribute ); - $filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ 'filter_' . $attribute ] ) ) ) : array(); // WPCS: sanitization ok, input var ok, CSRF ok. + if ( ! empty( $_GET ) ) { // WPCS: input var ok, CSRF ok. + foreach ( $_GET as $key => $value ) { // WPCS: input var ok, CSRF ok. + if ( 0 === strpos( $key, 'filter_' ) ) { + $attribute = wc_sanitize_taxonomy_name( str_replace( 'filter_', '', $key ) ); + $taxonomy = wc_attribute_taxonomy_name( $attribute ); + $filter_terms = ! empty( $value ) ? explode( ',', wc_clean( wp_unslash( $value ) ) ) : array(); - if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) ) { - continue; + if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) || ! wc_attribute_taxonomy_id_by_name( $attribute ) ) { + continue; + } + + $query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. + self::$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding. + self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); } - - $query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ), true ) ? wc_clean( wp_unslash( $_GET[ 'query_type_' . $attribute ] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok. - self::$_chosen_attributes[ $taxonomy ]['terms'] = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding. - self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' ); } } }