Skip to content

Instantly share code, notes, and snippets.

@kopepasah
Last active December 23, 2015 22:19
Show Gist options
  • Save kopepasah/6702403 to your computer and use it in GitHub Desktop.
Save kopepasah/6702403 to your computer and use it in GitHub Desktop.
Needed a way to list all the theme hooks in iThemes Exchange templates. Output is structured for Mediawiki, but can be modified to suit your needs.
<?php
$GLOBALS['hooks'] = array();
$GLOBALS['duplicates'] = array();
function list_theme_hooks( $directory ) {
global $hooks, $duplicates;
$files = array_diff( scandir( $directory ), array( '.', '..', '_list-files.php', '_list-hooks.php', '_get-contents.php', '.DS_Store', 'nonce.php', '_list-hooks-duplicates.php' ) );
foreach ( $files as $file ) {
if ( dirname( __FILE__ ) == $directory ) {
if ( is_dir( $directory . '/' . $file ) ) {
$folder = $file . '.php';
}
if ( $folder != $file ) {
if ( is_dir( $file ) ) {
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( dirname( __FILE__ ) . '/' . $file . '.php' ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action ) {
if ( isset( $hooks[$action] ) ) {
$duplicates[$action][] = $file . '.php';
} else {
$hooks[$action] = $file . '.php';
}
}
}
} else {
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( dirname( __FILE__ ) . '/' . $file ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action ) {
if ( isset( $hooks[$action] ) ) {
$duplicates[$action][] = $file;
} else {
$hooks[$action] = $file;
}
}
}
}
}
} else {
if ( ! is_dir( $directory . '/' . $file ) ) {
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( $directory . '/' . $file ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action ) {
if ( isset( $hooks[$action] ) ) {
$duplicates[$action][] = $file;
} else {
$hooks[$action] = $file;
}
}
}
}
}
if ( is_dir( $directory . '/' . $file ) )
list_theme_hooks( $directory . '/' . $file );
}
}
list_theme_hooks( dirname( __FILE__ ) );
echo "<pre style='color:black;background:white;padding:15px;font-family:\"Courier New\",Courier,monospace;font-size:12px;white-space:pre-wrap;text-align:left;max-width:100%;'>";
print_r( $hooks );
echo "</pre>";
echo "<pre style='color:black;background:white;padding:15px;font-family:\"Courier New\",Courier,monospace;font-size:12px;white-space:pre-wrap;text-align:left;max-width:100%;'>";
print_r( $duplicates );
echo "</pre>";
<?php
function list_theme_hooks( $directory ) {
$files = array_diff( scandir( $directory ), array( '.', '..', '_list-files.php', '_list-hooks.php', '_get-contents.php', '.DS_Store', 'nonce.php' ) );
foreach ( $files as $file ) {
if ( dirname( __FILE__ ) == $directory ) {
if ( is_dir( $directory . '/' . $file ) ) {
$folder = $file . '.php';
}
if ( $folder != $file ) {
if ( is_dir( $file ) ) {
header('Content-Type: text/plain');
echo "\n" . '*[[Exchange_Theme_Templates:_' . $file . '.php|' . $file . '.php]]' . "\n";
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( dirname( __FILE__ ) . '/' . $file . '.php' ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action )
echo '**[[Exchange_Theme_Hooks:_' . $action .'|' . $action .']]' . "\n";
} else {
echo "Currently no hooks for this file.\n";
}
} else {
header('Content-Type: text/plain');
echo "\n" . '*[[Exchange_Theme_Templates:_' . $file . '|' . $file . ']]' . "\n";
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( dirname( __FILE__ ) . '/' . $file ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action )
echo '**[[Exchange_Theme_Hooks:_' . $action .'|' . $action .']]' . "\n";
} else {
echo "**Currently no hooks for this file.\n";
}
}
}
} else {
if ( ! is_dir( $directory . '/' . $file ) ) {
header('Content-Type: text/plain');
echo "\n" . '*[[Exchange_Theme_Templates:_' . str_replace( dirname( __FILE__ ) . '/', '', $directory ) . '/' . $file . '|' . str_replace( dirname( __FILE__ ) . '/', '', $directory ) . '/' . $file . ']]' . "\n";
if ( preg_match_all( "|" . preg_quote( '<?php do_action(', '/') . "(.*?);|", file_get_contents( $directory . '/' . $file ), $matches ) ) {
$actions = str_replace( ' ', '', str_replace( '<?php do_action( \'', '', str_replace( '\' );', '', $matches[0] ) ) );
foreach ( $actions as $action )
echo '**[[Exchange_Theme_Hooks:_' . $action .'|' . $action .']]' . "\n";
} else {
echo "**Currently no hooks for this file.\n";
}
}
}
if ( is_dir( $directory . '/' . $file ) )
list_theme_hooks( $directory . '/' . $file );
}
}
list_theme_hooks( dirname( __FILE__ ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment