add_handlebar( 'esc_url', function ( $context ) { if ( function_exists( 'esc_url' ) ) { return esc_url( $context ); } return $context; } ); $this->add_handlebar( 'esc_attr', function ( $context ) { if ( function_exists( 'esc_attr' ) ) { return esc_attr( $context ); } return $context; } ); $this->add_handlebar( 'esc_html', function ( $context ) { if ( function_exists( 'esc_html' ) ) { return esc_html( $context ); } return $context; } ); $this->add_handlebar( 'safe_html', function ( $context ) { if ( function_exists( 'wp_kses_post' ) ) { return wp_kses_post( $context ); } return $context; } ); } public function add_handlebar( $key, $func ): void { $this->custom_handlebars[ $key ] = $func; } } class Component_Builder { use Custom_Handlebars; public string $component_name = ''; public string $module_path = ''; public string $project_path = ''; private string $dist_path = ''; function __construct( $args = [] ) { if ( ! isset( $args['backPath'] ) || ! isset( $args['projectPath'] ) || ! isset( $args['blockName'] ) || ! isset( $args['platform'] ) ) { throw new \Exception( 'Error: Missing arguments. Make sure all parameter passed.' ); } $this->module_path = $args['backPath']; $this->project_path = $args['projectPath']; $this->component_name = $args['blockName']; $this->dist_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path . '/exports' . '/' . $args['platform']; $this->register_default_handlebar_helpers(); $this->add_handlebar( 'base_url', function ( $context ) { $path = join( '/', [ $this->block_project, $this->block_name, 'templates' ] ); return join( '/', [ get_site_url(),'wp-content', 'axe-web-blocks', $path, '' ] ); } ); } function build(): void { $root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path; $this->buildTemplatePhpFile( $root_path ); } private function buildTemplatePhpFile( $root_path ): void { $file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" ); $output_folder = $this->dist_path . '/templates'; rename( $file_name, "$output_folder/$this->component_name.template.php" ); } private function get_handlebars_template( $path = '' ): string { $template = file_get_contents( $path ); $phpStr = LightnCandy::compile( $template, [ 'flags' => Flags::FLAG_NOESCAPE | Flags::FLAG_PARENT | Flags::FLAG_SPVARS | Flags::FLAG_ELSE | Flags::FLAG_JSLENGTH | Flags::FLAG_JSTRUE | Flags::FLAG_THIS, 'helpers' => $this->custom_handlebars ?? [], ] ); /** * NOTE: * PHP 8.0.0 has problems with the LightCandy lib. * If you're running the exact php8.0.0 version, try to downgrade to LightCandy@1.2.5. */ return self::create_cache_file( $path, $phpStr ); } private static function create_cache_file( string $file_path, string $content ): string { $file_path_parts = explode( ".", $file_path ); array_pop( $file_path_parts ); // remove ".hbs" format. $file_path_parts[] = 'php'; $file_path = join( '.', $file_path_parts ); $comment = " /** * FILE INFO: * This file was generated by LightCandy::compile function. * The original source is the .HBS(handlebars) file that is located next to this generated PHP file. * * Compiled at " . date( 'Y-m-d h:i:s' ) . " */ "; $t = file_put_contents( $file_path, '' ); if ( $t === false ) { throw new \Exception( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); } return $file_path; } } /** * Functions below will be triggered by JavaScript (index.js). */ function build( $args = [] ): void { ( new Component_Builder( $args ) )->build(); } /** * @throws ExportException */ function jsonToPhp( $args = [] ): ?string { $json = $args['json'] ?? []; return VarExporter::export( $json ); }