Register hooks automatically on startup.

This commit is contained in:
Vedanshu Jain 2024-09-11 15:09:52 +05:30
parent b4f9989c37
commit 932e659101
3 changed files with 148 additions and 9 deletions

View File

@ -707,11 +707,14 @@ final class WooCommerce {
*/
include_once WC_ABSPATH . 'includes/wccom-site/class-wc-wccom-site.php';
\Automattic\WooCommerce\HooksRegistry::load_hooks();
/**
* Libraries and packages.
*/
include_once WC_ABSPATH . 'packages/action-scheduler/action-scheduler.php';
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once WC_ABSPATH . 'includes/class-wc-cli.php';
}

View File

@ -4,10 +4,22 @@ declare(strict_types=1);
namespace Automattic\WooCommerce;
/**
* This class list and register core hooks for WooCommerce, without loading the file that contains the hooks. Classes that provides these hooks should be loaded by an Autoloader.
* This class list and register core hooks for WooCommerce, without loading the file that contains the hooks. Classes that provides these hooks should be loaded by an Autoloader or already loaded.
*/
class HooksRegistry {
/**
* Whether the hooks are already loaded or not.
*
* @var bool $hooks_loaded
*/
public static bool $hooks_loaded = false;
/**
* List of all actions that should be registered for all requests.
*
* @var array[] $all_request_actions
*/
private static array $all_request_actions = array(
array( 'pre_set_site_transient_update_plugins', array( 'WC_Helper_Updater', 'transient_update_plugins' ), 21, 1 ),
array( 'pre_set_site_transient_update_themes', array( 'WC_Helper_Updater', 'transient_update_themes' ), 21, 1 ),
@ -21,20 +33,36 @@ class HooksRegistry {
);
/**
* List of all filters that should be registered for all requests.
*
* @var array[] $all_request_filters
*/
private static array $all_request_filters = array(
array( 'rest_api_init', array( 'WC_Helper_Subscriptions_API', 'register_rest_routes' ) ),
array( 'rest_api_init', array( 'WC_Helper_Orders_API', 'register_rest_routes' ) ),
);
private static array $frontend_actions = array(
/**
* List of actions that should be registered for frontend requests.
*
* @var array $frontend_actions
*/
private static array $frontend_actions = array();
);
private static array $frontend_filters = array(
);
/**
* List of filters that should be registered for frontend requests.
*
* @var array $frontend_filters
*/
private static array $frontend_filters = array();
/**
* List of actions that should be registered for admin requests.
*
* @var array[] $admin_actions
*/
private static array $admin_actions = array(
array( 'admin_notices', array( 'WC_Woo_Update_Manager_Plugin', 'show_woo_update_manager_install_notice' ) ),
array( 'admin_init', array( 'WC_Helper_Updater', 'add_hook_for_modifying_update_notices' ) ),
@ -44,8 +72,89 @@ class HooksRegistry {
array( 'woocommerce_helper_loaded', array( 'WC_Helper_Compat', 'helper_loaded' ) ),
);
private static array $admin_filters = array(
/**
* List of filters that should be registered for admin requests.
*
* @var array $admin_filters
*/
private static array $admin_filters = array();
);
/**
* Load all registered hooks.
*/
public static function load_hooks() {
if ( self::$hooks_loaded ) {
wc_doing_it_wrong( 'HooksRegistry::load_hooks', 'HooksRegistry is already loaded.', '9.5.0' );
return;
}
if ( is_admin() ) {
foreach ( self::$admin_actions as $action ) {
add_action( ...$action );
}
foreach ( self::$admin_filters as $filter ) {
add_filter( ...$filter );
}
}
if ( ! is_admin() && ! defined( 'DOING_CRON' ) ) {
foreach ( self::$frontend_actions as $action ) {
add_action( ...$action );
}
foreach ( self::$frontend_filters as $filter ) {
add_filter( ...$filter );
}
}
foreach ( self::$all_request_actions as $action ) {
add_action( ...$action );
}
foreach ( self::$all_request_filters as $filter ) {
add_filter( ...$filter );
}
self::$hooks_loaded = true;
}
/**
* DANGEROUS: This method is used for testing and benchmarking. Do not call, unless you really know what you are doing.
*/
public static function unload_hooks() {
if ( ! self::$hooks_loaded ) {
return;
}
if ( is_admin() ) {
foreach ( self::$admin_actions as $action ) {
remove_action( ...$action );
}
foreach ( self::$admin_filters as $filter ) {
remove_filter( ...$filter );
}
}
if ( ! is_admin() && ! defined( 'DOING_CRON' ) ) {
foreach ( self::$frontend_actions as $action ) {
remove_action( ...$action );
}
foreach ( self::$frontend_filters as $filter ) {
remove_filter( ...$filter );
}
}
foreach ( self::$all_request_actions as $action ) {
remove_action( ...$action );
}
foreach ( self::$all_request_filters as $filter ) {
remove_filter( ...$filter );
}
self::$hooks_loaded = false;
}
}

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Automattic\WooCommerce\Tests;
use Automattic\WooCommerce\HooksRegistry;
/**
* Test HooksRegistry class.
*/
class HooksRegistryTest extends \WC_Unit_Test_Case {
/**
* @testDox Make sure hooks are loaded as expected
*/
public function test_load_hooks() {
$all_request_action = array( 'upgrader_process_complete', array( 'WC_Helper_Updater', 'upgrader_process_complete' ) );
$all_request_filter = array( 'rest_api_init', array( 'WC_Helper_Subscriptions_API', 'register_rest_routes' ) );
HooksRegistry::unload_hooks();
$this->assertFalse( has_filter( ...$all_request_action ) );
$this->assertFalse( has_filter( ...$all_request_filter ) );
HooksRegistry::load_hooks();
$this->assertEquals( 10, has_filter( ...$all_request_action ) );
$this->assertEquals( 10, has_filter( ...$all_request_filter ) );
}
}