-
Notifications
You must be signed in to change notification settings - Fork 2
Reduce boilerplate and embrace default config #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
22a2389
cd9190b
9b10c0d
8c1aa24
21b27e2
a6cbe51
4ddd782
7dccb27
38ac830
1a58d3e
89ca4fc
5ada5c6
07a61b2
e2eccae
393c4f7
9343443
4280d8c
a70385c
d05fbb7
88e9689
3a5fa7f
3efaf30
49cb89c
ad3f9f8
a66e926
1570a15
7ce1f09
17be6a9
3c99e1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/build/**/*.map | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,25 +13,29 @@ | |
* @package CreateBlock | ||
*/ | ||
|
||
if (! defined('ABSPATH') ) { | ||
exit; | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
// Include Composer's autoload file. | ||
if ( file_exists( plugin_dir_path( __FILE__ ) . 'vendor/autoload.php' ) ) { | ||
require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php'; | ||
} else { | ||
wp_trigger_error( 'Advanced Multi Block Plugin: Composer autoload file not found. Please run `composer install`.', E_USER_ERROR ); | ||
return; | ||
// Include our bundled autoload if not loaded globally. | ||
if ( ! class_exists( Advanced_Multi_Block\Plugin_Paths::class ) && file_exists( __DIR__ . '/vendor/autoload.php' ) ) { | ||
require_once __DIR__ . '/vendor/autoload.php'; | ||
} | ||
|
||
// Instantiate the classes. | ||
$advanced_multi_block_classes = array( | ||
\Advanced_Multi_Block\Plugin_Paths::class, | ||
\Advanced_Multi_Block\Register_Blocks::class, | ||
\Advanced_Multi_Block\Enqueues::class, | ||
if ( ! class_exists( Advanced_Multi_Block\Plugin_Paths::class ) ) { | ||
wp_trigger_error( 'Advanced Multi Block Plugin: Composer autoload file not found. Please run `composer install`.', E_USER_ERROR ); | ||
return; | ||
} | ||
|
||
// Instantiate our modules. | ||
$advanced_multi_block_modules = array( | ||
new Advanced_Multi_Block\Register_Blocks( __DIR__ . '/build' ), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach now avoid registering WP hooks early so that classes can all be instantiated before their |
||
new Advanced_Multi_Block\Enqueues( __DIR__ . '/build' ), | ||
); | ||
|
||
foreach ( $advanced_multi_block_classes as $advanced_multi_block_class ) { | ||
new $advanced_multi_block_class(); | ||
} | ||
|
||
foreach ( $advanced_multi_block_modules as $advanced_multi_block_module ) { | ||
if ( is_a( $advanced_multi_block_module, Advanced_Multi_Block\Plugin_Module::class ) ) { | ||
$advanced_multi_block_module->init(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Enqueue assets. | ||
* | ||
* @package Advanced_Multi_Block | ||
*/ | ||
|
||
namespace Advanced_Multi_Block; | ||
|
||
/** | ||
* Enqueues class. | ||
*/ | ||
class Enqueues extends Plugin_Module { | ||
/** | ||
* Path resolver for build directory. | ||
* | ||
* @var Plugin_Paths | ||
*/ | ||
private Plugin_Paths $build_dir; | ||
|
||
/** | ||
* Setup the class. | ||
* | ||
* @param string $build_path Absolute path to the build directory for all assets. | ||
*/ | ||
public function __construct( string $build_path ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This now receives the path of the |
||
$this->build_dir = new Plugin_Paths( $build_path ); | ||
} | ||
|
||
/** | ||
* Initialize the module. | ||
*/ | ||
public function init() { | ||
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_assets' ) ); | ||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ) ); | ||
} | ||
|
||
/** | ||
* Enqueues the block assets for the editor. | ||
*/ | ||
public function enqueue_block_assets() { | ||
$asset_meta = $this->build_dir->get_asset_meta( 'editor-script.js' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
if ( $asset_meta ) { | ||
wp_enqueue_script( | ||
'editor-script-js', | ||
$this->build_dir->get_url( 'editor-script.js' ), | ||
$asset_meta['dependencies'], | ||
$asset_meta['version'], | ||
false | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Enqueues the block assets for the frontend. | ||
*/ | ||
public function enqueue_frontend_assets() { | ||
$asset_meta = $this->build_dir->get_asset_meta( 'frontend-script.js' ); | ||
|
||
if ( $asset_meta ) { | ||
wp_enqueue_script( | ||
'frontend-script-js', | ||
$this->build_dir->get_url( 'frontend-script.js' ), | ||
$asset_meta['dependencies'], | ||
$asset_meta['version'], | ||
true | ||
); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Base class for plugin modules which can be initialized. | ||
* | ||
* @package Advanced_Multi_Block | ||
*/ | ||
|
||
namespace Advanced_Multi_Block; | ||
|
||
/** | ||
* Plugin module extended by other classes. | ||
*/ | ||
abstract class Plugin_Module { | ||
/** | ||
* Initialize the module. | ||
*/ | ||
abstract public function init(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** | ||
* Help resolve paths and URLs relative to plugin directories. | ||
* | ||
* @package Advanced_Multi_Block | ||
*/ | ||
|
||
namespace Advanced_Multi_Block; | ||
|
||
/** | ||
* Path resolver class. | ||
*/ | ||
class Plugin_Paths { | ||
|
||
/** | ||
* Absolute path to a directory inside the plugin. | ||
* | ||
* @var string | ||
*/ | ||
private string $plugin_dir; | ||
|
||
/** | ||
* Setup the class. | ||
* | ||
* @param string $plugin_dir Absolute path to the plugin directory. | ||
*/ | ||
public function __construct( string $plugin_dir ) { | ||
$this->plugin_dir = rtrim( $plugin_dir, '\\/' ); | ||
} | ||
|
||
/** | ||
* Get a URL for a path relative to the plugin directory. | ||
* | ||
* @param string|null $relative_path Optional. Relative path from the plugin directory. | ||
* Default null for the base plugin URL. | ||
* | ||
* @return string The full URL. | ||
*/ | ||
public function get_url( ?string $relative_path = null ): string { | ||
if ( isset( $relative_path ) ) { | ||
return plugins_url( ltrim( $relative_path, '/' ), $this->plugin_dir . '/fake.file' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That |
||
} | ||
|
||
return plugins_url( '', $this->plugin_dir . '/fake.file' ); | ||
} | ||
|
||
/** | ||
* Get the absolute path for a path relative to the plugin directory. | ||
* | ||
* @param string|null $relative_path Optional. Relative path from the plugin directory. | ||
* Default null for the base plugin path. | ||
* | ||
* @return string The full path. | ||
*/ | ||
public function get_path( ?string $relative_path = null ): string { | ||
if ( isset( $relative_path ) ) { | ||
return $this->plugin_dir . '/' . ltrim( $relative_path, '/' ); | ||
} | ||
|
||
return $this->plugin_dir; | ||
} | ||
|
||
/** | ||
* Get the asset meta for a built asset file. | ||
* | ||
* @param string $asset_file Asset file name relative to the plugin directory. | ||
* | ||
* @return array|null Array with 'dependencies' and 'version' keys, or null if not found. | ||
*/ | ||
public function get_asset_meta( string $asset_file ): ?array { | ||
$asset_path = $this->get_path( dirname( $asset_file ) . '/' . pathinfo( $asset_file, PATHINFO_FILENAME ) . '.asset.php' ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new helper that resolves |
||
|
||
if ( file_exists( $asset_path ) ) { | ||
$meta = include $asset_path; | ||
|
||
if ( is_array( $meta ) ) { | ||
return $meta; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we attempt to require the bundled
vendor/autoload.php
only if the class is not resolved by the global project autoloader. By default, callingclass_exists()
triggers the autoload logic so that attempts everything that a global Composer autoload has registered (if at all).