Skip to content

Commit

Permalink
Merge branch 'wp-hooks:master' into include-wp-cron-schedule-hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme authored Jul 10, 2022
2 parents 8de5fe0 + 6c7f173 commit 8647243
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 32 deletions.
12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "johnbillion/wp-hooks-generator",
"name": "wp-hooks/generator",
"description": "Generates a JSON representation of the WordPress actions and filters in your code",
"type": "library",
"license": "GPL-3.0-or-later",
Expand All @@ -21,10 +21,20 @@
],
"require": {
"php": ">=7",
"ext-libxml": "*",
"johnbillion/wp-parser-lib": "^1.3.0"
},
"require-dev": {
"oomphinc/composer-installers-extender": "^2",
"opis/json-schema": "^1"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/johnbillion"
}
],
"replace": {
"johnbillion/wp-hooks-generator": "*"
}
}
4 changes: 4 additions & 0 deletions interface/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export interface Hook {
* The hook name
*/
name: string;
/**
* Aliases of the hook name
*/
aliases?: string[];
/**
* The relative name of the file containing the hook
*/
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{
"name": "@johnbillion/wp-hooks-generator",
"version": "0.7.3",
"description": "Scans the WordPress actions and filters in your code and saves them as JSON.",
"version": "0.9.0",
"description": "Generates a JSON representation of the WordPress actions and filters in your code",
"private": true,
"repository": {
"type": "git",
"url": "git+https://github.com/johnbillion/wp-hooks-generator.git"
"url": "git+https://github.com/wp-hooks/generator.git"
},
"author": "John Blackbourn",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/johnbillion/wp-hooks-generator/issues"
"url": "https://github.com/wp-hooks/generator/issues"
},
"dependencies": {
"json-schema-to-typescript": "^7.1"
},
"scripts": {
"generate-interfaces": "json2ts --input schema.json --output=interface/index.d.ts"
},
"homepage": "https://github.com/johnbillion/wp-hooks-generator#readme"
"homepage": "https://github.com/wp-hooks/generator#readme"
}
16 changes: 10 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
# wp-hooks-generator
# WP Hooks Generator

Generates a JSON representation of the WordPress actions and filters in your code. Can be used with WordPress plugins, themes, and core.

Note: If you just want the hook files without generating them yourself, use the following packages instead:

