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.
This commit is contained in:
Kelly Dwan 2019-04-19 12:30:59 -04:00
parent f8e2a7c06e
commit 7bac46f286
2 changed files with 15 additions and 11 deletions

View File

@ -37,17 +37,24 @@ class WGPB_Block_Library {
* Constructor.
*/
public function __construct() {
if ( function_exists( 'register_block_type' ) ) {
add_action( 'init', array( 'WGPB_Block_Library', 'init' ) );
}
}
/**
* Initialize block library features.
*/
public static function init() {
// Shortcut out if we see the feature plugin, v1.4 or below.
// note: `FP_VERSION` is transformed to `WGPB_VERSION` in the grunt copy task.
if ( defined( 'FP_VERSION' ) && version_compare( FP_VERSION, '1.4.0', '<=' ) ) {
return;
}
if ( function_exists( 'register_block_type' ) ) {
add_action( 'init', array( 'WGPB_Block_Library', 'register_blocks' ) );
add_action( 'init', array( 'WGPB_Block_Library', 'register_assets' ) );
add_filter( 'block_categories', array( 'WGPB_Block_Library', 'add_block_category' ) );
add_action( 'admin_print_footer_scripts', array( 'WGPB_Block_Library', 'print_script_settings' ), 1 );
}
self::register_blocks();
self::register_assets();
add_filter( 'block_categories', array( 'WGPB_Block_Library', 'add_block_category' ) );
add_action( 'admin_print_footer_scripts', array( 'WGPB_Block_Library', 'print_script_settings' ), 1 );
}
/**

View File

@ -25,11 +25,8 @@ define( 'WGPB_ABSPATH', dirname( WGPB_PLUGIN_FILE ) . '/' );
function wgpb_initialize() {
require_once plugin_dir_path( __FILE__ ) . 'assets/php/class-wgpb-block-library.php';
// Remove core hooks in favor of our local feature plugin handlers.
remove_action( 'init', array( 'WC_Block_Library', 'register_blocks' ) );
remove_action( 'init', array( 'WC_Block_Library', 'register_assets' ) );
remove_filter( 'block_categories', array( 'WC_Block_Library', 'add_block_category' ) );
remove_action( 'admin_print_footer_scripts', array( 'WC_Block_Library', 'print_script_settings' ), 1 );
// Remove core hook in favor of our local feature plugin handler.
remove_action( 'init', array( 'WC_Block_Library', 'init' ) );
$files_exist = file_exists( plugin_dir_path( __FILE__ ) . '/build/featured-product.js' );
if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && ! $files_exist ) {