Skip to content

Commit

Permalink
Merge pull request #1081 from WordPress-Coding-Standards/feature/fix-…
Browse files Browse the repository at this point in the history
…hook-name-can-have-dash

PrefixAllGlobalsSniff: allow "prefix + non-word char" for improved support of hook names
  • Loading branch information
GaryJones authored Aug 3, 2017
2 parents eb54d05 + 06e57ef commit 594a119
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions WordPress/Sniffs/NamingConventions/PrefixAllGlobalsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,11 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
* Check if a function/class/constant/variable name is prefixed with one of the expected prefixes.
*
* @since 0.12.0
* @since 0.14.0 Allows for other non-word characters as well as underscores to better support hook names.
*
* @param string $name Name to check for a prefix.
*
* @return bool True when the name is the prefix or starts with the prefix + an underscore.
* @return bool True when the name is the prefix or starts with the prefix + a separator.
* False otherwise.
*/
private function is_prefixed( $name ) {
Expand All @@ -660,7 +661,12 @@ private function is_prefixed( $name ) {
$prefix_found = stripos( $name, $prefix . '_' );

if ( 0 === $prefix_found ) {
// Ok, prefix found as start of hook/constant name.
// Ok, prefix found at start of hook/constant name.
return true;
}

if ( preg_match( '`^' . preg_quote( $prefix, '`' ) . '\W`i', $name ) === 1 ) {
// Ok, prefix with other non-word character found at start of hook/constant name.
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,10 @@ class Acronym_Dynamic_Hooks {
do_action( $this->parent_property ); // Warning.
}
}

// Dashes and other non-word characters are ok as a hook name separator after the prefix.
// The rule that these should be underscores is handled by another sniff.
do_action( 'acronym-action' ); // OK.
apply_filters( 'acronym/filter', $var ); // OK.
do_action( "acronym-action-{$acronym_filter_var}" ); // OK.
apply_filters( 'acronym/filter-' . $acronym_filter_var ); // OK.

0 comments on commit 594a119

Please sign in to comment.