Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit e8bedde

Browse files
authored
Move conflict check to init action (#534)
Checking the feature plugin version on `init` ensures the constant is defined before we get to it, which is not guaranteed in `__construct` due to potential different load orders.
1 parent f975718 commit e8bedde

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

assets/php/class-wgpb-block-library.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,24 @@ public static function get_instance() {
3737
* Constructor.
3838
*/
3939
public function __construct() {
40+
if ( function_exists( 'register_block_type' ) ) {
41+
add_action( 'init', array( 'WGPB_Block_Library', 'init' ) );
42+
}
43+
}
44+
45+
/**
46+
* Initialize block library features.
47+
*/
48+
public static function init() {
4049
// Shortcut out if we see the feature plugin, v1.4 or below.
4150
// note: `FP_VERSION` is transformed to `WGPB_VERSION` in the grunt copy task.
4251
if ( defined( 'FP_VERSION' ) && version_compare( FP_VERSION, '1.4.0', '<=' ) ) {
4352
return;
4453
}
45-
if ( function_exists( 'register_block_type' ) ) {
46-
add_action( 'init', array( 'WGPB_Block_Library', 'register_blocks' ) );
47-
add_action( 'init', array( 'WGPB_Block_Library', 'register_assets' ) );
48-
add_filter( 'block_categories', array( 'WGPB_Block_Library', 'add_block_category' ) );
49-
add_action( 'admin_print_footer_scripts', array( 'WGPB_Block_Library', 'print_script_settings' ), 1 );
50-
}
54+
self::register_blocks();
55+
self::register_assets();
56+
add_filter( 'block_categories', array( 'WGPB_Block_Library', 'add_block_category' ) );
57+
add_action( 'admin_print_footer_scripts', array( 'WGPB_Block_Library', 'print_script_settings' ), 1 );
5158
}
5259

5360
/**

woocommerce-gutenberg-products-block.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@
2525
function wgpb_initialize() {
2626
require_once plugin_dir_path( __FILE__ ) . 'assets/php/class-wgpb-block-library.php';
2727

28-
// Remove core hooks in favor of our local feature plugin handlers.
29-
remove_action( 'init', array( 'WC_Block_Library', 'register_blocks' ) );
30-
remove_action( 'init', array( 'WC_Block_Library', 'register_assets' ) );
31-
remove_filter( 'block_categories', array( 'WC_Block_Library', 'add_block_category' ) );
32-
remove_action( 'admin_print_footer_scripts', array( 'WC_Block_Library', 'print_script_settings' ), 1 );
28+
// Remove core hook in favor of our local feature plugin handler.
29+
remove_action( 'init', array( 'WC_Block_Library', 'init' ) );
3330

3431
$files_exist = file_exists( plugin_dir_path( __FILE__ ) . '/build/featured-product.js' );
3532
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && ! $files_exist ) {

0 commit comments

Comments
 (0)