Skip to content

Commit 7a6d8a2

Browse files
authored
Fix (WooCommerce): remove css styles displayed on shop page (#3381)
* fix css styles displaying on shop page * move condition * move fix to separate file * minor change * minor change
1 parent e0b1d5c commit 7a6d8a2

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

src/compatibility/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
}
77

88
require_once( plugin_dir_path( __FILE__ ) . './neve/index.php' );
9+
require_once( plugin_dir_path( __FILE__ ) . './woocommerce.php' );

src/compatibility/woocommerce.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
// Exit if accessed directly.
4+
if ( ! defined( 'ABSPATH' ) ) {
5+
exit;
6+
}
7+
8+
/**
9+
* In WooCommerce Shop page, <style> tags are stripped out and the CSS styles are displayed in the frontend.
10+
* This function removes the <style> tags and CSS styles before they are stripped out.
11+
*/
12+
if ( ! function_exists( 'stackable_pre_kses_woocomerce_shop' ) ) {
13+
14+
function stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
15+
$optimized_css = get_post_meta( wc_get_page_id( 'shop' ), 'stackable_optimized_css', true );
16+
17+
// remove CSS before kses strips out <style> tags
18+
if ( ! empty( $optimized_css ) ) {
19+
$content = str_replace( '<style>' . $optimized_css . '</style>', '', $content );
20+
}
21+
22+
return $content;
23+
}
24+
25+
function is_woocommerce_shop_page() {
26+
// only add filter when on the WooCommerce Shop page
27+
if ( function_exists('is_shop' ) && is_shop() ) {
28+
add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3);
29+
}
30+
31+
}
32+
33+
add_action( 'woocommerce_before_main_content', 'is_woocommerce_shop_page' );
34+
}
35+
36+
if ( ! function_exists( 'stackable_check_if_woocommerce_shop' ) ) {
37+
38+
function stackable_check_if_woocommerce_shop( $optimize_css ) {
39+
// Load cached CSS for the WooCommerce Shop page
40+
// is_singular() returns false when on the Shop page so we need to use is_shop()
41+
return $optimize_css || ( function_exists('is_shop' ) && is_shop() );
42+
}
43+
44+
add_filter( 'stackable/load_cached_css_for_post', 'stackable_check_if_woocommerce_shop' );
45+
}
46+
47+
if ( ! function_exists( 'stackable_get_woocommerce_shop_page_id' ) ) {
48+
49+
function stackable_get_woocommerce_shop_page_id( $post_id ) {
50+
// use wc_get_page_id() to retrieve the page ID of the Shop page
51+
// do this because get_the_ID() returns the product page ID when on the Shop page
52+
if ( function_exists('is_shop' ) && is_shop() ) {
53+
return wc_get_page_id( 'shop' );
54+
}
55+
return $post_id;
56+
}
57+
58+
add_filter( 'stackable/get_post_id_for_cached_css', 'stackable_get_woocommerce_shop_page_id' );
59+
60+
}

src/css-optimize.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,10 @@ public function load_cached_css_for_post() {
213213
// wp_template_part then we might need to use the actions:
214214
// render_block_core_template_part_post and
215215
// render_block_core_template_part_file
216-
if ( is_singular() && ! is_preview() && ! is_attachment() ) {
217-
$post_id = get_the_ID();
216+
$optimize_css = is_singular() && ! is_preview() && ! is_attachment();
217+
$optimize_css = apply_filters( 'stackable/load_cached_css_for_post', $optimize_css );
218+
if ( $optimize_css ) {
219+
$post_id = apply_filters( 'stackable/get_post_id_for_cached_css', get_the_ID() );
218220
$this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true );
219221

220222
if ( ! empty( $this->optimized_css ) ) {

0 commit comments

Comments
 (0)