-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Init class methods and blocks classes, removing duplicated code
- Loading branch information
1 parent
13fc3f6
commit 489659b
Showing
8 changed files
with
173 additions
and
216 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
/** | ||
* Init | ||
* | ||
* @package wxyzblocks | ||
*/ | ||
|
||
namespace WXYZBlocks\Inc; | ||
|
||
/** | ||
* Class to be used by all blocks | ||
*/ | ||
class Blocks { | ||
|
||
/** | ||
* Custom constructor for handle WordPress Hooks | ||
*/ | ||
public static function initialize() { | ||
$self = new self(); | ||
add_filter( 'block_categories_all', [ $self, 'register_new_category' ] ); | ||
} | ||
|
||
/** | ||
* Register new blocks category | ||
* | ||
* @param array $categories Existing block categories. | ||
* @return array Modified block categories | ||
*/ | ||
public function register_new_category( $categories ) { | ||
$categories[] = [ | ||
'slug' => 'wxyz-blocks', | ||
'title' => 'XYW...Z Blocks', | ||
]; | ||
|
||
return $categories; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
/** | ||
* Block Z | ||
* | ||
* @package wxyzblocks | ||
*/ | ||
|
||
namespace WXYZBlocks\Inc; | ||
|
||
/** | ||
* Block Y | ||
*/ | ||
class SingleBlock { | ||
|
||
/** | ||
* Custom constructor for handle WordPress Hooks | ||
*/ | ||
public static function initialize() { | ||
$self = new self(); | ||
add_action( 'init', [ $self, 'blocks_list' ] ); | ||
add_action( 'init', [ $self, 'blocks_register' ] ); | ||
add_action( 'init', [ $self, 'blocks_enqueues' ] ); | ||
add_action( 'init', [ $self, 'custom_block_register' ] ); | ||
add_action( 'enqueue_block_editor_assets', [ $self, 'custom_block_enqueues' ] ); | ||
} | ||
|
||
/** | ||
* Store the block names and paths at an array | ||
* | ||
* @return array Full list of block names and paths | ||
*/ | ||
public static function blocks_list() { | ||
return [ | ||
'block-w', | ||
'block-x', | ||
'block-y', | ||
'block-z', | ||
]; | ||
} | ||
|
||
/** | ||
* Register all blocks. | ||
* | ||
* Iterates over the list of block names returned by the blocks_list method | ||
* and registers each block using the custom_block_register method. | ||
*/ | ||
public function blocks_register() { | ||
foreach ( self::blocks_list() as $block ) { | ||
self::custom_block_register( $block ); | ||
} | ||
} | ||
|
||
/** | ||
* Enqueue all block assets. | ||
* | ||
* Iterates over the list of block names returned by the blocks_list method | ||
* and enqueues the scripts and styles for each block using the custom_block_enqueues method. | ||
*/ | ||
public function blocks_enqueues() { | ||
foreach ( self::blocks_list() as $block ) { | ||
self::custom_block_enqueues( $block ); | ||
} | ||
} | ||
|
||
/** | ||
* Register Block | ||
* | ||
* @param block $block block from the blocks_list method. | ||
*/ | ||
public function custom_block_register( $block ) { | ||
register_block_type( __DIR__ . "/../blocks/$block" ); | ||
} | ||
|
||
/** | ||
* Enqueues | ||
* | ||
* @param block $block block from the blocks_list method. | ||
*/ | ||
public function custom_block_enqueues( $block ) { | ||
wp_enqueue_script( | ||
'$block', | ||
plugin_dir_url( __FILE__ ) . '../blocks/' . $block . '/build/index.js', | ||
[ 'wp-blocks', 'wp-i18n', 'wp-editor' ] | ||
); | ||
|
||
wp_enqueue_style( | ||
'$block', | ||
plugin_dir_url( __FILE__ ) . '..style/style.css', | ||
[], | ||
); | ||
} | ||
} |
Oops, something went wrong.