From 803709cb94a72270c995793333e9d16bb1b4850f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Oct 2016 15:57:09 +0100 Subject: [PATCH 01/11] Add related product functions and deprecate those in class. --- .../abstracts/abstract-wc-legacy-product.php | 30 ++++ includes/abstracts/abstract-wc-product.php | 129 ------------------ includes/wc-product-functions.php | 120 ++++++++++++++++ templates/single-product/related.php | 2 +- 4 files changed, 151 insertions(+), 130 deletions(-) diff --git a/includes/abstracts/abstract-wc-legacy-product.php b/includes/abstracts/abstract-wc-legacy-product.php index 3399a05320e..a14b38f4e6d 100644 --- a/includes/abstracts/abstract-wc-legacy-product.php +++ b/includes/abstracts/abstract-wc-legacy-product.php @@ -21,6 +21,36 @@ if ( ! defined( 'ABSPATH' ) ) { */ abstract class WC_Abstract_Legacy_Product extends WC_Data { + /** + * Get and return related products. + * @deprecated 2.7.0 Use wc_get_related_products instead. + */ + public function get_related( $limit = 5 ) { + _deprecated_function( 'WC_Product::get_related', '2.7', 'wc_get_related_products' ); + return wc_get_related_products( $this->get_id(), $limit ); + } + + /** + * Retrieves related product terms. + * @deprecated 2.7.0 Use wc_get_related_terms instead. + */ + protected function get_related_terms( $term ) { + _deprecated_function( 'WC_Product::get_related_terms', '2.7', 'wc_get_related_terms' ); + return array_merge( array( 0 ), wc_get_related_terms( $this->get_id(), $term ) ); + } + + /** + * Builds the related posts query. + * @deprecated 2.7.0 Use wc_get_related_products_query instead. + */ + protected function build_related_query( $cats_array, $tags_array, $exclude_ids, $limit ) { + _deprecated_function( 'WC_Product::build_related_query', '2.7', 'wc_get_related_products_query' ); + return wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit ); + } + + + + /** * The product's type (simple, variable etc). * diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 104e40ce1d1..171ccad9f7a 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -2074,57 +2074,6 @@ class WC_Product extends WC_Abstract_Legacy_Product { return absint( $this->shipping_class_id ); } - /** - * Get and return related products. - * - * Notes: - * - Results are cached in a transient for faster queries. - * - To make results appear random, we query and extra 10 products and shuffle them. - * - To ensure we always have enough results, it will check $limit before returning the cached result, if not recalc. - * - This used to rely on transient version to invalidate cache, but to avoid multiple transients we now just expire daily. - * This means if a related product is edited and no longer related, it won't be removed for 24 hours. Acceptable trade-off for performance. - * - Saving a product will flush caches for that product. - * - * @param int $limit (default: 5) Should be an integer greater than 0. - * @return array Array of post IDs - */ - public function get_related( $limit = 5 ) { - global $wpdb; - - $transient_name = 'wc_related_' . $this->get_id(); - $related_posts = get_transient( $transient_name ); - $limit = $limit > 0 ? $limit : 5; - - // We want to query related posts if they are not cached, or we don't have enough - if ( false === $related_posts || sizeof( $related_posts ) < $limit ) { - // Related products are found from category and tag - $tags_array = $this->get_related_terms( 'product_tag' ); - $cats_array = $this->get_related_terms( 'product_cat' ); - - // Don't bother if none are set - if ( 1 === sizeof( $cats_array ) && 1 === sizeof( $tags_array ) ) { - $related_posts = array(); - } else { - // Sanitize - $exclude_ids = array_map( 'absint', array_merge( array( 0, $this->get_id() ), $this->get_upsells() ) ); - - // Generate query - but query an extra 10 results to give the appearance of random results - $query = $this->build_related_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ); - - // Get the posts - $related_posts = $wpdb->get_col( implode( ' ', $query ) ); - } - - set_transient( $transient_name, $related_posts, DAY_IN_SECONDS ); - } - - // Randomise the results - shuffle( $related_posts ); - - // Limit the returned results - return array_slice( $related_posts, 0, $limit ); - } - /** * Returns a single product attribute. * @@ -2301,84 +2250,6 @@ class WC_Product extends WC_Abstract_Legacy_Product { return sprintf( '%s – %s', $identifier, $this->get_title() ); } - /** - * Retrieves related product terms. - * - * @param string $term - * @return array - */ - protected function get_related_terms( $term ) { - $terms_array = array( 0 ); - - $terms = apply_filters( 'woocommerce_get_related_' . $term . '_terms', wp_get_post_terms( $this->get_id(), $term ), $this->get_id() ); - foreach ( $terms as $term ) { - $terms_array[] = $term->term_id; - } - - return array_map( 'absint', $terms_array ); - } - - /** - * Builds the related posts query. - * - * @param array $cats_array - * @param array $tags_array - * @param array $exclude_ids - * @param int $limit - * @return string - */ - protected function build_related_query( $cats_array, $tags_array, $exclude_ids, $limit ) { - global $wpdb; - - $limit = absint( $limit ); - - $query = array(); - $query['fields'] = "SELECT DISTINCT ID FROM {$wpdb->posts} p"; - $query['join'] = " INNER JOIN {$wpdb->postmeta} pm ON ( pm.post_id = p.ID AND pm.meta_key='_visibility' )"; - $query['join'] .= " INNER JOIN {$wpdb->term_relationships} tr ON (p.ID = tr.object_id)"; - $query['join'] .= " INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)"; - $query['join'] .= " INNER JOIN {$wpdb->terms} t ON (t.term_id = tt.term_id)"; - - if ( get_option( 'woocommerce_hide_out_of_stock_items' ) === 'yes' ) { - $query['join'] .= " INNER JOIN {$wpdb->postmeta} pm2 ON ( pm2.post_id = p.ID AND pm2.meta_key='_stock_status' )"; - } - - $query['where'] = " WHERE 1=1"; - $query['where'] .= " AND p.post_status = 'publish'"; - $query['where'] .= " AND p.post_type = 'product'"; - $query['where'] .= " AND p.ID NOT IN ( " . implode( ',', $exclude_ids ) . " )"; - $query['where'] .= " AND pm.meta_value IN ( 'visible', 'catalog' )"; - - if ( get_option( 'woocommerce_hide_out_of_stock_items' ) === 'yes' ) { - $query['where'] .= " AND pm2.meta_value = 'instock'"; - } - - $relate_by_category = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $this->get_id() ); - $relate_by_tag = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $this->get_id() ); - - if ( $relate_by_category || $relate_by_tag ) { - $query['where'] .= ' AND ('; - - if ( $relate_by_category ) { - $query['where'] .= " ( tt.taxonomy = 'product_cat' AND t.term_id IN ( " . implode( ',', $cats_array ) . " ) ) "; - if ( $relate_by_tag ) { - $query['where'] .= ' OR '; - } - } - - if ( $relate_by_tag ) { - $query['where'] .= " ( tt.taxonomy = 'product_tag' AND t.term_id IN ( " . implode( ',', $tags_array ) . " ) ) "; - } - - $query['where'] .= ')'; - } - - $query['limits'] = " LIMIT {$limit} "; - $query = apply_filters( 'woocommerce_product_related_posts_query', $query, $this->get_id() ); - - return $query; - } - /** * Save taxonomy terms. * diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 0c89e29bb48..132ca39198d 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -751,3 +751,123 @@ function wc_get_product_visibility_options() { 'hidden' => __( 'Hidden', 'woocommerce' ), ) ); } + +/** + * Get relateed products. + * @param integer $product_id + * @param integer $limit + * @param array $exclude_ids + * @return array + */ +function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() ) { + global $wpdb; + + $product_id = absint( $product_id ); + $exclude_ids = array_map( 'absint', array_merge( array( 0, $product_id ), $exclude_ids ) ); + $transient_name = 'wc_related_' . $product_id; + $related_posts = get_transient( $transient_name ); + $limit = $limit > 0 ? $limit : 5; + + // We want to query related posts if they are not cached, or we don't have enough + if ( false === $related_posts || sizeof( $related_posts ) < $limit ) { + // Related products are found from category and tags + if ( apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ) { + $cats_array = wc_get_related_terms( $product_id, 'product_cat' ); + } else { + $cats_array = array(); + } + + if ( apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ) { + $tags_array = wc_get_related_terms( $product_id, 'product_tag' ); + } else { + $tags_array = array(); + } + + // Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related. + if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) { + $related_posts = array(); + } else { + // Generate query - but query an extra 10 results to give the appearance of random results + $query = apply_filters( 'woocommerce_product_related_posts_query', wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id ); + + // Get the posts + $related_posts = $wpdb->get_col( implode( ' ', $query ) ); + } + + set_transient( $transient_name, $related_posts, DAY_IN_SECONDS ); + } + + // Randomise the results + shuffle( $related_posts ); + + // Limit the returned results + return array_slice( $related_posts, 0, $limit ); +} + +/** + * Retrieves related product terms. + * @since 2.7.0 + * @param integer $product_id + * @param string $taxonomy + * @return array + */ +function wc_get_related_terms( $product_id, $taxonomy ) { + $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', wp_get_post_terms( $product_id, $term ), $product_id ); + return array_map( 'absint', wp_list_pluck( $terms, 'term_id' ) ); +} + +/** + * Builds the related posts query. + * + * @param array $cats_array + * @param array $tags_array + * @param array $exclude_ids + * @param int $limit + * @return string + */ +function wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit ) { + global $wpdb; + + $limit = absint( $limit ); + $query = array(); + $query['fields'] = "SELECT DISTINCT ID FROM {$wpdb->posts} p"; + $query['join'] = " INNER JOIN {$wpdb->postmeta} pm ON ( pm.post_id = p.ID AND pm.meta_key='_visibility' )"; + $query['join'] .= " INNER JOIN {$wpdb->term_relationships} tr ON (p.ID = tr.object_id)"; + $query['join'] .= " INNER JOIN {$wpdb->term_taxonomy} tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)"; + $query['join'] .= " INNER JOIN {$wpdb->terms} t ON (t.term_id = tt.term_id)"; + + if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { + $query['join'] .= " INNER JOIN {$wpdb->postmeta} pm2 ON ( pm2.post_id = p.ID AND pm2.meta_key='_stock_status' )"; + } + + $query['where'] = " WHERE 1=1"; + $query['where'] .= " AND p.post_status = 'publish'"; + $query['where'] .= " AND p.post_type = 'product'"; + $query['where'] .= " AND p.ID NOT IN ( " . implode( ',', $exclude_ids ) . " )"; + $query['where'] .= " AND pm.meta_value IN ( 'visible', 'catalog' )"; + + if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { + $query['where'] .= " AND pm2.meta_value = 'instock'"; + } + + if ( $cats_array || $tags_array ) { + $query['where'] .= ' AND ('; + + if ( $cats_array ) { + $query['where'] .= " ( tt.taxonomy = 'product_cat' AND t.term_id IN ( " . implode( ',', $cats_array ) . " ) ) "; + if ( $relate_by_tag ) { + $query['where'] .= ' OR '; + } + } + + if ( $tags_array ) { + $query['where'] .= " ( tt.taxonomy = 'product_tag' AND t.term_id IN ( " . implode( ',', $tags_array ) . " ) ) "; + } + + $query['where'] .= ')'; + } + + $query['limits'] = " LIMIT {$limit} "; + + return $query; +} diff --git a/templates/single-product/related.php b/templates/single-product/related.php index 330f6356030..5d5b159fbd9 100644 --- a/templates/single-product/related.php +++ b/templates/single-product/related.php @@ -26,7 +26,7 @@ if ( empty( $product ) || ! $product->exists() ) { return; } -if ( ! $related = $product->get_related( $posts_per_page ) ) { +if ( ! $related = wc_get_related_products( $product->get_id(), $posts_per_page, $product->get_upsells() ) ) { return; } From 38c18664c9beaa90cb310017c41e3feb2619080b Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Oct 2016 16:00:43 +0100 Subject: [PATCH 02/11] No need to exclude ID --- templates/single-product/related.php | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/single-product/related.php b/templates/single-product/related.php index 5d5b159fbd9..b63eed0778d 100644 --- a/templates/single-product/related.php +++ b/templates/single-product/related.php @@ -37,7 +37,6 @@ $args = apply_filters( 'woocommerce_related_products_args', array( 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'post__in' => $related, - 'post__not_in' => array( $product->id ), ) ); $products = new WP_Query( $args ); From 92484d140c40261b36e89c18ad466eed2315f62a Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 17 Oct 2016 13:23:49 -0200 Subject: [PATCH 03/11] Fixed coding standards and improved the docblocks --- includes/wc-product-functions.php | 50 ++++++++++++++++++------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 132ca39198d..4c66f464603 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -754,9 +754,11 @@ function wc_get_product_visibility_options() { /** * Get relateed products. - * @param integer $product_id - * @param integer $limit - * @param array $exclude_ids + * + * @since 2.7.0 + * @param int $product_id Product ID + * @param int $limit Limit of results. + * @param array $exclude_ids Exclude IDs from the results. * @return array */ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array() ) { @@ -768,9 +770,9 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( $related_posts = get_transient( $transient_name ); $limit = $limit > 0 ? $limit : 5; - // We want to query related posts if they are not cached, or we don't have enough + // We want to query related posts if they are not cached, or we don't have enough. if ( false === $related_posts || sizeof( $related_posts ) < $limit ) { - // Related products are found from category and tags + // Related products are found from category and tags. if ( apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ) { $cats_array = wc_get_related_terms( $product_id, 'product_cat' ); } else { @@ -787,47 +789,55 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) { $related_posts = array(); } else { - // Generate query - but query an extra 10 results to give the appearance of random results + // Generate query - but query an extra 10 results to give the appearance of random results. $query = apply_filters( 'woocommerce_product_related_posts_query', wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id ); - // Get the posts + // Get the posts. $related_posts = $wpdb->get_col( implode( ' ', $query ) ); } set_transient( $transient_name, $related_posts, DAY_IN_SECONDS ); } - // Randomise the results + // Randomise the results. shuffle( $related_posts ); - // Limit the returned results + // Limit the returned results. return array_slice( $related_posts, 0, $limit ); } /** * Retrieves related product terms. - * @since 2.7.0 - * @param integer $product_id - * @param string $taxonomy + * + * @since 2.7.0 + * @param int $product_id Product ID. + * @param string $taxonomy Taxonomy slug. * @return array */ function wc_get_related_terms( $product_id, $taxonomy ) { $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', wp_get_post_terms( $product_id, $term ), $product_id ); + return array_map( 'absint', wp_list_pluck( $terms, 'term_id' ) ); } /** * Builds the related posts query. * - * @param array $cats_array - * @param array $tags_array - * @param array $exclude_ids - * @param int $limit + * @since 2.7.0 + * @param array $cats_array List of categories IDs. + * @param array $tags_array List of tags IDs. + * @param array $exclude_ids Excluded IDs. + * @param int $limit Limit of results. * @return string */ function wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit ) { global $wpdb; + // Arrays to string. + $exclude_ids = implode( ',', $exclude_ids ); + $cats_array = implode( ',', $cats_array ); + $tags_array = implode( ',', $tags_array ); + $limit = absint( $limit ); $query = array(); $query['fields'] = "SELECT DISTINCT ID FROM {$wpdb->posts} p"; @@ -840,10 +850,10 @@ function wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $query['join'] .= " INNER JOIN {$wpdb->postmeta} pm2 ON ( pm2.post_id = p.ID AND pm2.meta_key='_stock_status' )"; } - $query['where'] = " WHERE 1=1"; + $query['where'] = ' WHERE 1=1'; $query['where'] .= " AND p.post_status = 'publish'"; $query['where'] .= " AND p.post_type = 'product'"; - $query['where'] .= " AND p.ID NOT IN ( " . implode( ',', $exclude_ids ) . " )"; + $query['where'] .= " AND p.ID NOT IN ( {$exclude_ids} )"; $query['where'] .= " AND pm.meta_value IN ( 'visible', 'catalog' )"; if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { @@ -854,14 +864,14 @@ function wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $query['where'] .= ' AND ('; if ( $cats_array ) { - $query['where'] .= " ( tt.taxonomy = 'product_cat' AND t.term_id IN ( " . implode( ',', $cats_array ) . " ) ) "; + $query['where'] .= " ( tt.taxonomy = 'product_cat' AND t.term_id IN ( {$cats_array} ) ) "; if ( $relate_by_tag ) { $query['where'] .= ' OR '; } } if ( $tags_array ) { - $query['where'] .= " ( tt.taxonomy = 'product_tag' AND t.term_id IN ( " . implode( ',', $tags_array ) . " ) ) "; + $query['where'] .= " ( tt.taxonomy = 'product_tag' AND t.term_id IN ( {$tags_array} ) ) "; } $query['where'] .= ')'; From c3b61359375956a7ebb7d9dafad59dc994a28e5e Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 17 Oct 2016 13:28:52 -0200 Subject: [PATCH 04/11] Get cached terms from wc_get_related_terms() --- includes/wc-product-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 4c66f464603..9b787fceeb7 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -815,7 +815,7 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( * @return array */ function wc_get_related_terms( $product_id, $taxonomy ) { - $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', wp_get_post_terms( $product_id, $term ), $product_id ); + $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', get_the_terms( $product_id, $term ), $product_id ); return array_map( 'absint', wp_list_pluck( $terms, 'term_id' ) ); } From aad06f6642bafcd1d9f4d4c33162de27c9278d45 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 17 Oct 2016 13:29:12 -0200 Subject: [PATCH 05/11] Fixed wrong variable in wc_get_related_terms --- includes/wc-product-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 9b787fceeb7..d9a868a47ed 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -815,7 +815,7 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( * @return array */ function wc_get_related_terms( $product_id, $taxonomy ) { - $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', get_the_terms( $product_id, $term ), $product_id ); + $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', get_the_terms( $product_id, $taxonomy ), $product_id ); return array_map( 'absint', wp_list_pluck( $terms, 'term_id' ) ); } From f42d85345308d21c81046ed8209fd1634fe13c92 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 17 Oct 2016 13:31:49 -0200 Subject: [PATCH 06/11] Use count() instead of sizeof() --- includes/wc-product-functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index d9a868a47ed..70d16c43294 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -756,7 +756,7 @@ function wc_get_product_visibility_options() { * Get relateed products. * * @since 2.7.0 - * @param int $product_id Product ID + * @param int $product_id Product ID. * @param int $limit Limit of results. * @param array $exclude_ids Exclude IDs from the results. * @return array @@ -771,7 +771,7 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( $limit = $limit > 0 ? $limit : 5; // We want to query related posts if they are not cached, or we don't have enough. - if ( false === $related_posts || sizeof( $related_posts ) < $limit ) { + if ( false === $related_posts || count( $related_posts ) < $limit ) { // Related products are found from category and tags. if ( apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ) { $cats_array = wc_get_related_terms( $product_id, 'product_cat' ); From 6bf077a367dc7d3d326e6e3acee9966e8de085e0 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Oct 2016 17:04:24 +0100 Subject: [PATCH 07/11] Sanitize ids later --- includes/wc-product-functions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 70d16c43294..bd115204ff2 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -765,7 +765,7 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( global $wpdb; $product_id = absint( $product_id ); - $exclude_ids = array_map( 'absint', array_merge( array( 0, $product_id ), $exclude_ids ) ); + $exclude_ids = array_merge( array( 0, $product_id ), $exclude_ids ); $transient_name = 'wc_related_' . $product_id; $related_posts = get_transient( $transient_name ); $limit = $limit > 0 ? $limit : 5; @@ -834,9 +834,9 @@ function wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, global $wpdb; // Arrays to string. - $exclude_ids = implode( ',', $exclude_ids ); - $cats_array = implode( ',', $cats_array ); - $tags_array = implode( ',', $tags_array ); + $exclude_ids = implode( ',', array_map( 'absint', $exclude_ids ) ); + $cats_array = implode( ',', array_map( 'absint', $cats_array ) ); + $tags_array = implode( ',', array_map( 'absint', $tags_array ) ); $limit = absint( $limit ); $query = array(); From e6e600ba8a4b6787cd1fd96e80b2d7159db7fe0e Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Oct 2016 17:07:19 +0100 Subject: [PATCH 08/11] Remove unneeded comments --- includes/wc-product-functions.php | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index bd115204ff2..3e6e477801d 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -753,7 +753,7 @@ function wc_get_product_visibility_options() { } /** - * Get relateed products. + * Get related products based on product category and tags. * * @since 2.7.0 * @param int $product_id Product ID. @@ -772,37 +772,22 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( // We want to query related posts if they are not cached, or we don't have enough. if ( false === $related_posts || count( $related_posts ) < $limit ) { - // Related products are found from category and tags. - if ( apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ) { - $cats_array = wc_get_related_terms( $product_id, 'product_cat' ); - } else { - $cats_array = array(); - } - - if ( apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ) { - $tags_array = wc_get_related_terms( $product_id, 'product_tag' ); - } else { - $tags_array = array(); - } + $cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? wc_get_related_terms( $product_id, 'product_cat' ) : array(); + $tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? wc_get_related_terms( $product_id, 'product_tag' ) : array(); // Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related. if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) { $related_posts = array(); } else { - // Generate query - but query an extra 10 results to give the appearance of random results. - $query = apply_filters( 'woocommerce_product_related_posts_query', wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id ); - - // Get the posts. - $related_posts = $wpdb->get_col( implode( ' ', $query ) ); + // Generate query - but query an extra 10 results to give the appearance of random results when later shuffled. + $related_posts = $wpdb->get_col( implode( ' ', apply_filters( 'woocommerce_product_related_posts_query', wc_get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id ) ) ); } set_transient( $transient_name, $related_posts, DAY_IN_SECONDS ); } - // Randomise the results. shuffle( $related_posts ); - // Limit the returned results. return array_slice( $related_posts, 0, $limit ); } From 50f21276fcdfd7ab573e70fd38e52ad347aa35bb Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Oct 2016 17:18:57 +0100 Subject: [PATCH 09/11] wc_get_product_term_ids instead of related wording and use in other places. get_the_terms is used here and also handles caching, something wp_get_post_terms does not. --- includes/abstracts/abstract-wc-legacy-product.php | 6 +++--- includes/wc-product-functions.php | 14 ++++++-------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/includes/abstracts/abstract-wc-legacy-product.php b/includes/abstracts/abstract-wc-legacy-product.php index a14b38f4e6d..b04e2ab2a26 100644 --- a/includes/abstracts/abstract-wc-legacy-product.php +++ b/includes/abstracts/abstract-wc-legacy-product.php @@ -32,11 +32,11 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data { /** * Retrieves related product terms. - * @deprecated 2.7.0 Use wc_get_related_terms instead. + * @deprecated 2.7.0 Use wc_get_product_term_ids instead. */ protected function get_related_terms( $term ) { - _deprecated_function( 'WC_Product::get_related_terms', '2.7', 'wc_get_related_terms' ); - return array_merge( array( 0 ), wc_get_related_terms( $this->get_id(), $term ) ); + _deprecated_function( 'WC_Product::get_related_terms', '2.7', 'wc_get_product_term_ids' ); + return array_merge( array( 0 ), wc_get_product_term_ids( $this->get_id(), $term ) ); } /** diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 3e6e477801d..8d4f7cde9dc 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -700,7 +700,7 @@ function wc_get_product_variation_attributes( $variation_id ) { * @return array */ function wc_get_product_cat_ids( $product_id ) { - $product_cats = wp_get_post_terms( $product_id, 'product_cat', array( "fields" => "ids" ) ); + $product_cats = wc_get_product_term_ids( $product_id, 'product_cat' ); foreach ( $product_cats as $product_cat ) { $product_cats = array_merge( $product_cats, get_ancestors( $product_cat, 'product_cat' ) ); @@ -772,8 +772,8 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( // We want to query related posts if they are not cached, or we don't have enough. if ( false === $related_posts || count( $related_posts ) < $limit ) { - $cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? wc_get_related_terms( $product_id, 'product_cat' ) : array(); - $tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? wc_get_related_terms( $product_id, 'product_tag' ) : array(); + $cats_array = apply_filters( 'woocommerce_product_related_posts_relate_by_category', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_cat_terms', wc_get_product_term_ids( $product_id, 'product_cat' ), $product_id ) : array(); + $tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array(); // Don't bother if none are set, unless woocommerce_product_related_posts_force_display is set to true in which case all products are related. if ( empty( $cats_array ) && empty( $tags_array ) && ! apply_filters( 'woocommerce_product_related_posts_force_display', false, $product_id ) ) { @@ -792,17 +792,15 @@ function wc_get_related_products( $product_id, $limit = 5, $exclude_ids = array( } /** - * Retrieves related product terms. + * Retrieves product term ids for a taxonomy. * * @since 2.7.0 * @param int $product_id Product ID. * @param string $taxonomy Taxonomy slug. * @return array */ -function wc_get_related_terms( $product_id, $taxonomy ) { - $terms = apply_filters( 'woocommerce_get_related_' . $taxonomy . '_terms', get_the_terms( $product_id, $taxonomy ), $product_id ); - - return array_map( 'absint', wp_list_pluck( $terms, 'term_id' ) ); +function wc_get_product_term_ids( $product_id, $taxonomy ) { + return wp_list_pluck( get_the_terms( $product_id, $taxonomy ), 'term_id' ); } /** From 560513cde74fb53005037486a92e4620af72ca3b Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 18 Oct 2016 11:19:35 +0100 Subject: [PATCH 10/11] Bump template version --- templates/single-product/related.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/single-product/related.php b/templates/single-product/related.php index b63eed0778d..a6fa13f48ff 100644 --- a/templates/single-product/related.php +++ b/templates/single-product/related.php @@ -13,7 +13,7 @@ * @see https://docs.woocommerce.com/document/template-structure/ * @author WooThemes * @package WooCommerce/Templates - * @version 1.6.4 + * @version 2.7.0 */ if ( ! defined( 'ABSPATH' ) ) { From 95de56d402a624af9c20916042e05dcf2455fe71 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 18 Oct 2016 19:08:40 +0100 Subject: [PATCH 11/11] Use get_upsell_ids --- templates/single-product/related.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/single-product/related.php b/templates/single-product/related.php index a6fa13f48ff..d3570b888e4 100644 --- a/templates/single-product/related.php +++ b/templates/single-product/related.php @@ -26,7 +26,7 @@ if ( empty( $product ) || ! $product->exists() ) { return; } -if ( ! $related = wc_get_related_products( $product->get_id(), $posts_per_page, $product->get_upsells() ) ) { +if ( ! $related = wc_get_related_products( $product->get_id(), $posts_per_page, $product->get_upsell_ids() ) ) { return; }