From 212c880d713dce3cc2caebf219b742155cf5b6e4 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Thu, 1 Feb 2018 05:18:54 +0000 Subject: [PATCH] Themes: Use `api.wordpress.org/themes/info/1.2/` to query theme information. See #43192. git-svn-id: https://develop.svn.wordpress.org/trunk@42632 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 2 +- src/wp-admin/includes/theme.php | 54 ++++++++++++++----- src/wp-admin/js/theme.js | 13 +---- .../class-wp-customize-manager.php | 19 +------ 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 8861e154f0..ab521ba0ad 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -3218,7 +3218,7 @@ function wp_ajax_query_themes() { } $theme->name = wp_kses( $theme->name, $themes_allowedtags ); - $theme->author = wp_kses( $theme->author, $themes_allowedtags ); + $theme->author = wp_kses( $theme->author['display_name'], $themes_allowedtags ); $theme->version = wp_kses( $theme->version, $themes_allowedtags ); $theme->description = wp_kses( $theme->description, $themes_allowedtags ); $theme->stars = wp_star_rating( diff --git a/src/wp-admin/includes/theme.php b/src/wp-admin/includes/theme.php index 50de9b2702..b9164a7e71 100644 --- a/src/wp-admin/includes/theme.php +++ b/src/wp-admin/includes/theme.php @@ -421,19 +421,27 @@ function get_theme_feature_list( $api = true ) { * for more information on the make-up of possible return objects depending on the value of `$action`. */ function themes_api( $action, $args = array() ) { + // include an unmodified $wp_version + include( ABSPATH . WPINC . '/version.php' ); if ( is_array( $args ) ) { $args = (object) $args; } - if ( ! isset( $args->per_page ) ) { - $args->per_page = 24; + if ( 'query_themes' == $action ) { + if ( ! isset( $args->per_page ) ) { + $args->per_page = 24; + } } if ( ! isset( $args->locale ) ) { $args->locale = get_user_locale(); } + if ( ! isset( $args->wp_version ) ) { + $args->wp_version = substr( $wp_version, 0, 3 ); // X.y + } + /** * Filters arguments used to query for installer pages from the WordPress.org Themes API. * @@ -465,22 +473,24 @@ function themes_api( $action, $args = array() ) { $res = apply_filters( 'themes_api', false, $action, $args ); if ( ! $res ) { - // include an unmodified $wp_version - include( ABSPATH . WPINC . '/version.php' ); + $url = 'http://api.wordpress.org/themes/info/1.2/'; + $url = add_query_arg( + array( + 'action' => $action, + 'request' => $args, + ), + $url + ); - $url = $http_url = 'http://api.wordpress.org/themes/info/1.0/'; + $http_url = $url; if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { $url = set_url_scheme( $url, 'https' ); } $http_args = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ), - 'body' => array( - 'action' => $action, - 'request' => serialize( $args ), - ), ); - $request = wp_remote_post( $url, $http_args ); + $request = wp_remote_get( $url, $http_args ); if ( $ssl && is_wp_error( $request ) ) { if ( ! wp_doing_ajax() ) { @@ -493,7 +503,7 @@ function themes_api( $action, $args = array() ) { headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE ); } - $request = wp_remote_post( $http_url, $http_args ); + $request = wp_remote_get( $http_url, $http_args ); } if ( is_wp_error( $request ) ) { @@ -507,8 +517,11 @@ function themes_api( $action, $args = array() ) { $request->get_error_message() ); } else { - $res = maybe_unserialize( wp_remote_retrieve_body( $request ) ); - if ( ! is_object( $res ) && ! is_array( $res ) ) { + $res = json_decode( wp_remote_retrieve_body( $request ), true ); + if ( is_array( $res ) ) { + // Object casting is required in order to match the info/1.0 format. + $res = (object) $res; + } elseif ( null === $res ) { $res = new WP_Error( 'themes_api_failed', sprintf( @@ -519,6 +532,21 @@ function themes_api( $action, $args = array() ) { wp_remote_retrieve_body( $request ) ); } + + if ( isset( $res->error ) ) { + $res = new WP_Error( 'themes_api_failed', $res->error ); + } + } + + // Back-compat for info/1.2 API, upgrade the theme objects in query_themes to objects. + if ( 'query_themes' == $action ) { + foreach ( $res->themes as $i => $theme ) { + $res->themes[ $i ] = (object) $theme; + } + } + // Back-compat for info/1.2 API, downgrade the feature_list result back to an array. + if ( 'feature_list' == $action ) { + $res = (array) $res; } } diff --git a/src/wp-admin/js/theme.js b/src/wp-admin/js/theme.js index 44cc0f15f5..632c39986b 100644 --- a/src/wp-admin/js/theme.js +++ b/src/wp-admin/js/theme.js @@ -334,18 +334,7 @@ themes.Collection = Backbone.Collection.extend({ data: { // Request data request: _.extend({ - per_page: 100, - fields: { - description: true, - tested: true, - requires: true, - rating: true, - downloaded: true, - downloadLink: true, - last_updated: true, - homepage: true, - num_ratings: true - } + per_page: 100 }, request) }, diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index f2042bbbeb..8da075634e 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -5619,19 +5619,6 @@ final class WP_Customize_Manager { // Arguments for all queries. $wporg_args = array( 'per_page' => 100, - 'fields' => array( - 'screenshot_url' => true, - 'description' => true, - 'rating' => true, - 'downloaded' => true, - 'downloadlink' => true, - 'last_updated' => true, - 'homepage' => true, - 'num_ratings' => true, - 'tags' => true, - 'parent' => true, - // 'extended_author' => true, @todo: WordPress.org throws a 500 server error when this is here. - ), ); $args = array_merge( $wporg_args, $args ); @@ -5674,10 +5661,8 @@ final class WP_Customize_Manager { ); $theme->name = wp_kses( $theme->name, $themes_allowedtags ); - $theme->author = wp_kses( $theme->author, $themes_allowedtags ); $theme->version = wp_kses( $theme->version, $themes_allowedtags ); $theme->description = wp_kses( $theme->description, $themes_allowedtags ); - $theme->tags = implode( ', ', $theme->tags ); $theme->stars = wp_star_rating( array( 'rating' => $theme->rating, @@ -5702,8 +5687,8 @@ final class WP_Customize_Manager { // Map available theme properties to installed theme properties. $theme->id = $theme->slug; $theme->screenshot = array( $theme->screenshot_url ); - $theme->authorAndUri = $theme->author; - // The .org API can return the full parent theme details if passed the 'parent' arg, or if passed the 'template' option it'll return that in the event it's a child theme. + $theme->authorAndUri = wp_kses( $theme->author['display_name'], $themes_allowedtags ); + if ( isset( $theme->parent ) ) { $theme->parent = $theme->parent['slug']; } else {