|
| 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 | +} |
0 commit comments