'ids', 'number' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); self::install(); restore_current_blog(); } return; } self::install(); } /** * Install everything the plugin needs on the current site. * * @return void */ public static function install(): void { Schema::install(); Capabilities::install(); Settings::add_defaults(); update_option( 'rentbaik_version', RENTBAIK_VERSION, false ); // Register the vehicle post type now so its pretty URLs work right away. \Rentbaik\Fleet\VehiclePostType::register_types(); \Rentbaik\Frontend\Pages::create_missing(); flush_rewrite_rules( false ); if ( false === get_option( 'rentbaik_installed_at', false ) ) { update_option( 'rentbaik_installed_at', time(), false ); } \Rentbaik\Admin\SetupWizard::queue_redirect(); } /** * Deactivation callback: stop scheduled jobs, keep all data. * * @param bool $network_wide Whether the plugin is network deactivated. * @return void */ public static function deactivate( $network_wide = false ): void { if ( is_multisite() && $network_wide ) { foreach ( get_sites( array( 'fields' => 'ids', 'number' => 0, ) ) as $site_id ) { switch_to_blog( (int) $site_id ); self::clear_cron(); restore_current_blog(); } return; } self::clear_cron(); } /** * Remove the scheduled jobs of the current site. * * @return void */ private static function clear_cron(): void { foreach ( self::CRON_HOOKS as $hook ) { wp_clear_scheduled_hook( $hook ); } } /** * Multisite hooks: set up new sites and remove tables of deleted sites. * * @return void */ public static function register(): void { if ( ! is_multisite() ) { return; } // After WordPress has created the new site's own tables (priority 10). add_action( 'wp_initialize_site', array( self::class, 'new_site' ), 20 ); add_filter( 'wpmu_drop_tables', array( self::class, 'drop_tables' ), 10, 2 ); } /** * A site was added to the network: install Rentbaik there when it is network active. * * @param \WP_Site $site New site. * @return void */ public static function new_site( $site ): void { if ( ! function_exists( 'is_plugin_active_for_network' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } if ( ! is_plugin_active_for_network( plugin_basename( RENTBAIK_FILE ) ) ) { return; } switch_to_blog( (int) $site->blog_id ); self::install(); restore_current_blog(); } /** * A site is deleted: drop its Rentbaik tables too. * * @param string[] $tables Tables WordPress will drop. * @param int $site_id Site ID. * @return string[] */ public static function drop_tables( $tables, $site_id = 0 ): array { global $wpdb; $prefix = $wpdb->get_blog_prefix( (int) $site_id ) . 'rentbaik_'; foreach ( Schema::TABLES as $name ) { $tables[] = $prefix . $name; } return array_values( array_unique( (array) $tables ) ); } }