Skip to content
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

Allow for registering scripts within block.json to be handled with asset loader #55

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions asset-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
const LOADED = true;

require plugin_dir_path( __FILE__ ) . 'inc/admin.php';
require plugin_dir_path( __FILE__ ) . 'inc/blocks.php';
require plugin_dir_path( __FILE__ ) . 'inc/manifest.php';
require plugin_dir_path( __FILE__ ) . 'inc/paths.php';
require plugin_dir_path( __FILE__ ) . 'inc/namespace.php';
48 changes: 48 additions & 0 deletions inc/blocks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Define functions for registering a block from metadata with asset-loader.
*/

declare( strict_types=1 );

namespace Asset_Loader\Blocks;

use WP_Block_Type;

/**
* Get list of keys in block.json which should be filtered to load from a manifest.
*
* @return string[] An array of keys for block.json
*/
function get_asset_keys() : array {

$asset_keys = [
'editorScript',
'script',
'viewScript',
'editorStyle',

];

return apply_filters( 'asset_loader_block_asset_keys', $asset_keys );
}

function register_block_type_with_manifests( string|WP_Block_Type $block_type, array $args = [] ) {

// Early return if we are not using block.json.
if ( ! is_string( $block_type ) ) {
return \register_block_type( $block_type, $args );
}

add_filter( 'block_type_metadata', __NAMESPACE__ . '\\filter_block_metadata_scripts', 10, 1 );

$block = \register_block_type( $block_type, $args );

remove_filter( 'block_type_metadata', __NAMESPACE__ . '\\filter_block_metadata_scripts' );

return $block;
}

function filter_block_metadata_scripts( $metadata ) {
$asset_keys = get_asset_keys();
}