*/ class Epicformbuilder_Activator { /** * Creates the plugin's tables and uploads directories. * * On a network activation every site in the network is set up, so the * caller must pass the $network_wide flag WordPress hands to the * activation hook: the plugin's own active-for-network state cannot be * inspected reliably from here. * * @since 1.0.0 * * @param bool $network_wide Whether the plugin is being activated for every * site in a multisite network. */ public static function activate( $network_wide = false ) { global $wpdb; if ( is_multisite() && $network_wide ) { $site_ids = get_sites( [ 'fields' => 'ids', 'number' => 0, 'update_site_meta_cache' => false, ] ); foreach ( $site_ids as $site_id ) { switch_to_blog( $site_id ); // create tables for each site self::process_activation( $wpdb ); restore_current_blog(); } } else { self::process_activation( $wpdb ); } } private static function process_activation( $wpdb ) { /* Create Plugin Database Tables */ self::create_table_exports( $wpdb ); self::create_table_form_drafts( $wpdb ); self::create_table_forms( $wpdb ); self::create_table_pending_values( $wpdb ); self::create_table_statistics( $wpdb ); self::create_table_submissions( $wpdb ); /* Prefill Database With Default Data */ self::insert_default_data( $wpdb ); // A fresh install already has the current schema, so the history that // produced it must be recorded as done rather than replayed — at best // wasted work, at worst destructive. Epicformbuilder_Migrations_Runner::stamp_all(); self::prepare_uploads_directory(); } /** * @return void */ private static function create_table_exports( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::EXPORTS; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( uid varchar(40) NOT NULL DEFAULT '', form_uid varchar(40) NOT NULL DEFAULT '', from_utc datetime NOT NULL, to_utc datetime NOT NULL, file varchar(255) DEFAULT NULL, items int(11) NOT NULL, status tinyint(4) NOT NULL, started_utc datetime NOT NULL, finished_utc datetime DEFAULT NULL, PRIMARY KEY (uid) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * @return void */ private static function create_table_form_drafts( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::FORM_DRAFTS; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( uid varchar(40) NOT NULL DEFAULT '', json longtext NOT NULL, is_deleted tinyint(1) NOT NULL DEFAULT 0, created_utc datetime NOT NULL, updated_utc datetime NOT NULL, PRIMARY KEY (uid) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * @return void */ private static function create_table_forms( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::FORMS; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( uid varchar(40) NOT NULL DEFAULT '', revision smallint(4) NOT NULL DEFAULT 1, json longtext NOT NULL, is_deleted tinyint(1) NOT NULL DEFAULT 0, created_utc datetime NOT NULL, updated_utc datetime NOT NULL, PRIMARY KEY (uid,revision) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * Values the server established before a submission existed. * * One row per item — an uploaded object, a payment order — rather than a * JSON blob per submission, because parallel uploads write concurrently * and a read-modify-write on a shared blob loses rows. * * @param wpdb $wpdb WordPress database abstraction object. * * @return void */ private static function create_table_pending_values( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::PENDING_VALUES; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, submission_uid varchar(40) NOT NULL DEFAULT '', form_uid varchar(40) NOT NULL DEFAULT '', field_id varchar(40) NOT NULL DEFAULT '', item_key text NOT NULL, item_key_hash char(64) CHARACTER SET ascii NOT NULL DEFAULT '', value longtext NOT NULL, meta longtext, created_utc datetime NOT NULL, expires_utc datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY uniq_item (submission_uid,field_id,item_key_hash), KEY idx_expiry (expires_utc) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * @return void */ private static function create_table_statistics( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::STATISTICS; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( form_uid varchar(40) NOT NULL DEFAULT '', json longtext NOT NULL, created_utc date NOT NULL, PRIMARY KEY (form_uid,created_utc) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * @return void */ private static function create_table_submissions( $wpdb ) { $table_name = $wpdb->prefix . Epicformbuilder_Tables::SUBMISSIONS; $charset_collate = $wpdb->get_charset_collate(); $sql = " CREATE TABLE {$table_name} ( uid varchar(40) NOT NULL DEFAULT '', form_uid varchar(40) NOT NULL DEFAULT '', revision smallint(4) NOT NULL DEFAULT 1, json longtext NOT NULL, is_deleted tinyint(1) NOT NULL DEFAULT 0, viewed_utc datetime DEFAULT NULL, anonymized_utc datetime DEFAULT NULL, created_utc datetime NOT NULL, serial_monthly_form_total smallint(6) DEFAULT NULL, serial_monthly_total smallint(6) DEFAULT NULL, PRIMARY KEY (uid), KEY form_uid (form_uid) ) ENGINE=InnoDB {$charset_collate}; "; dbDelta( $sql ); } /** * @return void */ private static function insert_default_data( $wpdb ) { } /** * Create the plugin uploads directories and protect them from the web * * Protecting the legacy wp-content/uploads/epicformbuilder/ directory * here, rather than only the current one, reaches installs that have * been running since before uploads moved under * Epicformbuilder_Services_Uploads::base_dir(). This only runs on * activation now; an already-active install that needs it re-run gets * there via a migration, not this method. * * @return void */ private static function prepare_uploads_directory() { Epicformbuilder_Services_Uploads::ensure_protected(); Epicformbuilder_Services_Uploads::protect_legacy_directory(); } }