Make WordPress Core

Ticket #27639: 27581.patch

File 27581.patch, 4.6 KB (added by adamsilverstein, 12 years ago)

use wp-ajax and json api for apiCall

  • src/wp-admin/admin-ajax.php

     
    5858        'wp-remove-post-lock', 'dismiss-wp-pointer', 'upload-attachment', 'get-attachment',
    5959        'query-attachments', 'save-attachment', 'save-attachment-compat', 'send-link-to-editor',
    6060        'send-attachment-to-editor', 'save-attachment-order', 'heartbeat', 'get-revision-diffs',
    61         'save-user-color-scheme', 'update-widget',
     61        'save-user-color-scheme', 'update-widget', 'query-themes'
    6262);
    6363
    6464// Register core Ajax calls.
  • src/wp-admin/includes/ajax-actions.php

     
    22042204        update_user_meta( get_current_user_id(), 'admin_color', $color_scheme );
    22052205        wp_send_json_success();
    22062206}
     2207
     2208/**
     2209 * Perform a query_themes lookup on api.wordpress.org.
     2210 *
     2211 * @since x.x.x
     2212 */
     2213function wp_ajax_query_themes() {
     2214        $url = 'http://api.wordpress.org/themes/info/1.1/?action=query_themes';
     2215
     2216        check_ajax_referer( 'query-themes', 'nonce' );
     2217
     2218        $query_themes_cache_key = md5( serialize( $_POST['api'] ) );
     2219
     2220        if ( false === ( $json = get_transient( $query_themes_cache_key ) ) ) {
     2221                $request = wp_remote_post( $url, array(
     2222                        'body' => wp_unslash( $_POST['api'] ),
     2223                ) );
     2224
     2225                if ( is_wp_error( $request ) ) {
     2226                        wp_send_json_error();
     2227                }
     2228                if ( 200 != wp_remote_retrieve_response_code( $request ) ) {
     2229                        wp_send_json_error();
     2230                }
     2231
     2232                $json = json_decode( wp_remote_retrieve_body( $request ) );
     2233
     2234                if ( !$json ) {
     2235                        wp_send_json_error();
     2236                }
     2237
     2238                set_transient( $query_themes_cache_key, $json, 5 * MINUTE_IN_SECONDS );
     2239        }
     2240        wp_send_json_success( $json );
     2241
     2242}
     2243 No newline at end of file
  • src/wp-admin/js/theme.js

     
    251251
    252252                // Otherwise, send a new API call and add it to the cache.
    253253                if ( ! query && ! isPaginated ) {
    254                         query = this.apiCall( request ).done( function( data ) {
     254                        query = this.apiCall( request ).done( function( response ) {
     255                                data = response.data;
    255256                                // Update the collection with the queried data.
    256257                                self.reset( data.themes );
    257258                                count = data.info.results;
     
    273274                } else {
    274275                        // If it's a paginated request we need to fetch more themes...
    275276                        if ( isPaginated ) {
    276                                 return this.apiCall( request, isPaginated ).done( function( data ) {
     277                                return this.apiCall( request, isPaginated ).done( function( response ) {
     278                                        data = response.data;
     279
    277280                                        // Add the new themes to the current collection
    278281                                        // @todo update counter
    279282                                        self.add( data.themes );
     
    322325
    323326                // Ajax request to .org API
    324327                return $.ajax({
    325                         url: 'https://api.wordpress.org/themes/info/1.1/?action=query_themes',
     328                        url: ajaxurl,
    326329
    327330                        // We want JSON data
    328331                        dataType: 'json',
    329332                        type: 'POST',
    330                         crossDomain: true,
    331333
    332334                        // Request data
    333335                        data: {
    334                                 action: 'query_themes',
    335                                 request: _.extend({
    336                                         per_page: 72,
    337                                         fields: {
    338                                                 description: true,
    339                                                 tested: true,
    340                                                 requires: true,
    341                                                 rating: true,
    342                                                 downloaded: true,
    343                                                 downloadLink: true,
    344                                                 last_updated: true,
    345                                                 homepage: true,
    346                                                 num_ratings: true
    347                                         }
    348                                 }, request)
     336                                action: 'query-themes',
     337                                nonce: themes.data.settings._nonceQuery,
     338                                api: {
     339                                        action: 'query_themes',
     340                                        request: _.extend({
     341                                                per_page: 72,
     342                                                fields: {
     343                                                        description: true,
     344                                                        tested: true,
     345                                                        requires: true,
     346                                                        rating: true,
     347                                                        downloaded: true,
     348                                                        downloadLink: true,
     349                                                        last_updated: true,
     350                                                        homepage: true,
     351                                                        num_ratings: true
     352                                                }
     353                                        }, request)
     354                                }
    349355                        },
    350356
    351357                        beforeSend: function() {
  • src/wp-admin/theme-install.php

     
    4141                'installURI'    => current_user_can( 'install_themes' ) ? self_admin_url( 'theme-install.php' ) : null,
    4242                'adminUrl'      => parse_url( self_admin_url(), PHP_URL_PATH ),
    4343                'updateURI'     => self_admin_url( 'update.php' ),
    44                 '_nonceInstall' => wp_create_nonce( 'install-theme' )
     44                '_nonceInstall' => wp_create_nonce( 'install-theme' ),
     45                '_nonceQuery'   => wp_create_nonce( 'query-themes' ),
    4546        ),
    4647        'l10n' => array(
    4748                'addNew' => __( 'Add New Theme' ),