diff --git a/build.js b/build.js index f04ad40..c870182 100755 --- a/build.js +++ b/build.js @@ -12,11 +12,14 @@ const {isDev, developmentBlockName} = getConfigs(); const blockName = !isDev && config.has('blockName') ? config.get('blockName') : developmentBlockName; export const PLATFORM_OPTIONS = [{ + name: 'wordpress-blocks', + title: 'WordPress AFC Block' +}, { name: 'wordpress', title: 'WordPress' }, { - name: 'wordpress-blocks', - title: 'WordPress Block' + name: 'wordpress-component-manager', + title: 'WordPress (Component Manager)' }, { name: 'wordpress-elementor', title: 'WordPress Elementor' @@ -47,9 +50,11 @@ console.log('--------------------\nDone!'); export async function buildExportFiles(platform) { if (platform.name.startsWith('wordpress')) { if (platform.name === 'wordpress-blocks') { - await buildWordPress(blockName, true); + await buildWordPress(blockName, {block: true}); } else if (platform.name === 'wordpress-elementor') { - await buildWordPress(blockName, false, true); + await buildWordPress(blockName, {elementor: true}); + } else if (platform.name === 'wordpress-component-manager') { + await buildWordPress(blockName, {componentManager: true}); } else { await buildWordPress(blockName); } diff --git a/package.json b/package.json index a052d2d..37b9f5f 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,9 @@ "scripts": { "start": "component-dev", "info": "node debug.js", - "dev": "NODE_ENV=development NODE_CONFIG_DIR=blocks/text-block/config BLOCK_NAME=text-block node server.js", + "dev": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node server.js", "build": "rollup --config rollup.config.js", - "build-platform": "NODE_ENV=development NODE_CONFIG_DIR=blocks/text-block/config BLOCK_NAME=text-block node ./build.js", + "build-platform": "NODE_ENV=development NODE_CONFIG_DIR=blocks/header/config BLOCK_NAME=header node ./build.js", "build-platform-cli": "component-build", "dev-js": "NODE_ENV=development rollup --config rollup.config.js --watch" }, diff --git a/platforms/wordpress/templates/Template_Basic_Component.php b/platforms/wordpress/templates/Template_Basic_Component.php index 8a9f39e..b261026 100644 --- a/platforms/wordpress/templates/Template_Basic_Component.php +++ b/platforms/wordpress/templates/Template_Basic_Component.php @@ -2,48 +2,41 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>; -class <%= blockClassModel %>_Component { +class <%= blockClassModel %>_Component <% if (isComponentManager) { %>extends \Core\Component <% } %>{ + const VERSION = '<%= version %>'; - public function __construct() { + <% if (!isComponentManager) { %>public function __construct() { // add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ] ); - add_action( 'after_setup_theme', [ $this, 'register_assets' ] ); + add_action( 'after_setup_theme', [ $this, 'register_assets' ] ); } - + <% } %> 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 - - // $base_path = plugin_dir_url( __FILE__ ); // In Plugins - $base_path = get_template_directory_uri() . '/components/partials/<%= blockFilename %>/'; - - wp_register_style( '<%= blockFilename %>', - $base_path . 'templates/styles/<%= blockFilename %>.min.css', - [ 'style-wp' ], - $version - ); - wp_enqueue_style( '<%= blockFilename %>' ); - - wp_register_script( 'script-<%= blockFilename %>', - $base_path . 'templates/scripts/<%= blockFilename %>.min.js', - [ 'jquery', 'swiper' ], - $version, - true - ); - wp_enqueue_script( 'script-<%= blockFilename %>' ); + $base_path = plugin_dir_url( __FILE__ ); // In Plugins + // $base_path = get_template_directory_uri() . '/components/partials/<%= blockFilename %>/'; // In Theme + + wp_register_style( 'block-<%= blockFilename %>', $base_path . 'templates/styles/<%= blockFilename %>.min.css', [ 'assets-style' ], self::VERSION ); + wp_enqueue_style( 'block-<%= blockFilename %>' ); + + wp_register_script( 'block-<%= blockFilename %>', $base_path . 'templates/scripts/<%= blockFilename %>.min.js', [ 'assets-script' ], self::VERSION, true ); + wp_enqueue_script( 'block-<%= blockFilename %>' ); } - public function render( $args = [] ): void { - $args = array_merge( [], Helpers\<%= blockClassModel %>_Defaults::default_args( $args ), $args); + public function get_content( $args = [] ): string { + $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 ); - echo apply_filters( 'the_content', wpautop( $output ) ); - } + return apply_filters( 'axeweb_blocks/<%= ownerFilename %>/<%= blockFilename %>::content', $output ); + }<% if (isElementor) { %> - <% if (isElementor) { %>function register_elementor_widget( $widgets_manager ): void { - require_once "helpers/<%= blockClassModel %>_Elementor_Widget.php"; - $widgets_manager->register_widget_type( new Helpers\<%= blockClassModel %>_Elementor_Widget() ); - }<% } %> + function register_elementor_widget( $widgets_manager ): void { + require_once "helpers/<%= blockClassModel %>_Elementor_Widget.php"; + $widgets_manager->register_widget_type( new Helpers\<%= blockClassModel %>_Elementor_Widget() ); + } + <% } %> } -// ( new <%= blockClassModel %>_Component() ); // Initialization +<%= blockClassModel %>_Component::get_instance(); diff --git a/platforms/wordpress/templates/Template_Component.php b/platforms/wordpress/templates/Template_Component.php index ed0f3df..3e98e8d 100644 --- a/platforms/wordpress/templates/Template_Component.php +++ b/platforms/wordpress/templates/Template_Component.php @@ -3,23 +3,24 @@ namespace AXEWEB_Blocks\Blocks\<%= ownerClass %>\<%= blockClassModel %>; class <%= blockClassModel %>_Component extends \Core\Component { + const VERSION = '<%= version %>'; 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 { + <% 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 // $base_path = get_template_directory_uri() . '/components/partials/<%= blockFilename %>/'; - 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 );<% } %> + wp_enqueue_style( 'block-<%= blockFilename %>', plugins_url( 'templates/styles/<%= blockFilename %>.min.css', __FILE__ ), ['assets-style'], self::VERSION );<% if (include_script) { %> + wp_enqueue_script( 'block-<%= blockFilename %>', plugins_url( 'templates/scripts/<%= blockFilename %>.min.js', __FILE__ ), ['assets-script'], self::VERSION, true );<% } %> wp_enqueue_script( 'script-block-<%= blockFilename %>' ); }<% } %> @@ -31,9 +32,9 @@ class <%= blockClassModel %>_Component extends \Core\Component { $widgets_manager->register_widget_type( new Helpers\<%= blockClassModel %>_Elementor_Widget() ); } -<% } %><% if (include_acf_block) { %> function register_acf_block() { - $this->register_block( __DIR__ . "/<%= blockFilename %>.block.json", [ - 'style_assets' => [ +<% } %><% if (include_acf_block) { %>function register_acf_block() { + $this->register_block( __DIR__ . "/<%= blockFilename %>.block.json", [ + 'style_assets' => [ [ 'name' => '<%= blockFilename %>', 'url' => plugins_url( 'templates/styles/<%= blockFilename %>.min.css', __FILE__ ), @@ -45,7 +46,7 @@ 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) { %> diff --git a/platforms/wordpress/wordpress-adapter.js b/platforms/wordpress/wordpress-adapter.js index eae5e42..92429a4 100644 --- a/platforms/wordpress/wordpress-adapter.js +++ b/platforms/wordpress/wordpress-adapter.js @@ -9,7 +9,11 @@ import execPhp from "exec-php"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); -export async function buildWordPress(blockName, isBlock = false, isElementor = false) { +export async function buildWordPress(blockName, args = {}) { + const isBlock = !!args.block; + const isElementor = !!args.elementor; + const isComponentManager = !!args.componentManager; + const {modulesPath, projectPath} = getConfigs(); const distPath = path.join(projectPath, 'exports', 'wordpress'); @@ -38,6 +42,7 @@ export async function buildWordPress(blockName, isBlock = false, isElementor = f include_script: true, include_elementor_widget: isElementor, isElementor, + isComponentManager, }); await copyFile(blockFilePath, path.join(distPath, data.blockFilename + '.block.json'));