| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Libs; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Excludes FireBox assets from WP Rocket's JS/CSS optimizations. |
| 21 |
* |
| 22 |
* WP Rocket's "Delay JavaScript Execution" holds every script back until the |
| 23 |
* visitor first interacts with the page, which silently breaks time, scroll, |
| 24 |
* exit-intent and auto-show campaigns. Its minify/combine and "Remove Unused |
| 25 |
* CSS" features strip or rewrite campaign assets the same way. These filters |
| 26 |
* register FireBox's own exclusions so campaigns keep working without the |
| 27 |
* user pasting exclusion patterns into WP Rocket's settings. |
| 28 |
*/ |
| 29 |
class WPRocket |
| 30 |
{ |
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
if (!defined('WP_ROCKET_VERSION')) |
| 34 |
{ |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
add_filter('rocket_delay_js_exclusions', [$this, 'excludeFromDelayJS']); |
| 39 |
add_filter('rocket_exclude_js', [$this, 'excludeFromMinifyJS']); |
| 40 |
add_filter('rocket_exclude_defer_js', [$this, 'excludeFromDeferJS']); |
| 41 |
add_filter('rocket_exclude_css', [$this, 'excludeFromMinifyCSS']); |
| 42 |
add_filter('rocket_rucss_safelist', [$this, 'excludeFromRemoveUnusedCSS']); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Excludes FireBox scripts from "Delay JavaScript Execution". |
| 47 |
* |
| 48 |
* Patterns are matched against the whole script tag, so 'firebox' covers |
| 49 |
* both the plugin's file URLs and the id attribute WordPress prints on |
| 50 |
* the firebox-* handles' inline scripts (e.g. firebox-main-js-before). |
| 51 |
* |
| 52 |
* @param array $excluded |
| 53 |
* |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function excludeFromDelayJS($excluded) |
| 57 |
{ |
| 58 |
$excluded = (array) $excluded; |
| 59 |
|
| 60 |
$excluded[] = 'firebox'; |
| 61 |
|
| 62 |
return $excluded; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Excludes FireBox scripts from JS minification/combining. |
| 67 |
* |
| 68 |
* @param array $excluded |
| 69 |
* |
| 70 |
* @return array |
| 71 |
*/ |
| 72 |
public function excludeFromMinifyJS($excluded) |
| 73 |
{ |
| 74 |
return $this->addPublicMediaPattern($excluded, 'js/(.*)'); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Excludes FireBox scripts from "Load JavaScript deferred". The scripts |
| 79 |
* already register with WordPress' own defer strategy; excluding them |
| 80 |
* keeps WP Rocket from rewriting the tags a second time. |
| 81 |
* |
| 82 |
* @param array $excluded |
| 83 |
* |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public function excludeFromDeferJS($excluded) |
| 87 |
{ |
| 88 |
return $this->addPublicMediaPattern($excluded, 'js/(.*)'); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Excludes FireBox stylesheets from CSS minification/combining. |
| 93 |
* |
| 94 |
* @param array $excluded |
| 95 |
* |
| 96 |
* @return array |
| 97 |
*/ |
| 98 |
public function excludeFromMinifyCSS($excluded) |
| 99 |
{ |
| 100 |
return $this->addPublicMediaPattern($excluded, 'css/(.*)'); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Safelists FireBox stylesheets for "Remove Unused CSS". Campaigns are |
| 105 |
* hidden (or not yet in the DOM) when WP Rocket samples the page, so |
| 106 |
* without this their styles are treated as unused and stripped. |
| 107 |
* |
| 108 |
* The pattern must contain a literal '.css' — WP Rocket's SaaS decides |
| 109 |
* filename-vs-selector by that substring (see WP Rocket's 3.11.0.2 |
| 110 |
* update_safelist_items migration), and '(.*)' matches slashes, so this |
| 111 |
* one pattern covers the vendor/shortcodes/blocks subdirectories too. |
| 112 |
* |
| 113 |
* @param array $safelist |
| 114 |
* |
| 115 |
* @return array |
| 116 |
*/ |
| 117 |
public function excludeFromRemoveUnusedCSS($safelist) |
| 118 |
{ |
| 119 |
return $this->addPublicMediaPattern($safelist, 'css/(.*).css'); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Appends a pattern scoped to the plugin's public media path, e.g. |
| 124 |
* /wp-content/plugins/firebox/media/public/js/(.*) |
| 125 |
* |
| 126 |
* @param array $list |
| 127 |
* @param string $pattern |
| 128 |
* |
| 129 |
* @return array |
| 130 |
*/ |
| 131 |
private function addPublicMediaPattern($list, $pattern) |
| 132 |
{ |
| 133 |
$list = (array) $list; |
| 134 |
|
| 135 |
$path = wp_parse_url(FBOX_MEDIA_PUBLIC_URL, PHP_URL_PATH); |
| 136 |
|
| 137 |
// Never fall through to a bare 'js/(.*)' pattern that would exclude |
| 138 |
// every site script from the optimization. |
| 139 |
if (!is_string($path) || $path === '' || $path === '/') |
| 140 |
{ |
| 141 |
return $list; |
| 142 |
} |
| 143 |
|
| 144 |
$list[] = $path . $pattern; |
| 145 |
|
| 146 |
return $list; |
| 147 |
} |
| 148 |
} |
| 149 |
|