Ticket #4047: 4047.diff
File 4047.diff, 6.2 KB (added by , 5 years ago) |
---|
-
new file wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-wp-rest-meetups-controller.php
diff --git wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-wp-rest-meetups-controller.php wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/class-wp-rest-meetups-controller.php new file mode 100644 index 000000000..52a307947
- + 1 <?php 2 3 /** 4 * Class to access Meetup CPT posts via the v2 REST API. 5 * 6 * @see WP_REST_Posts_Controller 7 */ 8 class WordCamp_REST_Meetups_Controller extends WP_REST_Posts_Controller { 9 /** 10 * Retrieves the Meetup post's schema, conforming to JSON Schema. 11 * 12 * Meetup-specific modifications to the standard post schema. 13 * 14 * @access public 15 * 16 * @return array Item schema data. 17 */ 18 public function get_item_schema() { 19 $schema = parent::get_item_schema(); 20 21 // Since there is more than one public post status, show it in REST response 22 if ( false === array_search( 'view', $schema['properties']['status']['context'] ) ) { 23 $schema['properties']['status']['context'][] = 'view'; 24 } 25 26 return $this->add_additional_fields_schema( $schema ); 27 } 28 29 /** 30 * Sanitizes and validates the list of post statuses, including whether the 31 * user can query private statuses. 32 * 33 * Based on the method in WP_REST_Posts_Controller, but takes into account that 34 * there are multiple public statuses for the Meetup CPT. 35 * 36 * @access public 37 * 38 * @param string|array $statuses One or more post statuses. 39 * @param WP_REST_Request $request Full details about the request. 40 * @param string $parameter Additional parameter to pass to validation. 41 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object. 42 */ 43 public function sanitize_post_statuses( $statuses, $request, $parameter ) { 44 $statuses = wp_parse_slug_list( $statuses ); 45 46 $public_statuses = Meetup_Loader::get_public_post_statuses(); 47 48 foreach ( $statuses as $status ) { 49 if ( in_array( $status, $public_statuses ) ) { 50 continue; 51 } 52 53 $post_type_obj = get_post_type_object( $this->post_type ); 54 55 if ( current_user_can( $post_type_obj->cap->edit_posts ) ) { 56 $result = rest_validate_request_arg( $status, $request, $parameter ); 57 if ( is_wp_error( $result ) ) { 58 return $result; 59 } 60 } else { 61 return new WP_Error( 'rest_forbidden_status', __( 'Status is forbidden.' ), array( 'status' => rest_authorization_required_code() ) ); 62 } 63 } 64 65 return $statuses; 66 } 67 68 /** 69 * Checks if user can read the Meetup post. 70 * 71 * First make our custom check against public Meetup statuses and 72 * after that fallback to default WP_REST_Posts_Controller for assurance. 73 * 74 * @access public 75 * 76 * @param object $post Post object. 77 * @return bool Whether the post can be read. 78 */ 79 public function check_read_permission( $post ) { 80 $public_statuses = Meetup_Loader::get_public_post_statuses(); 81 82 // If post status is not listed as public, it cannot be read 83 if ( ! in_array( $post->post_status, $public_statuses ) ) { 84 return false; 85 } 86 87 // Fallback to default read permission check 88 return WP_REST_Posts_Controller::check_read_permission( $post ); 89 } 90 } -
wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/meetup-loader.php
diff --git wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/meetup-loader.php wordcamp.org/public_html/wp-content/plugins/wcpt/wcpt-meetup/meetup-loader.php index 5a196f96f..bd63c17e9 100644
if ( ! class_exists( 'Meetup_Loader' ) ) : 25 25 parent::__construct(); 26 26 add_action( 'init', array( $this, 'register_meetup_taxonomy' ) ); 27 27 add_action( 'set_object_terms', array( $this, 'log_meetup_tags' ), 10, 6 ); 28 add_filter( 'rest_wp_meetup_collection_params', array( $this, 'set_rest_post_status_default' ) ); 28 29 } 29 30 30 31 /** … … if ( ! class_exists( 'Meetup_Loader' ) ) : 105 106 * Include files specific for meetup event 106 107 */ 107 108 public function includes() { 109 require_once ( WCPT_DIR . 'wcpt-meetup/class-wp-rest-meetups-controller.php' ); 108 110 } 109 111 110 112 /** … … if ( ! class_exists( 'Meetup_Loader' ) ) : 141 143 'author', 142 144 ); 143 145 144 // Register WordCamp post type146 // Register meetup post type 145 147 register_post_type( 146 148 Meetup_Application::POST_TYPE, array( 147 'labels' => $wcpt_labels, 148 'rewrite' => $wcpt_rewrite, 149 'supports' => $wcpt_supports, 150 'menu_position' => '100', 151 'public' => true, 152 'show_ui' => true, 153 'can_export' => true, 154 'capability_type' => Meetup_Application::POST_TYPE, 155 'map_meta_cap' => true, 156 'hierarchical' => false, 157 'has_archive' => true, 158 'query_var' => true, 159 'menu_icon' => 'dashicons-wordpress', 160 'show_in_rest' => true, 161 'rest_base' => 'meetups', 149 'labels' => $wcpt_labels, 150 'rewrite' => $wcpt_rewrite, 151 'supports' => $wcpt_supports, 152 'menu_position' => '100', 153 'public' => true, 154 'show_ui' => true, 155 'can_export' => true, 156 'capability_type' => Meetup_Application::POST_TYPE, 157 'map_meta_cap' => true, 158 'hierarchical' => false, 159 'has_archive' => true, 160 'query_var' => true, 161 'menu_icon' => 'dashicons-wordpress', 162 'show_in_rest' => true, 163 'rest_base' => 'meetups', 164 'rest_controller_class' => 'WordCamp_REST_Meetups_Controller', 162 165 ) 163 166 ); 164 167 } … … if ( ! class_exists( 'Meetup_Loader' ) ) : 181 184 return Meetup_Application::get_public_post_statuses(); 182 185 } 183 186 187 /** 188 * Change the default status used for the Meetup CPT in the v2 REST API. 189 * 190 * @hooked filter rest_wp_meetup_collection_params 191 * 192 * @param array $query_params 193 * 194 * @return array 195 */ 196 public function set_rest_post_status_default( $query_params ) { 197 if ( isset( $query_params['status'] ) ) { 198 $query_params['status']['default'] = Meetup_Loader::get_public_post_statuses(); 199 } 200 201 return $query_params; 202 } 203 184 204 } 185 205 186 206 endif;