From 457c675927a4c9fbf5bf8374ed4cc1dac1f240a8 Mon Sep 17 00:00:00 2001 From: mxkae Date: Wed, 18 Dec 2024 00:10:12 +0800 Subject: [PATCH 1/5] fix css styles displaying on shop page --- src/css-optimize.php | 13 ++++++++++--- src/kses.php | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/css-optimize.php b/src/css-optimize.php index 82e1620e7..6c324135a 100644 --- a/src/css-optimize.php +++ b/src/css-optimize.php @@ -209,12 +209,19 @@ public static function parse_block_style( $block, &$style_arr ) { * @return void */ public function load_cached_css_for_post() { - // DEV NOTE: If we'll also do this for wp_template and + // DEV NOTE #1: If we'll also do this for wp_template and // wp_template_part then we might need to use the actions: // render_block_core_template_part_post and // render_block_core_template_part_file - if ( is_singular() && ! is_preview() && ! is_attachment() ) { - $post_id = get_the_ID(); + // DEV NOTE #2: Check for WooCommerce Shop Page as well + if ( ( is_singular() && ! is_preview() && ! is_attachment() ) || ( function_exists('is_shop' ) && is_shop() ) ) { + if ( function_exists('is_shop' ) && is_shop() ) { + // use wc_get_page_id() instead of get_the_ID() because + // the latter returns the product page ID instead of the shop page ID + $post_id = wc_get_page_id( 'shop' ); + } else { + $post_id = get_the_ID(); + } $this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true ); if ( ! empty( $this->optimized_css ) ) { diff --git a/src/kses.php b/src/kses.php index 69106ac50..461fcdeaf 100644 --- a/src/kses.php +++ b/src/kses.php @@ -8,6 +8,29 @@ exit; } +/** + * In WooCommerce Shop page, ', '', $content ); + } + + } + return $content; + } + + add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3); +} + if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) { /** From aac312bcff37ef692fa4d4be77093acf099fa45d Mon Sep 17 00:00:00 2001 From: mxkae Date: Wed, 18 Dec 2024 00:17:21 +0800 Subject: [PATCH 2/5] move condition --- src/kses.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kses.php b/src/kses.php index 461fcdeaf..013a96fb6 100644 --- a/src/kses.php +++ b/src/kses.php @@ -12,11 +12,11 @@ * In WooCommerce Shop page, ', '', $content ); + } + + return $content; + } + + function is_woocommerce_shop_page() { + // only add filter when on the WooCommerce Shop page + if ( function_exists('is_shop' ) && is_shop() ) { + add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3); + } + + } + + add_action( 'wp', 'is_woocommerce_shop_page' ); +} + +if ( ! function_exists( 'stackable_check_if_woocommerce_shop' ) ) { + + function stackable_check_if_woocommerce_shop( $optimize_css ) { + // Load cached CSS for the WooCommerce Shop page + // is_singular() returns false when on the Shop page so we need to use is_shop() + return $optimize_css || ( function_exists('is_shop' ) && is_shop() ); + } + + add_filter( 'stackable/load_cached_css_for_post', 'stackable_check_if_woocommerce_shop' ); +} + +if ( ! function_exists( 'stackable_get_woocommerce_shop_page_id' ) ) { + + function stackable_get_woocommerce_shop_page_id( $post_id ) { + // use wc_get_page_id() to retrieve the page ID of the Shop page + // do this because get_the_ID() returns the product page ID when on the Shop page + if ( function_exists('is_shop' ) && is_shop() ) { + return wc_get_page_id( 'shop' ); + } + return $post_id; + } + + add_filter( 'stackable/get_post_id_for_cached_css', 'stackable_get_woocommerce_shop_page_id' ); + +} diff --git a/src/css-optimize.php b/src/css-optimize.php index 6c324135a..32500de4e 100644 --- a/src/css-optimize.php +++ b/src/css-optimize.php @@ -213,15 +213,11 @@ public function load_cached_css_for_post() { // wp_template_part then we might need to use the actions: // render_block_core_template_part_post and // render_block_core_template_part_file - // DEV NOTE #2: Check for WooCommerce Shop Page as well - if ( ( is_singular() && ! is_preview() && ! is_attachment() ) || ( function_exists('is_shop' ) && is_shop() ) ) { - if ( function_exists('is_shop' ) && is_shop() ) { - // use wc_get_page_id() instead of get_the_ID() because - // the latter returns the product page ID instead of the shop page ID - $post_id = wc_get_page_id( 'shop' ); - } else { - $post_id = get_the_ID(); - } + $optimize_css = is_singular() && ! is_preview() && ! is_attachment(); + $optimize_css = apply_filters( 'stackable/load_cached_css_for_post', $optimize_css ); + if ( $optimize_css ) { + $post_id = apply_filters( 'stackable/get_post_id_for_cached_css', get_the_ID() ); + $this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true ); if ( ! empty( $this->optimized_css ) ) { diff --git a/src/kses.php b/src/kses.php index 013a96fb6..69106ac50 100644 --- a/src/kses.php +++ b/src/kses.php @@ -8,29 +8,6 @@ exit; } -/** - * In WooCommerce Shop page, ', '', $content ); - } - - } - return $content; - } - - add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3); -} - if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) { /** From d5392330ec67358a8cb2a6755bfa0a3789719e1c Mon Sep 17 00:00:00 2001 From: mxkae Date: Wed, 18 Dec 2024 19:36:41 +0800 Subject: [PATCH 4/5] minor change --- src/css-optimize.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/css-optimize.php b/src/css-optimize.php index 32500de4e..c4ff1b3b7 100644 --- a/src/css-optimize.php +++ b/src/css-optimize.php @@ -209,7 +209,7 @@ public static function parse_block_style( $block, &$style_arr ) { * @return void */ public function load_cached_css_for_post() { - // DEV NOTE #1: If we'll also do this for wp_template and + // DEV NOTE: If we'll also do this for wp_template and // wp_template_part then we might need to use the actions: // render_block_core_template_part_post and // render_block_core_template_part_file @@ -217,7 +217,6 @@ public function load_cached_css_for_post() { $optimize_css = apply_filters( 'stackable/load_cached_css_for_post', $optimize_css ); if ( $optimize_css ) { $post_id = apply_filters( 'stackable/get_post_id_for_cached_css', get_the_ID() ); - $this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true ); if ( ! empty( $this->optimized_css ) ) { From e34f8295da8c6ec1259a8f64dfa6bcfbe5a0d688 Mon Sep 17 00:00:00 2001 From: mxkae Date: Wed, 18 Dec 2024 19:54:56 +0800 Subject: [PATCH 5/5] minor change --- src/compatibility/woocommerce.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compatibility/woocommerce.php b/src/compatibility/woocommerce.php index 3d77099d5..47dfe27f0 100644 --- a/src/compatibility/woocommerce.php +++ b/src/compatibility/woocommerce.php @@ -30,7 +30,7 @@ function is_woocommerce_shop_page() { } - add_action( 'wp', 'is_woocommerce_shop_page' ); + add_action( 'woocommerce_before_main_content', 'is_woocommerce_shop_page' ); } if ( ! function_exists( 'stackable_check_if_woocommerce_shop' ) ) {