memo_blog ) { $this->memo = null; $this->memo_blog = $blog; } if ( null !== $this->memo ) { return $this->memo; } $rows = get_option( self::OPTION, array() ); $out = array(); foreach ( is_array( $rows ) ? $rows : array() as $row ) { if ( is_array( $row ) && ! empty( $row['id'] ) ) { $out[ $row['id'] ] = Profile::from_array( $row ); } } uasort( $out, static function ( Profile $a, Profile $b ) { return strcasecmp( $a->display_name(), $b->display_name() ); } ); $this->memo = $out; return $out; } /** * Finds a site by ID. * * @param string $id Site ID. * @return Profile|null */ public function find( $id ) { $all = $this->all(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Sites that use a connection, as their main connection or for any one source. * * @param string $connection_id Connection ID. * @return Profile[] */ public function using_connection( $connection_id ) { return array_filter( $this->all(), static function ( Profile $p ) use ( $connection_id ) { return in_array( (string) $connection_id, $p->connection_ids(), true ); } ); } /** * Inserts or updates a site. Assigns an ID to new sites. * * @param Profile $profile Site to save. * @return Profile */ public function save( Profile $profile ) { OptionUpdate::change( self::OPTION, static function ( array $rows ) use ( $profile ) { if ( '' === $profile->id ) { do { $profile->id = 's' . strtolower( wp_generate_password( 9, false ) ); } while ( isset( $rows[ $profile->id ] ) ); $profile->created = time(); } $rows[ $profile->id ] = $profile->to_array(); return $rows; } ); $this->memo = null; return $profile; } /** * Deletes a site. * * @param string $id Site ID. * @return void */ public function delete( $id ) { OptionUpdate::change( self::OPTION, static function ( array $rows ) use ( $id ) { unset( $rows[ $id ] ); return $rows; } ); $this->memo = null; } }