Making WordPress.org

Changeset 5533


Ignore:
Timestamp:
06/01/2017 08:46:01 PM (7 years ago)
Author:
coreymckrill
Message:

WordCamp Post Types: Consolidate v2 REST functionality, parity w/ v1

This moves all the actions/filters related to v2 REST API endpoints
to a separate file and adds the (hopefully) last bit of response data
that was included in v1, but was missing in v2 (speaker avatars).

For the speaker avatar data, I decided to emulate how Core presents
the avatar data for post authors in API responses. So there is a top-
level avatar_urls field that is an object containing multiple
Gravatar URLs for different image sizes.

Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/wc-post-types.php

    r5529 r5533  
    5757        add_filter( 'the_content', array( $this, 'add_session_info_to_speaker_posts' ) );
    5858
    59         // REST API
    60         add_action( 'init',                     array( $this, 'expose_public_post_meta'  )        );
    61         add_filter( 'rest_prepare_wcb_speaker', array( $this, 'link_speaker_to_sessions' ), 10, 2 );
    62         add_filter( 'rest_prepare_wcb_session', array( $this, 'link_session_to_speakers' ), 10, 2 );
    63 
    6459        add_filter( 'dashboard_glance_items', array( $this, 'glance_items' ) );
    6560        add_filter( 'option_default_comment_status', array( $this, 'default_comment_ping_status' ) );
    6661        add_filter( 'option_default_ping_status', array( $this, 'default_comment_ping_status' ) );
     62
     63        add_action( 'init', array( $this, 'rest_init' ), 9 );
    6764    }
    6865
     
    7774        register_setting( 'wcb_sponsor_options', 'wcb_sponsor_level_order', array( $this, 'validate_sponsor_options' ) );
    7875        add_action( 'pre_get_posts', array( $this, 'admin_pre_get_posts' ) );
     76    }
     77
     78    /**
     79     * Runs during init, because rest_api_init is too late.
     80     */
     81    function rest_init() {
     82        require_once( 'inc/rest-api.php' );
    7983    }
    8084
     
    13981402
    13991403        return $content . $sessions_html;
    1400     }
    1401 
    1402     /**
    1403      * Add non-sensitive meta fields to the speaker/session REST API endpoints
    1404      *
    1405      * if we ever want to register meta for purposes other than exposing it in the API, then this function will
    1406      * probably need to be re-thought and re-factored.
    1407      */
    1408     public function expose_public_post_meta() {
    1409         $public_session_fields = array(
    1410             '_wcpt_session_time' => array(
    1411                 'type'   => 'integer',
    1412                 'single' => true,
    1413             ),
    1414 
    1415             '_wcpt_session_type' => array(
    1416                 'single' => true,
    1417             ),
    1418 
    1419             '_wcpt_session_slides' => array(
    1420                 'single' => true,
    1421             ),
    1422 
    1423             '_wcpt_session_video' => array(
    1424                 'single' => true,
    1425             ),
    1426         );
    1427 
    1428         wcorg_register_meta_only_on_endpoint( 'post', $public_session_fields, '/wp-json/wp/v2/sessions/' );
    1429 
    1430         $public_sponsor_fields = array(
    1431             '_wcpt_sponsor_website' => array(
    1432                 'single' => true,
    1433             )
    1434         );
    1435 
    1436         wcorg_register_meta_only_on_endpoint( 'post', $public_sponsor_fields, '/wp-json/wp/v2/sponsors/' );
    1437     }
    1438 
    1439     /**
    1440      * Link all sessions to the speaker in the `speakers` API endpoint
    1441      *
    1442      * This allows clients to request a speaker and get all their sessions embedded in the response, avoiding
    1443      * extra HTTP requests
    1444      *
    1445      * @param WP_REST_Response $response
    1446      * @param WP_Post          $post
    1447      *
    1448      * @return WP_REST_Response
    1449      */
    1450     function link_speaker_to_sessions( $response, $post ) {
    1451         $sessions = get_posts( array(
    1452             'post_type'      => 'wcb_session',
    1453             'posts_per_page' => 100,
    1454             'fields'         => 'ids',
    1455 
    1456             'meta_query' => array(
    1457                 array(
    1458                     'key'   => '_wcpt_speaker_id',
    1459                     'value' => $post->ID,
    1460                 ),
    1461             ),
    1462         ) );
    1463 
    1464         foreach( $sessions as $session_id ) {
    1465             $response->add_link(
    1466                 'sessions',
    1467                 get_rest_url( null, "/wp/v2/sessions/$session_id" ),
    1468                 array( 'embeddable' => true )
    1469             );
    1470         }
    1471 
    1472         return $response;
    1473     }
    1474 
    1475     /**
    1476      * Link all speakers to the session in the `sessions` API endpoint
    1477      *
    1478      * This allows clients to request a session and get all its speakers embedded in the response, avoiding extra
    1479      * HTTP requests
    1480      *
    1481      * @param WP_REST_Response $response
    1482      * @param WP_Post          $post
    1483      *
    1484      * @return WP_REST_Response
    1485      */
    1486     function link_session_to_speakers( $response, $post ) {
    1487         $speaker_ids = get_post_meta( $post->ID, '_wcpt_speaker_id', false );
    1488 
    1489         foreach( $speaker_ids as $speaker_id ) {
    1490             $response->add_link(
    1491                 'speakers',
    1492                 get_rest_url( null, "/wp/v2/speakers/$speaker_id" ),
    1493                 array( 'embeddable' => true )
    1494             );
    1495         }
    1496 
    1497         return $response;
    14981404    }
    14991405
Note: See TracChangeset for help on using the changeset viewer.