Make WordPress Core

Ticket #40748: 40748.2.diff

File 40748.2.diff, 6.5 KB (added by rmccue, 8 years ago)

Switch from admin-ajax to the REST API

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

     
    6464        'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
    6565        'press-this-add-category', 'crop-image', 'generate-password', 'save-wporg-username', 'delete-plugin',
    6666        'search-plugins', 'search-install-plugins', 'activate-plugin', 'update-theme', 'delete-theme',
    67         'install-theme', 'get-post-thumbnail-html', 'get-community-events',
     67        'install-theme', 'get-post-thumbnail-html',
    6868);
    6969
    7070// Deprecated
  • src/wp-admin/includes/ajax-actions.php

     
    297297}
    298298
    299299/**
    300  * Handles AJAX requests for community events
    301  *
    302  * @since 4.8.0
    303  */
    304 function wp_ajax_get_community_events() {
    305         require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
    306 
    307         check_ajax_referer( 'community_events' );
    308 
    309         $search         = isset( $_POST['location'] ) ? wp_unslash( $_POST['location'] ) : '';
    310         $timezone       = isset( $_POST['timezone'] ) ? wp_unslash( $_POST['timezone'] ) : '';
    311         $user_id        = get_current_user_id();
    312         $saved_location = get_user_option( 'community-events-location', $user_id );
    313         $events_client  = new WP_Community_Events( $user_id, $saved_location );
    314         $events         = $events_client->get_events( $search, $timezone );
    315 
    316         if ( is_wp_error( $events ) ) {
    317                 wp_send_json_error( array(
    318                         'error' => $events->get_error_message(),
    319                 ) );
    320         } else {
    321                 if ( isset( $events['location'] ) ) {
    322                         // Store the location network-wide, so the user doesn't have to set it on each site.
    323                         update_user_option( $user_id, 'community-events-location', $events['location'], true );
    324                 }
    325 
    326                 wp_send_json_success( $events );
    327         }
    328 }
    329 
    330 /**
    331300 * Ajax handler for dashboard widgets.
    332301 *
    333302 * @since 3.4.0
  • src/wp-admin/includes/dashboard.php

     
    144144        $events_client = new WP_Community_Events( $user_id, $user_location );
    145145
    146146        $script_data = array(
    147                 'nonce' => wp_create_nonce( 'community_events' ),
    148                 'cache' => $events_client->get_cached_events(),
     147                'nonce'  => wp_create_nonce( 'wp_rest' ),
     148                'cache'  => $events_client->get_cached_events(),
     149                'apiUrl' => rest_url( 'dashboard/v1/community-events' ),
    149150
    150151                'l10n' => array(
    151152                        'enter_closest_city' => __( 'Enter your closest city to find nearby events.' ),
  • src/wp-admin/includes/rest-endpoints.php

     
     1<?php
     2/**
     3 * Administration API: REST API handlers
     4 *
     5 * @package WordPress
     6 * @subpackage Administration
     7 * @since 4.8.0
     8 */
     9
     10/**
     11 * Register REST endpoints for the dashboard/v1 namespace.
     12 *
     13 * @since 4.8.0
     14 */
     15function wp_dashboard_rest_register() {
     16        // Community Events Dashboard Widget.
     17        register_rest_route( 'dashboard/v1', 'community-events', array(
     18                'methods'             => WP_REST_Server::EDITABLE,
     19                'callback'            => 'wp_dashboard_rest_get_community_events',
     20                'permission_callback' => 'is_user_logged_in',
     21                'args'                => array(
     22                        'location' => array(
     23                                'type' => 'string',
     24                                'default' => '',
     25                        ),
     26                        'timezone' => array(
     27                                'type' => 'string',
     28                                'default' => '',
     29                        ),
     30                ),
     31        ) );
     32}
     33
     34/**
     35 * Handles AJAX requests for community events
     36 *
     37 * @since 4.8.0
     38 */
     39function wp_dashboard_rest_get_community_events( WP_REST_Request $request ) {
     40        require_once( ABSPATH . 'wp-admin/includes/class-wp-community-events.php' );
     41
     42        $user_id        = get_current_user_id();
     43        $saved_location = get_user_option( 'community-events-location', $user_id );
     44        $events_client  = new WP_Community_Events( $user_id, $saved_location );
     45        $events         = $events_client->get_events( $request['location'], $request['timezone'] );
     46
     47        if ( is_wp_error( $events ) ) {
     48                return $events;
     49        }
     50
     51        if ( isset( $events['location'] ) ) {
     52                // Store the location network-wide, so the user doesn't have to set it on each site.
     53                update_user_option( $user_id, 'community-events-location', $events['location'], true );
     54        }
     55
     56        return $events;
     57}
  • src/wp-admin/js/dashboard.js

     
    288288                getEvents: function( requestParams ) {
    289289                        var initiatedBy,
    290290                            app = this,
    291                             $spinner = $( '.community-events-form' ).children( '.spinner' );
     291                            $spinner = $( '.community-events-form' ).children( '.spinner' ),
     292                            request;
    292293
    293294                        requestParams          = requestParams || {};
    294295                        requestParams._wpnonce = communityEventsData.nonce;
     
    298299
    299300                        $spinner.addClass( 'is-active' );
    300301
    301                         wp.ajax.post( 'get-community-events', requestParams )
     302                        request = {
     303                                type: 'POST',
     304                                url:  communityEventsData.apiUrl,
     305                                data: requestParams,
     306                        };
     307                        $.ajax( request )
    302308                                .always( function() {
    303309                                        $spinner.removeClass( 'is-active' );
    304310                                })
  • src/wp-includes/default-filters.php

     
    389389add_action( 'rest_api_init', 'rest_api_default_filters',   10, 1 );
    390390add_action( 'rest_api_init', 'register_initial_settings',  10 );
    391391add_action( 'rest_api_init', 'create_initial_rest_routes', 99 );
     392add_action( 'rest_api_init', 'wp_dashboard_rest_register', 99 );
    392393add_action( 'parse_request', 'rest_api_loaded' );
    393394
    394395/**
  • src/wp-settings.php

     
    239239require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-post-meta-fields.php' );
    240240require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-term-meta-fields.php' );
    241241require( ABSPATH . WPINC . '/rest-api/fields/class-wp-rest-user-meta-fields.php' );
     242require( ABSPATH . 'wp-admin/includes/rest-endpoints.php' );
    242243
    243244$GLOBALS['wp_embed'] = new WP_Embed();
    244245