*/ protected function input_properties(): array { return array( 'type' => array( 'type' => 'string', 'enum' => array( 'plugin', 'theme' ), 'description' => __( 'Which kind of asset. WP Rollback supports these two and not WordPress core.', 'acrossai-abilities-manager' ), ), 'plugin' => array( 'type' => 'string', 'description' => __( 'Plugin file, folder name, display name or partial match. Required when type is plugin.', 'acrossai-abilities-manager' ), ), 'stylesheet' => array( 'type' => 'string', 'description' => __( 'Theme directory name. Required when type is theme.', 'acrossai-abilities-manager' ), ), 'limit' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => Asset_Versions::MAX_LIMIT, 'default' => Asset_Versions::DEFAULT_LIMIT, 'description' => __( 'Page size, newest first.', 'acrossai-abilities-manager' ), ), 'offset' => array( 'type' => 'integer', 'minimum' => 0, 'default' => 0, 'description' => __( 'Zero-based offset into the release list.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.45 * @return array */ protected function required_input(): array { return array( 'type' ); } /** * @since 0.0.45 * @return array */ protected function output_properties(): array { return array( 'type' => array( 'type' => 'string' ), 'slug' => array( 'type' => 'string' ), 'name' => array( 'type' => 'string' ), 'installed_version' => array( 'type' => 'string' ), 'versions' => array( 'type' => 'array' ), 'total' => array( 'type' => 'integer' ), 'limit' => array( 'type' => 'integer' ), 'offset' => array( 'type' => 'integer' ), 'has_more' => array( 'type' => 'boolean' ), 'archived_versions' => array( 'type' => 'array' ), ); } /** * @since 0.0.45 * @return array{readonly:bool,destructive:bool,idempotent:bool} */ protected function annotations(): array { return array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ); } /** * @since 0.0.45 * @param array $input Caller input. * @return array|\WP_Error */ protected function run( array $input ) { $type = self::scalar( $input['type'] ?? '' ); $identifier = 'theme' === $type ? self::scalar( $input['stylesheet'] ?? '' ) : self::scalar( $input['plugin'] ?? '' ); $asset = Asset_Lookup::resolve( $type, $identifier ); if ( is_wp_error( $asset ) ) { return $asset; } // Local archives are reported even when WordPress.org has nothing, because they are the one // route by which a premium asset can still be rolled back. $archived = Wp_Rollback_Guard::archived_versions( $asset['slug'] ); $published = Asset_Lookup::published_versions( $type, $asset['slug'] ); if ( is_wp_error( $published ) ) { if ( array() !== $archived ) { // Not on WordPress.org, but this site holds archives — a materially different // answer from "no versions at all", and the caller needs to know which. $archive_page = Asset_Versions::page( Asset_Versions::releases( $archived ), (int) ( $input['limit'] ?? Asset_Versions::DEFAULT_LIMIT ), (int) ( $input['offset'] ?? 0 ) ); return array_merge( array( 'type' => $asset['type'], 'slug' => $asset['slug'], 'name' => $asset['name'], 'installed_version' => $asset['installed_version'], 'archived_versions' => array_keys( $archived ), 'message' => sprintf( /* translators: 1: asset name, 2: archive count */ __( '"%1$s" is not published on WordPress.org, but this site holds %2$d locally archived version(s) that WP Rollback can restore.', 'acrossai-abilities-manager' ), $asset['name'], count( $archived ) ), ), $this->decorate( $archive_page, $asset['installed_version'] ) ); } return $published; } $page = Asset_Versions::page( Asset_Versions::releases( $published ), (int) ( $input['limit'] ?? Asset_Versions::DEFAULT_LIMIT ), (int) ( $input['offset'] ?? 0 ) ); return array_merge( array( 'type' => $asset['type'], 'slug' => $asset['slug'], 'name' => $asset['name'], 'installed_version' => $asset['installed_version'], 'archived_versions' => array_keys( $archived ), 'message' => sprintf( /* translators: 1: total, 2: asset name, 3: installed version */ __( 'WordPress.org publishes %1$d release(s) for "%2$s"; %3$s is installed.', 'acrossai-abilities-manager' ), $page['total'], $asset['name'], '' !== $asset['installed_version'] ? $asset['installed_version'] : __( 'an unknown version', 'acrossai-abilities-manager' ) ), ), $this->decorate( $page, $asset['installed_version'] ) ); } /** * Turn a page of version strings into rows that answer "is this a downgrade?". * * @since 0.0.45 * @param array $page Page from {@see Asset_Versions::page()}. * @param string $installed Installed version. * @return array */ private function decorate( array $page, string $installed ): array { $rows = array(); foreach ( $page['versions'] as $version ) { $rows[] = array( 'version' => $version, 'relation' => Asset_Versions::relation( $version, $installed ), 'installed' => $version === $installed, ); } return array( 'versions' => $rows, 'total' => $page['total'], 'limit' => $page['limit'], 'offset' => $page['offset'], 'has_more' => $page['has_more'], ); } }