Skip to content

Commit

Permalink
move fix to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
mxkae committed Dec 18, 2024
1 parent aac312b commit 77bf14d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/compatibility/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
}

require_once( plugin_dir_path( __FILE__ ) . './neve/index.php' );
require_once( plugin_dir_path( __FILE__ ) . './woocommerce.php' );
60 changes: 60 additions & 0 deletions src/compatibility/woocommerce.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
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 stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
$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;
}

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' );

}
14 changes: 5 additions & 9 deletions src/css-optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand Down
23 changes: 0 additions & 23 deletions src/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,6 @@
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 stackable_pre_kses_woocomerce_shop( $content, $allowed_html, $context ) {
// Check if we are on the WooCommerce Shop page
if ( function_exists('is_shop' ) && 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 77bf14d

Please sign in to comment.