PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / class-actionscheduler-setup.php

class-actionscheduler-setup.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/class-actionscheduler-setup.php

101 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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