migrator = $migrator; $this->capabilities = $capabilities; } /** * Builds an installer outside the container, for the activation hook. * * @since 0.1.0 * * @return self */ public static function create(): self { return new self( new Migrator( $GLOBALS['wpdb'] ), new Capabilities() ); } /** * Runs the migrations and sets up the capability. * * @since 0.1.0 * * @return bool Whether the database is up to date. */ public function install(): bool { $ok = $this->migrator->migrate(); $this->capabilities->install(); update_option( self::VERSION_OPTION, INTERVIEWNEA_VERSION, true ); if ( $ok ) { update_option( self::PREFIX_OPTION, $GLOBALS['wpdb']->prefix, true ); } if ( false === get_option( 'interviewnea_installed_at' ) ) { add_option( 'interviewnea_installed_at', time(), '', false ); } return $ok; } /** * Installs on 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 on a site created after a network-wide activation. * * @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( INTERVIEWNEA_BASENAME ) ) { return; } switch_to_blog( (int) $site->blog_id ); $this->install(); restore_current_blog(); } /** * Re-runs the install when the plugin or database version changed. * * @since 0.1.0 * * @return void */ public function maybe_upgrade(): void { $prefix = $GLOBALS['wpdb']->prefix; if ( get_option( self::VERSION_OPTION ) === INTERVIEWNEA_VERSION && ! $this->migrator->needs_upgrade() ) { // One cheap string compare per request. Only when the table prefix changed (copied database, multisite // switch, test environment) are the tables looked for, and created again if they are missing. if ( get_option( self::PREFIX_OPTION ) === $prefix ) { return; } if ( $this->migrator->tables_exist() ) { update_option( self::PREFIX_OPTION, $prefix, true ); return; } $this->migrator->forget_version(); } $this->install(); } /** * Last migration error. * * @since 0.1.0 * * @return string */ public function last_error(): string { return $this->migrator->last_error(); } }