Skip to content

Commit 77bf14d

Browse files
committed
move fix to separate file
1 parent aac312b commit 77bf14d

File tree

4 files changed

+66
-32
lines changed

4 files changed

+66
-32
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( 'wp', '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: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,11 @@ 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-
// DEV NOTE #2: Check for WooCommerce Shop Page as well
217-
if ( ( is_singular() && ! is_preview() && ! is_attachment() ) || ( function_exists('is_shop' ) && is_shop() ) ) {
218-
if ( function_exists('is_shop' ) && is_shop() ) {
219-
// use wc_get_page_id() instead of get_the_ID() because
220-
// the latter returns the product page ID instead of the shop page ID
221-
$post_id = wc_get_page_id( 'shop' );
222-
} else {
223-
$post_id = get_the_ID();
224-
}
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() );
220+
225221
$this->optimized_css = get_post_meta( $post_id, 'stackable_optimized_css', true );
226222

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

src/kses.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,6 @@
88
exit;
99
}
1010

11-
/**
12-
* In WooCommerce Shop page, <style> tags are stripped out and the CSS styles are displayed in the frontend.
13-
* This function removes the <style> tags and CSS styles before they are stripped out.
14-
*/
15-
if ( ! function_exists( 'stackable_pre_kses_woocomerce_shop' ) ) {
16-
17-
function stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
18-
// Check if we are on the WooCommerce Shop page
19-
if ( function_exists('is_shop' ) && is_shop() ) {
20-
$optimized_css = get_post_meta( wc_get_page_id( 'shop' ), 'stackable_optimized_css', true );
21-
22-
// remove CSS before kses strips out <style> tags
23-
if ( ! empty( $optimized_css ) ) {
24-
$content = str_replace( '<style>' . $optimized_css . '</style>', '', $content );
25-
}
26-
27-
}
28-
return $content;
29-
}
30-
31-
add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3);
32-
}
33-
3411
if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) {
3512

3613
/**

0 commit comments

Comments
 (0)