From a61510f1366f33fbee3281a6457c5ae6256f6511 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Thu, 8 Dec 2022 04:56:45 +0200 Subject: [PATCH 1/4] Updated build process of ACF Block. Added initBlock function. --- layouts/scripts/frame/frame.js | 5 +++ .../templates/Template_Component.php | 42 ++++++++----------- platforms/wordpress/wordpress-adapter.js | 28 ++++++------- 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/layouts/scripts/frame/frame.js b/layouts/scripts/frame/frame.js index a616269..973e968 100644 --- a/layouts/scripts/frame/frame.js +++ b/layouts/scripts/frame/frame.js @@ -1,5 +1,10 @@ 'use strict'; +// Blocks Initialization. +function initBlock(blockName = '', selector = '', cb) { + document.querySelectorAll(selector).forEach((el) => cb(el)); +} + // Scrollbars / Frame resizes notifications. (function () { let height; diff --git a/platforms/wordpress/templates/Template_Component.php b/platforms/wordpress/templates/Template_Component.php index 081b075..087b1f7 100644 --- a/platforms/wordpress/templates/Template_Component.php +++ b/platforms/wordpress/templates/Template_Component.php @@ -2,18 +2,16 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>; -include_once __DIR__ . "/helpers/<%= blockClassModel %>_Defaults.php"; -include_once __DIR__ . "/helpers/<%= blockClassModel %>_API.php"; - class <%= blockClassModel %>_Component extends \Core\Component { public function get_content( $args = [] ): string { - $args = array_merge( Helpers\<%= blockClassModel %>_Defaults::default_args(), $args ); - $args = Helpers\<%= blockClassModel %>_API::prepare_args( $args ); + $default_args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::default_args', [] ); // Not really practical. + + $args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::prepare_args', array_merge( $default_args, $args ) ); + + $output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( $args, self::class ); - $output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( array_merge( [], $args ), self::class ); - //return apply_filters( 'the_content', $output ); - return $output; + return apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::content', $output ); } <% if (!include_acf_block && !include_native_gutenberg_block) { %>function register_assets(): void { @@ -37,22 +35,18 @@ class <%= blockClassModel %>_Component extends \Core\Component { <% } %><% if (include_acf_block) { %> function register_acf_block() { $this->register_block( __DIR__ . "/<%= blockFilename %>.block.json", [ - 'enqueue_assets' => function () { - wp_enqueue_style( 'block-<%= blockFilename %>', plugins_url( 'templates/styles/<%= blockFilename %>.min.css', __FILE__ ), ['assets-style'], get_blocks_version() );<% if (include_script) { %> - wp_enqueue_script( 'block-<%= blockFilename %>', plugins_url( 'templates/scripts/<%= blockFilename %>.min.js', __FILE__ ), ['assets-script'], get_blocks_version(), true );<% } %> - }, - 'default' => Helpers\<%= blockClassModel %>_Defaults::default_args(), - 'supports' => [ - //'jsx' => true, - 'color' => [ - 'background' => true, - 'text' => true, - ], - 'spacing' => [ - 'margin' => [ 'top', 'bottom' ], - 'padding' => [ 'top', 'bottom' ] - ], - ] + 'style_assets' => [ + [ + 'name' => '<%= blockFilename %>', + 'url' => plugins_url( 'templates/styles/<%= blockFilename %>.min.css', __FILE__ ), + ] + ], + 'script_assets' => [ + [ + 'name' => '<%= blockFilename %>', + 'url' => plugins_url( 'templates/scripts/<%= blockFilename %>.min.js', __FILE__ ), + ] + ] ] ); } diff --git a/platforms/wordpress/wordpress-adapter.js b/platforms/wordpress/wordpress-adapter.js index f2a05b2..eae5e42 100644 --- a/platforms/wordpress/wordpress-adapter.js +++ b/platforms/wordpress/wordpress-adapter.js @@ -67,13 +67,13 @@ export async function buildWordPress(blockName, isBlock = false, isElementor = f json: await readJSONFile(path.join(projectPath, 'data', 'default.json'), "utf8"), }); - await createFiles(Object.assign({}, data, {defaultData: phpDataObject}), [{ - from: `templates/helpers/Template_Defaults.php`, - to: `helpers/${data.blockClassModel}_Defaults.php`, - }], { - pathDist: distPath, - generatorsPath: path.join(__dirname), - }); + // await createFiles(Object.assign({}, data, {defaultData: phpDataObject}), [{ + // from: `templates/helpers/Template_Defaults.php`, + // to: `helpers/${data.blockClassModel}_Defaults.php`, + // }], { + // pathDist: distPath, + // generatorsPath: path.join(__dirname), + // }); if (isElementor) { await createFiles(data, [{ @@ -94,13 +94,13 @@ export async function buildWordPress(blockName, isBlock = false, isElementor = f generatorsPath: path.join(__dirname) }); - await createFiles(data, [{ - from: `templates/helpers/Template_API.php`, - to: `helpers/${data.blockClassModel}_API.php`, - }], { - pathDist: distPath, - generatorsPath: path.join(__dirname) - }); + // await createFiles(data, [{ + // from: `templates/helpers/Template_API.php`, + // to: `helpers/${data.blockClassModel}_API.php`, + // }], { + // pathDist: distPath, + // generatorsPath: path.join(__dirname) + // }); } else { await createFiles(data, [{ from: `templates/Template_Basic_Component.php`, From 25862c4b1cd4eedb5f9520b3d9c853ec3c9d62eb Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Thu, 8 Dec 2022 05:00:43 +0200 Subject: [PATCH 2/4] 1.0.22 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2d5eb5e..5f24975 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@axe-web/block-dev-tool", - "version": "1.0.21", + "version": "1.0.22", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@axe-web/block-dev-tool", - "version": "1.0.21", + "version": "1.0.22", "license": "ISC", "dependencies": { "@braintree/sanitize-url": "^6.0.0", diff --git a/package.json b/package.json index 8c83310..a052d2d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@axe-web/block-dev-tool", - "version": "1.0.21", + "version": "1.0.22", "author": { "name": "AXE-WEB", "email": "office@axe-web.com", From 6626ecff2a7cf377d2bee569eb175dd3329ae4d7 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Thu, 8 Dec 2022 05:16:21 +0200 Subject: [PATCH 3/4] Update spacings/code-format of PHP bundle. --- .editorconfig | 4 ++++ .../wordpress/templates/Template_Component.php | 18 ++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.editorconfig b/.editorconfig index 0c4982b..624da1a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -18,6 +18,10 @@ indent_size = 2 indent_style = tab indent_size = 4 +[*.php] +indent_style = tab +indent_size = 4 + [*.md] trim_trailing_whitespace = false diff --git a/platforms/wordpress/templates/Template_Component.php b/platforms/wordpress/templates/Template_Component.php index 087b1f7..ef39d78 100644 --- a/platforms/wordpress/templates/Template_Component.php +++ b/platforms/wordpress/templates/Template_Component.php @@ -5,13 +5,13 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>; class <%= blockClassModel %>_Component extends \Core\Component { public function get_content( $args = [] ): string { - $default_args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::default_args', [] ); // Not really practical. + $default_args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::default_args', [] ); // Not really practical. - $args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::prepare_args', array_merge( $default_args, $args ) ); + $args = apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::prepare_args', array_merge( $default_args, $args ) ); - $output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( $args, self::class ); + $output = ( include( __DIR__ . '/templates/<%= blockFilename %>.template.php' ) )( $args, self::class ); - return apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::content', $output ); + return apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::content', $output ); } <% if (!include_acf_block && !include_native_gutenberg_block) { %>function register_assets(): void { @@ -24,7 +24,6 @@ class <%= blockClassModel %>_Component extends \Core\Component { wp_enqueue_script( 'script-block-<%= blockFilename %>' ); }<% } %> - <% if (include_elementor_widget) { %>function register_custom_logic(): void { add_action( 'elementor/widgets/widgets_registered', [ $this, 'register_elementor_widget' ] ); } @@ -34,8 +33,8 @@ class <%= blockClassModel %>_Component extends \Core\Component { } <% } %><% if (include_acf_block) { %> function register_acf_block() { - $this->register_block( __DIR__ . "/<%= blockFilename %>.block.json", [ - 'style_assets' => [ + $this->register_block( __DIR__ . "/<%= blockFilename %>.block.json", [ + 'style_assets' => [ [ 'name' => '<%= blockFilename %>', 'url' => plugins_url( 'templates/styles/<%= blockFilename %>.min.css', __FILE__ ), @@ -47,9 +46,8 @@ class <%= blockClassModel %>_Component extends \Core\Component { 'url' => plugins_url( 'templates/scripts/<%= blockFilename %>.min.js', __FILE__ ), ] ] - ] ); - } - + ] ); + } <% } %><% if (include_native_gutenberg_block) { %> function register_native_gutenberg_block() { register_block_type( __DIR__ . '/templates/gutenberg-block/block.json' );<% if (include_script) { %> From d8c3d4c54bf3dd93718c517ff83be8e71e7365a0 Mon Sep 17 00:00:00 2001 From: Roman Axelrod Date: Thu, 8 Dec 2022 05:40:39 +0200 Subject: [PATCH 4/4] - Added compile note (date) in PHP bundle. - Update spacings/code-format of PHP bundle. --- platforms/php/build.php | 182 +++++++++--------- .../templates/Template_Component.php | 2 - 2 files changed, 92 insertions(+), 92 deletions(-) diff --git a/platforms/php/build.php b/platforms/php/build.php index 162c10b..0e2d1d8 100644 --- a/platforms/php/build.php +++ b/platforms/php/build.php @@ -9,125 +9,127 @@ use LightnCandy\LightnCandy; use Brick\VarExporter\VarExporter; trait Custom_Handlebars { - public array $custom_handlebars = []; + public array $custom_handlebars = []; - public function register_default_handlebar_helpers() { - $this->add_handlebar( 'esc_url', function ( $context ) { - if ( function_exists( 'esc_url' ) ) { - return esc_url( $context ); - } + public function register_default_handlebar_helpers() { + $this->add_handlebar( 'esc_url', function ( $context ) { + if ( function_exists( 'esc_url' ) ) { + return esc_url( $context ); + } - return $context; - } ); + return $context; + } ); - $this->add_handlebar( 'esc_attr', function ( $context ) { - if ( function_exists( 'esc_attr' ) ) { - return esc_attr( $context ); - } + $this->add_handlebar( 'esc_attr', function ( $context ) { + if ( function_exists( 'esc_attr' ) ) { + return esc_attr( $context ); + } - return $context; - } ); + return $context; + } ); - $this->add_handlebar( 'esc_html', function ( $context ) { - if ( function_exists( 'esc_html' ) ) { - return esc_html( $context ); - } + $this->add_handlebar( 'esc_html', function ( $context ) { + if ( function_exists( 'esc_html' ) ) { + return esc_html( $context ); + } - return $context; - } ); + return $context; + } ); - $this->add_handlebar( 'safe_html', function ( $context ) { - if ( function_exists( 'wp_kses_post' ) ) { - return wp_kses_post( $context ); - } + $this->add_handlebar( 'safe_html', function ( $context ) { + if ( function_exists( 'wp_kses_post' ) ) { + return wp_kses_post( $context ); + } - return $context; - } ); - } + return $context; + } ); + } - public function add_handlebar( $key, $func ) { - $this->custom_handlebars[ $key ] = $func; - } + public function add_handlebar( $key, $func ) { + $this->custom_handlebars[ $key ] = $func; + } } class Component_Builder { - use Custom_Handlebars; - - public string $component_name = ''; - public string $module_path = ''; - public string $project_path = ''; - - function __construct( $component_name, $module_path, $project_path ) { - $this->module_path = $module_path; - $this->project_path = $project_path; - $this->component_name = $component_name; - $this->register_default_handlebar_helpers(); - } - - function build(): void { - $root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path; - - $this->buildTemplatePhpFile( $root_path ); - } - - private function buildTemplatePhpFile( $root_path ) { - $file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" ); - - $output_folder = $root_path . '/exports/wordpress/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, - '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 = " + use Custom_Handlebars; + + public string $component_name = ''; + public string $module_path = ''; + public string $project_path = ''; + + function __construct( $component_name, $module_path, $project_path ) { + $this->module_path = $module_path; + $this->project_path = $project_path; + $this->component_name = $component_name; + $this->register_default_handlebar_helpers(); + } + + function build(): void { + $root_path = __DIR__ . '/' . $this->module_path . '/../../' . $this->project_path; + + $this->buildTemplatePhpFile( $root_path ); + } + + private function buildTemplatePhpFile( $root_path ) { + $file_name = $this->get_handlebars_template( "$root_path/src/$this->component_name.template.hbs" ); + + $output_folder = $root_path . '/exports/wordpress/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, + '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 ) { - die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); - } + $t = file_put_contents( $file_path, '' ); + if ( $t === false ) { + die( "Error: Can't generate HBS template to PHP file. Cache folder is not accessible." ); + } - return $file_path; - } + return $file_path; + } } function build( $args = [] ) { - ( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build(); + ( new Component_Builder( $args['blockName'], $args['backPath'], $args['projectPath'] ) )->build(); } /** * @throws ExportException */ function jsonToPhp( $args = [] ): ?string { - $json = $args['json'] ?? []; + $json = $args['json'] ?? []; - return VarExporter::export( $json ); + return VarExporter::export( $json ); } diff --git a/platforms/wordpress/templates/Template_Component.php b/platforms/wordpress/templates/Template_Component.php index ef39d78..ed0f3df 100644 --- a/platforms/wordpress/templates/Template_Component.php +++ b/platforms/wordpress/templates/Template_Component.php @@ -13,7 +13,6 @@ class <%= blockClassModel %>_Component extends \Core\Component { return apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::content', $output ); } - <% if (!include_acf_block && !include_native_gutenberg_block) { %>function register_assets(): void { $version = get_plugin_data( __DIR__ . "/../../scytale-custom-blocks.php" )['Version']; // In Plugins // $version = \Core\Global_Functions::get_current_version_number(); // In Theme @@ -62,7 +61,6 @@ class <%= blockClassModel %>_Component extends \Core\Component { ); } );<% } %> }<% } %> - } <%= blockClassModel %>_Component::get_instance();