Skip to content

Commit

Permalink
Merge pull request #19 from ManiruzzamanAkash/fix/dynamic-block-renderer
Browse files Browse the repository at this point in the history
Fix: Fixed and improved dynamic block renderer issue and asset registration optimization
  • Loading branch information
ManiruzzamanAkash authored Jan 1, 2023
2 parents 091195a + fd8890f commit 14cd924
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 22,746 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A simple starter kit to work in WordPress plugin development using WordPress Res
16. JestUnit Test
17. Jest-Pupetter e2e Test
18. PHP OOP plugin architecture [Traits + Interfaces + Abstract Classes]
19. Gutenberg blocks
19. Gutenberg blocks, Dynamic blocks

### Quick Start
```sh
Expand Down Expand Up @@ -192,6 +192,11 @@ Or, it could be your custom processed URL.
[View Detailed documentations with parameters and responses of the REST API](https://github.com/ManiruzzamanAkash/wp-react-kit/blob/main/Rest-API-Docs.MD)

### Version & Changelogs
**v0.7.0 - 01/01/2023**

1. Fix: Dynamic block renderer issue.
1. Fix: Asset registering multiple times issue.

**v0.5.0 - 15/11/2022**

1. New Feature : Job Create.
Expand Down Expand Up @@ -346,7 +351,7 @@ https://i.ibb.co/QYgvD83/Mobile-View-Selected-Job.png
"Mobile responsive views-2")

## Gutenberg blocks
Inside `src/blocks` you'll find gutenberg block for ready block setup -
Inside `src/blocks` you'll find gutenberg block for ready block setup. We've made blocks like dynamic block so that future changes would not create any issue.

**Demo preview -**
![React Kit Header Block demo](
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Changelogs

**v0.7.0 - 01/01/2023**

1. Fix: Dynamic block renderer issue.
1. Fix: Asset registering multiple times issue.

**v0.5.0 - 14/11/2022**

1. New Feature : Job Create.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "akash/jobplace",
"description": "A Job posting platform made by WordPress",
"name": "akash/wp-react-kit",
"description": "A simple starter kit to work in WordPress plugin development using WordPress Rest API, WP-script and many more...",
"type": "wordpress-plugin",
"license": "GPL-2.0-or-later",
"authors": [
Expand Down
36 changes: 1 addition & 35 deletions includes/Assets/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ public function __construct() {
public function register_all_scripts() {
$this->register_styles( $this->get_styles() );
$this->register_scripts( $this->get_scripts() );

// Register block scripts.
$this->register_all_blocks();
}

/**
Expand Down Expand Up @@ -64,7 +61,7 @@ public function get_scripts(): array {
return [
'job-place-app' => [
'src' => JOB_PLACE_BUILD . '/index.js',
'version' => filemtime( JOB_PLACE_DIR . '/build/index.js' ),
'version' => $dependency['version'],
'deps' => $dependency['dependencies'],
'in_footer' => true,
],
Expand Down Expand Up @@ -114,35 +111,4 @@ public function enqueue_admin_assets() {
wp_enqueue_style( 'job-place-css' );
wp_enqueue_script( 'job-place-app' );
}

/**
* Register blocks.
*
* @since 0.6.0
*
* @return void
*/
public function register_all_blocks() {
$blocks = [
'header/',
];

foreach( $blocks as $block ) {
$block_folder = JOB_PLACE_PATH . '/build/blocks' . '/' . $block;
$block_options = [];

$markup_file_path = $block_folder . '/markup.php';

if ( file_exists( $markup_file_path ) ) {
$block_options['render_callback'] = function( $attributes, $content, $block ) use ( $block_folder ) {
$context = $block->context;
ob_start();
include $block_folder . '/markup.php';
return ob_get_clean();
};
};

register_block_type_from_metadata( $block_folder, $block_options );
}
}
}
90 changes: 90 additions & 0 deletions includes/Blocks/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

namespace Akash\JobPlace\Blocks;

/**
* Block Manager class.
*
* Responsible for managing all the blocks.
*/
class Manager {

/**
* Constructor.
*
* @since 0.7.0
*/
public function __construct() {
add_action( 'init', [ $this, 'register_all_blocks' ] );
}

/**
* Register all blocks.
*
* @since 0.7.0
*
* @return void
*/
public function register_all_blocks(): void {
global $wp_version;

$is_pre_wp_6 = version_compare( $wp_version, '6.0', '<' );

if ( $is_pre_wp_6 ) {
// Filter the plugins URL to allow us to have blocks in themes with linked assets.
add_filter( 'plugins_url', [ $this, 'filter_plugins_url' ], 10, 2 );
}

$this->register_block_metas();

if ( $is_pre_wp_6 ) {
// Remove the filter after we register the blocks
remove_filter( 'plugins_url', [ $this, 'filter_plugins_url' ], 10, 2 );
}
}

/**
* Register all block by block jsons.
*
* @since 0.7.0
*
* @return void
*/
protected function register_block_metas(): void {
$blocks = [
'header/',
];

foreach ( $blocks as $block ) {
$block_folder = JOB_PLACE_PATH . '/build/blocks/' . $block;
$block_options = [];
$markup_file_path = JOB_PLACE_TEMPLATE_PATH . '/blocks/' . $block . 'markup.php';

if ( file_exists( $markup_file_path ) ) {
$block_options['render_callback'] = function( $attributes, $content, $block ) use ( $markup_file_path ) {
$context = $block->context;
ob_start();
include $markup_file_path;
return ob_get_clean();
};
}

register_block_type_from_metadata( $block_folder, $block_options );
}
}

/**
* Filter the plugins_url to allow us to use assets from theme.
*
* @since 0.7.0
*
* @param string $url The plugins url
* @param string $path The path to the asset.
*
* @return string The overridden url to the block asset.
*/
public function filter_plugins_url( string $url, string $path ): string {
$file = preg_replace( '/\.\.\//', '', $path );
return trailingslashit( get_stylesheet_directory_uri() ) . $file;
}
}
19 changes: 9 additions & 10 deletions job-place.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* Description: A simple starter kit to work in WordPress plugin development using WordPress Rest API, WP-script and many more...
* Requires at least: 5.8
* Requires PHP: 7.4
* Version: 0.6.0
* Version: 0.7.0
* Tested upto: 6.1.1
* Author: Maniruzzaman Akash<manirujjamanakash@gmail.com>
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -25,7 +26,7 @@ final class Wp_React_Kit {
*
* @var string
*/
const VERSION = '0.6.0';
const VERSION = '0.7.0';

/**
* Plugin slug.
Expand Down Expand Up @@ -210,6 +211,12 @@ public function includes() {
if ( $this->is_request( 'admin' ) ) {
$this->container['admin_menu'] = new Akash\JobPlace\Admin\Menu();
}

// Common classes
$this->container['assets'] = new Akash\JobPlace\Assets\Manager();
$this->container['blocks'] = new Akash\JobPlace\Blocks\Manager();
$this->container['rest_api'] = new Akash\JobPlace\REST\Api();
$this->container['jobs'] = new Akash\JobPlace\Jobs\Manager();
}

/**
Expand Down Expand Up @@ -240,11 +247,6 @@ public function init_hooks() {
public function init_classes() {
// Init necessary hooks
new Akash\JobPlace\User\Hooks();

// Common classes
$this->container['assets'] = new Akash\JobPlace\Assets\Manager();
$this->container['rest_api'] = new Akash\JobPlace\REST\Api();
$this->container['jobs'] = new Akash\JobPlace\Jobs\Manager();
}

/**
Expand All @@ -261,9 +263,6 @@ public function localization_setup() {

// Load the React-pages translations.
if ( is_admin() ) {
// Check if handle is registered in wp-script
$this->container['assets']->register_all_scripts();

// Load wp-script translation for job-place-app
wp_set_script_translations( 'job-place-app', 'jobplace', plugin_dir_path( __FILE__ ) . 'languages/' );
}
Expand Down
Loading

0 comments on commit 14cd924

Please # to comment.