Skip to content

Commit

Permalink
improve(#96): Update hook for enqueueing block editor assets
Browse files Browse the repository at this point in the history
  • Loading branch information
maxinacube committed Jan 24, 2024
1 parent f166501 commit 833cd7b
Showing 1 changed file with 24 additions and 35 deletions.
59 changes: 24 additions & 35 deletions class-post-type-spotlight-block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function __construct() {

add_action( 'init', [ $this, 'filter_rest_query' ] );
add_action( 'init', [ $this, 'block_init' ] );
add_action( 'init', [ $this, 'block_scripts' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'block_scripts' ] );

add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 999, 3 );

Expand Down Expand Up @@ -268,47 +268,36 @@ public function orderby_featured_first( $orderby, $query ) {
public function block_scripts() {

$post_type = false;

if ( is_admin() ) {
if ( isset($_GET['post'])) {
$post_id = $_GET['post'];
$post_type = get_post_type($post_id);
} elseif ( isset( $_GET['post_type'] ) ) {
$post_type = $_GET['post_type'];
}

if ( $post_type && use_block_editor_for_post_type($post_type)) {

$script_asset_path = POST_TYPE_SPOTLIGHT_PATH . 'blocks/build/index.asset.php';
$script_asset_path = POST_TYPE_SPOTLIGHT_PATH . 'blocks/build/index.asset.php';

if ( ! file_exists( $script_asset_path ) ) {
throw new \Error(
$script_asset_path . ' Missing: You need to run `npm start` or `npm run build` for the "post-type-spotlight/blocks" script first.'
);
}
if ( ! file_exists( $script_asset_path ) ) {
throw new \Error(
$script_asset_path . ' Missing: You need to run `npm start` or `npm run build` for the "post-type-spotlight/blocks" script first.'
);
}

$index_js = POST_TYPE_SPOTLIGHT_PLUGIN_URL . 'blocks/build/index.js';
$index_js = POST_TYPE_SPOTLIGHT_PLUGIN_URL . 'blocks/build/index.js';

$script_asset = require $script_asset_path;
$script_asset = require $script_asset_path;

$dependencies = array_merge(
$script_asset['dependencies'],
[
'wp-edit-post',
]
);
$dependencies = array_merge(
$script_asset['dependencies'],
[
'wp-edit-post',
]
);

wp_register_script(
'post-type-spotlight-block-editor',
$index_js,
$dependencies,
$script_asset['version'],
true
);
wp_register_script(
'post-type-spotlight-block-editor',
$index_js,
$dependencies,
$script_asset['version'],
true
);

wp_enqueue_script( 'post-type-spotlight-block-editor' );
}
}
wp_enqueue_script( 'post-type-spotlight-block-editor' );

}

/**
Expand Down

0 comments on commit 833cd7b

Please sign in to comment.