Skip to content

Commit 2df1902

Browse files
authored
feat: added enqueue_frontend_assets_for_content static function (#3634)
* feat: added enqueue_frontend_assets_for_content static function * removed number of arguments in hooks --------- Co-authored-by: [email protected] <>
1 parent cfefc88 commit 2df1902

File tree

3 files changed

+60
-25
lines changed

3 files changed

+60
-25
lines changed

src/compatibility/ewww.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
}
77

88
if ( ! function_exists( 'stackable_load_image_optimizer_polyfill_frontend_script' ) ) {
9-
function stackable_load_image_optimizer_polyfill_frontend_script( $block_content, $block ) {
9+
function stackable_load_image_optimizer_polyfill_frontend_script( $block_content ) {
1010
// If Easy IO setting is activated for EWWW Image Optimizer, dynamic images becomes blurry.
1111
// Load the script to fix the issue.
1212
if ( ! is_admin() ) {
@@ -26,7 +26,7 @@ function stackable_load_image_optimizer_polyfill_frontend_script( $block_content
2626
function stackable_ewww_image_optimzer_plugin_checker() {
2727
if ( ! is_admin() && defined( 'EWWW_IMAGE_OPTIMIZER_PLUGIN_FILE' ) ) {
2828
// Load the script in the frontend if EWWW Image Optimizer is active.
29-
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
29+
add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script' );
3030
}
3131
}
3232

src/init.php

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/lightbox/index.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111

1212
if ( ! function_exists( 'stackable_load_lightbox_frontend_script' ) ) {
13-
function stackable_load_lightbox_frontend_script( $block_content, $block ) {
13+
function stackable_load_lightbox_frontend_script( $block_content ) {
1414
if ( strpos( $block_content, 'stk--has-lightbox' ) !== false ) {
1515
wp_enqueue_script(
1616
'stk-frontend-image-lightbox',
@@ -32,6 +32,6 @@ function stackable_load_lightbox_frontend_script( $block_content, $block ) {
3232
}
3333

3434
if ( ! is_admin() ) {
35-
add_action( 'stackable/enqueue_scripts', 'stackable_load_lightbox_frontend_script', 10, 2 );
35+
add_action( 'stackable/enqueue_scripts', 'stackable_load_lightbox_frontend_script' );
3636
}
3737
}

0 commit comments

Comments
 (0)