prefix . 'ranq_forms_entries'; } /** * Hooks that keep the table there on a network of sites. * * On a single site the activation hook is enough. On a network it is not: * a site added after the plugin was switched on never runs activation, so * without this it would have no entries table and every form sent on it * would have nowhere to go, quietly. * * @return void */ public static function hooks() { add_action( 'wp_initialize_site', array( __CLASS__, 'new_site' ), 20 ); // A last resort for a site that ended up without the table anyway: a // network site from before this version, or the plugin copied in over // FTP instead of activated. Only in the dashboard, so the front end // does not pay for the check. add_action( 'admin_init', array( __CLASS__, 'repair' ) ); } /** * Runs on activation. * * @param bool $network_wide Whether it was switched on for the whole network. * * @return void */ public static function activate( $network_wide = false ) { if ( $network_wide && is_multisite() ) { $sites = get_sites( array( 'fields' => 'ids', 'number' => 0, ) ); foreach ( $sites as $site_id ) { switch_to_blog( (int) $site_id ); self::build(); restore_current_blog(); } return; } self::build(); } /** * A site was just added to the network. * * @param WP_Site $site The new site. * * @return void */ public static function 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( RANQ_FORMS_FILE ) ) ) { return; } switch_to_blog( (int) $site->blog_id ); self::build(); restore_current_blog(); } /** * Builds the table if this site is missing it. * * @return void */ public static function repair() { if ( self::DB_VERSION === get_option( self::DB_VERSION_OPTION ) ) { return; } self::build(); } /** * The table, and the note that it is there. * * @return void */ private static function build() { self::create_tables(); // Not written with autoload, so it does not hang on every request. update_option( self::DB_VERSION_OPTION, self::DB_VERSION, false ); } /** * Runs on deactivation. Deletes nothing, only clears what is temporary. * * @return void */ public static function deactivate() { wp_cache_flush(); } /** * The entries table. * * Entries go into a table of their own, not into wp_posts. A site with ten * thousand entries must not slow down search and the page list in the * dashboard. * * @return void */ private static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::entries_table(); $collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, form_id bigint(20) unsigned NOT NULL DEFAULT 0, fields longtext NOT NULL, status varchar(20) NOT NULL DEFAULT 'published', ip varchar(45) NOT NULL DEFAULT '', user_agent varchar(255) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY form_id (form_id), KEY status (status), KEY created_at (created_at) ) {$collate};"; dbDelta( $sql ); } }