From 41f438c0b270f00425be1c2c80818154b7fc0c9d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 23 Feb 2015 14:40:16 +0000 Subject: [PATCH] Generate hook docs --- Gruntfile.js | 4 +- apigen.neon | 2 +- apigen/hook-docs.php | 183 +++++++++++++++++++ apigen/theme-woocommerce/@layout.latte | 9 +- apigen/theme-woocommerce/resources/style.css | 4 + 5 files changed, 197 insertions(+), 5 deletions(-) create mode 100644 apigen/hook-docs.php diff --git a/Gruntfile.js b/Gruntfile.js index 4a6a9d62af801..41431933632f3 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -211,7 +211,9 @@ module.exports = function( grunt ) { }, apigen: { command: [ - 'apigen generate' + 'apigen generate', + 'cd apigen', + 'php hook-docs.php' ].join( '&&' ) } }, diff --git a/apigen.neon b/apigen.neon index 155e3e76d134e..7f2937c2f430c 100644 --- a/apigen.neon +++ b/apigen.neon @@ -50,4 +50,4 @@ deprecated: true todo: true # add link to ZIP archive of documentation -download: true \ No newline at end of file +download: false \ No newline at end of file diff --git a/apigen/hook-docs.php b/apigen/hook-docs.php new file mode 100644 index 0000000000000..3beed33bfd5af --- /dev/null +++ b/apigen/hook-docs.php @@ -0,0 +1,183 @@ +' . $hook . ''; + } + + public static function process_hooks() { + // If we have one, get the PHP files from it. + $template_files = self::get_files( '*.php', GLOB_MARK, '../templates/' ); + $template_files[] = '../includes/wc-template-functions.php'; + $template_files[] = '../includes/wc-template-hooks.php'; + + $shortcode_files = self::get_files( '*.php', GLOB_MARK, '../includes/shortcodes/' ); + $widget_files = self::get_files( '*.php', GLOB_MARK, '../includes/widgets/' ); + $admin_files = self::get_files( '*.php', GLOB_MARK, '../includes/admin/' ); + $class_files = self::get_files( '*.php', GLOB_MARK, '../includes/' ); + $other_files = array( + '../woocommerce.php' + ); + + self::$files_to_scan = array( + 'Template Hooks' => $template_files, + 'Shortcode Hooks' => $shortcode_files, + 'Widget Hooks' => $widget_files, + 'Class Hooks' => $class_files, + 'Admin Hooks' => $admin_files, + 'Other Hooks' => $other_files, + ); + + $scanned = array(); + + ob_start(); + + echo '
'; + echo '

Action and Filter Hook Reference

'; + echo '

The following is a full list of actions and filters found in WooCommerce core.

'; + + foreach ( self::$files_to_scan as $heading => $files ) { + self::$custom_hooks_found = array(); + + foreach ( $files as $f ) { + self::$current_file = basename( $f ); + $tokens = token_get_all( file_get_contents( $f ) ); + $token_type = false; + $current_class = ''; + $current_function = ''; + + if ( in_array( self::$current_file, $scanned ) ) { + continue; + } + + $scanned[] = self::$current_file; + + foreach ( $tokens as $index => $token ) { + if ( is_array( $token ) ) { + if ( $token[0] == T_CLASS ) { + $token_type = 'class'; + } elseif ( $token[0] == T_FUNCTION ) { + $token_type = 'function'; + } elseif ( $token[1] === 'do_action' ) { + $token_type = 'action'; + } elseif ( $token[1] === 'apply_filters' ) { + $token_type = 'filter'; + } elseif ( $token_type && ! empty( trim( $token[1] ) ) ) { + switch ( $token_type ) { + case 'class' : + $current_class = $token[1]; + break; + case 'function' : + $current_function = $token[1]; + break; + case 'filter' : + case 'action' : + $hook = trim( $token[1], "'" ); + if ( isset( self::$custom_hooks_found[ $hook ] ) ) { + self::$custom_hooks_found[ $hook ]['file'][] = self::$current_file; + } else { + self::$custom_hooks_found[ $hook ] = array( + 'line' => $token[2], + 'class' => $current_class, + 'function' => $current_function, + 'file' => array( self::$current_file ), + 'type' => $token_type + ); + } + break; + } + $token_type = false; + } + } + } + } + + foreach ( self::$custom_hooks_found as $hook => $details ) { + if ( ! strstr( $hook, 'woocommerce' ) && ! strstr( $hook, 'product' ) && ! strstr( $hook, 'wc_' ) ) { + unset( self::$custom_hooks_found[ $hook ] ); + } + } + + ksort( self::$custom_hooks_found ); + + if ( ! empty( self::$custom_hooks_found ) ) { + echo '

' . $heading . '

'; + + echo ''; + + foreach ( self::$custom_hooks_found as $hook => $details ) { + echo ' + + + + ' . "\n"; + } + + echo '
HookTypeFile(s)
' . self::get_hook_link( $hook, $details ) . '' . $details['type'] . '' . implode( ', ', array_unique( $details['file'] ) ) . '
'; + } + } + + echo '