provision_current_site(); // 1.1.0 added the field width column: give existing built-in fields their layout width. if ( '' !== $installed && version_compare( $installed, '1.1.0', '<' ) ) { ADVC_Fields::backfill_widths(); } // 1.3.0 removed the pre-filled service names. if ( '' !== $installed && version_compare( $installed, '1.3.0', '<' ) ) { ADVC_Fields::clear_default_services(); } } } /** * Fires on plugin activation. On a single site this provisions that * site. On a multisite network, if the plugin was Network Activated, * it loops every existing site and provisions each one individually * via switch_to_blog() — the standard pattern documented in the * WordPress Plugin Handbook for network-wide activation. * * Note for very large networks (many thousands of sites): this runs * synchronously during activation. For that scale, consider batching * get_sites() with 'number'/'offset' and provisioning asynchronously; * this straightforward loop is production-ready for typical networks. * * @since 1.0.0 * @since 1.1.0 Added multisite / network-wide activation handling. * @param bool $network_wide Whether the plugin is being network-activated. */ public function activate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { $site_ids = get_sites( array( 'fields' => 'ids' ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); $this->provision_current_site(); restore_current_blog(); } } else { $this->provision_current_site(); } } /** * Provision the currently active site: create/upgrade tables, seed * default fields, create the Contact Us page, record the DB version, * and flush that site's rewrite rules. * * @since 1.1.0 */ private function provision_current_site() { ADVC_DB::install(); ADVC_Fields::maybe_seed_defaults(); ADVC_Page::create_default_page(); update_option( 'advc_db_version', ADVC_DB_VERSION ); flush_rewrite_rules(); } /** * Auto-provision a newly created site on a network where Advisers * Contacts is network-activated, so admins never have to manually * "reactivate" the plugin per new site. * * @since 1.1.0 * @param WP_Site $new_site Newly created site object. */ public function provision_new_site( $new_site ) { if ( ! is_multisite() ) { return; } if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! is_plugin_active_for_network( ADVC_BASENAME ) ) { return; } switch_to_blog( (int) $new_site->blog_id ); $this->provision_current_site(); restore_current_blog(); } /** * Add this plugin's tables to WordPress core's own list of tables to * drop when a site is deleted from a network, via the `wpmu_drop_tables` * filter. This is the documented, official way for a plugin to keep * its per-site tables from becoming orphaned data after a site delete. * * @since 1.1.0 * @param string[] $tables Table names WordPress will drop for the site being deleted. * @return string[] */ public function register_tables_for_site_deletion( $tables ) { $tables[] = ADVC_DB::submissions_table(); $tables[] = ADVC_DB::fields_table(); $tables[] = ADVC_DB::referrers_table(); return $tables; } /** * Fires on plugin deactivation. Data is intentionally kept; only * transient rewrite rules are cleared. Full removal happens in * uninstall.php if the site owner deletes the plugin. * * @since 1.0.0 * @param bool $network_wide Whether the plugin was network-deactivated. */ public function deactivate( $network_wide = false ) { if ( is_multisite() && $network_wide ) { $site_ids = get_sites( array( 'fields' => 'ids' ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); flush_rewrite_rules(); restore_current_blog(); } } else { flush_rewrite_rules(); } } } ADVC_Plugin::get_instance();