validator = new Validator(); $this->repository = new Repository($this->validator); $this->queue = new Queue($this->repository); $this->rest = new Rest($this); $this->privacy = new Privacy($this); } public function register() { if ($this->registered) { return; } $this->registered = true; add_filter('cron_schedules', [__CLASS__, 'cron_schedules']); add_action('ssffw_process_queue', [$this->queue, 'process']); add_action('ssffw_retention', [$this->privacy, 'cleanup']); $this->rest->register(); $this->privacy->register(); foreach (['admin' => 'Admin', 'frontend' => 'Frontend'] as $file => $class) { $path = SSFFW_PATH . 'includes/class-' . $file . '.php'; if (is_file($path)) { require_once $path; ssffw_register_legacy_alias($class); $fqcn = __NAMESPACE__ . '\\' . $class; if (class_exists($fqcn)) { (new $fqcn($this))->register(); } } } if (get_option('ssffw_db_version') !== SSFFW_VERSION) { self::install(); } add_action('init', [__CLASS__, 'schedule']); } public static function cron_schedules($schedules) { $schedules['ssffw_minute'] = [ 'interval' => 60, 'display' => __('SSF Forms every minute', 'ssf-forms'), ]; return $schedules; } public static function schedule() { if (!wp_next_scheduled('ssffw_process_queue')) { wp_schedule_event(time() + 60, 'ssffw_minute', 'ssffw_process_queue'); } if (!wp_next_scheduled('ssffw_retention')) { wp_schedule_event(time() + 300, 'daily', 'ssffw_retention'); } } public static function activate() { add_filter('cron_schedules', [__CLASS__, 'cron_schedules']); self::install(); self::schedule(); } public static function deactivate() { wp_clear_scheduled_hook('ssffw_process_queue'); wp_clear_scheduled_hook('ssffw_retention'); } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $p = $wpdb->prefix . 'ssffw_'; $sql = [ "CREATE TABLE {$p}forms ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, title varchar(200) NOT NULL, status varchar(20) NOT NULL DEFAULT 'draft', version bigint(20) unsigned NOT NULL DEFAULT 1, schema_json longtext NOT NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY status (status) ) {$charset};", "CREATE TABLE {$p}entries ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, form_id bigint(20) unsigned NOT NULL, submission_id varchar(128) NOT NULL, submission_key char(64) NOT NULL, payload_hash char(64) NOT NULL, schema_version bigint(20) unsigned NOT NULL, schema_json longtext NOT NULL, values_json longtext NOT NULL, status varchar(20) NOT NULL DEFAULT 'accepted', created_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY form_submission (form_id,submission_key), KEY form_created (form_id,created_at), KEY created_at (created_at) ) {$charset};", "CREATE TABLE {$p}actions ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, entry_id bigint(20) unsigned NOT NULL, action varchar(64) NOT NULL, route varchar(190) NOT NULL, action_key char(64) NOT NULL, payload_json longtext NOT NULL, status varchar(20) NOT NULL DEFAULT 'queued', attempts int unsigned NOT NULL DEFAULT 0, max_attempts int unsigned NOT NULL DEFAULT 3, last_error text NOT NULL, next_attempt_at datetime NOT NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY action_key (action_key), KEY entry_id (entry_id), KEY ready (status,next_attempt_at) ) {$charset};", "CREATE TABLE {$p}rates ( rate_key char(64) NOT NULL, hits int unsigned NOT NULL DEFAULT 1, expires_at bigint(20) unsigned NOT NULL, PRIMARY KEY (rate_key), KEY expires_at (expires_at) ) {$charset};", ]; foreach ($sql as $statement) { dbDelta($statement); } update_option('ssffw_db_version', SSFFW_VERSION, false); } }