'wp_build_template', '{{HANDLE_PREFIX}}' => 'wp', '{{PAGE_SLUG}}' => 'example-page', '{{PAGE_SLUG_UNDERSCORE}}' => 'example_page', '{{CAPABILITY}}' => 'manage_options', '{{VERSION}}' => '1.0.0', '{{BASE_URL}}' => 'plugin_dir_url( __FILE__ )', '{{INIT_MODULES_JSON}}' => '["@wordpress/example-init-module"]', '{{INIT_MODULES_PHP_ARRAY}}' => "\t\t\t\$boot_dependencies[] = array( 'import' => 'static', 'id' => '@wordpress/example-init-module' );", '{{PAGE_INCLUDES}}' => "foreach ( [ __DIR__ . '/pages/example-page/page.php', __DIR__ . '/pages/example-page/page-wp-admin.php', ] as \$file ) { if ( file_exists( \$file ) ) { require_once \$file; } }", '{{PAGE_ROUTE_FUNCTIONS}}' => '// The route registration functions of each page, generated from routes-page-functions.php.template.', '{{MODULES}}' => '// The entries generated by the build.', '{{ROUTES}}' => '// The entries generated by the build.', '{{SCRIPTS}}' => '// The entries generated by the build.', '{{STYLES}}' => '// The entries generated by the build.', '{{WIDGETS}}' => '// The entries generated by the build.', ); /** * The parser that parses the rendered code. */ private Parser $parser; /** * Constructor. * * @param Parser $parser The parser that parses the rendered code. */ public function __construct( Parser $parser ) { $this->parser = $parser; } /** * Parses a file, rendering it first if it is a wp-build template. * * @param string $file Path to the file to parse. * @return Stmt[] * @throws ParserErrorsException When the code cannot be parsed. */ public function parseFile( string $file ): array { if ( ! self::is_template( $file ) ) { return $this->parser->parseFile( $file ); } $template = file_get_contents( $file ); if ( false === $template ) { // Let the wrapped parser report the file as unreadable. return $this->parser->parseFile( $file ); } return $this->parser->parseString( self::render( $template, $file ) ); } /** * Parses a string of PHP code. * * @param string $sourceCode The code to parse. * @return Stmt[] * @throws ParserErrorsException When the code cannot be parsed. */ public function parseString( string $sourceCode ): array { return $this->parser->parseString( $sourceCode ); } /** * Whether a file is a wp-build PHP template. * * @param string $file Path to the file. */ private static function is_template( string $file ): bool { return str_ends_with( $file, '.php.template' ); } /** * Renders a template into valid PHP, keeping each line where it is. * * @param string $template The template. * @param string $file Path to the template, for the error message. * @return string The rendered code. * @throws LogicException When the template has a placeholder without a substitute. */ private static function render( string $template, string $file ): string { $code = strtr( $template, self::REPLACEMENTS ); // A template for a fragment of generated code has no opening tag of its own. if ( ! str_starts_with( $code, '