Skip to content

Commit

Permalink
optimized load_frontend_scripts_conditionally by changing conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Apr 26, 2024
1 parent eb58436 commit 59cb79b
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/deprecated/v2/optimization-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ class Stackable_Optimization_Settings_V2 {

private $is_script_loaded = false;

private $optimize_script_load = false;

/**
* Initialize
*/
function __construct() {
if ( has_stackable_v2_frontend_compatibility() || has_stackable_v2_editor_compatibility() ) {
$this->optimize_script_load = get_option( 'stackable_optimize_script_load' );

// Register our setting.
add_action( 'admin_init', array( $this, 'register_optimization_settings' ) );
add_action( 'rest_api_init', array( $this, 'register_optimization_settings' ) );
Expand All @@ -30,7 +34,9 @@ function __construct() {
add_action( 'init', array( $this, 'disable_frontend_scripts' ), 9 );

// Load the scripts only when Stackable blocks are detected.
add_filter( 'render_block', array( $this, 'load_frontend_scripts_conditionally' ), 10, 2 );
if ( ! is_admin() ) {
add_filter( 'render_block', array( $this, 'load_frontend_scripts_conditionally' ), 10, 2 );
}

// Add the optimization setting.
add_action( 'stackable_settings_page_mid', array( $this, 'add_optimization_settings' ) );
Expand Down Expand Up @@ -65,7 +71,7 @@ public function register_optimization_settings() {
* @since 2.17.0
*/
public function disable_frontend_scripts() {
if ( get_option( 'stackable_optimize_script_load' ) && ! is_admin() ) {
if ( $this->optimize_script_load && ! is_admin() ) {
remove_action( 'init', 'stackable_block_assets_v2' );
remove_action( 'enqueue_block_assets', 'stackable_add_required_block_styles_v2' );
}
Expand All @@ -87,18 +93,17 @@ public function load_frontend_scripts_conditionally( $block_content, $block ) {
return $block_content;
}

if ( ! $this->is_script_loaded ) {
if ( get_option( 'stackable_optimize_script_load' ) && ! is_admin() ) {
$block_name = isset( $block['blockName'] ) ? $block['blockName'] : '';
if (
stripos( $block_name, 'ugb/' ) === 0 ||
stripos( $block_content, '<!-- wp:ugb/' ) !== false ||
stripos( $block_content, 'ugb-highlight' ) !== false
) {
stackable_block_enqueue_frontend_assets_v2();
stackable_add_required_block_styles_v2();
$this->is_script_loaded = true;
}
if ( $this->optimize_script_load && ! $this->is_script_loaded ) {
if (
( isset( $block['blockName'] ) && strpos( $block_name, 'ugb/' ) === 0 ) ||
strpos( $block_content, '<!-- wp:ugb/' ) !== false ||
strpos( $block_content, 'ugb-highlight' ) !== false
) {
stackable_block_enqueue_frontend_assets_v2();
stackable_add_required_block_styles_v2();
$this->is_script_loaded = true;

remove_filter( 'render_block', array( $this, 'load_frontend_scripts_conditionally' ), 10 );
}
}

Expand Down

0 comments on commit 59cb79b

Please sign in to comment.