Skip to content

Conversation

@bfintal
Copy link
Contributor

@bfintal bfintal commented Oct 23, 2025

Summary by CodeRabbit

  • Chores
    • Simplified and standardized script and asset loading hooks to streamline frontend enqueueing.
  • Refactor
    • Converted asset loading logic to a static-style approach for more reliable and consistent script management.

@bfintal bfintal requested a review from kaeizen October 23, 2025 12:20
@coderabbitai
Copy link

coderabbitai bot commented Oct 23, 2025

Walkthrough

Simplified several hook callback signatures to accept only $block_content. Refactored Stackable_Init to use public static properties and methods for frontend asset management and added enqueue_frontend_assets_for_content($post_content). Adjusted hook invocations to pass one argument.

Changes

Cohort / File(s) Summary
Hook callback signature updates
src/compatibility/ewww.php, src/lightbox/index.php
Removed unused $block parameter from hook callbacks stackable_load_image_optimizer_polyfill_frontend_script and stackable_load_lightbox_frontend_script; updated add_action registrations to pass a single argument.
Static refactoring of asset management
src/init.php
Converted scripts_loaded and is_main_script_loaded to public static properties; converted register_frontend_assets() and block_enqueue_frontend_assets() to public static methods; replaced instance ($this->) references with self::; updated do_action calls to pass only $block_content; added enqueue_frontend_assets_for_content($post_content) static method.

Sequence Diagram(s)

sequenceDiagram
  participant PostRenderer as Post Renderer
  participant Init as Stackable_Init (static)
  participant Hook as action: stackable/enqueue_scripts
  participant Enqueuer as WP Enqueue System

  Note over PostRenderer, Init: New flow for enqueuing assets based on post content
  PostRenderer->>Init: call enqueue_frontend_assets_for_content(post_content)
  Init->>Hook: do_action('stackable/enqueue_scripts', block_content)
  Hook->>Init: stackable_load_*_frontend_script(block_content)
  Init->>Enqueuer: wp_enqueue_script/style (tracks via self::$scripts_loaded)
  Enqueuer-->>Init: assets registered/enqueued
  Note right of Init: static properties track loaded state
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🐰 I hopped through code with glee,
Trimmed the hooks, set methods free.
Static paths and tidy calls,
Assets lined up in neat small stalls.
thump-thump

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The PR title "feat: added enqueue_frontend_assets_for_content static function" directly references a real and specific change in the changeset. The summary confirms that a new static method enqueue_frontend_assets_for_content($post_content) was introduced in src/init.php as part of the broader refactoring work. While the PR also includes additional changes such as converting properties to static, updating function signatures across multiple files, and simplifying hook registrations, the title appropriately highlights one of the key additions without needing to encompass every detail of the changeset, which aligns with the principle that a title need not cover all changes.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/enqueue_assets_function

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8e0e8c and 4fa8871.

📒 Files selected for processing (2)
  • src/compatibility/ewww.php (2 hunks)
  • src/lightbox/index.php (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/lightbox/index.php
🧰 Additional context used
🧬 Code graph analysis (1)
src/compatibility/ewww.php (1)
freemius/includes/class-freemius.php (1)
  • add_action (19532-19541)
🪛 PHPMD (2.15.0)
src/compatibility/ewww.php

9-9: Avoid unused parameters such as '$block_content'. (undefined)

(UnusedFormalParameter)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: PHP 8.2 and WP 6.7.2
  • GitHub Check: PHP 8.2 and WP latest
  • GitHub Check: PHP 8.2 and WP 6.6.2
  • GitHub Check: PHP 8.2 and WP 6.5.5
  • GitHub Check: PHP 7.3 and WP 6.5.5
  • GitHub Check: PHP 7.3 and WP latest
🔇 Additional comments (2)
src/compatibility/ewww.php (2)

9-24: LGTM! Function signature simplified correctly.

The removal of the $block parameter aligns with the PR's objective to simplify hook callbacks. The $block_content parameter, though unused in the function body (correctly flagged by PHPMD), must remain for WordPress hook compatibility since the stackable/enqueue_scripts action passes it.


29-29: Hook invocation verification confirms the code change is correct.

The hook is invoked at two locations in src/init.php (lines 248 and 301), both passing a single argument to stackable/enqueue_scripts. This matches the updated add_action call, which now defaults to 1 accepted argument.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link

github-actions bot commented Oct 23, 2025

🤖 Pull request artifacts

file commit
pr3634-stackable-3634-merge.zip 4fa8871

github-actions bot added a commit that referenced this pull request Oct 23, 2025
github-actions bot added a commit that referenced this pull request Oct 23, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/compatibility/ewww.php (1)

9-24: Unused parameter indicates potential optimization opportunity.

The $block_content parameter is never used (correctly flagged by PHPMD). The function enqueues the polyfill unconditionally on the first block render. Consider either:

  1. Using $block_content to conditionally enqueue only when needed (e.g., checking for specific image-related classes)
  2. Registering this on a different, non-block-specific hook if content inspection isn't necessary

Based on static analysis hints.

🧹 Nitpick comments (2)
src/lightbox/index.php (1)

35-35: Update the accepted argument count to match function signature.

The add_action call specifies 2 accepted arguments, but the function now only accepts 1 parameter. While PHP will ignore the extra argument, updating this to 10, 1 improves consistency.

Apply this diff:

-	add_action( 'stackable/enqueue_scripts', 'stackable_load_lightbox_frontend_script', 10, 2 );
+	add_action( 'stackable/enqueue_scripts', 'stackable_load_lightbox_frontend_script', 10, 1 );
src/compatibility/ewww.php (1)

29-29: Update the accepted argument count to match function signature.

The add_action call specifies 2 accepted arguments, but the function now only accepts 1 parameter. Update this to 10, 1 for consistency.

Apply this diff:

-		add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 2 );
+		add_action( 'stackable/enqueue_scripts', 'stackable_load_image_optimizer_polyfill_frontend_script', 10, 1 );
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ff9a16b and f8e0e8c.

📒 Files selected for processing (3)
  • src/compatibility/ewww.php (1 hunks)
  • src/init.php (9 hunks)
  • src/lightbox/index.php (1 hunks)
🧰 Additional context used
🪛 PHPMD (2.15.0)
src/compatibility/ewww.php

9-9: Avoid unused parameters such as '$block_content'. (undefined)

(UnusedFormalParameter)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: build
  • GitHub Check: PHP 7.3 and WP latest
  • GitHub Check: PHP 8.2 and WP 6.6.2
  • GitHub Check: PHP 8.2 and WP 6.7.2
  • GitHub Check: PHP 8.2 and WP latest
  • GitHub Check: PHP 8.2 and WP 6.5.5
  • GitHub Check: PHP 7.3 and WP 6.5.5
🔇 Additional comments (7)
src/lightbox/index.php (1)

13-13: LGTM! Function signature simplified appropriately.

The removal of the unused $block parameter streamlines the function signature.

src/init.php (6)

23-23: LGTM! Static properties are appropriate for tracking enqueue state.

Converting these properties to static is correct since they track global state to prevent duplicate script loading across the entire request lifecycle.

Also applies to: 30-30


95-95: LGTM! Static method conversions enable flexible asset loading.

Converting these methods to static is appropriate since they don't rely on instance state and allows them to be called directly (e.g., from the new enqueue_frontend_assets_for_content method).

Also applies to: 258-267


84-86: LGTM! Static property references are consistent throughout.

All references to $scripts_loaded and $is_main_script_loaded have been correctly updated to use self::$property syntax.

Also applies to: 174-174, 189-190, 219-225, 234-236, 240-243, 277-281, 293-295


248-248: LGTM! Hook signature simplified consistently.

The do_action call now passes only $block_content, which aligns with the simplified function signatures in the other files. Note this is a breaking change for any external code that hooks into stackable/enqueue_scripts expecting two parameters.


301-301: Clarify the behavioral difference: full post content vs individual block content.

This method calls do_action( 'stackable/enqueue_scripts', $post_content ) with the full post content, whereas load_frontend_scripts_conditionally (line 248) passes individual $block_content to the same action.

Handlers like stackable_load_lightbox_frontend_script check for specific classes (e.g., stk--has-lightbox) in the content. Passing the full post content means these checks scan a larger haystack, which still works but may have different performance characteristics.

Is this behavioral difference intentional? Consider documenting when each method should be used and how handlers should accommodate both scenarios.


275-302: LGTM! New static method enables programmatic asset loading.

The enqueue_frontend_assets_for_content method provides a clean static interface for enqueuing frontend assets based on post content. The implementation correctly:

  • Checks for Stackable blocks before enqueuing
  • Extracts unique block names to trigger per-block enqueue actions
  • Updates static state to prevent duplicate loading
  • Maintains consistency with existing asset loading patterns

