WP_Interactivity_API::parse_directive_name( string $directive_name ): array|null
- Since
- 6.9.0
- Source
wp-includes/interactivity-api/class-wp-interactivity-api.php:798
Description
- Suffix: An optional suffix used during directive processing, extracted after the first double hyphen "--".
- Unique ID: An optional unique identifier, extracted after the first triple hyphen "---".
This function has an equivalent version for the client side.
See parseDirectiveName in https://github.com/WordPress/gutenberg/blob/trunk/packages/interactivity/src/vdom.ts:
An empty suffix or unique ID is normalized to null, but the string "0" is preserved. The client's || null discards only the empty string, since every non-empty string is truthy in JavaScript. Do not use empty() for these checks: it would discard "0" and diverge from the client.
Compatibility
- WordPress
- since 6.9.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.
Parameters
$directive_namestring- The directive attribute name.
Return value
array|null- An array containing the directive prefix, optional suffix, and optional unique ID, or null if the directive name cannot be parsed.
Performance profile
How much work a call to WP_Interactivity_API::parse_directive_name() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 7–53
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Interactivity_API::parse_directive_name() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–18 | none |
$suffix_index !== 0 && $suffix_index !== false | 32–53 | strspn() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 81 | 7–53 | 12 | |
| 8.5 | 81 | 7–53 | 12 | |
| 8.4 | 81 | 7–53 | 12 | 32 fewer instructions than PHP 8.3 |
| 8.3 | 113 | 13–82 | 12 | |
| 8.2 | 113 | 13–82 | 12 | |
| 8.1 | 113 | 13–82 | 12 | |
| 7.4 | 113 | 13–82 | 12 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Used by · 2
- WP_Interactivity_API::_process_directives()Processes the interactivity directives contained within the HTML content and updates the markup accordingly.
- WP_Interactivity_API::get_directive_entries()Parse the HTML element and get all the valid directives with the given prefix.
Source code
private function parse_directive_name( string $directive_name ): ?array { // Remove the first 8 characters (assumes "data-wp-" prefix) $name = (string) substr( $directive_name, 8 ); // Ensure the name only contains valid characters (anything a-z, A-Z, 0-9, -, or _). if ( 1 !== preg_match( '/^[a-zA-Z0-9\-_]+$/', $name ) ) { return null; } // Find the first occurrence of '--' to separate the prefix. $suffix_index = strpos( $name, '--' ); /* * A prefix cannot begin with a hyphen, so a name which does is not a directive at all. This * covers both a lone leading hyphen, as in "data-wp--bind", and a leading double hyphen, as * in "data-wp---foo", where treating the hyphens as a suffix separator would instead leave * the prefix empty. It also covers "data-wp----unique-id", where only a unique ID is supplied * without any prefix or suffix. */ if ( 0 === $suffix_index || '-' === $name[0] ) { return null; } // Without a '--' the whole name is the prefix. (This naturally also means there is no unique ID after '---'.) if ( false === $suffix_index ) { return array( 'prefix' => $name, 'suffix' => null, 'unique_id' => null, ); } $prefix = substr( $name, 0, $suffix_index ); $remaining = substr( $name, $suffix_index ); // If remaining starts with '---' but not '----', it's a unique_id if ( 3 === strspn( $remaining, '-' ) ) { $unique_id = (string) substr( $remaining, 3 ); return array( 'prefix' => $prefix, 'suffix' => null, 'unique_id' => '' === $unique_id ? null : $unique_id, ); } // Otherwise, remove the first two dashes for a potential suffix $suffix = (string) substr( $remaining, 2 ); // Look for '---' in the suffix for a unique_id $unique_id_index = strpos( $suffix, '---' ); if ( false !== $unique_id_index && '-' !== ( $suffix[ $unique_id_index + 3 ] ?? '' ) ) { $unique_id = (string) substr( $suffix, $unique_id_index + 3 ); $suffix = (string) substr( $suffix, 0, $unique_id_index ); return array( 'prefix' => $prefix, 'suffix' => '' === $suffix ? null : $suffix, 'unique_id' => '' === $unique_id ? null : $unique_id, ); } return array( 'prefix' => $prefix, 'suffix' => '' === $suffix ? null : $suffix, 'unique_id' => null, ); }Changelog
Introduced in 6.9.0. One change between 6.9.7 and 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
array to array|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/interactivity-api/class-wp-interactivity-api.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.