Skip to content

Add support for dynamic hook names with concatenation#20

Merged
akirk merged 2 commits intoakirk:mainfrom
obenland:feature/dynamic-hook-names
Sep 30, 2025
Merged

Add support for dynamic hook names with concatenation#20
akirk merged 2 commits intoakirk:mainfrom
obenland:feature/dynamic-hook-names

Conversation

@obenland
Copy link
Copy Markdown
Contributor

@obenland obenland commented Sep 30, 2025

Summary

  • Adds support for dynamic WordPress hooks with concatenated variable names
  • Hook slug/title uses {$var} syntax for clarity
  • Example code uses wildcard * for practical usage

Problem

Dynamic hooks like do_action( 'activitypub_inbox_' . $type, $data, $user_id, $activity ) were being extracted as just activitypub_inbox_ (with trailing underscore), making the documentation unclear about the dynamic nature of the hook.

Current output:

  • Hook name: activitypub_inbox_
  • Example: add_action( 'activitypub_inbox_', ... )

Solution

The tool now detects concatenation patterns and properly documents dynamic hooks:

  • Hook name/slug: activitypub_inbox_{$type} (shows the variable placeholder)
  • Example code: add_action( 'activitypub_inbox_*', ... ) (uses wildcard for practical usage)

Implementation Details

  • Added extract_dynamic_parts() method to detect and extract concatenated variables
  • Added get_hook_name_for_example() method to convert {$var} to * in examples
  • Detects concatenation operator (.) after hook string
  • Extracts variable names from tokens and appends as {$variable}
  • Works with both 'prefixed' and 'default' example styles

Test Plan

  • Added test_dynamic_hook_extraction() to verify hook name includes {$var}
  • Added test_dynamic_hook_documentation_uses_wildcard() to verify examples use *
  • All 39 tests passing
  • Verified output format matches expected behavior

Example Output

For code like:

do_action( 'activitypub_inbox_' . $type, $data, $user_id, $activity );

Generated documentation:

  • Page title: activitypub_inbox_{$type}
  • Example:
add_action(
   'activitypub_inbox_*',
    function( array $data, int $user_id, Activity $activity ) {
        // Your code here.
    }
);

obenland and others added 2 commits September 30, 2025 07:51
Dynamic hooks (e.g., 'prefix_' . $var) are now properly documented:
- Hook name/slug uses {$var} syntax (e.g., activitypub_inbox_{$type})
- Example code uses wildcard * (e.g., activitypub_inbox_*)

Implementation:
- Detects concatenation operator (.) after hook string
- Extracts variable names and appends as {$var}
- Added extract_dynamic_parts() helper to extract concatenated variables
- Added get_hook_name_for_example() helper to replace {$var} with *
- Works with both 'prefixed' and 'default' example styles

Tests:
- Added test_dynamic_hook_extraction() to verify hook name format
- Added test_dynamic_hook_documentation_uses_wildcard() to verify example format
- All 39 tests passing
@akirk akirk merged commit f0f9c3c into akirk:main Sep 30, 2025
1 check passed
@akirk
Copy link
Copy Markdown
Owner

akirk commented Sep 30, 2025

Thank you!

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