Skip to content

Instantly share code, notes, and snippets.

@CharlieEtienne
Last active May 3, 2019 09:59
Show Gist options
  • Save CharlieEtienne/371fb5432a1d3758fcb3a6e203469d47 to your computer and use it in GitHub Desktop.
Save CharlieEtienne/371fb5432a1d3758fcb3a6e203469d47 to your computer and use it in GitHub Desktop.
Display the names of all files that have been included using include, include_once, require or require_once.
<?php
if ( ! function_exists( 'display_included_files' ) ) {
/**
* Display the names of all files that have been included using include, include_once, require or require_once.
*
* @param bool|array $filters Array of strings to filter results. Ex: ['my-template', 'elementor'].
* @param bool $display Display on screen or in javascript console. Default false.
*/
function display_included_files( $filters = false, $display = false ) {
$files = get_included_files();
if ( $filters && is_array( $filters ) ) {
foreach ( $files as $key => $file ) {
foreach ( $filters as $filter ) {
if ( strstr( $file, $filter ) === false ) {
unset( $files[ $key ] );
}
}
}
}
if ( $display ) {
$text = var_export( $files, true );
$text = trim($text);
$text = highlight_string( "INCLUDED FILES: \n" . "<?php " . $text, true); // highlight_string() requires opening PHP tag or otherwise it will not colorize the text
$text = trim($text);
$text = preg_replace("|^\\<code\\>\\<span style\\=\"color\\: #[a-fA-F0-9]{0,6}\"\\>|", "", $text, 1); // remove prefix
$text = preg_replace("|\\</code\\>\$|", "", $text, 1); // remove suffix 1
$text = trim($text); // remove line breaks
$text = preg_replace("|\\</span\\>\$|", "", $text, 1); // remove suffix 2
$text = trim($text); // remove line breaks
$text = preg_replace("|^(\\<span style\\=\"color\\: #[a-fA-F0-9]{0,6}\"\\>)(&lt;\\?php&nbsp;)(.*?)(\\</span\\>)|", "\$1\$3\$4", $text); // remove custom added "<?php "
echo '<pre style="position: absolute;z-index: 9999999;bottom: 0;width: 100%;height: 50%; overflow: scroll;">';
echo $text;
echo '</pre>';
} else {
ob_start();
$output = 'console.group("%c INCLUDED FILES: " + "%c", "color: #676e8a; font-size: 16px;","color: #a6accd; font-size: 16px;");';
$output .= 'console.log(' . json_encode( $files ) . ');';
$output .= 'console.groupEnd();';
$output .= 'console.log("%c%s", "font-size: 20px;","\n");';
$output = sprintf( '<script>%s</script>', $output );
echo $output;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment