exists() ? $theme_obj->get_stylesheet_directory() : trailingslashit( get_theme_root() ) . $component; } $source_dir = trailingslashit( $source_dir ); if ( ! is_dir( $source_dir ) ) { return false; } $discovered_strings = array(); // --- LAYER A: DIRECT STRUCTURAL MANIFEST HEADER EXTRACTION --- if ( 'theme' === $type ) { $theme_obj = wp_get_theme( $component ); if ( $theme_obj->exists() ) { $discovered_strings[ trim( (string) $theme_obj->get( 'Name' ) ) ] = true; $discovered_strings[ trim( (string) $theme_obj->get( 'ThemeURI' ) ) ] = true; $discovered_strings[ trim( (string) $theme_obj->get( 'Description' ) ) ] = true; $discovered_strings[ trim( (string) $theme_obj->get( 'Author' ) ) ] = true; $discovered_strings[ trim( (string) $theme_obj->get( 'AuthorURI' ) ) ] = true; } } elseif ( 'plugin' === $type ) { if ( ! function_exists( 'get_plugin_data' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $plugin_files = glob( $source_dir . '*.php' ); if ( ! empty( $plugin_files ) && is_array( $plugin_files ) ) { foreach ( $plugin_files as $file ) { $plugin_data = get_plugin_data( $file, false, false ); if ( ! empty( $plugin_data['Name'] ) ) { $discovered_strings[ trim( (string) $plugin_data['Name'] ) ] = true; $discovered_strings[ trim( (string) $plugin_data['PluginURI'] ) ] = true; $discovered_strings[ trim( (string) $plugin_data['Description'] ) ] = true; $discovered_strings[ trim( (string) $plugin_data['Author'] ) ] = true; $discovered_strings[ trim( (string) $plugin_data['AuthorURI'] ) ] = true; break; } } } } // --- LAYER B: PURE SOURCE CODE SCANNING ENGINE --- try { $directory = new RecursiveDirectoryIterator( $source_dir, RecursiveDirectoryIterator::SKIP_DOTS ); $iterator = new RecursiveIteratorIterator( $directory ); // Expanded regex tracking schema capturing standard, context, and plural WP i18n hooks $pattern = '/\b(?:__|_e|_x|_ex|_n|_nx|esc_html__|esc_html_e|esc_html_x|esc_attr__|esc_attr_e|esc_attr_x)\s*\(\s*(?|\'((?:[^\'\\\\]|\\\\.)*)\'|"((?:[^"\\\\]|\\\\.)*)")/s'; foreach ( $iterator as $file_info ) { if ( ! $file_info->isFile() ) { continue; } $ext = strtolower( $file_info->getExtension() ); // Modern FSE Compatibility Layer: Scan both core PHP logic structures and HTML block markup configurations if ( in_array( $ext, array( 'php', 'html', 'json' ), true ) ) { $file_path = $file_info->getPathname(); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents $file_content = file_get_contents( $file_path ); if ( empty( $file_content ) ) { continue; } // Extract conventional syntax assignments if ( preg_match_all( $pattern, $file_content, $matches ) ) { foreach ( $matches[1] as $raw_string ) { if ( '' !== $raw_string ) { $clean_string = stripcslashes( $raw_string ); $discovered_strings[ $clean_string ] = true; } } } // Block Theme theme.json explicit static string parsing fallback if ( 'json' === $ext && 'theme.json' === basename( $file_path ) ) { $json_data = json_decode( $file_content, true ); if ( is_array( $json_data ) ) { $check_keys = array( 'title', 'name' ); array_walk_recursive( $json_data, function( $value, $key ) use ( $check_keys, &$discovered_strings ) { if ( in_array( $key, $check_keys, true ) && is_string( $value ) && '' !== trim( $value ) ) { $discovered_strings[ trim( $value ) ] = true; } }); } } } } } catch ( Exception $e ) { return false; } // Clean internal empty references from array maps safely unset( $discovered_strings[''] ); $discovered_strings = array_filter( $discovered_strings ); // --- CONSTRUCT GETTEXT POT HEADERS --- $pot_content = "# Copyright (C) " . gmdate( 'Y' ) . " AF Themes\n"; $pot_content .= "# This file is distributed under the same license as the {$component} package.\n"; $pot_content .= "msgid \"\"\n"; $pot_content .= "msgstr \"\"\n"; $pot_content .= "\"Project-Id-Version: {$component} Templates\\n\"\n"; $pot_content .= "\"Report-Msgid-Bugs-To: https://wordpress.org/support/\\n\"\n"; $pot_content .= "\"POT-Creation-Date: " . gmdate( 'Y-m-d H:i:sO' ) . "\\n\"\n"; $pot_content .= "\"MIME-Version: 1.0\\n\"\n"; $pot_content .= "\"Content-Type: text/plain; charset=UTF-8\\n\"\n"; $pot_content .= "\"Content-Transfer-Encoding: 8bit\\n\"\n"; $pot_content .= "\"X-Generator: AF Translate Run Time Engine\\n\"\n\n"; // --- POPULATE DATA MAP ENTRIES --- if ( ! empty( $discovered_strings ) ) { foreach ( array_keys( $discovered_strings ) as $raw_string ) { $escaped_string = addcslashes( $raw_string, "\"\\" ); $pot_content .= "msgid \"{$escaped_string}\"\n"; $pot_content .= "msgstr \"\"\n\n"; } } return false !== file_put_contents( $destination_path, $pot_content ); } }