diff --git a/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php b/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php index 67ee8eb205..c5e34b1e68 100644 --- a/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php +++ b/inc/ThirdParty/Plugins/Ecommerce/WooCommerceSubscriber.php @@ -92,6 +92,9 @@ public static function get_subscribed_events() { $events['wp_head'] = 'show_empty_product_gallery_with_delayJS'; $events['rocket_delay_js_exclusions'] = 'show_notempty_product_gallery_with_delayJS'; + + $events['wp_ajax_woocommerce_product_ordering'] = [ 'disallow_rocket_clean_post', 9 ]; + $events['woocommerce_after_product_ordering'] = [ 'allow_rocket_clean_post' ]; } if ( class_exists( 'WC_API' ) ) { @@ -592,4 +595,39 @@ public function show_notempty_product_gallery_with_delayJS( $exclusions = [] ): return array_merge( $exclusions, $exclusions_gallery ); } + + /** + * Disable post cache clearing during product sorting. + * + * @return void + */ + public function disallow_rocket_clean_post() : void { + $this->event_manager->remove_callback( 'clean_post_cache', 'rocket_clean_post' ); + } + + /** + * Re-enable post cache clearing after product sorting. + * + * @param integer $product_id ID of sorted product. + * @return void + */ + public function allow_rocket_clean_post( int $product_id ) : void { + $urls = []; + $category_list = wc_get_product_category_list( $product_id ); + + if ( preg_match_all( '/]*?\s+)?href=(["\'])(?.*?)\1/i', $category_list, $matches ) ) { + $urls = $matches['urls']; + } + + $shop_page = get_permalink( wc_get_page_id( 'shop' ) ); + + if ( empty( $shop_page ) ) { + $shop_page = home_url( 'shop' ); + } + + $urls[] = $shop_page; + + rocket_clean_files( $urls ); + $this->event_manager->add_callback( 'clean_post_cache', 'rocket_clean_post' ); + } }