Skip to content

Commit

Permalink
Merge pull request #15 from sarahcssiqueira/develop
Browse files Browse the repository at this point in the history
Refactor encompassing duplicated code removed, updates Init class, doc updates
  • Loading branch information
sarahcssiqueira committed Jun 23, 2024
2 parents 756b287 + 489659b commit caeeb7d
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 219 deletions.
8 changes: 5 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"phpCodeSniffer.standard": "Automatic",
"phpCodeSniffer.exec.osx":"./vendor/bin/phpcs"
}
"phpsab.fixerEnable":true,
"phpsab.snifferEnable": true,
}


47 changes: 0 additions & 47 deletions inc/BlockW.php

This file was deleted.

47 changes: 0 additions & 47 deletions inc/BlockX.php

This file was deleted.

47 changes: 0 additions & 47 deletions inc/BlockY.php

This file was deleted.

47 changes: 0 additions & 47 deletions inc/BlockZ.php

This file was deleted.

38 changes: 38 additions & 0 deletions inc/Blocks.php
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;
}

}
47 changes: 31 additions & 16 deletions inc/Init.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Init
* Init class
*
* @package wxyzblocks
*/
Expand All @@ -12,24 +12,39 @@
*/
class Init {


/**
* Constructor for handle WordPress Hooks
* Store the classes inside an array
*
* @return array Full list of classes
*/
public function __construct() {
add_filter( 'block_categories_all', [ $this, 'register_new_category' ] );
}

/*
Register custom category
*/
public function register_new_category( $categories ) {
$categories[] = [
'slug' => 'wxyz-blocks',
'title' => 'XYW...Z Blocks',
public static function classes_list() {
return [
Blocks::class,
SingleBlock::class,
];
}

return $categories;
/**
* Loop through the classes list, instatiate them,
* and call the initialize() method if it exists
*/
public static function register_classes_list() {
foreach ( self::classes_list() as $class ) {
$classname = self::instantiate( $class );
if ( method_exists( $classname, 'initialize' ) ) {
$classname->initialize();
}
}
}

}
/**
* Initialize the classes
*
* @param class $class class from the services array.
* @return class instance new instance of the class
*/
private static function instantiate( $class ) {
$classname = new $class();
return $classname;
}
};
92 changes: 92 additions & 0 deletions inc/SingleBlock.php
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',
[],
);
}
}
Loading

0 comments on commit caeeb7d

Please sign in to comment.