migrator = $migrator; $this->capabilities = $capabilities; $this->logger = $logger; } /** * Builds an installer without the container. Used by the activation hook, * which runs before the plugin has booted. * * @since 0.1.0 * * @return self */ public static function create(): self { return new self( new Migrator( $GLOBALS['wpdb'] ), new Capabilities(), new Logger( new Settings() ) ); } /** * Installs or updates the current site. * * @since 0.1.0 * * @return bool True when the database is up to date. */ public function install(): bool { $ok = $this->migrator->migrate(); if ( ! $ok ) { $this->logger->error( 'Database migration failed.', array( 'error' => $this->migrator->last_error() ) ); } $this->capabilities->install(); update_option( self::VERSION_OPTION, HOSTKHATA_VERSION, true ); if ( false === get_option( 'hostkhata_installed_at' ) ) { add_option( 'hostkhata_installed_at', time(), '', false ); } return $ok; } /** * Installs every site of a network. * * @since 0.1.0 * * @return void */ public function install_network(): void { $site_ids = get_sites( array( 'fields' => 'ids', 'number' => 0, ) ); foreach ( $site_ids as $site_id ) { switch_to_blog( (int) $site_id ); $this->install(); restore_current_blog(); } } /** * Installs a newly created site when the plugin is network active. * * @since 0.1.0 * * @param WP_Site $site New site. * @return void */ public function install_new_site( WP_Site $site ): void { if ( ! is_plugin_active_for_network( HOSTKHATA_BASENAME ) ) { return; } switch_to_blog( (int) $site->blog_id ); $this->install(); restore_current_blog(); } /** * Runs the installer when the database or the plugin version is behind. * * @since 0.1.0 * * @return void */ public function maybe_upgrade(): void { $version_matches = get_option( self::VERSION_OPTION ) === HOSTKHATA_VERSION; if ( $version_matches && ! $this->migrator->needs_upgrade() ) { return; } $this->install(); } /** * Last migration error message. * * @since 0.1.0 * * @return string */ public function last_error(): string { return $this->migrator->last_error(); } }