From 0f5cb0bb62c8fe22cc3d176d387d2ccb5b2fa458 Mon Sep 17 00:00:00 2001 From: Office <11071985+kkmuffme@users.noreply.github.com> Date: Sun, 16 Jan 2022 20:25:48 +0100 Subject: [PATCH 01/14] make relative autoload path absolute if relative path not exists fix that it doesnt work when running from a different directory than where it's installed --- src/generate.php | 2 +- src/validate.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generate.php b/src/generate.php index f60d238..a494d20 100755 --- a/src/generate.php +++ b/src/generate.php @@ -4,7 +4,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'; $options = getopt( '', [ "input:", diff --git a/src/validate.php b/src/validate.php index 809ee4a..47e0243 100755 --- a/src/validate.php +++ b/src/validate.php @@ -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 From 34eef09cea1804e69852178dbc1ae8bbe88a0a62 Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Fri, 20 May 2022 09:17:17 +0200 Subject: [PATCH 02/14] output file name when outputting an error --- src/generate.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/generate.php b/src/generate.php index 085458f..5957309 100755 --- a/src/generate.php +++ b/src/generate.php @@ -108,7 +108,14 @@ function hooks_parse_files( array $files, string $root, array $ignore_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 ) ) { + echo $filename . "\n"; + echo $processing_errors . "\n"; + } if ( ! empty( $file->uses['hooks'] ) ) { $file_hooks = array_merge( $file_hooks, export_hooks( $file->uses['hooks'], $path ) ); From accbd15bf34f2b6758af61ed0ce0cbe65e263d4d Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Fri, 20 May 2022 09:23:00 +0200 Subject: [PATCH 03/14] write errors to stderr --- src/generate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/generate.php b/src/generate.php index 5957309..5f00121 100755 --- a/src/generate.php +++ b/src/generate.php @@ -113,8 +113,8 @@ function hooks_parse_files( array $files, string $root, array $ignore_hooks ) : $file->process(); $processing_errors = ob_get_clean(); if ( !empty( $processing_errors ) ) { - echo $filename . "\n"; - echo $processing_errors . "\n"; + fwrite( STDERR, $filename . PHP_EOL ); + fwrite( STDERR, $processing_errors . PHP_EOL ); } if ( ! empty( $file->uses['hooks'] ) ) { From 3dd2f0f0df5f3bd8cab064a2959769d0c05da20a Mon Sep 17 00:00:00 2001 From: kkmuffme <11071985+kkmuffme@users.noreply.github.com> Date: Wed, 25 May 2022 09:21:27 +0200 Subject: [PATCH 04/14] skip file if it's non readable --- src/generate.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/generate.php b/src/generate.php index 5f00121..09b9fb1 100755 --- a/src/generate.php +++ b/src/generate.php @@ -103,6 +103,9 @@ 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 ); From b59a740010c7abeb51fa0a5d3eb5dd1294a758bf Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 3 Jun 2022 13:44:30 +0200 Subject: [PATCH 05/14] Version 0.7.4. --- package-lock.json | 2 +- package.json | 2 +- schema.json | 2 +- src/generate.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7634e51..6a919d6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@johnbillion/wp-hooks-generator", - "version": "0.7.3", + "version": "0.7.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c43a070..6c100ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@johnbillion/wp-hooks-generator", - "version": "0.7.3", + "version": "0.7.4", "description": "Scans the WordPress actions and filters in your code and saves them as JSON.", "private": true, "repository": { diff --git a/schema.json b/schema.json index c892a3a..7d14e53 100644 --- a/schema.json +++ b/schema.json @@ -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/johnbillion/wp-hooks-generator/blob/0.7.4/schema.json", "title": "HooksContainer", "description": "The container for the list of hooks", "type": "object", diff --git a/src/generate.php b/src/generate.php index a275c63..16c557b 100755 --- a/src/generate.php +++ b/src/generate.php @@ -237,7 +237,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/johnbillion/wp-hooks-generator/0.7.4/schema.json', 'hooks' => $actions, ]; @@ -249,7 +249,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/johnbillion/wp-hooks-generator/0.7.4/schema.json', 'hooks' => $filters, ]; From 25edca9a671b1ea1525c0b29fb83b7bff5521323 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 3 Jun 2022 15:45:38 +0200 Subject: [PATCH 06/14] Parse a "Possible hook names include" list and store the result as aliases. --- src/generate.php | 50 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/generate.php b/src/generate.php index 16c557b..c4898ce 100755 --- a/src/generate.php +++ b/src/generate.php @@ -4,6 +4,8 @@ namespace JohnBillion\WPHooksGenerator; +use DOMDocument; + require_once file_exists( 'vendor/autoload.php' ) ? 'vendor/autoload.php' : dirname( __DIR__, 4 ) . '/vendor/autoload.php'; $options = getopt( '', [ @@ -217,18 +219,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 + */ +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( '', 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 From e18b3bfd4bdc70269ecf9f7403e3d62de0603ca4 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 3 Jun 2022 15:45:50 +0200 Subject: [PATCH 07/14] Update the schema. --- interface/index.d.ts | 4 ++++ schema.json | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/interface/index.d.ts b/interface/index.d.ts index 2ce8a58..6a8b962 100644 --- a/interface/index.d.ts +++ b/interface/index.d.ts @@ -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 */ diff --git a/schema.json b/schema.json index 7d14e53..bb99627 100644 --- a/schema.json +++ b/schema.json @@ -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", From aa4ca214515c9259660e46c576094b693eb41e88 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 3 Jun 2022 15:46:35 +0200 Subject: [PATCH 08/14] The libxml extension is needed for DOMDocument. --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 0c7f077..eb9f61c 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,7 @@ ], "require": { "php": ">=7", + "ext-libxml": "*", "johnbillion/wp-parser-lib": "^1.3.0" }, "require-dev": { From 0cf7a67aaf0d6e41ca5abc88a8ebba5e7998e65d Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Fri, 3 Jun 2022 17:31:06 +0200 Subject: [PATCH 09/14] Version 0.8.0. --- package-lock.json | 2 +- package.json | 2 +- schema.json | 2 +- src/generate.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6a919d6..288cbfc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@johnbillion/wp-hooks-generator", - "version": "0.7.4", + "version": "0.8.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 6c100ef..a951ada 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@johnbillion/wp-hooks-generator", - "version": "0.7.4", + "version": "0.8.0", "description": "Scans the WordPress actions and filters in your code and saves them as JSON.", "private": true, "repository": { diff --git a/schema.json b/schema.json index bb99627..e4ffdca 100644 --- a/schema.json +++ b/schema.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://github.com/johnbillion/wp-hooks-generator/blob/0.7.4/schema.json", + "$id": "https://github.com/johnbillion/wp-hooks-generator/blob/0.8.0/schema.json", "title": "HooksContainer", "description": "The container for the list of hooks", "type": "object", diff --git a/src/generate.php b/src/generate.php index c4898ce..689d4c9 100755 --- a/src/generate.php +++ b/src/generate.php @@ -273,7 +273,7 @@ function parse_aliases( string $html ) : array { } ) ); $actions = [ - '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.7.4/schema.json', + '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.8.0/schema.json', 'hooks' => $actions, ]; @@ -285,7 +285,7 @@ function parse_aliases( string $html ) : array { } ) ); $filters = [ - '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.7.4/schema.json', + '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.8.0/schema.json', 'hooks' => $filters, ]; From f45af367a78192e42eefb938fd603ea1590475c6 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sat, 11 Jun 2022 23:40:26 +0300 Subject: [PATCH 10/14] This isn't necessary. --- src/generate.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/generate.php b/src/generate.php index 689d4c9..679f10a 100755 --- a/src/generate.php +++ b/src/generate.php @@ -199,12 +199,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 ) ); } From 0abcbff0b058dfaaa8f9cfd4596ae864b13e3c04 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 3 Jul 2022 13:25:15 +0100 Subject: [PATCH 11/14] Rename the package. --- composer.json | 2 +- package-lock.json | 2 +- package.json | 6 +++--- readme.md | 4 +++- schema.json | 2 +- src/generate.php | 4 ++-- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index eb9f61c..b15c50c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/package-lock.json b/package-lock.json index 288cbfc..2a2a55f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "@johnbillion/wp-hooks-generator", + "name": "wp-hooks/generator", "version": "0.8.0", "lockfileVersion": 1, "requires": true, diff --git a/package.json b/package.json index a951ada..e27aca7 100644 --- a/package.json +++ b/package.json @@ -5,12 +5,12 @@ "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" @@ -18,5 +18,5 @@ "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" } diff --git a/readme.md b/readme.md index 37a78bd..d5ab63a 100644 --- a/readme.md +++ b/readme.md @@ -8,7 +8,9 @@ Note: If you just want the hook files without generating them yourself, use the ## Installation - composer require johnbillion/wp-hooks-generator +```shell +composer require wp-hooks/generator +``` ## Generating the Hook Files diff --git a/schema.json b/schema.json index e4ffdca..7863042 100644 --- a/schema.json +++ b/schema.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://github.com/johnbillion/wp-hooks-generator/blob/0.8.0/schema.json", + "$id": "https://github.com/wp-hooks/generator/blob/0.8.0/schema.json", "title": "HooksContainer", "description": "The container for the list of hooks", "type": "object", diff --git a/src/generate.php b/src/generate.php index 679f10a..7083a9f 100755 --- a/src/generate.php +++ b/src/generate.php @@ -268,7 +268,7 @@ function parse_aliases( string $html ) : array { } ) ); $actions = [ - '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.8.0/schema.json', + '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.8.0/schema.json', 'hooks' => $actions, ]; @@ -280,7 +280,7 @@ function parse_aliases( string $html ) : array { } ) ); $filters = [ - '$schema' => 'https://raw.githubusercontent.com/johnbillion/wp-hooks-generator/0.8.0/schema.json', + '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.8.0/schema.json', 'hooks' => $filters, ]; From 924336c99a15cff09d81db3a6f068232a519a67e Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 3 Jul 2022 13:25:25 +0100 Subject: [PATCH 12/14] Docs. --- package.json | 2 +- readme.md | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index e27aca7..0be97af 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@johnbillion/wp-hooks-generator", "version": "0.8.0", - "description": "Scans the WordPress actions and filters in your code and saves them as JSON.", + "description": "Generates a JSON representation of the WordPress actions and filters in your code", "private": true, "repository": { "type": "git", diff --git a/readme.md b/readme.md index d5ab63a..93bea56 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# 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. @@ -14,14 +14,16 @@ 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']; From 0abd3b393f25e00345a27b09238d925c1fb835d7 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 3 Jul 2022 13:33:17 +0100 Subject: [PATCH 13/14] More docs. --- composer.json | 9 +++++++++ readme.md | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b15c50c..829da74 100644 --- a/composer.json +++ b/composer.json @@ -27,5 +27,14 @@ "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": "*" } } diff --git a/readme.md b/readme.md index 93bea56..4e65d53 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ Generates a JSON representation of the WordPress actions and filters in your cod 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 From 6c7f173d85452db52a59a3a6bb943cbe7d845cde Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 3 Jul 2022 13:35:07 +0100 Subject: [PATCH 14/14] Version 0.9.0. --- package-lock.json | 2 +- package.json | 2 +- schema.json | 2 +- src/generate.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2a2a55f..3cd76a7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "wp-hooks/generator", - "version": "0.8.0", + "version": "0.9.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 0be97af..ad6eb0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@johnbillion/wp-hooks-generator", - "version": "0.8.0", + "version": "0.9.0", "description": "Generates a JSON representation of the WordPress actions and filters in your code", "private": true, "repository": { diff --git a/schema.json b/schema.json index 7863042..da0abed 100644 --- a/schema.json +++ b/schema.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://github.com/wp-hooks/generator/blob/0.8.0/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", diff --git a/src/generate.php b/src/generate.php index 7083a9f..e4a9b93 100755 --- a/src/generate.php +++ b/src/generate.php @@ -268,7 +268,7 @@ function parse_aliases( string $html ) : array { } ) ); $actions = [ - '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.8.0/schema.json', + '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.9.0/schema.json', 'hooks' => $actions, ]; @@ -280,7 +280,7 @@ function parse_aliases( string $html ) : array { } ) ); $filters = [ - '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.8.0/schema.json', + '$schema' => 'https://raw.githubusercontent.com/wp-hooks/generator/0.9.0/schema.json', 'hooks' => $filters, ];