Merge pull request #19140 from woocommerce/fix/subcategory-cache

Clear subcategory cache
This commit is contained in:
Mike Jolley 2018-03-16 14:08:58 +00:00 committed by GitHub
commit 6cc5f2e171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 13 deletions

View File

@ -2,16 +2,10 @@
/**
* WC_Cache_Helper class.
*
* @class WC_Cache_Helper
* @version 2.2.0
* @package WooCommerce/Classes
* @category Class
* @author WooThemes
* @package WooCommerce/Classes
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
defined( 'ABSPATH' ) || exit;
/**
* WC_Cache_Helper.
@ -26,6 +20,8 @@ class WC_Cache_Helper {
add_action( 'admin_notices', array( __CLASS__, 'notices' ) );
add_action( 'delete_version_transients', array( __CLASS__, 'delete_version_transients' ) );
add_action( 'wp', array( __CLASS__, 'prevent_caching' ) );
add_action( 'clean_term_cache', array( __CLASS__, 'clean_term_cache' ), 10, 2 );
add_action( 'edit_terms', array( __CLASS__, 'clean_term_cache' ), 10, 2 );
}
/**
@ -93,7 +89,7 @@ class WC_Cache_Helper {
public static function geolocation_ajax_redirect() {
if ( 'geolocation_ajax' === get_option( 'woocommerce_default_customer_address' ) && ! is_checkout() && ! is_cart() && ! is_account_page() && ! is_ajax() && empty( $_POST ) ) { // WPCS: CSRF ok, input var ok.
$location_hash = self::geolocation_ajax_get_location_hash();
$current_hash = isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : ''; // WPCS: sanitization ok, input var ok.
$current_hash = isset( $_GET['v'] ) ? wc_clean( wp_unslash( $_GET['v'] ) ) : ''; // WPCS: sanitization ok, input var ok, CSRF ok.
if ( empty( $current_hash ) || $current_hash !== $location_hash ) {
global $wp;
@ -141,7 +137,10 @@ class WC_Cache_Helper {
if ( false === $transient_value || true === $refresh ) {
self::delete_version_transients( $transient_value );
set_transient( $transient_name, $transient_value = time() );
$transient_value = time();
set_transient( $transient_name, $transient_value );
}
return $transient_value;
}
@ -196,11 +195,40 @@ class WC_Cache_Helper {
if ( $enabled && ! in_array( '_wc_session_', $settings, true ) ) {
?>
<div class="error">
<p><?php echo wp_kses_post( sprintf( __( 'In order for <strong>database caching</strong> to work with WooCommerce you must add %1$s to the "Ignored Query Strings" option in <a href="%2$s">W3 Total Cache settings</a>.', 'woocommerce' ), '<code>_wc_session_</code>', esc_url( admin_url( 'admin.php?page=w3tc_dbcache' ) ) ) ); ?></p>
<p>
<?php
/* translators: 1: key 2: URL */
echo wp_kses_post( sprintf( __( 'In order for <strong>database caching</strong> to work with WooCommerce you must add %1$s to the "Ignored Query Strings" option in <a href="%2$s">W3 Total Cache settings</a>.', 'woocommerce' ), '<code>_wc_session_</code>', esc_url( admin_url( 'admin.php?page=w3tc_dbcache' ) ) ) );
?>
</p>
</div>
<?php
}
}
/**
* Clean term caches added by WooCommerce.
*
* @since 3.3.4
* @param array|int $ids Array of ids or single ID to clear cache for.
* @param string $taxonomy Taxonomy name.
*/
public static function clean_term_cache( $ids, $taxonomy ) {
if ( 'product_cat' === $taxonomy ) {
$ids = is_array( $ids ) ? $ids : array( $ids );
foreach ( $ids as $id ) {
$clear_ids[] = $id;
$clear_ids = array_merge( $clear_ids, get_ancestors( $id, 'product_cat', 'taxonomy' ) );
}
$clear_ids = array_unique( $clear_ids );
foreach ( $clear_ids as $id ) {
wp_cache_delete( 'product-category-hierarchy-' . $id, 'product_cat' );
}
}
}
}
WC_Cache_Helper::init();

View File

@ -1999,7 +1999,7 @@ if ( ! function_exists( 'woocommerce_get_product_subcategories' ) ) {
*/
function woocommerce_get_product_subcategories( $parent_id = 0 ) {
$parent_id = absint( $parent_id );
$product_categories = wp_cache_get( 'product-categories-' . $parent_id, 'product_cat' );
$product_categories = wp_cache_get( 'product-category-hierarchy-' . $parent_id, 'product_cat' );
if ( false === $product_categories ) {
// NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( https://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work.
@ -2011,7 +2011,8 @@ if ( ! function_exists( 'woocommerce_get_product_subcategories' ) ) {
'taxonomy' => 'product_cat',
'pad_counts' => 1,
) ) );
wp_cache_set( 'product-categories-' . $parent_id, $product_categories, 'product_cat' );
wp_cache_set( 'product-category-hierarchy-' . $parent_id, $product_categories, 'product_cat' );
}
if ( apply_filters( 'woocommerce_product_subcategories_hide_empty', true ) ) {