| 1 |
<?php |
| 2 |
/** |
| 3 |
* Action Scheduler Setup |
| 4 |
* |
| 5 |
* Ensures required Action Scheduler database tables exist for each site. |
| 6 |
* Supports single-site and multisite setups. |
| 7 |
* |
| 8 |
* @package WP_Defender |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WP_Defender; |
| 12 |
|
| 13 |
use ActionScheduler_HybridStore; |
| 14 |
use ActionScheduler_LoggerSchema; |
| 15 |
use ActionScheduler_StoreSchema; |
| 16 |
use WP_Site; |
| 17 |
|
| 18 |
/** |
| 19 |
* Class ActionScheduler_Setup |
| 20 |
* |
| 21 |
* Ensures Action Scheduler database tables are created for each site in a multisite network, |
| 22 |
* as well as for single-site setups. Handles scenarios such as |
| 23 |
* admin fallback checks, new subsite creation. |
| 24 |
*/ |
| 25 |
class ActionScheduler_Setup { |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize hooks for setting up Action Scheduler tables. |
| 29 |
*/ |
| 30 |
public static function init(): void { |
| 31 |
add_action( 'plugins_loaded', array( __CLASS__, 'maybe_create_tables' ) ); |
| 32 |
add_action( 'wp_initialize_site', array( __CLASS__, 'create_tables_for_new_site' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Ensure tables exist on the current site. |
| 37 |
* Runs as a fallback during plugin load. |
| 38 |
*/ |
| 39 |
public static function maybe_create_tables(): void { |
| 40 |
if ( ! self::tables_exist() ) { |
| 41 |
self::register_tables(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set up Action Scheduler tables for a new site in a multisite network. |
| 47 |
* |
| 48 |
* @param WP_Site $site The new site object. |
| 49 |
*/ |
| 50 |
public static function create_tables_for_new_site( WP_Site $site ): void { |
| 51 |
switch_to_blog( $site->blog_id ); |
| 52 |
self::register_tables(); |
| 53 |
restore_current_blog(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Register Action Scheduler tables on the current site. |
| 58 |
*/ |
| 59 |
private static function register_tables(): void { |
| 60 |
self::load_dependencies(); |
| 61 |
|
| 62 |
$store = new ActionScheduler_HybridStore(); |
| 63 |
add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 ); |
| 64 |
|
| 65 |
$store_schema = new ActionScheduler_StoreSchema(); |
| 66 |
$logger_schema = new ActionScheduler_LoggerSchema(); |
| 67 |
|
| 68 |
$store_schema->register_tables( true ); |
| 69 |
$logger_schema->register_tables( true ); |
| 70 |
|
| 71 |
remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Check if Action Scheduler tables exist. |
| 76 |
* |
| 77 |
* @return bool |
| 78 |
*/ |
| 79 |
private static function tables_exist(): bool { |
| 80 |
self::load_dependencies(); |
| 81 |
|
| 82 |
$store_schema = new ActionScheduler_StoreSchema(); |
| 83 |
return $store_schema->tables_exist(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Load required Action Scheduler classes if not already loaded. |
| 88 |
*/ |
| 89 |
private static function load_dependencies(): void { |
| 90 |
if ( ! class_exists( 'ActionScheduler_HybridStore' ) ) { |
| 91 |
require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/data-stores/ActionScheduler_HybridStore.php'; |
| 92 |
} |
| 93 |
if ( ! class_exists( 'ActionScheduler_StoreSchema' ) ) { |
| 94 |
require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/schema/ActionScheduler_StoreSchema.php'; |
| 95 |
} |
| 96 |
if ( ! class_exists( 'ActionScheduler_LoggerSchema' ) ) { |
| 97 |
require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/schema/ActionScheduler_LoggerSchema.php'; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|