Changeset 8755
- Timestamp:
- 05/08/2019 11:43:13 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordcamp.org/public_html/wp-content/plugins/wc-post-types/inc/rest-api.php
r8583 r8755 7 7 8 8 namespace WordCamp\Post_Types\REST_API; 9 use WP_Rest_Server ;9 use WP_Rest_Server, WP_Post_Type; 10 10 11 11 defined( 'WPINC' ) || die(); … … 14 14 15 15 /** 16 * Add non-sensitive meta fields to the speaker/session REST API endpoints16 * Add non-sensitive meta fields to WordCamp post type REST API endpoints. 17 17 * 18 18 * If we ever want to register meta for purposes other than exposing it in the API, then this function will 19 19 * probably need to be re-thought and re-factored. 20 20 * 21 * @uses wcorg_register_meta_only_on_endpoint()22 *23 21 * @return void 24 22 */ 25 23 function expose_public_post_meta() { 26 $public_session_fields = array( 27 '_wcpt_session_time' => array( 28 'type' => 'integer', 29 'single' => true, 30 ), 31 32 '_wcpt_session_type' => array( 33 'single' => true, 34 ), 35 36 '_wcpt_session_slides' => array( 37 'single' => true, 38 ), 39 40 '_wcpt_session_video' => array( 41 'single' => true, 42 ), 43 ); 44 45 wcorg_register_meta_only_on_endpoint( 'post', $public_session_fields, '/wp-json/wp/v2/sessions/' ); 46 47 $public_sponsor_fields = array( 48 '_wcpt_sponsor_website' => array( 49 'single' => true, 50 ), 51 ); 52 53 wcorg_register_meta_only_on_endpoint( 'post', $public_sponsor_fields, '/wp-json/wp/v2/sponsors/' ); 24 $meta_defaults = [ 25 'show_in_rest' => true, 26 'single' => true, 27 ]; 28 29 // Session. 30 register_post_meta( 'wcb_session', '_wcpt_session_time', wp_parse_args( [ 'type' => 'integer' ], $meta_defaults ) ); 31 register_post_meta( 'wcb_session', '_wcpt_session_type', $meta_defaults ); 32 register_post_meta( 'wcb_session', '_wcpt_session_slides', $meta_defaults ); 33 register_post_meta( 'wcb_session', '_wcpt_session_video', $meta_defaults ); 34 35 // Sponsor. 36 register_post_meta( 'wcb_sponsor', '_wcpt_sponsor_website', $meta_defaults ); 54 37 } 55 38 … … 148 131 ); 149 132 } 133 150 134 add_action( 'rest_api_init', __NAMESPACE__ . '\register_additional_rest_fields' ); 135 136 /** 137 * Validate simple meta query parameters in an API request and add them to the args passed to WP_Query. 138 * 139 * @param array $args The prepared args for the WP_Query object. 140 * @param array $request The args from the REST API request. 141 * 142 * @return array 143 */ 144 function prepare_meta_query_args( $args, $request ) { 145 if ( isset( $request['wc_meta_key'], $request['wc_meta_value'] ) ) { 146 $args['meta_key'] = $request['wc_meta_key']; 147 $args['meta_value'] = $request['wc_meta_value']; 148 } 149 150 return $args; 151 } 152 153 add_filter( 'rest_wcb_session_query', __NAMESPACE__ . '\prepare_meta_query_args', 10, 2 ); 154 155 /** 156 * Add meta field schemas to Sessions collection parameters. 157 * 158 * This enables and validates simple meta query parameters for the Sessions endpoint. Specific meta keys are 159 * safelisted by filtering for ones that have `show_in_rest` set to `true`. 160 * 161 * TODO: This is necessary because as of version 5.2, WP does not support meta queries on the posts endpoint. 162 * See https://core.trac.wordpress.org/ticket/47194 163 * 164 * The parameters registered here are prefixed with `wc_` because we don't want to have a collision with a 165 * future implementation in Core, if there ever is one. 166 * 167 * @param array $query_params 168 * @param WP_Post_Type $post_type 169 * 170 * @return array 171 */ 172 function add_meta_collection_params( $query_params, $post_type ) { 173 // Avoid exposing potentially sensitive data. 174 $public_meta_fields = wp_list_filter( get_registered_meta_keys( 'post', $post_type->name ), [ 'show_in_rest' => true ] ); 175 176 $query_params['wc_meta_key'] = [ 177 'description' => __( 'Limit result set to posts with a value set for a specific meta key. Use in conjunction with the wc_meta_value parameter.', 'wordcamporg' ), 178 'type' => 'string', 179 'enum' => array_keys( $public_meta_fields ), 180 ]; 181 182 $query_params['wc_meta_value'] = [ 183 'description' => __( 'Limit result set to posts with a specific meta value. Use in conjunction with the wc_meta_key parameter.', 'wordcamporg' ), 184 'type' => 'string', 185 ]; 186 187 return $query_params; 188 } 189 190 add_filter( 'rest_wcb_session_collection_params', __NAMESPACE__ . '\add_meta_collection_params', 10, 2 ); 151 191 152 192 /**
Note: See TracChangeset
for help on using the changeset viewer.