( $page_id <= 0 || (int) get_option( 'page_on_front' ) === $page_id ) {
return $urls;
}
if ( 'publish' !== get_post_status( $page_id ) ) {
return $urls;
}
if ( $wp_rewrite->use_trailing_slashes ) {
$pattern = "?$pattern";
}
$urls = get_rocket_i18n_translated_post_urls( $page_id, $post_type, $pattern );
return $urls;
}
/**
* Automatically cache v query string when WC geolocation with cache compatibility option is active
*
* @since 2.8.6
* @author Rémy Perona
*
* @param array $query_strings list of query strings to cache.
* @return array Updated list of query strings to cache
*/
public function cache_geolocation_query_string( $query_strings ) {
if ( 'geolocation_ajax' !== get_option( 'woocommerce_default_customer_address' ) ) {
return $query_strings;
}
$query_strings[] = 'v';
return $query_strings;
}
/**
* Returns WooCommerce API endpoint
*
* @since 3.1
* @author Remy Perona
*
* @return string
*/
private function get_wc_api_endpoint() {
return home_url( '/wc-api/v(.*)' );
}
/**
* Exclude WooCommerce REST API URL from cache
*
* @since 2.6.5
*
* @param array $urls URLs to exclude from cache.
* @return array Updated list of URLs to exclude from cache
*/
public function exclude_wc_rest_api( $urls ) {
/**
* By default, don't cache the WooCommerce REST API.
*
* @since 2.6.5
*
* @param bool false will force to cache the WooCommerce REST API
*/
if ( apply_filters( 'rocket_cache_reject_wc_rest_api', true ) ) {
$urls[] = rocket_clean_exclude_file( $this->get_wc_api_endpoint() );
}
return $urls;
}
/**
* Serves the empty cart cache
*
* @since 3.1
* @author Remy Perona
*
* @return void
*/
public function serve_cache_empty_cart() {
if ( ! $this->is_get_refreshed_fragments() || rocket_bypass() ) {
return;
}
$cart = $this->get_cache_empty_cart();
if ( false !== $cart ) {
@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
echo $cart; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Dynamic content is properly escaped in the view.
die();
}
}
/**
* Creates the empty cart cache
*
* @since 3.1
* @author Remy Perona
*
* @return void
*/
public function cache_empty_cart() {
if ( ! $this->is_get_refreshed_fragments() || rocket_bypass() ) {
return;
}
$cart = $this->get_cache_empty_cart();
if ( false !== $cart ) {
return;
}
ob_start( [ $this, 'save_cache_empty_cart' ] );
}
/**
* Gets the empty cart cache
*
* @since 3.1
* @author Remy Perona
*
* @return string
*/
private function get_cache_empty_cart() {
$lang = rocket_get_current_language();
if ( $lang ) {
return get_transient( 'rocket_get_refreshed_fragments_cache_' . $lang );
}
return get_transient( 'rocket_get_refreshed_fragments_cache' );
}
/**
* Saves the empty cart JSON in a transient
*
* @since 3.1
* @author Remy Perona
*
* @param string $content Current buffer content.
* @return string
*/
private function save_cache_empty_cart( $content ) {
$lang = rocket_get_current_language();
if ( $lang ) {
set_transient( 'rocket_get_refreshed_fragments_cache_' . $lang, $content, 7 * DAY_IN_SECONDS );
return $content;
}
set_transient( 'rocket_get_refreshed_fragments_cache', $content, 7 * DAY_IN_SECONDS );
return $content;
}
/**
* Checks if the request is for get_refreshed_fragments and the cart is empty
*
* @since 3.1
* @author Remy Perona
*
* @return boolean
*/
private function is_get_refreshed_fragments() {
if ( ! isset( $_GET['wc-ajax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return false;
}
if ( 'get_refreshed_fragments' !== $_GET['wc-ajax'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
return false;
}
if ( ! empty( $_COOKIE['woocommerce_cart_hash'] ) ) {
return false;
}
if ( ! empty( $_COOKIE['woocommerce_items_in_cart'] ) ) {
return false;
}
return true;
}
/**
* Deletes the empty cart cache
*
* @since 3.1
* @author Remy Perona
*
* @return void
*/
public function delete_cache_empty_cart() {
$langs = get_rocket_i18n_code();
if ( $langs ) {
foreach ( $langs as $lang ) {
delete_transient( 'rocket_get_refreshed_fragments_cache_' . $lang );
}
}
delete_transient( 'rocket_get_refreshed_fragments_cache' );
}
/**
* Excludes WC product attributes taxonomies from CPCSS generation
*
* @since 3.3.5
* @author Remy Perona
*
* @param array $excluded_taxonomies Taxonomies excluded from CPCSS generation.
* @return array
*/
public function exclude_product_attributes_cpcss( $excluded_taxonomies ) {
if ( ! function_exists( 'wc_get_attribute_taxonomy_names' ) ) {
return $excluded_taxonomies;
}
return array_merge( $excluded_taxonomies, wc_get_attribute_taxonomy_names() );
}
/**
* Exclude product_shipping_class taxonomy from post purge
*
* @since 3.9.1
*
* @param array $excluded_taxonomies Array of excluded taxonomies names.
*
* @return array
*/
public function exclude_product_shipping_taxonomy( $excluded_taxonomies ) {
$excluded_taxonomies[] = 'product_shipping_class';
return $excluded_taxonomies;
}
/**
* Check if current product page has images in gallery.
*
* @since 3.9.1
*
* @return bool
*/
private function product_has_gallery_images() {
$product = wc_get_product( get_the_ID() );
if ( empty( $product ) ) {
return false;
}
return ! empty( $product->get_gallery_image_ids() );
}
/**
* Show product gallery main image directly when delay JS is enabled.
*
* @since 3.9.1
*/
public function show_empty_product_gallery_with_delayJS() {
if ( ! $this->delayjs_html->is_allowed() ) {
return;
}
if ( ! is_product() ) {
return;
}
if ( $this->product_has_gallery_images() ) {
return;
}
echo '';
}
/**
* Exclude some JS files from delay JS when product gallery has images.
*
* @since 3.9.1
*
* @param array $exclusions Exclusions array.
*
* @return array
*/
public function show_notempty_product_gallery_with_delayJS( $exclusions = [] ): array {
global $wp_version;
if ( ! is_array( $exclusions ) ) {
$exclusions = (array) $exclusions;
}
if ( ! $this->delayjs_html->is_allowed() ) {
return $exclusions;
}
if ( ! is_product() ) {
return $exclusions;
}
if ( ! $this->product_has_gallery_images() ) {
return $exclusions;
}
$exclusions_gallery = [
'/jquery-?[0-9.]*(.min|.slim|.slim.min)?.js',
'/woocommerce/assets/js/zoom/jquery.zoom(.min)?.js',
'/woocommerce/assets/js/photoswipe/',
'/woocommerce/assets/js/flexslider/jquery.flexslider(.min)?.js',
'/woocommerce/assets/js/frontend/single-product(.min)?.js',
];
if (
isset( $wp_version )
&&
version_compare( $wp_version, '5.7', '<' )
) {
$exclusions_gallery[] = '/jquery-migrate(.min)?.js';
}
/**
* Filters the JS files excluded from delay JS when WC product gallery has images.
*
* @since 3.10.2
*
* @param array $exclusions_gallery Array of excluded filepaths.
*/
$exclusions_gallery = apply_filters( 'rocket_wc_product_gallery_delay_js_exclusions', $exclusions_gallery );
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' );
}
}