Making WordPress.org

Changeset 4804


Ignore:
Timestamp:
01/26/2017 11:29:48 PM (7 years ago)
Author:
coreymckrill
Message:

WCPT: Enable a v2 REST endpoint for the WordCamp CPT

This endpoint mimics the posts endpoint, except with the following
modifications:

  • WCPT uses custom post statuses, more than one of which is considered public. The default REST response includes all posts with a public post status.
  • Since a response may contain posts of more than one post status, that property is exposed in the view context.
  • Several post meta fields are exposed in the view context.
Location:
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-wordcamp/wordcamp-loader.php

    r4803 r4804  
    2222     */
    2323    function __construct() {
    24         add_action( 'plugins_loaded',      array( $this, 'includes'                          ) );
    25         add_action( 'init',                array( $this, 'register_post_types'               ) );
    26         add_action( 'init',                array( $this, 'register_post_statuses'            ) );
    27         add_filter( 'pre_get_posts',       array( $this, 'query_public_statuses_on_archives' ) );
    28         add_action( 'wp_insert_post_data', array( $this, 'set_scheduled_date'                ) );
    29         add_filter( 'wordcamp_rewrite_rules', array( $this, 'wordcamp_rewrite_rules' ) );
    30         add_filter( 'query_vars', array( $this, 'query_vars' ) );
     24        add_action( 'plugins_loaded',                  array( $this, 'includes'                          ) );
     25        add_action( 'init',                            array( $this, 'register_post_types'               ) );
     26        add_action( 'init',                            array( $this, 'register_post_statuses'            ) );
     27        add_filter( 'pre_get_posts',                   array( $this, 'query_public_statuses_on_archives' ) );
     28        add_action( 'wp_insert_post_data',             array( $this, 'set_scheduled_date'                ) );
     29        add_filter( 'wordcamp_rewrite_rules',          array( $this, 'wordcamp_rewrite_rules'            ) );
     30        add_filter( 'query_vars',                      array( $this, 'query_vars'                        ) );
     31        add_filter( 'rest_wordcamp_collection_params', array( $this, 'set_rest_post_status_default'      ) );
     32        add_action( 'rest_api_init',                   array( $this, 'register_rest_public_fields'       ) );
    3133    }
    3234
     
    4042    function includes () {
    4143        // Load the files
     44        require_once ( WCPT_DIR . 'wcpt-wordcamp/class-wp-rest-wordcamps-controller.php' );
    4245        require_once ( WCPT_DIR . 'wcpt-wordcamp/wordcamp-template.php' );
    4346
     
    9396        // Register WordCamp post type
    9497        register_post_type( WCPT_POST_TYPE_ID, array(
    95             'labels'            => $wcpt_labels,
    96             'rewrite'           => $wcpt_rewrite,
    97             'supports'          => $wcpt_supports,
    98             'menu_position'     => '100',
    99             'public'            => true,
    100             'show_ui'           => true,
    101             'can_export'        => true,
    102             'capability_type'   => 'post',
    103             'hierarchical'      => false,
    104             'has_archive'       => true,
    105             'query_var'         => true,
    106             'menu_icon'         => 'dashicons-wordpress',
     98            'labels'                => $wcpt_labels,
     99            'rewrite'               => $wcpt_rewrite,
     100            'supports'              => $wcpt_supports,
     101            'menu_position'         => '100',
     102            'public'                => true,
     103            'show_ui'               => true,
     104            'can_export'            => true,
     105            'capability_type'       => 'post',
     106            'hierarchical'          => false,
     107            'has_archive'           => true,
     108            'query_var'             => true,
     109            'menu_icon'             => 'dashicons-wordpress',
     110            'show_in_rest'          => true,
     111            'rest_base'             => 'wordcamps',
     112            'rest_controller_class' => 'WordCamp_REST_WordCamps_Controller',
    107113        ) );
    108114    }
     
    355361
    356362    /**
     363     * Meta field keys to publicly expose in the v2 REST API
     364     *
     365     * @see wcorg_json_expose_whitelisted_meta_data() in mu-plugins/wcorg-json-api.php
     366     *
     367     * @return array
     368     */
     369    public static function get_public_meta_keys() {
     370        return array(
     371            // Sourced from wcorg_json_expose_whitelisted_meta_data()
     372            'Start Date (YYYY-mm-dd)',
     373            'End Date (YYYY-mm-dd)',
     374            'Location',
     375            'URL',
     376            'Twitter',
     377            'WordCamp Hashtag',
     378            'Number of Anticipated Attendees',
     379            'Organizer Name',
     380            'WordPress.org Username',
     381            'Venue Name',
     382            'Physical Address',
     383            'Maximum Capacity',
     384            'Available Rooms',
     385            'Website URL',
     386            'Exhibition Space Available',
     387            // Additional venue data
     388            '_venue_coordinates',
     389            '_venue_city',
     390            '_venue_state',
     391            '_venue_country_code',
     392            '_venue_country_name',
     393            '_venue_zip',
     394        );
     395    }
     396
     397    /**
     398     * Register fields to publicly expose in the v2 REST API
     399     *
     400     * @hooked action rest_api_init
     401     */
     402    public function register_rest_public_fields() {
     403        $keys = self::get_public_meta_keys();
     404
     405        foreach ( $keys as $key ) {
     406            register_rest_field(
     407                'wordcamp',
     408                $key,
     409                array(
     410                    'get_callback' => function( $object, $field_name ) {
     411                        return get_post_meta( $object['id'], $field_name, true );
     412                    }
     413                )
     414            );
     415        }
     416    }
     417
     418    /**
     419     * Change the default status used for the WordCamp CPT in the v2 REST API.
     420     *
     421     * @hooked filter rest_wordcamp_collection_params
     422     *
     423     * @param array $query_params
     424     *
     425     * @return array
     426     */
     427    public function set_rest_post_status_default( $query_params ) {
     428        if ( isset( $query_params['status'] ) ) {
     429            $query_params['status']['default'] = WordCamp_Loader::get_public_post_statuses();
     430        }
     431
     432        return $query_params;
     433    }
     434
     435    /**
    357436     * Additional rules for the WordCamp post type.
    358437     *
Note: See TracChangeset for help on using the changeset viewer.