prefix . 'alpeg_forms_entries'; } /** * Activation (also network-wide on multisite). * * @param bool $network_wide Network-wide activation. */ public static function activate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $site_id ) { switch_to_blog( $site_id ); self::install_site(); restore_current_blog(); } return; } self::install_site(); } /** * Install for the current site. */ public static function install_site() { self::create_tables(); if ( false === get_option( 'alpeg_forms_settings' ) ) { add_option( 'alpeg_forms_settings', alpeg_forms_default_settings(), '', false ); } if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK ); } Files::protect_directory(); update_option( 'alpeg_forms_db_version', ALPEG_FORMS_DB_VERSION, false ); } /** * Create or update the table. */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table(); $collate = $wpdb->get_charset_collate(); // dbDelta is picky: one column per line, two spaces after PRIMARY KEY. $sql = "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, form_id varchar(64) NOT NULL DEFAULT '', form_title varchar(190) NOT NULL DEFAULT '', post_id bigint(20) unsigned NOT NULL DEFAULT 0, page_url varchar(500) NOT NULL DEFAULT '', data longtext NOT NULL, email varchar(190) NOT NULL DEFAULT '', ip varchar(45) NOT NULL DEFAULT '', user_agent varchar(255) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, consent tinyint(1) NOT NULL DEFAULT 0, status varchar(20) NOT NULL DEFAULT 'unread', mail_status varchar(20) NOT NULL DEFAULT '', mail_error varchar(255) NOT NULL DEFAULT '', import_ref varchar(100) NOT NULL DEFAULT '', created_at datetime NOT NULL DEFAULT '1970-01-01 00:00:00', PRIMARY KEY (id), KEY form_id (form_id), KEY email (email), KEY status (status), KEY created_at (created_at), KEY import_ref (import_ref) ) {$collate};"; dbDelta( $sql ); } /** * Bring the schema up to date after an update. */ public static function maybe_upgrade() { if ( get_option( 'alpeg_forms_db_version' ) !== ALPEG_FORMS_DB_VERSION ) { self::install_site(); } } /** * Deactivation: only remove the cron job, data stays. * * @param bool $network_wide Network-wide deactivation. */ public static function deactivate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0 ) ) as $site_id ) { switch_to_blog( $site_id ); wp_clear_scheduled_hook( self::CRON_HOOK ); restore_current_blog(); } return; } wp_clear_scheduled_hook( self::CRON_HOOK ); } /** * Multisite hooks: set up new sites, remove the table when a site is deleted. */ public static function init_multisite() { if ( ! is_multisite() ) { return; } add_action( 'wp_initialize_site', array( __CLASS__, 'on_new_site' ), 200 ); add_filter( 'wpmu_drop_tables', array( __CLASS__, 'drop_tables' ), 10, 2 ); } /** * A new site was added to the network: install right away if we are network-active. * * @param \WP_Site $site New site. */ public static function on_new_site( $site ) { if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! is_plugin_active_for_network( plugin_basename( ALPEG_FORMS_FILE ) ) ) { return; } switch_to_blog( (int) $site->blog_id ); self::install_site(); restore_current_blog(); } /** * A site is deleted: drop its entries table too. Its upload folder * (including our files) is removed by WordPress itself. * * @param string[] $tables Tables WordPress will drop. * @param int $site_id Site ID. * @return string[] */ public static function drop_tables( $tables, $site_id ) { global $wpdb; $tables[] = $wpdb->get_blog_prefix( $site_id ) . 'alpeg_forms_entries'; return $tables; } }