| @@ -12,8 +12,15 @@ | ||
| 12 | 12 | */ |
| 13 | 13 | private static $instance = null; |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | + * Whether the page should be cached | |
| 17 | + * | |
| 18 | + * @var bool|array | |
| 19 | + */ | |
| 20 | + private $should_cache_page; | |
| 21 | + | |
| 22 | + /** | |
| 16 | 23 | * Constructor |
| 17 | 24 | */ |
| 18 | 25 | private function __construct() { |
| 19 | 26 | // Initialize WP_Optimize_Minify_Config to make available wp_optimize_minify_config() |
| @@ -31,8 +38,10 @@ | ||
| 31 | 38 | * @return string |
| 32 | 39 | */ |
| 33 | 40 | private function optimize($buffer, $flags): string { |
| 34 | 41 | $buffer = apply_filters('wp_optimize_buffer', $buffer, $flags); |
| 42 | + | |
| 43 | + $this->should_cache_page = WP_Optimize()->get_page_cache()->should_cache_page(); | |
| 35 | 44 | |
| 36 | 45 | if (WP_Optimize_Utils::is_valid_html($buffer)) { |
| 37 | 46 | $buffer = $this->maybe_host_google_fonts_locally($buffer); |
| 38 | 47 | $buffer = $this->maybe_remove_unused_css($buffer); |
| @@ -54,10 +63,19 @@ | ||
| 54 | 63 | * @return string |
| 55 | 64 | */ |
| 56 | 65 | private function maybe_cache_page($buffer, $flags) { |
| 57 | 66 | |
| 58 | - if (!$this->is_wp_cli() && WP_Optimize()->get_page_cache()->should_cache_page()) { | |
| 59 | - return wpo_cache($buffer, $flags); | |
| 67 | + if (!$this->is_wp_cli()) { | |
| 68 | + $wpo_page_cache = WP_Optimize()->get_page_cache(); | |
| 69 | + | |
| 70 | + // If an array with the reasons why the page isn't cached is returned | |
| 71 | + if (is_array($this->should_cache_page)) { | |
| 72 | + do_action('wpo_page_not_cached', $this->should_cache_page); | |
| 73 | + | |
| 74 | + $wpo_page_cache->maybe_add_no_cache_because_info($this->should_cache_page); | |
| 75 | + } | |
| 76 | + | |
| 77 | + if (true === $this->should_cache_page) return wpo_cache($buffer, $flags); | |
| 60 | 78 | } |
| 61 | 79 | |
| 62 | 80 | return $buffer; |
| 63 | 81 | } |
| @@ -99,10 +117,11 @@ | ||
| 99 | 117 | */ |
| 100 | 118 | private function maybe_remove_unused_css($buffer) { |
| 101 | 119 | |
| 102 | 120 | if (is_user_logged_in()) return $buffer; |
| 103 | - | |
| 104 | - if (WP_Optimize::is_premium() && wp_optimize_minify_config()->get('enable_unused_css')) { | |
| 121 | + | |
| 122 | + // Remove unused CSS available in Premium and only when the page cache is enabled | |
| 123 | + if (WP_Optimize::is_premium() && wp_optimize_minify_config()->get('enable_unused_css') && true === $this->should_cache_page) { | |
| 105 | 124 | $unused_css_class = WP_Optimize_Minify_Unused_Css::get_instance(); |
| 106 | 125 | return $unused_css_class->remove_unused_css($buffer); |
| 107 | 126 | } |
| 108 | 127 | return $buffer; |
| @@ -115,9 +134,9 @@ | ||
| 115 | 134 | */ |
| 116 | 135 | private function should_initialise() { |
| 117 | 136 | |
| 118 | 137 | // Skip admin, AJAX, WP-CLI, cron, static assets and page builder edit modes. |
| 119 | - if (is_admin() || $this->is_ajax() || $this->is_wp_cli() || $this->is_cron_job() || $this->is_static_asset_request() || WPO_Page_Builder_Compatibility::instance()->is_edit_mode()) { | |
| 138 | + if (is_admin() || $this->is_ajax() || $this->is_wp_cli() || $this->is_cron_job() || $this->is_static_asset_request() || WPO_Page_Builder_Compatibility::is_edit_mode()) { | |
| 120 | 139 | return false; |
| 121 | 140 | } |
| 122 | 141 | |
| 123 | 142 | return true; |