* [johnbillion/wp-hooks](https://github.com/johnbillion/wp-hooks) for WordPress core
* [wp-hooks/wordpress-core](https://github.com/wp-hooks/wordpress-core) for WordPress core

## Installation

composer require johnbillion/wp-hooks-generator
```shell
composer require wp-hooks/generator
```

## Generating the Hook Files

./bin/wp-hooks-generator --input=src --output=hooks
```shell
./bin/wp-hooks-generator --input=src --output=hooks
```

## Usage of the Generated Hook Files in PHP

```php
// Get hooks as JSON:
$actions_json = file_get_contents( 'hook/actions.json' );
$filters_json = file_get_contents( 'hook/filters.json' );
$actions_json = file_get_contents( 'hooks/actions.json' );
$filters_json = file_get_contents( 'hooks/filters.json' );

// Convert hooks to PHP:
$actions = json_decode( $actions_json, true )['hooks'];
Expand Down
9 changes: 8 additions & 1 deletion schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://github.com/johnbillion/wp-hooks-generator/blob/0.7.3/schema.json",
"$id": "https://github.com/wp-hooks/generator/blob/0.9.0/schema.json",
"title": "HooksContainer",
"description": "The container for the list of hooks",
"type": "object",
Expand Down Expand Up @@ -41,6 +41,13 @@
"init"
]
},
"aliases": {
"description": "Aliases of the hook name",
"type": "array",
"items": {
"type": "string"
}
},
"file": {
"description": "The relative name of the file containing the hook",
"type": "string",
Expand Down
73 changes: 57 additions & 16 deletions src/generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace JohnBillion\WPHooksGenerator;

require_once 'vendor/autoload.php';
use DOMDocument;

require_once file_exists( 'vendor/autoload.php' ) ? 'vendor/autoload.php' : dirname( __DIR__, 4 ) . '/vendor/autoload.php';

$options = getopt( '', [
"input:",
Expand Down Expand Up @@ -103,12 +105,22 @@ function hooks_parse_files( array $files, string $root, array $ignore_hooks ) :
$output = array();

foreach ( $files as $filename ) {
if ( !is_readable( $filename ) ) {
continue;
}
$file = new \WP_Parser\File_Reflector( $filename );
$file_hooks = [];
$path = ltrim( substr( $filename, strlen( $root ) ), DIRECTORY_SEPARATOR );
$file->setFilename( $path );

// should throw things, but for some reason returns errors instead, so we just collect them manually
ob_start();
$file->process();
$processing_errors = ob_get_clean();
if ( !empty( $processing_errors ) ) {
fwrite( STDERR, $filename . PHP_EOL );
fwrite( STDERR, $processing_errors . PHP_EOL );
}

if ( ! empty( $file->uses['hooks'] ) ) {
$file_hooks = array_merge( $file_hooks, export_hooks( $file->uses['hooks'], $path ) );
Expand Down Expand Up @@ -267,12 +279,7 @@ function( array $matches ) : string {
foreach ( $docblock->getTags() as $i => $tag ) {
$content = '';

if ( method_exists( $tag, 'getVersion' ) ) {
$version = $tag->getVersion();
if ( ! empty( $version ) ) {
$content = $version;
}
} else {
if ( ! method_exists( $tag, 'getVersion' ) ) {
$content = $tag->getDescription();
$content = \WP_Parser\format_description( preg_replace( '#\n\s+#', ' ', $content ) );
}
Expand All @@ -287,18 +294,52 @@ function( array $matches ) : string {
$doc['long_description'] = '';
}

$out[] = array(
'name' => $hook->getName(),
'file' => $path,
'type' => $hook->getType(),
'doc' => $doc,
'args' => count( $hook->getNode()->args ) - 1,
);
$aliases = parse_aliases( $doc['long_description_html'] );

$result = [];

$result['name'] = $hook->getName();

if ( $aliases ) {
$result['aliases'] = $aliases;
}

$result['file'] = $path;
$result['type'] = $hook->getType();
$result['doc'] = $doc;
$result['args'] = count( $hook->getNode()->args ) - 1;

$out[] = $result;
}

return $out;
}

/**
* @return array<int, string>
*/
function parse_aliases( string $html ) : array {
if ( false === strpos( $html, 'Possible hook names include' ) ) {
return [];
}

$aliases = [];

$html = explode( 'Possible hook names include', $html, 2 );
$html = explode( '</ul>', end( $html ) );

$dom = new DOMDocument();
$dom->loadHTML( reset( $html ) );

foreach ( $dom->getElementsByTagName( 'li' ) as $li ) {
$aliases[] = $li->nodeValue;
}

sort( $aliases );

return $aliases;
}

$output = hooks_parse_files( $files, $source_dir, $ignore_hooks );

// Actions
Expand All @@ -307,7 +348,7 @@ function( array $matches ) : string {
} ) );

$actions = [
'$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.7.3/schema.json',
'$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.9.0/schema.json',
'hooks' => $actions,
];

Expand All @@ -319,7 +360,7 @@ function( array $matches ) : string {
} ) );

$filters = [
'$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.7.3/schema.json',
'$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.9.0/schema.json',
'hooks' => $filters,
];

Expand Down
2 changes: 1 addition & 1 deletion src/validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace JohnBillion\WPHooksGenerator;

require_once 'vendor/autoload.php';
require_once file_exists( 'vendor/autoload.php' ) ? 'vendor/autoload.php' : dirname( __DIR__, 4 ) . '/vendor/autoload.php';

use Opis\JsonSchema\{
Validator, ValidationResult, ValidationError, Schema
Expand Down

0 comments on commit 8647243

Please sign in to comment.