HEX
Server: Apache
System: Linux d5123.usc1.stableserver.net 5.14.0-570.17.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Sat May 24 12:53:17 EDT 2025 x86_64
User: d5123 (1001)
PHP: 8.4.21
Disabled: NONE
Upload Files
File: /home/d5123/myboofola_com/wp-content/plugins/defender-security/src/class-actionscheduler-setup.php
<?php
/**
 * Action Scheduler Setup
 *
 * Ensures required Action Scheduler database tables exist for each site.
 * Supports single-site and multisite setups.
 *
 * @package WP_Defender
 */

namespace WP_Defender;

use ActionScheduler_HybridStore;
use ActionScheduler_LoggerSchema;
use ActionScheduler_StoreSchema;
use WP_Site;

/**
 * Class ActionScheduler_Setup
 *
 * Ensures Action Scheduler database tables are created for each site in a multisite network,
 * as well as for single-site setups. Handles scenarios such as
 * admin fallback checks, new subsite creation.
 */
class ActionScheduler_Setup {

	/**
	 * Initialize hooks for setting up Action Scheduler tables.
	 */
	public static function init(): void {
		add_action( 'plugins_loaded', array( __CLASS__, 'maybe_create_tables' ) );
		add_action( 'wp_initialize_site', array( __CLASS__, 'create_tables_for_new_site' ) );
	}

	/**
	 * Ensure tables exist on the current site.
	 * Runs as a fallback during plugin load.
	 */
	public static function maybe_create_tables(): void {
		if ( ! self::tables_exist() ) {
			self::register_tables();
		}
	}

	/**
	 * Set up Action Scheduler tables for a new site in a multisite network.
	 *
	 * @param WP_Site $site The new site object.
	 */
	public static function create_tables_for_new_site( WP_Site $site ): void {
		switch_to_blog( $site->blog_id );
		self::register_tables();
		restore_current_blog();
	}

	/**
	 * Register Action Scheduler tables on the current site.
	 */
	private static function register_tables(): void {
		self::load_dependencies();

		if ( ! class_exists( 'ActionScheduler_HybridStore' ) ) {
			return;
		}

		$store = new ActionScheduler_HybridStore();
		add_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10, 2 );

		if ( ! class_exists( 'ActionScheduler_StoreSchema' ) || ! class_exists( 'ActionScheduler_LoggerSchema' ) ) {
			return;
		}

		$store_schema  = new ActionScheduler_StoreSchema();
		$logger_schema = new ActionScheduler_LoggerSchema();

		$store_schema->register_tables( true );
		$logger_schema->register_tables( true );

		remove_action( 'action_scheduler/created_table', array( $store, 'set_autoincrement' ), 10 );
	}

	/**
	 * Check if Action Scheduler tables exist.
	 *
	 * @return bool
	 */
	private static function tables_exist(): bool {
		self::load_dependencies();

		if ( ! class_exists( 'ActionScheduler_StoreSchema' ) ) {
			return false;
		}

		$store_schema = new ActionScheduler_StoreSchema();
		return $store_schema->tables_exist();
	}

	/**
	 * Load required Action Scheduler classes if not already loaded.
	 */
	private static function load_dependencies(): void {
		if ( ! class_exists( 'ActionScheduler_HybridStore' ) ) {
			require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/data-stores/ActionScheduler_HybridStore.php';
		}
		if ( ! class_exists( 'ActionScheduler_StoreSchema' ) ) {
			require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/schema/ActionScheduler_StoreSchema.php';
		}
		if ( ! class_exists( 'ActionScheduler_LoggerSchema' ) ) {
			require_once WP_DEFENDER_DIR . 'vendor/woocommerce/action-scheduler/classes/schema/ActionScheduler_LoggerSchema.php';
		}
	}
}