render_html( $self->collect_entries() ); } private function __construct() { $this->roots = $this->collect_roots(); } private function collect_roots(): array { $roots = []; if ( defined( 'WP_CONTENT_DIR' ) ) { $this->add_root( $roots, WP_CONTENT_DIR, 'wp-content' ); } $stylesheet_dir = get_stylesheet_directory(); $template_dir = get_template_directory(); $stylesheet_real = realpath( $stylesheet_dir ); $template_real = realpath( $template_dir ); if ( $stylesheet_real && $template_real && self::normalize_path( $stylesheet_real ) === self::normalize_path( $template_real ) ) { $this->add_root( $roots, $template_dir, 'Theme' ); } else { $this->add_root( $roots, $stylesheet_dir, 'Child theme' ); $this->add_root( $roots, $template_dir, 'Parent theme' ); } return $roots; } private function add_root( array &$roots, string $path, string $label ): void { $real = realpath( $path ); if ( ! $real || ! is_dir( $real ) ) { return; } $real = self::normalize_path( $real ); if ( ! isset( $roots[ $real ] ) ) { $roots[ $real ] = $label; } } private function collect_entries(): array { $entries = []; $seen = []; foreach ( $this->roots as $root => $label ) { foreach ( self::ALLOWED_FILES as $name ) { $file = $this->resolve_file_path( $root, $name ); if ( ! $file || isset( $seen[ $file ] ) ) { continue; } $seen[ $file ] = true; $entries[] = $this->build_entry( $file, $label ); } } return $entries; } private function build_entry( string $file, string $label ): array { $content = $this->read_file( $file ); $hooks = $content !== null ? $this->extract_hooks( $content ) : []; return [ 'path' => $file, 'masked_path' => $this->mask_path( $file ), 'location_label' => $label, 'hooks' => $hooks, 'readable' => $content !== null, ]; } private function resolve_file_path( string $root, string $name ): ?string { if ( ! in_array( $name, self::ALLOWED_FILES, true ) ) { return null; } if ( $name === '' || $name !== basename( $name ) ) { return null; } $root_dir = self::ensure_trailing_slash( $root ); $file = $root_dir . $name; if ( ! is_file( $file ) || ! is_readable( $file ) || is_link( $file ) ) { return null; } $real_file = realpath( $file ); if ( ! $real_file ) { return null; } $real_file = self::normalize_path( $real_file ); $real_root = self::ensure_trailing_slash( self::normalize_path( $root_dir ) ); if ( strpos( $real_file, $real_root ) !== 0 ) { return null; } if ( basename( $real_file ) !== $name ) { return null; } return $real_file; } private function read_file( string $file ): ?string { if ( ! $this->ensure_wp_filesystem() ) { return null; } global $wp_filesystem; if ( ! $wp_filesystem ) { return null; } $size = method_exists( $wp_filesystem, 'size' ) ? $wp_filesystem->size( $file ) : false; if ( $size === false || $size > self::MAX_BYTES ) { return null; } $data = $wp_filesystem->get_contents( $file ); if ( $data === false || strlen( $data ) > self::MAX_BYTES ) { return null; } $data = str_replace( [ "\r\n", "\r" ], "\n", $data ); if ( preg_match( '/[\x00-\x08\x0B\x0C\x0E-\x1F]/', $data ) ) { return null; } return $data; } private function extract_hooks( string $code ): array { $hooks = []; foreach ( self::HOOK_PATTERNS as $pattern ) { if ( preg_match_all( $pattern, $code, $m, PREG_OFFSET_CAPTURE ) ) { foreach ( $m[0] as $i => $_ ) { $context = $m[1][ $i ][0]; $name = $m[3][ $i ][0]; $offset = (int) $m[0][ $i ][1]; $line = self::offset_to_line( $code, $offset ); $hooks[] = [ 'name' => $name, 'context' => $context, 'line' => $line, ]; } } } $hooks = $this->dedupe_hooks( $hooks ); usort( $hooks, static function ( array $a, array $b ): int { $line_a = (int) ( $a['line'] ?? 0 ); $line_b = (int) ( $b['line'] ?? 0 ); if ( $line_a === $line_b ) { return strcmp( (string) ( $a['name'] ?? '' ), (string) ( $b['name'] ?? '' ) ); } return $line_a <=> $line_b; } ); return $hooks; } private function dedupe_hooks( array $hooks ): array { $result = []; $seen = []; foreach ( $hooks as $hook ) { $key = ( $hook['context'] ?? '' ) . '|' . ( $hook['name'] ?? '' ) . '|' . (int) ( $hook['line'] ?? 0 ); if ( isset( $seen[ $key ] ) ) { continue; } $seen[ $key ] = true; $result[] = $hook; } return $result; } private function mask_path( string $path ): string { $real = realpath( $path ); $wc = defined( 'WP_CONTENT_DIR' ) ? realpath( WP_CONTENT_DIR ) : null; if ( $real ) { $real = self::normalize_path( $real ); } if ( $wc ) { $wc = self::normalize_path( $wc ); } if ( $real && $wc ) { $wc_slash = self::ensure_trailing_slash( $wc ); if ( strpos( $real, $wc_slash ) === 0 ) { return '...' . substr( $real, strlen( $wc ) ); } } $len = strlen( $path ); return '...' . substr( $path, max( 0, $len - 40 ) ); } private function render_html( array $entries ): string { ob_start(); ?>

render_entry( $entry ); ?>
file_label( $path ); $loc_label = isset( $entry['location_label'] ) ? (string) $entry['location_label'] : ''; $masked = isset( $entry['masked_path'] ) ? (string) $entry['masked_path'] : ''; $hooks = ( isset( $entry['hooks'] ) && is_array( $entry['hooks'] ) ) ? $entry['hooks'] : []; $readable = ! empty( $entry['readable'] ); ?>

—

render_hooks_table( $hooks ); ?>

$hook ) : ?>
#