Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance: add frontend variables only when there is a block that needs them #3019

Merged
merged 7 commits into from
Jan 17, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function __construct() {

// Adds a special class to the body tag, to indicate we can now run animations.
add_action( 'wp_footer', array( $this, 'init_animation' ) );
add_action( 'wp_footer', array( $this, 'init_stackable_vars' ) );

// Add the fallback values for the default block width and wide block width.
// These are used for the inside "Content width" option of Columns.
Expand Down Expand Up @@ -101,12 +102,6 @@ public function register_frontend_assets() {
wp_register_script( 'ugb-block-frontend-js', null, [], STACKABLE_VERSION );
}

$args = apply_filters( 'stackable_localize_frontend_script', array(
'restUrl' => get_rest_url(),
'i18n' => array(), // Translatable labels used in the frontend should go here.
) );
wp_localize_script( 'ugb-block-frontend-js', 'stackable', $args );

// Register inline frontend styles, these are always loaded.
// Register via a dummy style.
wp_register_style( 'ugb-style-css-nodep', false );
Expand Down Expand Up @@ -455,6 +450,13 @@ public function load_plugin_textdomain() {
public function init_animation() {
echo '<script>requestAnimationFrame(() => document.body.classList.add( "stk--anim-init" ))</script>';
}

public function init_stackable_vars() {
$args = apply_filters( 'stackable_localize_frontend_script', array() );
if ( ! empty( $args ) ) {
echo '<script> stackable = ' . json_encode( $args ) . ' </script> ';
}
}
}

new Stackable_init();
Expand Down
Loading