Comment on lines +286 to +290
preg_match_all( '/<!-- wp:stackable\/([a-zA-Z_-]+)/', $post_content, $stackable_blocks );
// Go through each unique block name.
foreach ( $stackable_blocks[1] as $_block_name ) {
// Clean up the block name, trailing "-" from the end since it may have "--" in the end if the post content is compressed.
$block_name = trim( $_block_name, '-' );
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Verify the regex pattern and trailing dash trimming logic.

The regex /<!-- wp:stackable\/([a-zA-Z_-]+)/ captures block names, and line 290 trims trailing dashes "since it may have '--' in the end if the post content is compressed." This seems like a workaround for a specific edge case.

Ensure that:

  1. The regex pattern correctly handles all valid Stackable block names
  2. The trimming logic doesn't inadvertently truncate valid block names with trailing hyphens
  3. Compressed content scenarios are properly tested

You can verify the block name extraction logic with this script:


🏁 Script executed:

#!/bin/bash
# Description: Find all Stackable block names in the codebase to verify the regex pattern covers them

# Search for block registrations to find all valid block names
rg -n --type=php -C2 'register_block_type.*stackable/' | head -50

Length of output: 0


Now I need to search the codebase directly to find the actual block names and verify the regex pattern:Let me run shell scripts to search the codebase directly for block definitions and usage:


🏁 Script executed:

#!/bin/bash
# Find block HTML comments in the codebase to see actual block names
rg -n --type=php '<!-- wp:stackable' | head -30

Length of output: 442


🏁 Script executed:

#!/bin/bash
# Look for where blocks might be defined or registered in PHP
rg -n --type=php "stackable/" | head -50

Length of output: 3397


🏁 Script executed:

#!/bin/bash
# Search for any file that might define block names
find . -name "*.json" -o -name "block*.php" | head -20

Length of output: 572


🏁 Script executed:

#!/bin/bash
# Look for the enqueue_frontend_assets_for_content method implementation context
rg -n -A20 "enqueue_frontend_assets_for_content" --type=php | head -60

Length of output: 1463


Let me verify if there are any block names with underscores or uppercase letters:


🏁 Script executed:

#!/bin/bash
# Search all block.json files to find actual block names
find . -name "block.json" -type f -exec grep -h '"name"' {} \; | sort -u | head -50

Length of output: 1576


🏁 Script executed:

#!/bin/bash
# Look for block names in stk-block-types.php to get a complete picture
rg -n "'name' => 'stackable/" src/stk-block-types.php | head -100

Length of output: 1926


🏁 Script executed:

#!/bin/bash
# Check if there's any test data or documentation about block names
rg -n "stackable/[a-zA-Z0-9_-]+" --type=php src/ | grep -E "(stackable/[A-Z]|stackable/.*_)" | head -20

Length of output: 2369


🏁 Script executed:

#!/bin/bash
# Look at the actual HTML comment context in tests or examples
rg -B2 -A2 "<!-- wp:stackable/" --type=php --type=json | head -80

Length of output: 1280


Improve regex pattern to match WordPress block naming conventions.

The regex /<!-- wp:stackable\/([a-zA-Z_-]+)/ at line 286 includes uppercase letters [A-Z] and underscores [_] which don't appear in any actual Stackable block names and violate WordPress block naming standards. All 50+ Stackable blocks use only lowercase letters and hyphens (e.g., button-group, call-to-action, icon-button).

The trimming at line 290 is a valid workaround but indicates the regex lacks proper boundary detection. Recommended fix:

preg_match_all( '/<!-- wp:stackable\/([a-z0-9-]+)(?:\s|-->)/', $post_content, $stackable_blocks );

This uses the correct character class [a-z0-9-] and adds an explicit boundary (?:\s|-->) to eliminate reliance on trim as a corrective measure. The trim call becomes redundant but can remain for safety.

🤖 Prompt for AI Agents
In src/init.php around lines 286 to 290, the current regex /<!--
wp:stackable\/([a-zA-Z_-]+)/ is too permissive (allows uppercase and
underscores) and relies on trimming; replace it with a pattern that matches
WordPress block names only (lowercase letters, numbers and hyphens) and enforces
a boundary (e.g., use a pattern equivalent to /<!--
wp:stackable\/([a-z0-9-]+)(?:\s|-->)/ ), and then remove the redundant trim call
(you may keep it as a harmless safety net).

@bfintal bfintal assigned bfintal and unassigned kaeizen Oct 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants