@@ -20,14 +20,14 @@ class Stackable_Init {
2020 * Holds the scripts which are already enqueued, to ensure we only do it once per script.
2121 * @var Array
2222 */
23- public $ scripts_loaded = array ();
23+ public static $ scripts_loaded = array ();
2424
2525 /**
2626 * Enqueue the frontend scripts, ensures we only do it once.
2727 *
2828 * @var boolean
2929 */
30- public $ is_main_script_loaded = false ;
30+ public static $ is_main_script_loaded = false ;
3131
3232 /**
3333 * Add our hooks.
@@ -81,9 +81,9 @@ function __construct() {
8181 * @return void
8282 */
8383 public function maybe_force_css_load () {
84- if ( ! $ this -> is_main_script_loaded && apply_filters ( 'stackable_force_css_load ' , false ) ) {
85- $ this -> block_enqueue_frontend_assets ();
86- $ this -> is_main_script_loaded = true ;
84+ if ( ! self :: $ is_main_script_loaded && apply_filters ( 'stackable_force_css_load ' , false ) ) {
85+ self :: block_enqueue_frontend_assets ();
86+ self :: $ is_main_script_loaded = true ;
8787 }
8888 }
8989
@@ -92,7 +92,7 @@ public function maybe_force_css_load() {
9292 *
9393 * @since 0.1
9494 */
95- public function register_frontend_assets () {
95+ public static function register_frontend_assets () {
9696 // Frontend block styles.
9797 wp_register_style (
9898 'ugb-style-css ' ,
@@ -171,7 +171,7 @@ public function register_frontend_assets() {
171171 */
172172 public function load_frontend_scripts_conditionally_head () {
173173 // Only do this in the frontend.
174- if ( $ this -> is_main_script_loaded ) {
174+ if ( self :: $ is_main_script_loaded ) {
175175 return ;
176176 }
177177
@@ -186,8 +186,8 @@ public function load_frontend_scripts_conditionally_head() {
186186 stripos ( $ post ->post_content , 'stk-highlight ' ) !== false
187187 ) {
188188 // Enqueue our main scripts and styles.
189- $ this -> block_enqueue_frontend_assets ();
190- $ this -> is_main_script_loaded = true ;
189+ self :: block_enqueue_frontend_assets ();
190+ self :: $ is_main_script_loaded = true ;
191191 }
192192 }
193193 }
@@ -216,12 +216,12 @@ public function load_frontend_scripts_conditionally( $block_content, $block ) {
216216
217217 // Load our main frontend scripts if there's a Stackable block
218218 // loaded in the frontend.
219- if ( ! $ this -> is_main_script_loaded && ! is_admin () ) {
219+ if ( ! self :: $ is_main_script_loaded && ! is_admin () ) {
220220 if ( strpos ( $ block_content , '<!-- wp:stackable/ ' ) !== false ||
221- strpos ( $ block_content , 'stk-highlight ' ) !== false
221+ strpos ( $ block_content , 'stk-highlight ' ) !== false
222222 ) {
223- $ this -> block_enqueue_frontend_assets ();
224- $ this -> is_main_script_loaded = true ;
223+ self :: block_enqueue_frontend_assets ();
224+ self :: $ is_main_script_loaded = true ;
225225 }
226226 }
227227
@@ -231,21 +231,21 @@ public function load_frontend_scripts_conditionally( $block_content, $block ) {
231231 }
232232
233233 // Load our main frontend scripts if not yet loaded.
234- if ( ! $ this -> is_main_script_loaded && ! is_admin () ) {
235- $ this -> block_enqueue_frontend_assets ();
236- $ this -> is_main_script_loaded = true ;
234+ if ( ! self :: $ is_main_script_loaded && ! is_admin () ) {
235+ self :: block_enqueue_frontend_assets ();
236+ self :: $ is_main_script_loaded = true ;
237237 }
238238
239239 // Enqueue the block script once.
240- if ( ! isset ( $ this -> scripts_loaded [ $ block ['blockName ' ] ] ) ) {
240+ if ( ! isset ( self :: $ scripts_loaded [ $ block ['blockName ' ] ] ) ) {
241241 $ stackable_block = substr ( $ block ['blockName ' ], 10 );
242242 do_action ( 'stackable/ ' . $ stackable_block . '/enqueue_scripts ' );
243- $ this -> scripts_loaded [ $ block ['blockName ' ] ] = true ;
243+ self :: $ scripts_loaded [ $ block ['blockName ' ] ] = true ;
244244 }
245245
246246 // Check whether the current block needs to enqueue some scripts.
247247 // This gets called across all the blocks.
248- do_action ( 'stackable/enqueue_scripts ' , $ block_content, $ block );
248+ do_action ( 'stackable/enqueue_scripts ' , $ block_content );
249249
250250 return $ block_content ;
251251 }
@@ -255,8 +255,8 @@ public function load_frontend_scripts_conditionally( $block_content, $block ) {
255255 *
256256 * @since 2.17.2
257257 */
258- public function block_enqueue_frontend_assets () {
259- $ this -> register_frontend_assets ();
258+ public static function block_enqueue_frontend_assets () {
259+ self :: register_frontend_assets ();
260260 wp_enqueue_style ( 'ugb-style-css ' );
261261 if ( is_frontend () ) {
262262 wp_enqueue_style ( 'ugb-block-style-inheritance-nodep ' );
@@ -266,6 +266,41 @@ public function block_enqueue_frontend_assets() {
266266 do_action ( 'stackable_block_enqueue_frontend_assets ' );
267267 }
268268
269+ /**
270+ * Enqueue frontend scripts and styles for a given post content.
271+ *
272+ * @param string $post_content The post content.
273+ * @return void
274+ */
275+ public static function enqueue_frontend_assets_for_content ( $ post_content ) {
276+ // If a Stackable block is present in the post content, enqueue the frontend assets.
277+ if ( ! self ::$ is_main_script_loaded && ! is_admin () ) {
278+ if ( stripos ( $ post_content , '<!-- wp:stackable/ ' ) !== false ) {
279+ self ::block_enqueue_frontend_assets ();
280+ self ::$ is_main_script_loaded = true ;
281+ }
282+ }
283+
284+ // Gather all the unique Stackable blocks and load all the block scripts once.
285+ // Gather all the "<!-- wp:stackable/BLOCK_NAME"
286+ preg_match_all ( '/<!-- wp:stackable\/([a-zA-Z_-]+)/ ' , $ post_content , $ stackable_blocks );
287+ // Go through each unique block name.
288+ foreach ( $ stackable_blocks [1 ] as $ _block_name ) {
289+ // Clean up the block name, trailing "-" from the end since it may have "--" in the end if the post content is compressed.
290+ $ block_name = trim ( $ _block_name , '- ' );
291+
292+ // Enqueue the block script once.
293+ if ( ! isset ( self ::$ scripts_loaded [ $ block_name ] ) ) {
294+ do_action ( 'stackable/ ' . $ block_name . '/enqueue_scripts ' );
295+ self ::$ scripts_loaded [ $ block_name ] = true ;
296+ }
297+ }
298+
299+ // Check whether the current block needs to enqueue some scripts.
300+ // This gets called across all the blocks.
301+ do_action ( 'stackable/enqueue_scripts ' , $ post_content );
302+ }
303+
269304 /**
270305 * Enqueue CodeMirror separately. This originally was enqueued in
271306 * `register_block_editor_assets`, but we want to enqueue this only when
0 commit comments