Skip to content

Conversation

kasparsd
Copy link

@kasparsd kasparsd commented Sep 4, 2025

As mentioned in the comment on the article:

  1. Fix autoloader for projects where the plugin is added via composer -- include the local vendor/autoload.php only if a plugin class isn't resolved by the global autoloader.

  2. Remove all the extra configuration files for eslint and stylelint. In my experience they quickly drift/conflict from the bundled dependencies of @wordpress/scripts causing significant maintenance hurdles. It might be easier to rely on defaults since that is "zero-config" and works great.

  3. Introduce Plugin_Module abstract class which helps avoid adding WP hooks in class __construct().

  4. Make classes agnostic to their location within the plugin. Pass configuration such as build path for assets as a dependency.

  5. Use npm-run-all package (similar to Gutenberg) to ensure that all npm scripts work across different operating systems.

  6. Move PHP classes to classes directory and rename files to match the WPCS rules.

  7. Fix all reported WPCS issues.

Please see the comments inline for context.

*
* @param string $build_path Absolute path to the build directory for all assets.
*/
public function __construct( string $build_path ) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now receives the path of the build directory via constructor argument. It is now the responsibility of the caller of this class (plugin bootstrap) to define where that directory lives (which also gives us a single place to update it).

* Enqueues the block assets for the editor.
*/
public function enqueue_block_assets() {
$asset_meta = $this->build_dir->get_asset_meta( 'editor-script.js' );
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Plugin_Paths now has a helper for resolving the *.asset.php meta file and returning the data, if the file is present. Removes a lot of repeated boilerplate.

*/
public function get_url( ?string $relative_path = null ): string {
if ( isset( $relative_path ) ) {
return plugins_url( ltrim( $relative_path, '/' ), $this->plugin_dir . '/fake.file' );
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That /fake.file definitely feels like a hack but I'm not aware of a better helper that would accept a directory path as the argument :)

* @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' );
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the new helper that resolves *.asset.php meta data for any JS/CSS file.

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' ) ) {
Copy link
Author

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, calling class_exists() triggers the autoload logic so that attempts everything that a global Composer autoload has registered (if at all).

<exclude-pattern>/build/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>src/blocks-manifest.php</exclude-pattern>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was non-existent. The file is created under /build/blocks-manifest.php which is already excluded above.

<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>src/blocks-manifest.php</exclude-pattern>
<exclude-pattern>build/*.asset.php</exclude-pattern>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also already excluded via /build/ rule above.

<config name="minimum_supported_wp_version" value="6.6"/>

<rule ref="WordPress">
<exclude name="Generic.Arrays.DisallowShortArraySyntax"/>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these opinionated excludes.

<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<property name="prefixes" type="array">
<element value="advanced_multi_block"/>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice feature. I've updated all the instances where this wasn't used.

<rule ref="WordPress.WP.I18n">
<properties>
<property name="text_domain" type="array">
<element value="advanced-multi-block"/>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today I learned that the WordPress ruleset doesn't extract this from the plugin meta header automatically which I assumed it would.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant