Skip to content

Commit

Permalink
fix css styles displaying on shop page
Browse files Browse the repository at this point in the history
  • Loading branch information
mxkae committed Dec 17, 2024
1 parent 81f1002 commit 457c675
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/css-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
23 changes: 23 additions & 0 deletions src/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,29 @@
exit;
}

/**
* In WooCommerce Shop page, <style> tags are stripped out and the CSS styles are displayed in the frontend.
* This function removes the <style> tags and CSS styles before they are stripped out.
*/
if ( ! function_exists( 'stackable_pre_kses_woocomerce_shop' ) && function_exists( 'wc_get_page_id' ) ) {

function stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
// Check if we are on the WooCommerce Shop page
if ( is_shop() ) {
$optimized_css = get_post_meta( wc_get_page_id( 'shop' ), 'stackable_optimized_css', true );

// remove CSS before kses strips out <style> tags
if ( ! empty( $optimized_css ) ) {
$content = str_replace( '<style>' . $optimized_css . '</style>', '', $content );
}

}
return $content;
}

add_filter('pre_kses', 'stackable_pre_kses_woocomerce_shop', 10, 3);
}

if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) {

/**
Expand Down

0 comments on commit 457c675

Please sign in to comment.