network_wide = $network_wide; $this->disable_ext(); $this->enable_addons(); // Append table names in $wpdb. ap_append_table_names(); if ( $this->network_wide ) { $this->network_activate(); } else { $this->activate(); } } /** * Disable old AnsPress extensions. */ public function disable_ext() { deactivate_plugins( [ 'categories-for-anspress/categories-for-anspress.php', 'tags-for-anspress/tags-for-anspress.php', 'anspress-email/anspress-email.php', 'question-labels/question-labels.php', 'anspress-paid-membership/anspress-paid-membership.php', ] ); } /** * Enable default addons. */ public function enable_addons() { ap_activate_addon( 'free/reputation.php' ); ap_activate_addon( 'free/email.php' ); } /** * Ap_qameta table. */ public function qameta_table() { global $wpdb; // @codingStandardsIgnoreLine $this->tables[] = 'CREATE TABLE `' . $wpdb->ap_qameta . '` ( `post_id` bigint(20) NOT NULL, `selected_id` bigint(20) DEFAULT NULL, `comments` bigint(20) DEFAULT 0, `answers` bigint(20) DEFAULT 0, `ptype` varchar(100) DEFAULT NULL, `featured` tinyint(1) DEFAULT 0, `selected` tinyint(1) DEFAULT 0, `votes_up` bigint(20) DEFAULT 0, `votes_down` bigint(20) DEFAULT 0, `subscribers` TEXT DEFAULT NULL, `views` bigint(20) DEFAULT 0, `closed` tinyint(1) DEFAULT 0, `flags` bigint(20) DEFAULT 0, `terms` LONGTEXT DEFAULT NULL, `attach` LONGTEXT DEFAULT NULL, `activities` LONGTEXT DEFAULT NULL, `fields` LONGTEXT DEFAULT NULL, `roles` varchar(100) DEFAULT NULL, `last_updated` timestamp NULL DEFAULT NULL, UNIQUE KEY `post_id` (`post_id`) )' . $this->charset_collate . ';'; } /** * AnsPress ap_votes table. */ public function votes_table() { global $wpdb; $this->tables[] = 'CREATE TABLE `' . $wpdb->ap_votes . '` ( `vote_id` bigint(20) NOT NULL AUTO_INCREMENT, `vote_post_id` bigint(20) NOT NULL, `vote_user_id` bigint(20) NOT NULL, `vote_rec_user` bigint(20) NOT NULL, `vote_type` varchar(100) DEFAULT NULL, `vote_value` varchar(100) DEFAULT NULL, `vote_date` timestamp NULL DEFAULT NULL, PRIMARY KEY (`vote_id`) )' . $this->charset_collate . ';'; } /** * AnsPress views table. */ public function views_table() { global $wpdb; $this->tables[] = 'CREATE TABLE `' . $wpdb->ap_views . '` ( `view_id` bigint(20) NOT NULL AUTO_INCREMENT, `view_user_id` bigint(20) DEFAULT NULL, `view_type` varchar(100) DEFAULT NULL, `view_ref_id` bigint(20) DEFAULT NULL, `view_ip` varchar(39), `view_date` timestamp NULL DEFAULT NULL, PRIMARY KEY (`view_id`) )' . $this->charset_collate . ';'; } /** * AnsPress reputation table. */ public function reputation_table() { global $wpdb; $this->tables[] = 'CREATE TABLE `' . $wpdb->ap_reputations . '` ( `rep_id` bigint(20) NOT NULL AUTO_INCREMENT, `rep_user_id` bigint(20) DEFAULT NULL, `rep_event` varchar(100) DEFAULT NULL, `rep_ref_id` bigint(20) DEFAULT NULL, `rep_date` timestamp NULL DEFAULT NULL, PRIMARY KEY (`rep_id`) )' . $this->charset_collate . ';'; } /** * AnsPress email subscription related table. */ public function subscribers_table() { global $wpdb; $this->tables[] = 'CREATE TABLE `' . $wpdb->ap_subscribers . '` ( `subs_id` bigint(20) NOT NULL AUTO_INCREMENT, `subs_user_id` bigint(20) NOT NULL, `subs_ref_id` bigint(20) NOT NULL, `subs_event` varchar(100) NOT NULL, PRIMARY KEY (`subs_id`) )' . $this->charset_collate . ';'; } /** * Insert and update tables */ public function insert_tables() { global $wpdb; $this->charset_collate = ! empty( $wpdb->charset ) ? 'DEFAULT CHARACTER SET ' . $wpdb->charset : ''; $this->qameta_table(); $this->votes_table(); $this->views_table(); $this->reputation_table(); $this->subscribers_table(); require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); if ( count( $this->tables ) > 0 ) { foreach ( $this->tables as $table ) { dbDelta( $table ); } } } /** * Create base pages, add roles, add caps and create tables */ public function activate() { // add roles. $ap_roles = new AP_Roles; $ap_roles->add_roles(); $ap_roles->add_capabilities(); if ( ap_opt( 'ap_version' ) !== AP_VERSION ) { ap_opt( 'ap_installed', 'false' ); ap_opt( 'ap_version', AP_VERSION ); } if ( get_option( 'anspress_db_version' ) === '' ) { update_option( 'anspress_using_previous', 'true' ); } $this->insert_tables(); update_option( 'anspress_db_version', AP_DB_VERSION ); update_option( 'anspress_opt', get_option( 'anspress_opt' ) + ap_default_options() ); // Create main pages. ap_create_base_page(); ap_opt( 'ap_flush', 'true' ); flush_rewrite_rules( false ); } /** * Network activate. */ public function network_activate() { global $wpdb; // Get all blogs in the network and activate plugin on each one $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); // db call ok, cache ok. foreach ( (array) $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); // @codingStandardsIgnoreLine $this->activate(); restore_current_blog(); } } }