Skip to content

Commit

Permalink
Normalize hook names
Browse files Browse the repository at this point in the history
- Find hooks in a case insensitive way
- If hook is unrecognized, at least capitalize it,
  remove spaces and remove invalid characters.
  • Loading branch information
mooeypoo committed Jan 14, 2019
1 parent 7142c20 commit 25ad5e1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
34 changes: 32 additions & 2 deletions includes/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
* Read hook data and parse it for processing twig templates
*/
class Hooks {
protected $data;
protected $templating;
protected $lookupMap = [];

public function __construct( $templating = null ) {
$filename = dirname( __DIR__ ) . '/templates/_hooks/data/hooks.json';
$this->data = json_decode( file_get_contents( $filename ), true );
$this->templating = $templating;
if ( !$this->templating ) {
$this->templating = new Templating();
}

// Create a search map (lowercase => proper case) for searches
foreach ( array_keys( $this->data ) as $hookName ) {
$this->lookupMap[ strtolower( $hookName ) ] = $hookName;
}
}

public function getHookNames() {
Expand All @@ -35,7 +44,7 @@ public function getHooksMethods( $hookNames = [] ) {
}

public function getHookContent( $hookName ) {
$hookName = ucfirst( $hookName );
$hookName = $this->normalizeHookName( $hookName );
$data = $this->getHookData( $hookName );
if ( !$data ) {
$data = [
Expand Down Expand Up @@ -106,12 +115,33 @@ public static function createFunctionNameFromHookName( $hookName ) {
return 'on' . $className;
}

public function getHookData( $hookName = null ) {
public function getHookData( $hookName = '' ) {
$hookName = $this->normalizeHookName( $hookName );

$data = null;
if ( $hookName !== null ) {
$data = Generator::getObjectProp( $this->data, [ $hookName ] );
}

return $data;
}

/**
* Normalize the given hook name:
* - If the name is found in the system, normalize it
* to use the proper capitalization.
* - If it is not found, return it as-is
*
* @param string $hookName Given hook name
* @return string Normalized hook name
*/
public function normalizeHookName( $hookName = '' ) {
$foundHookName = Generator::getObjectProp( $this->lookupMap, [ strtolower( $hookName ) ] );

if ( !$foundHookName ) {
$hookName = ucfirst( preg_replace( '/[^A-Za-z0-9\-]/', '', $hookName ) );
}
// Unrecognized hooks should at least be capitalized
return $foundHookName ? $foundHookName : ucfirst( $hookName );
}
}
19 changes: 19 additions & 0 deletions tests/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,23 @@ public function testGetHookContent() {
);
}
}
public function testNormalizeHookName() {
$cases = [
'articledelete' => 'ArticleDelete',
'adDNeWaCcOuNt' => 'AddNewAccount',
'editpage::importformdata' => 'EditPage::importFormData',
'fooBarBaz' => 'FooBarBaz', // Not found; capitalize
'f%oo B*&a%rB!a|z123 ' => 'FooBarBaz123', // Not found; clean up and capitalize
];

$hooks = new Hooks();
foreach ( $cases as $lookup => $expected ) {
$this->assertEquals(
$expected,
$hooks->normalizeHookName( $lookup ),
"Normalizing: $lookup => $expected"
);
}

}
}

0 comments on commit 25ad5e1

Please sign in to comment.