'ids' ) ) as $site_id ) { switch_to_blog( $site_id ); wmguru_setup_site(); restore_current_blog(); } return; } wmguru_setup_site(); } register_activation_hook( WMGURU_PLUGIN_FILE, 'wmguru_activate' ); /** * A new site created after the plugin is already network-active never fires * the activation hook for it either. Despite switching to the new site while * it sets the site up, WordPress fires this action back in the context of * the site the request started from (`wp_insert_site()` calls * `wp_initialize_site()`, which restores the original blog, and only then * fires 'wp_initialize_site') - so an explicit switch is required here, or * `wmguru_setup_site()` runs against the wrong site's options. * * @param WP_Site $new_site The newly created site. */ function wmguru_new_site( $new_site ) { $network_plugins = get_site_option( 'active_sitewide_plugins', array() ); if ( ! isset( $network_plugins[ plugin_basename( WMGURU_PLUGIN_FILE ) ] ) ) { return; } switch_to_blog( $new_site->id ); wmguru_setup_site(); restore_current_blog(); } add_action( 'wp_initialize_site', 'wmguru_new_site', 100 ); /** * Deactivation only stops the schedules. URL rewriting stops with the hooks * themselves, so visitors get the untouched originals immediately; the cache * is left in place (it is removed on uninstall). */ function wmguru_clear_site_schedules() { wp_clear_scheduled_hook( 'wmguru_daily_cleanup' ); wp_clear_scheduled_hook( 'wmguru_run_delivery_test' ); } /** * @param bool $network_wide Whether the plugin is being network-deactivated. */ function wmguru_deactivate( $network_wide = false ) { if ( $network_wide && is_multisite() ) { foreach ( get_sites( array( 'fields' => 'ids' ) ) as $site_id ) { switch_to_blog( $site_id ); wmguru_clear_site_schedules(); restore_current_blog(); } return; } wmguru_clear_site_schedules(); } register_deactivation_hook( WMGURU_PLUGIN_FILE, 'wmguru_deactivate' );