Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
28 changes: 15 additions & 13 deletions includes/Frontend/Animations.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,26 @@ public function __construct() {
* @return void
*/
private function init_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 100 );
add_action( 'init', array( $this, 'register_scripts' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 5 );
add_action( 'enqueue_block_editor_assets', array( $this, 'register_animation_attributes' ), 15 );
add_filter( 'render_block', array( $this, 'add_animation_classes_to_blocks' ), 10, 2 );
}

/**
* Enqueue scripts and styles.
* Register frontend scripts and styles for conditional enqueueing.
*
* @return void
*/
public function enqueue_scripts() {
// Enqueue custom animations CSS.
wp_enqueue_style(
public function register_scripts() {
wp_register_style(
'frontblocks-animations',
FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.css',
array(),
FRBL_VERSION
);

wp_enqueue_script(
wp_register_script(
'frontblocks-animations-custom',
FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.js',
array(),
Expand All @@ -67,13 +66,8 @@ public function enqueue_scripts() {
* @return void
*/
public function enqueue_block_editor_assets() {
// Enqueue custom animations CSS for editor.
wp_enqueue_style(
'frontblocks-animations-editor',
FRBL_PLUGIN_URL . 'assets/animations/frontblocks-animations.css',
array(),
FRBL_VERSION
);
// Enqueue custom animations CSS for editor (reuse registered frontend style).
wp_enqueue_style( 'frontblocks-animations' );

// Enqueue custom block editor script.
wp_enqueue_script(
Expand Down Expand Up @@ -208,6 +202,14 @@ public function add_animation_classes_to_blocks( $block_content, $block ) {
return $block_content;
}

// Enqueue frontend assets only when a block with animation features is present.
if ( ! wp_style_is( 'frontblocks-animations', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-animations' );
}
if ( ! wp_script_is( 'frontblocks-animations-custom', 'enqueued' ) ) {
wp_enqueue_script( 'frontblocks-animations-custom' );
}

$properties = array();

// Animation properties.
Expand Down
31 changes: 31 additions & 0 deletions includes/Frontend/Carousel.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function __construct() {
* @return void
*/
private function init_hooks() {
add_action( 'init', array( $this, 'register_frontend_assets' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
add_action( 'enqueue_block_assets', array( $this, 'enqueue_block_canvas_assets' ) );
add_filter( 'render_block_generateblocks/grid', array( $this, 'add_custom_attributes_to_grid_block' ), 10, 2 );
Expand All @@ -40,6 +41,30 @@ private function init_hooks() {
add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
}

/**
* Register frontend carousel assets for conditional enqueueing.
*
* @return void
*/
public function register_frontend_assets() {
// Assets are registered in Plugin_Main::register_scripts().
// This method exists as a hook point for any additional registration needs.
}

/**
* Enqueue carousel frontend assets when a carousel block is detected.
*
* @return void
*/
private function enqueue_carousel_assets() {
if ( ! wp_style_is( 'frontblocks-carousel', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-carousel' );
}
if ( ! wp_script_is( 'frontblocks-carousel-custom', 'enqueued' ) ) {
wp_enqueue_script( 'frontblocks-carousel-custom' );
}
}

/**
* Enqueue carousel CSS in the editor canvas (iframe).
*
Expand Down Expand Up @@ -129,6 +154,8 @@ public function add_custom_attributes_to_grid_block( $block_content, $block ) {
$block_content,
1 // Only replace the first occurrence.
);

$this->enqueue_carousel_assets();
}

return $block_content;
Expand Down Expand Up @@ -195,6 +222,8 @@ public function add_custom_attributes_to_element_block( $block_content, $block )
$block_content,
1 // Only replace the first occurrence.
);

$this->enqueue_carousel_assets();
}

return $block_content;
Expand Down Expand Up @@ -261,6 +290,8 @@ public function add_custom_attributes_to_core_group_block( $block_content, $bloc
$block_content,
1 // Only replace the first occurrence.
);

$this->enqueue_carousel_assets();
}

return $block_content;
Expand Down
19 changes: 13 additions & 6 deletions includes/Frontend/ContainerEdgeAlignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ContainerEdgeAlignment {
* Constructor.
*/
public function __construct() {
add_action( 'init', array( $this, 'register_frontend_assets' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) );
add_filter( 'render_block', array( $this, 'add_edge_alignment_classes' ), 10, 2 );
}

Expand Down Expand Up @@ -59,20 +59,19 @@ public function enqueue_editor_assets() {
}

/**
* Enqueue frontend assets.
* Register frontend assets for conditional enqueueing.
*
* @return void
*/
public function enqueue_frontend_assets() {
wp_enqueue_style(
public function register_frontend_assets() {
wp_register_style(
'frbl-edge-alignment',
FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment.css',
array(),
FRBL_VERSION
);

// Add inline script to calculate margins dynamically.
wp_enqueue_script(
wp_register_script(
'frbl-edge-alignment-js',
FRBL_PLUGIN_URL . 'assets/container-edge-alignment/frontblocks-edge-alignment-frontend.js',
array(),
Expand Down Expand Up @@ -116,6 +115,14 @@ public function add_edge_alignment_classes( $block_content, $block ) {
return $block_content;
}

// Enqueue frontend assets only when an edge-aligned block is detected.
if ( ! wp_style_is( 'frbl-edge-alignment', 'enqueued' ) ) {
wp_enqueue_style( 'frbl-edge-alignment' );
}
if ( ! wp_script_is( 'frbl-edge-alignment-js', 'enqueued' ) ) {
wp_enqueue_script( 'frbl-edge-alignment-js' );
}

// Add class to the block.
// Find the first occurrence of class=" and add our class.
if ( false !== strpos( $block_content, 'class="' ) ) {
Expand Down
17 changes: 12 additions & 5 deletions includes/Frontend/GravityFormsInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ public function __construct() {
* @return void
*/
private function init_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 );
add_action( 'init', array( $this, 'register_scripts' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
add_filter( 'render_block', array( $this, 'add_inline_attributes_to_gf_block' ), 10, 2 );
add_action( 'init', array( $this, 'register_custom_attributes' ), 5 );
}

/**
* Enqueue scripts and styles.
* Register frontend scripts and styles for conditional enqueueing.
*
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_style(
public function register_scripts() {
wp_register_style(
'frontblocks-gf-inline',
FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline.css',
array(),
FRBL_VERSION
);

wp_enqueue_script(
wp_register_script(
'frontblocks-gf-inline-runtime',
FRBL_PLUGIN_URL . 'assets/gravityforms-inline/frontblocks-gf-inline.js',
array(),
Expand Down Expand Up @@ -110,6 +110,13 @@ public function add_inline_attributes_to_gf_block( $block_content, $block ) {

// Add inline class and attributes if enabled.
if ( $inline_enabled ) {
// Enqueue frontend assets only when the GF inline feature is active.
if ( ! wp_style_is( 'frontblocks-gf-inline', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-gf-inline' );
}
if ( ! wp_script_is( 'frontblocks-gf-inline-runtime', 'enqueued' ) ) {
wp_enqueue_script( 'frontblocks-gf-inline-runtime' );
}
// Try multiple approaches to add the class.
// Method 1: Add to wp-block-gravityforms-form wrapper.
if ( strpos( $block_content, 'wp-block-gravityforms-form' ) !== false ) {
Expand Down
26 changes: 20 additions & 6 deletions includes/Frontend/Headline.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ class Headline {
public function __construct() {
add_action( 'init', array( $this, 'register_assets' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_styles' ), 100 );
add_filter( 'generateblocks_attr_headline', array( $this, 'add_line_class_attribute' ), 10 );
add_filter( 'generateblocks_attr_text', array( $this, 'add_marquee_speed_attribute' ), 10, 2 );
add_filter( 'render_block_generateblocks/text', array( $this, 'maybe_enqueue_frontend_assets' ), 10, 2 );
add_filter( 'render_block_generateblocks/headline', array( $this, 'maybe_enqueue_frontend_assets' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -84,13 +85,26 @@ public function enqueue_editor_assets() {
}

/**
* Enqueue frontend styles and scripts.
* Conditionally enqueue frontend assets when a GenerateBlocks headline/text block is rendered.
*
* @return void
* @param string $block_content Block content.
* @param array $block Block data.
* @return string
*/
public function enqueue_frontend_styles() {
wp_enqueue_style( 'frontblocks-headline-styles' );
wp_enqueue_script( 'frontblocks-headline-marquee' );
public function maybe_enqueue_frontend_assets( $block_content, $block ) {
$attrs = $block['attrs'] ?? array();

// Enqueue headline styles when any headline/text block with frbl features is rendered.
if ( ! wp_style_is( 'frontblocks-headline-styles', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-headline-styles' );
}

// Enqueue marquee script only when marquee is active on this block.
if ( ! empty( $attrs['frblMarqueeSpeed'] ) && ! wp_script_is( 'frontblocks-headline-marquee', 'enqueued' ) ) {
wp_enqueue_script( 'frontblocks-headline-marquee' );
}

return $block_content;
}

/**
Expand Down
13 changes: 9 additions & 4 deletions includes/Frontend/InsertPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct() {
* @return void
*/
private function init_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 99 );
add_action( 'init', array( $this, 'register_scripts' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ) );
add_action( 'wp_ajax_frbl_search_posts', array( $this, 'search_posts_callback' ) );
add_action( 'wp_ajax_nopriv_frbl_search_posts', array( $this, 'search_posts_callback' ) );
Expand All @@ -42,12 +42,12 @@ private function init_hooks() {
}

/**
* Enqueue scripts and styles.
* Register frontend scripts and styles for conditional enqueueing.
*
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_style(
public function register_scripts() {
wp_register_style(
'frontblocks-insert-post',
FRBL_PLUGIN_URL . 'assets/insert-post/frontblocks-insert-post.css',
array(),
Expand Down Expand Up @@ -180,6 +180,11 @@ public function register_insert_post_block() {
* @return string HTML output.
*/
public function render_insert_post_block( $attributes ) {
// Enqueue frontend styles only when this block is rendered.
if ( ! wp_style_is( 'frontblocks-insert-post', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-insert-post' );
}

$post_id = $attributes['selectedPostId'] ?? 0;

if ( ! $post_id ) {
Expand Down
22 changes: 15 additions & 7 deletions includes/Frontend/ShapeAnimations.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,35 @@ public function __construct() {
* @return void
*/
private function init_hooks() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 100 );
add_action( 'init', array( $this, 'register_scripts' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor_assets' ), 5 );
add_action( 'enqueue_block_editor_assets', array( $this, 'register_shape_animation_attributes' ), 15 );
add_filter( 'render_block', array( $this, 'add_animation_classes_to_shape' ), 10, 2 );
}

/**
* Enqueue frontend scripts and styles.
* Register frontend scripts and styles for conditional enqueueing.
*
* @return void
*/
public function enqueue_scripts() {
wp_enqueue_style(
public function register_scripts() {
wp_register_style(
'frontblocks-shape-animations',
FRBL_PLUGIN_URL . 'assets/shape-animations/frontblocks-shape-animations.css',
array(),
FRBL_VERSION
);

// Enqueue Lottie library from CDN.
wp_enqueue_script(
// Register Lottie library from CDN.
wp_register_script(
'lottie-player',
'https://cdnjs.cloudflare.com/ajax/libs/lottie-web/5.12.2/lottie.min.js',
array(),
'5.12.2',
true
);

wp_enqueue_script(
wp_register_script(
'frontblocks-shape-animations',
FRBL_PLUGIN_URL . 'assets/shape-animations/frontblocks-shape-animations.js',
array( 'lottie-player' ),
Expand Down Expand Up @@ -183,6 +183,14 @@ public function add_animation_classes_to_shape( $block_content, $block ) {
return $block_content;
}

// Enqueue frontend assets only when a shape animation block is detected.
if ( ! wp_style_is( 'frontblocks-shape-animations', 'enqueued' ) ) {
wp_enqueue_style( 'frontblocks-shape-animations' );
}
if ( ! wp_script_is( 'frontblocks-shape-animations', 'enqueued' ) ) {
wp_enqueue_script( 'frontblocks-shape-animations' );
}

// Parse JSON data.
$json_data = json_decode( $attrs['frblCustomSvgAnimationJson'], true );

Expand Down
Loading
Loading