55use NewfoldLabs \WP \Module \Data \WonderBlocks \Requests \Fetch ;
66class CSSUtilities {
77
8+ /**
9+ * The single instance of the class.
10+ *
11+ * @var CSSUtilities|null
12+ */
13+ private static $ instance = null ;
14+
815 /**
916 * The production base URL.
1017 *
@@ -19,10 +26,22 @@ class CSSUtilities {
1926 */
2027 protected static $ local_base_url = 'http://localhost:8888 ' ;
2128
29+ /**
30+ * Get the single instance of the class.
31+ *
32+ * @return CSSUtilities The instance of the class.
33+ */
34+ public static function get_instance (): CSSUtilities {
35+ if ( null === self ::$ instance ) {
36+ self ::$ instance = new self ();
37+ }
38+ return self ::$ instance ;
39+ }
40+
2241 /**
2342 * Constructor.
2443 */
25- public function __construct () {
44+ private function __construct () {
2645 \add_action ( 'enqueue_block_assets ' , array ( $ this , 'enqueue ' ) );
2746 \add_action ( 'enqueue_nfd_wonder_blocks_utilities ' , array ( $ this , 'enqueue ' ) );
2847 }
@@ -33,38 +52,52 @@ public function __construct() {
3352 * @return void
3453 */
3554 public function enqueue () {
36-
37- $ base_url = $ this ->get_base_url ();
55+ // Refresh assets if 24 hours have passed since the last refresh.
56+ $ this ->conditional_refresh_assets ();
3857
39- $ remote_css = $ base_url . ' /assets/css/utilities.css ' ;
40- $ remote_js = $ base_url . ' /assets/js/utilities.js ' ;
58+ $ css_content = $ this -> get_asset_content ( ' utilities_css ' ) ;
59+ $ js_content = $ this -> get_asset_content ( ' utilities_js ' ) ;
4160
42- $ css_url = $ this ->get_asset_url ($ remote_css , NFD_WONDER_BLOCKS_URL . '/assets/build/utilities.css ' );
43- $ css_version = $ this ->get_asset_version ($ remote_css );
44-
45- $ js_url = $ this ->get_asset_url ($ remote_js , NFD_WONDER_BLOCKS_URL . '/assets/build/utilities.js ' );
46- $ js_version = $ this ->get_asset_version ($ remote_js );
47-
48- \wp_register_style (
49- 'nfd-wonder-blocks-utilities ' ,
50- $ css_url ,
51- array (),
52- $ css_version
53- );
54-
55- \wp_register_script (
56- 'nfd-wonder-blocks-utilities ' ,
57- $ js_url ,
58- array (),
59- $ js_version
60- );
61-
62- \wp_enqueue_style ( 'nfd-wonder-blocks-utilities ' );
63- \wp_enqueue_script ( 'nfd-wonder-blocks-utilities ' );
61+ if ( $ css_content ) {
62+ \wp_register_style ( 'nfd-wonder-blocks-utilities ' , false );
63+ \wp_enqueue_style ( 'nfd-wonder-blocks-utilities ' );
64+ \wp_add_inline_style ( 'nfd-wonder-blocks-utilities ' , $ css_content );
65+ } else {
66+ \wp_enqueue_style (
67+ 'nfd-wonder-blocks-utilities ' ,
68+ constant ( 'NFD_WONDER_BLOCKS_URL ' ) . '/assets/build/utilities.css ' ,
69+ array (),
70+ constant ( 'NFD_WONDER_BLOCKS_VERSION ' )
71+ );
72+ }
73+
74+ if ( $ js_content ) {
75+ \wp_register_script ( 'nfd-wonder-blocks-utilities ' , false );
76+ \wp_enqueue_script ( 'nfd-wonder-blocks-utilities ' );
77+ \wp_add_inline_script ( 'nfd-wonder-blocks-utilities ' , $ js_content );
78+ } else {
79+ \wp_enqueue_script (
80+ 'nfd-wonder-blocks-utilities ' ,
81+ constant ( 'NFD_WONDER_BLOCKS_URL ' ) . '/assets/build/utilities.js ' ,
82+ array (),
83+ constant ( 'NFD_WONDER_BLOCKS_VERSION ' )
84+ );
85+ }
6486
6587 \wp_add_inline_style ( 'nfd-wonder-blocks-utilities ' , $ this ->get_inline_css () );
6688 }
6789
90+ /**
91+ * Get the content of an asset.
92+ *
93+ * @param string $asset The asset to get the content of.
94+ * @return string The content of the asset.
95+ */
96+ private function get_asset_content ( string $ option_key ) {
97+ $ sanitized_key = sanitize_key ( 'nfd_ ' . $ option_key );
98+ return get_option ( $ sanitized_key , false );
99+ }
100+
68101 /**
69102 * Generates inline CSS based on the current active theme.
70103 *
@@ -75,7 +108,6 @@ public function enqueue() {
75108 * @return string The generated CSS.
76109 */
77110 private function get_inline_css () {
78-
79111 $ theme = \wp_get_theme ()->get_template ();
80112 $ css = '' ;
81113
@@ -112,7 +144,7 @@ private function get_inline_css() {
112144 --nfd-cp-text-secondary: var(--wp--preset--color--secondary, #000);
113145 } " ;
114146
115- $ css = "body .is-layout-constrained:has(.wndb-container.is-layout-constrained) > .wndb-container.is-layout-constrained {
147+ $ css . = "body .is-layout-constrained:has(.wndb-container.is-layout-constrained) > .wndb-container.is-layout-constrained {
116148 width: 100%;
117149 max-width: unset;
118150 } " ;
@@ -131,71 +163,92 @@ private function get_inline_css() {
131163
132164 return $ css ;
133165 }
134-
166+
135167 /**
136- * Get the URL for the asset, checking if the remote URL is valid.
137- *
138- * @param string $remote_url The remote URL for the asset.
139- * @param string $fallback_url The fallback URL if the remote asset is invalid.
140- * @return string The valid URL for the asset.
168+ * Get the base URL
169+ *
170+ * @return string The base URL.
141171 */
142- private function get_asset_url ( string $ remote_url , string $ fallback_url ) : string {
143- return $ this ->is_valid_remote_file ( $ remote_url ) ? $ remote_url : $ fallback_url ;
172+ public function get_base_url (): string {
173+ if ( defined ( 'NFD_DATA_WB_DEV_MODE ' ) && constant ( 'NFD_DATA_WB_DEV_MODE ' ) ) {
174+ return self ::$ local_base_url ;
175+ }
176+
177+ return self ::$ production_base_url ;
144178 }
145179
146180 /**
147- * Get the version number for the asset, either from remote or fallback .
181+ * Conditionally refresh CSS and JS assets from remote sources if 24 hours have passed .
148182 *
149- * @param string $remote_url The remote URL for the asset.
150- * @return int|string The version number.
183+ * @return void
151184 */
152- private function get_asset_version ( string $ remote_url ) {
153- return $ this ->is_valid_remote_file ( $ remote_url ) ? $ this ->get_remote_assets_version () : NFD_WONDER_BLOCKS_VERSION ;
185+ public function conditional_refresh_assets () {
186+ $ last_refresh = get_option ( 'nfd_utilities_last_refresh_time ' , 0 );
187+ $ current_time = time ();
188+
189+ if ( ( $ current_time - $ last_refresh ) > DAY_IN_SECONDS || ( defined ( 'NFD_DATA_WB_DEV_MODE ' ) && constant ( 'NFD_DATA_WB_DEV_MODE ' ) ) ) {
190+ $ this ->refresh_assets ();
191+ update_option ( 'nfd_utilities_last_refresh_time ' , $ current_time );
192+ }
154193 }
155-
194+
156195 /**
157- * Check if a remote file is valid.
158- *
159- * @param string $url URL of the remote file.
196+ * Refresh CSS and JS assets from remote sources.
197+ * This method can be manually triggered by other actions or hooks as needed.
160198 *
161- * @return bool
199+ * @return void
162200 */
163- private function is_valid_remote_file ( string $ url ): bool {
164- $ response = \wp_remote_get ( $ url , array ( 'timeout ' => 5 ) );
165- if ( is_wp_error ( $ response ) ) {
166- return false ;
167- }
168-
169- $ status_code = \wp_remote_retrieve_response_code ( $ response );
170- return $ status_code === 200 ;
201+ public function refresh_assets () {
202+ $ this ->fetch_and_store_asset ( '/assets/css/utilities.css ' , 'utilities_css ' );
203+ $ this ->fetch_and_store_asset ( '/assets/js/utilities.js ' , 'utilities_js ' );
171204 }
172-
205+
173206 /**
174- * Get the version number for remote assets.
207+ * Fetch and store the asset content in the database with minification.
208+ *
209+ * @param string $path The path of the remote asset.
210+ * @param string $option_key The option key to store the content.
175211 *
176- * @return int The version number.
212+ * @return void
177213 */
178- private function get_remote_assets_version () : int {
179- $ version = get_transient ('nfd_utilities_version ' );
214+ private function fetch_and_store_asset ( string $ path , string $ option_key ) {
215+ $ base_url = $ this ->get_base_url ();
216+ $ url = esc_url_raw ( $ base_url . $ path );
217+
218+ $ response = \wp_remote_get ( $ url );
180219
181- if ( ! $ version ) {
182- $ version = time ();
183- set_transient ('nfd_utilities_version ' , $ version , DAY_IN_SECONDS );
220+ if ( ! is_wp_error ( $ response ) && 200 === wp_remote_retrieve_response_code ( $ response ) ) {
221+ $ content = wp_remote_retrieve_body ( $ response );
222+
223+ // Minify the content for storage
224+ $ minified_content = $ this ->minify_content ( $ content , $ path );
225+ $ sanitized_key = sanitize_key ( 'nfd_ ' . $ option_key );
226+ update_option ( $ sanitized_key , wp_slash ( $ minified_content ) );
184227 }
185-
186- return $ version ;
187228 }
188-
229+
189230 /**
190- * Get the base URL
191- *
192- * @return string The base URL.
231+ * Minify the CSS or JS content.
232+ *
233+ * @param string $content The content to minify.
234+ * @param string $path The path to determine if it's CSS or JS.
235+ *
236+ * @return string The minified content.
193237 */
194- public function get_base_url (): string {
195- if ( defined ( 'NFD_DATA_WB_DEV_MODE ' ) && constant ( 'NFD_DATA_WB_DEV_MODE ' ) ) {
196- return self ::$ local_base_url ;
238+ private function minify_content ( string $ content , string $ path ): string {
239+ if ( strpos ( $ path , '.css ' ) !== false ) {
240+ // Minify CSS by removing comments, whitespace, etc.
241+ $ content = preg_replace ( '/\s+/ ' , ' ' , $ content );
242+ $ content = preg_replace ( '/\s*([{};:>+~,])\s*/ ' , '$1 ' , $ content );
243+ $ content = preg_replace ( '/;}/ ' , '} ' , $ content );
244+ $ content = preg_replace ( '/\/\*.*?\*\// ' , '' , $ content );
245+ } elseif ( strpos ( $ path , '.js ' ) !== false ) {
246+ // Minify JS by removing comments and whitespace.
247+ $ content = preg_replace ( '/\/\*.*?\*\//s ' , '' , $ content );
248+ $ content = preg_replace ( '/\s+/ ' , ' ' , $ content );
249+ $ content = preg_replace ( '/\s*([{};,:>+\-~])\s*/ ' , '$1 ' , $ content );
250+ $ content = preg_replace ( '/;}/ ' , '} ' , $ content );
197251 }
198-
199- return self ::$ production_base_url ;
252+ return trim ( $ content );
200253 }
201254}
0 commit comments