Ticket #41821: 41821.diff
File 41821.diff, 33.7 KB (added by , 6 years ago) |
---|
-
src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
43 43 */ 44 44 public function register_routes() { 45 45 46 register_rest_route( 47 $this->namespace, '/' . $this->rest_base, array( 48 array( 49 'methods' => WP_REST_Server::READABLE, 50 'callback' => array( $this, 'get_items' ), 51 'permission_callback' => array( $this, 'get_items_permissions_check' ), 52 'args' => $this->get_collection_params(), 53 ), 54 array( 55 'methods' => WP_REST_Server::CREATABLE, 56 'callback' => array( $this, 'create_item' ), 57 'permission_callback' => array( $this, 'create_item_permissions_check' ), 58 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 59 ), 60 'schema' => array( $this, 'get_public_item_schema' ), 61 ) 62 ); 46 register_rest_route( $this->namespace, '/' . $this->rest_base, array( 47 array( 48 'methods' => WP_REST_Server::READABLE, 49 'callback' => array( $this, 'get_items' ), 50 'permission_callback' => array( $this, 'get_items_permissions_check' ), 51 'args' => $this->get_collection_params(), 52 ), 53 array( 54 'methods' => WP_REST_Server::CREATABLE, 55 'callback' => array( $this, 'create_item' ), 56 'permission_callback' => array( $this, 'create_item_permissions_check' ), 57 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), 58 ), 59 'schema' => array( $this, 'get_public_item_schema' ), 60 ) ); 63 61 64 register_rest_route( 65 $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 66 'args' => array( 67 'id' => array( 68 'description' => __( 'Unique identifier for the object.' ), 69 'type' => 'integer', 70 ), 62 register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( 63 'args' => array( 64 'id' => array( 65 'description' => __( 'Unique identifier for the object.' ), 66 'type' => 'integer', 71 67 ), 72 array(73 'methods' => WP_REST_Server::READABLE,74 'callback' => array( $this, 'get_item' ),75 'permission_callback' => array( $this, 'get_item_permissions_check' ),76 'args' => array(77 'context' => $this->get_context_param( array( 'default' => 'view' ) ),78 'password' => array(79 'description' => __( 'The password for the parent post of the comment (if the post is password protected).' ),80 'type' => 'string',81 ),68 ), 69 array( 70 'methods' => WP_REST_Server::READABLE, 71 'callback' => array( $this, 'get_item' ), 72 'permission_callback' => array( $this, 'get_item_permissions_check' ), 73 'args' => array( 74 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 75 'password' => array( 76 'description' => __( 'The password for the parent post of the comment (if the post is password protected).' ), 77 'type' => 'string', 82 78 ), 83 79 ), 84 array( 85 'methods' => WP_REST_Server::EDITABLE, 86 'callback' => array( $this, 'update_item' ), 87 'permission_callback' => array( $this, 'update_item_permissions_check' ), 88 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 89 ), 90 array( 91 'methods' => WP_REST_Server::DELETABLE, 92 'callback' => array( $this, 'delete_item' ), 93 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 94 'args' => array( 95 'force' => array( 96 'type' => 'boolean', 97 'default' => false, 98 'description' => __( 'Whether to bypass trash and force deletion.' ), 99 ), 100 'password' => array( 101 'description' => __( 'The password for the parent post of the comment (if the post is password protected).' ), 102 'type' => 'string', 103 ), 80 ), 81 array( 82 'methods' => WP_REST_Server::EDITABLE, 83 'callback' => array( $this, 'update_item' ), 84 'permission_callback' => array( $this, 'update_item_permissions_check' ), 85 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), 86 ), 87 array( 88 'methods' => WP_REST_Server::DELETABLE, 89 'callback' => array( $this, 'delete_item' ), 90 'permission_callback' => array( $this, 'delete_item_permissions_check' ), 91 'args' => array( 92 'force' => array( 93 'type' => 'boolean', 94 'default' => false, 95 'description' => __( 'Whether to bypass trash and force deletion.' ), 104 96 ), 97 'password' => array( 98 'description' => __( 'The password for the parent post of the comment (if the post is password protected).' ), 99 'type' => 'string', 100 ), 105 101 ), 106 'schema' => array( $this, 'get_public_item_schema'),107 )108 ) ;102 ), 103 'schema' => array( $this, 'get_public_item_schema' ), 104 ) ); 109 105 } 110 106 111 107 /** … … 219 215 $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] ); 220 216 } 221 217 218 /* 219 * If hierarchical is true, get just top level comments. Keep parent from response if set. 220 * Children will be populated in $this->prepare_item_for_response(). 221 */ 222 if ( isset( $request[ 'hierarchical' ] ) && $request[ 'hierarchical' ] ) { 223 $prepared_args[ 'parent' ] = $request['parent'] ?: 0; 224 } 225 222 226 $prepared_args['no_found_rows'] = false; 223 227 224 228 $prepared_args['date_query'] = array(); … … 248 252 * @param WP_REST_Request $request The current request. 249 253 */ 250 254 $prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request ); 251 252 $query 255 256 $query = new WP_Comment_Query; 253 257 $query_result = $query->query( $prepared_args ); 254 258 255 259 $comments = array(); 256 260 257 261 foreach ( $query_result as $comment ) { … … 259 263 continue; 260 264 } 261 265 262 $data 266 $data = $this->prepare_item_for_response( $comment, $request ); 263 267 $comments[] = $this->prepare_response_for_collection( $data ); 264 268 } 265 269 … … 270 274 // Out-of-bounds, run the query again without LIMIT for total count. 271 275 unset( $prepared_args['number'], $prepared_args['offset'] ); 272 276 273 $query 277 $query = new WP_Comment_Query; 274 278 $prepared_args['count'] = true; 275 279 276 280 $total_comments = $query->query( $prepared_args ); 277 $max_pages 281 $max_pages = ceil( $total_comments / $request['per_page'] ); 278 282 } 279 283 280 284 $response = rest_ensure_response( $comments ); … … 318 322 return $error; 319 323 } 320 324 321 $id 325 $id = (int) $id; 322 326 $comment = get_comment( $id ); 323 327 if ( empty( $comment ) ) { 324 328 return $error; … … 379 383 return $comment; 380 384 } 381 385 382 $data 386 $data = $this->prepare_item_for_response( $comment, $request ); 383 387 $response = rest_ensure_response( $data ); 384 388 385 389 return $response; … … 419 423 420 424 // Limit who can set comment `author`, `author_ip` or `status` to anything other than the default. 421 425 if ( isset( $request['author'] ) && get_current_user_id() !== $request['author'] && ! current_user_can( 'moderate_comments' ) ) { 422 return new WP_Error( 423 'rest_comment_invalid_author', 426 return new WP_Error( 'rest_comment_invalid_author', 424 427 /* translators: %s: request parameter */ 425 428 sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author' ), 426 429 array( 'status' => rest_authorization_required_code() ) … … 429 432 430 433 if ( isset( $request['author_ip'] ) && ! current_user_can( 'moderate_comments' ) ) { 431 434 if ( empty( $_SERVER['REMOTE_ADDR'] ) || $request['author_ip'] !== $_SERVER['REMOTE_ADDR'] ) { 432 return new WP_Error( 433 'rest_comment_invalid_author_ip', 435 return new WP_Error( 'rest_comment_invalid_author_ip', 434 436 /* translators: %s: request parameter */ 435 437 sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'author_ip' ), 436 438 array( 'status' => rest_authorization_required_code() ) … … 439 441 } 440 442 441 443 if ( isset( $request['status'] ) && ! current_user_can( 'moderate_comments' ) ) { 442 return new WP_Error( 443 'rest_comment_invalid_status', 444 return new WP_Error( 'rest_comment_invalid_status', 444 445 /* translators: %s: request parameter */ 445 446 sprintf( __( "Sorry, you are not allowed to edit '%s' for comments." ), 'status' ), 446 447 array( 'status' => rest_authorization_required_code() ) … … 522 523 if ( is_user_logged_in() && $missing_author ) { 523 524 $user = wp_get_current_user(); 524 525 525 $prepared_comment['user_id'] 526 $prepared_comment['comment_author'] 526 $prepared_comment['user_id'] = $user->ID; 527 $prepared_comment['comment_author'] = $user->display_name; 527 528 $prepared_comment['comment_author_email'] = $user->user_email; 528 $prepared_comment['comment_author_url'] 529 $prepared_comment['comment_author_url'] = $user->user_url; 529 530 } 530 531 531 532 // Honor the discussion setting that requires a name and email address of the comment author. … … 578 579 * skipping further processing. 579 580 * 580 581 * @since 4.7.0 581 * @since 4.8.0 `$prepared_comment`can now be a WP_Error to shortcircuit insertion.582 * @since 4.8.0 $prepared_comment can now be a WP_Error to shortcircuit insertion. 582 583 * 583 584 * @param array|WP_Error $prepared_comment The prepared comment data for wp_insert_comment(). 584 585 * @param WP_REST_Request $request Request used to insert the comment. … … 638 639 $response->set_status( 201 ); 639 640 $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment_id ) ) ); 640 641 642 641 643 return $response; 642 644 } 643 645 … … 810 812 811 813 if ( $force ) { 812 814 $previous = $this->prepare_item_for_response( $comment, $request ); 813 $result 815 $result = wp_delete_comment( $comment->comment_ID, true ); 814 816 $response = new WP_REST_Response(); 815 $response->set_data( 816 array( 817 'deleted' => true, 818 'previous' => $previous->get_data(), 819 ) 820 ); 817 $response->set_data( array( 'deleted' => true, 'previous' => $previous->get_data() ) ); 821 818 } else { 822 819 // If this type doesn't support trashing, error out. 823 820 if ( ! $supports_trash ) { … … 829 826 return new WP_Error( 'rest_already_trashed', __( 'The comment has already been trashed.' ), array( 'status' => 410 ) ); 830 827 } 831 828 832 $result 833 $comment 829 $result = wp_trash_comment( $comment->comment_ID ); 830 $comment = get_comment( $comment->comment_ID ); 834 831 $response = $this->prepare_item_for_response( $comment, $request ); 835 832 } 836 833 … … 862 859 * @return WP_REST_Response Response object. 863 860 */ 864 861 public function prepare_item_for_response( $comment, $request ) { 865 866 $fields = $this->get_fields_for_response( $request ); 867 $data = array(); 868 869 if ( in_array( 'id', $fields, true ) ) { 870 $data['id'] = (int) $comment->comment_ID; 871 } 872 873 if ( in_array( 'post', $fields, true ) ) { 874 $data['post'] = (int) $comment->comment_post_ID; 875 } 876 877 if ( in_array( 'parent', $fields, true ) ) { 878 $data['parent'] = (int) $comment->comment_parent; 879 } 880 881 if ( in_array( 'author', $fields, true ) ) { 882 $data['author'] = (int) $comment->user_id; 883 } 884 885 if ( in_array( 'author_name', $fields, true ) ) { 886 $data['author_name'] = $comment->comment_author; 887 } 888 889 if ( in_array( 'author_email', $fields, true ) ) { 890 $data['author_email'] = $comment->comment_author_email; 891 } 892 893 if ( in_array( 'author_url', $fields, true ) ) { 894 $data['author_url'] = $comment->comment_author_url; 895 } 896 897 if ( in_array( 'author_ip', $fields, true ) ) { 898 $data['author_ip'] = $comment->comment_author_IP; 899 } 900 901 if ( in_array( 'author_user_agent', $fields, true ) ) { 902 $data['author_user_agent'] = $comment->comment_agent; 903 } 904 905 if ( in_array( 'date', $fields, true ) ) { 906 $data['date'] = mysql_to_rfc3339( $comment->comment_date ); 907 } 908 909 if ( in_array( 'date_gmt', $fields, true ) ) { 910 $data['date_gmt'] = mysql_to_rfc3339( $comment->comment_date_gmt ); 911 } 912 913 if ( in_array( 'content', $fields, true ) ) { 914 $data['content'] = array( 862 $data = array( 863 'id' => (int) $comment->comment_ID, 864 'post' => (int) $comment->comment_post_ID, 865 'parent' => (int) $comment->comment_parent, 866 'author' => (int) $comment->user_id, 867 'author_name' => $comment->comment_author, 868 'author_email' => $comment->comment_author_email, 869 'author_url' => $comment->comment_author_url, 870 'author_ip' => $comment->comment_author_IP, 871 'author_user_agent' => $comment->comment_agent, 872 'date' => mysql_to_rfc3339( $comment->comment_date ), 873 'date_gmt' => mysql_to_rfc3339( $comment->comment_date_gmt ), 874 'content' => array( 915 875 /** This filter is documented in wp-includes/comment-template.php */ 916 876 'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ), 917 877 'raw' => $comment->comment_content, 918 ); 878 ), 879 'link' => get_comment_link( $comment ), 880 'status' => $this->prepare_status_response( $comment->comment_approved ), 881 'type' => get_comment_type( $comment->comment_ID ), 882 ); 883 884 $children = $comment->get_children(); 885 // Loop through children and prepare them too 886 if (!empty($children)){ 887 foreach ($children as $child){ 888 $child_response = $this->prepare_item_for_response($child, $request); 889 $data['children'][] = $child_response->data; 890 } 919 891 } 892 893 $schema = $this->get_item_schema(); 920 894 921 if ( in_array( 'link', $fields, true ) ) { 922 $data['link'] = get_comment_link( $comment ); 923 } 924 925 if ( in_array( 'status', $fields, true ) ) { 926 $data['status'] = $this->prepare_status_response( $comment->comment_approved ); 927 } 928 929 if ( in_array( 'type', $fields, true ) ) { 930 $data['type'] = get_comment_type( $comment->comment_ID ); 931 } 932 933 if ( in_array( 'author_avatar_urls', $fields, true ) ) { 895 if ( ! empty( $schema['properties']['author_avatar_urls'] ) ) { 934 896 $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email ); 935 897 } 936 898 937 if ( in_array( 'meta', $fields, true) ) {899 if ( ! empty( $schema['properties']['meta'] ) ) { 938 900 $data['meta'] = $this->meta->get_value( $comment->comment_ID, $request ); 939 901 } 940 902 … … 971 933 */ 972 934 protected function prepare_links( $comment ) { 973 935 $links = array( 974 'self' 936 'self' => array( 975 937 'href' => rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $comment->comment_ID ) ), 976 938 ), 977 939 'collection' => array( … … 990 952 $post = get_post( $comment->comment_post_ID ); 991 953 992 954 if ( ! empty( $post->ID ) ) { 993 $obj 955 $obj = get_post_type_object( $post->post_type ); 994 956 $base = ! empty( $obj->rest_base ) ? $obj->rest_base : $obj->name; 995 957 996 958 $links['up'] = array( … … 1009 971 } 1010 972 1011 973 // Only grab one comment to verify the comment has children. 1012 $comment_children = $comment->get_children( 1013 array( 1014 'number' => 1, 1015 'count' => true, 1016 ) 1017 ); 974 $comment_children = $comment->get_children( array( 975 'number' => 1, 976 'count' => true 977 ) ); 1018 978 1019 979 if ( ! empty( $comment_children ) ) { 1020 980 $args = array( 1021 'parent' => $comment->comment_ID ,981 'parent' => $comment->comment_ID 1022 982 ); 1023 983 1024 984 $rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) ); … … 1127 1087 $user = new WP_User( $request['author'] ); 1128 1088 1129 1089 if ( $user->exists() ) { 1130 $prepared_comment['user_id'] 1131 $prepared_comment['comment_author'] 1090 $prepared_comment['user_id'] = $user->ID; 1091 $prepared_comment['comment_author'] = $user->display_name; 1132 1092 $prepared_comment['comment_author_email'] = $user->user_email; 1133 $prepared_comment['comment_author_url'] 1093 $prepared_comment['comment_author_url'] = $user->user_url; 1134 1094 } else { 1135 1095 return new WP_Error( 'rest_comment_author_invalid', __( 'Invalid comment author ID.' ), array( 'status' => 400 ) ); 1136 1096 } … … 1198 1158 */ 1199 1159 public function get_item_schema() { 1200 1160 $schema = array( 1201 '$schema' => 'http://json-schema.org/draft-04/schema#',1202 'title' => 'comment',1203 'type' => 'object',1204 'properties' => array(1205 'id' 1206 'description' => __( 'Unique identifier for the object.' ),1207 'type' => 'integer',1208 'context' => array( 'view', 'edit', 'embed' ),1209 'readonly' => true,1161 '$schema' => 'http://json-schema.org/draft-04/schema#', 1162 'title' => 'comment', 1163 'type' => 'object', 1164 'properties' => array( 1165 'id' => array( 1166 'description' => __( 'Unique identifier for the object.' ), 1167 'type' => 'integer', 1168 'context' => array( 'view', 'edit', 'embed' ), 1169 'readonly' => true, 1210 1170 ), 1211 'author' 1212 'description' => __( 'The ID of the user object, if author was a user.' ),1213 'type' => 'integer',1214 'context' => array( 'view', 'edit', 'embed' ),1171 'author' => array( 1172 'description' => __( 'The ID of the user object, if author was a user.' ), 1173 'type' => 'integer', 1174 'context' => array( 'view', 'edit', 'embed' ), 1215 1175 ), 1216 'author_email' 1217 'description' => __( 'Email address for the object author.' ),1218 'type' => 'string',1219 'format' => 'email',1220 'context' => array( 'edit' ),1221 'arg_options' => array(1176 'author_email' => array( 1177 'description' => __( 'Email address for the object author.' ), 1178 'type' => 'string', 1179 'format' => 'email', 1180 'context' => array( 'edit' ), 1181 'arg_options' => array( 1222 1182 'sanitize_callback' => array( $this, 'check_comment_author_email' ), 1223 1183 'validate_callback' => null, // skip built-in validation of 'email'. 1224 1184 ), 1225 1185 ), 1226 'author_ip' 1227 'description' => __( 'IP address for the object author.' ),1228 'type' => 'string',1229 'format' => 'ip',1230 'context' => array( 'edit' ),1186 'author_ip' => array( 1187 'description' => __( 'IP address for the object author.' ), 1188 'type' => 'string', 1189 'format' => 'ip', 1190 'context' => array( 'edit' ), 1231 1191 ), 1232 'author_name' 1233 'description' => __( 'Display name for the object author.' ),1234 'type' => 'string',1235 'context' => array( 'view', 'edit', 'embed' ),1236 'arg_options' => array(1192 'author_name' => array( 1193 'description' => __( 'Display name for the object author.' ), 1194 'type' => 'string', 1195 'context' => array( 'view', 'edit', 'embed' ), 1196 'arg_options' => array( 1237 1197 'sanitize_callback' => 'sanitize_text_field', 1238 1198 ), 1239 1199 ), 1240 'author_url' 1241 'description' => __( 'URL for the object author.' ),1242 'type' => 'string',1243 'format' => 'uri',1244 'context' => array( 'view', 'edit', 'embed' ),1200 'author_url' => array( 1201 'description' => __( 'URL for the object author.' ), 1202 'type' => 'string', 1203 'format' => 'uri', 1204 'context' => array( 'view', 'edit', 'embed' ), 1245 1205 ), 1246 'author_user_agent' => array(1247 'description' => __( 'User agent for the object author.' ),1248 'type' => 'string',1249 'context' => array( 'edit' ),1250 'arg_options' => array(1206 'author_user_agent' => array( 1207 'description' => __( 'User agent for the object author.' ), 1208 'type' => 'string', 1209 'context' => array( 'edit' ), 1210 'arg_options' => array( 1251 1211 'sanitize_callback' => 'sanitize_text_field', 1252 1212 ), 1213 ), 1214 'children' => array( 1215 'description' => __( "Comment's children, if hierarchical comments are requested." ), 1216 'type' => 'array', 1217 'context' => array( 'view', 'embed' ), 1253 1218 ), 1254 'content' 1255 'description' => __( 'The content for the object.' ),1256 'type' => 'object',1257 'context' => array( 'view', 'edit', 'embed' ),1258 'arg_options' => array(1219 'content' => array( 1220 'description' => __( 'The content for the object.' ), 1221 'type' => 'object', 1222 'context' => array( 'view', 'edit', 'embed' ), 1223 'arg_options' => array( 1259 1224 'sanitize_callback' => null, // Note: sanitization implemented in self::prepare_item_for_database() 1260 1225 'validate_callback' => null, // Note: validation implemented in self::prepare_item_for_database() 1261 1226 ), 1262 'properties' => array(1263 'raw' => array(1264 'description' => __( 'Content for the object, as it exists in the database.' ),1265 'type' => 'string',1266 'context' => array( 'edit' ),1227 'properties' => array( 1228 'raw' => array( 1229 'description' => __( 'Content for the object, as it exists in the database.' ), 1230 'type' => 'string', 1231 'context' => array( 'edit' ), 1267 1232 ), 1268 'rendered' => array(1269 'description' => __( 'HTML content for the object, transformed for display.' ),1270 'type' => 'string',1271 'context' => array( 'view', 'edit', 'embed' ),1272 'readonly' => true,1233 'rendered' => array( 1234 'description' => __( 'HTML content for the object, transformed for display.' ), 1235 'type' => 'string', 1236 'context' => array( 'view', 'edit', 'embed' ), 1237 'readonly' => true, 1273 1238 ), 1274 1239 ), 1275 1240 ), 1276 'date' 1277 'description' => __( "The date the object was published, in the site's timezone." ),1278 'type' => 'string',1279 'format' => 'date-time',1280 'context' => array( 'view', 'edit', 'embed' ),1241 'date' => array( 1242 'description' => __( "The date the object was published, in the site's timezone." ), 1243 'type' => 'string', 1244 'format' => 'date-time', 1245 'context' => array( 'view', 'edit', 'embed' ), 1281 1246 ), 1282 'date_gmt' 1283 'description' => __( 'The date the object was published, as GMT.' ),1284 'type' => 'string',1285 'format' => 'date-time',1286 'context' => array( 'view', 'edit' ),1247 'date_gmt' => array( 1248 'description' => __( 'The date the object was published, as GMT.' ), 1249 'type' => 'string', 1250 'format' => 'date-time', 1251 'context' => array( 'view', 'edit' ), 1287 1252 ), 1288 'link' 1289 'description' => __( 'URL to the object.' ),1290 'type' => 'string',1291 'format' => 'uri',1292 'context' => array( 'view', 'edit', 'embed' ),1293 'readonly' => true,1253 'link' => array( 1254 'description' => __( 'URL to the object.' ), 1255 'type' => 'string', 1256 'format' => 'uri', 1257 'context' => array( 'view', 'edit', 'embed' ), 1258 'readonly' => true, 1294 1259 ), 1295 'parent' 1296 'description' => __( 'The ID for the parent of the object.' ),1297 'type' => 'integer',1298 'context' => array( 'view', 'edit', 'embed' ),1299 'default' => 0,1260 'parent' => array( 1261 'description' => __( 'The ID for the parent of the object.' ), 1262 'type' => 'integer', 1263 'context' => array( 'view', 'edit', 'embed' ), 1264 'default' => 0, 1300 1265 ), 1301 'post' 1302 'description' => __( 'The ID of the associated post object.' ),1303 'type' => 'integer',1304 'context' => array( 'view', 'edit' ),1305 'default' => 0,1266 'post' => array( 1267 'description' => __( 'The ID of the associated post object.' ), 1268 'type' => 'integer', 1269 'context' => array( 'view', 'edit' ), 1270 'default' => 0, 1306 1271 ), 1307 'status' 1308 'description' => __( 'State of the object.' ),1309 'type' => 'string',1310 'context' => array( 'view', 'edit' ),1311 'arg_options' => array(1272 'status' => array( 1273 'description' => __( 'State of the object.' ), 1274 'type' => 'string', 1275 'context' => array( 'view', 'edit' ), 1276 'arg_options' => array( 1312 1277 'sanitize_callback' => 'sanitize_key', 1313 1278 ), 1314 1279 ), 1315 'type' 1316 'description' => __( 'Type of Comment for the object.' ),1317 'type' => 'string',1318 'context' => array( 'view', 'edit', 'embed' ),1319 'readonly' => true,1280 'type' => array( 1281 'description' => __( 'Type of Comment for the object.' ), 1282 'type' => 'string', 1283 'context' => array( 'view', 'edit', 'embed' ), 1284 'readonly' => true, 1320 1285 ), 1321 1286 ), 1322 1287 ); … … 1336 1301 } 1337 1302 1338 1303 $schema['properties']['author_avatar_urls'] = array( 1339 'description' => __( 'Avatar URLs for the object author.' ),1340 'type' => 'object',1341 'context' => array( 'view', 'edit', 'embed' ),1342 'readonly' => true,1343 'properties' => $avatar_properties,1304 'description' => __( 'Avatar URLs for the object author.' ), 1305 'type' => 'object', 1306 'context' => array( 'view', 'edit', 'embed' ), 1307 'readonly' => true, 1308 'properties' => $avatar_properties, 1344 1309 ); 1345 1310 } 1346 1311 … … 1362 1327 $query_params['context']['default'] = 'view'; 1363 1328 1364 1329 $query_params['after'] = array( 1365 'description' => __( 'Limit response to comments published after a given ISO8601 compliant date.' ),1366 'type' => 'string',1367 'format' => 'date-time',1330 'description' => __( 'Limit response to comments published after a given ISO8601 compliant date.' ), 1331 'type' => 'string', 1332 'format' => 'date-time', 1368 1333 ); 1369 1334 1370 1335 $query_params['author'] = array( 1371 'description' => __( 'Limit result set to comments assigned to specific user IDs. Requires authorization.' ),1372 'type' => 'array',1373 'items' => array(1374 'type' => 'integer',1336 'description' => __( 'Limit result set to comments assigned to specific user IDs. Requires authorization.' ), 1337 'type' => 'array', 1338 'items' => array( 1339 'type' => 'integer', 1375 1340 ), 1376 1341 ); 1377 1342 1378 1343 $query_params['author_exclude'] = array( 1379 'description' => __( 'Ensure result set excludes comments assigned to specific user IDs. Requires authorization.' ),1380 'type' => 'array',1381 'items' => array(1382 'type' => 'integer',1344 'description' => __( 'Ensure result set excludes comments assigned to specific user IDs. Requires authorization.' ), 1345 'type' => 'array', 1346 'items' => array( 1347 'type' => 'integer', 1383 1348 ), 1384 1349 ); 1385 1350 1386 1351 $query_params['author_email'] = array( 1387 'default' => null,1388 'description' => __( 'Limit result set to that from a specific author email. Requires authorization.' ),1389 'format' => 'email',1390 'type' => 'string',1352 'default' => null, 1353 'description' => __( 'Limit result set to that from a specific author email. Requires authorization.' ), 1354 'format' => 'email', 1355 'type' => 'string', 1391 1356 ); 1392 1357 1393 1358 $query_params['before'] = array( 1394 'description' => __( 'Limit response to comments published before a given ISO8601 compliant date.' ),1395 'type' => 'string',1396 'format' => 'date-time',1359 'description' => __( 'Limit response to comments published before a given ISO8601 compliant date.' ), 1360 'type' => 'string', 1361 'format' => 'date-time', 1397 1362 ); 1398 1363 1399 1364 $query_params['exclude'] = array( 1400 'description' => __( 'Ensure result set excludes specific IDs.' ),1401 'type' => 'array',1402 'items' => array(1403 'type' => 'integer',1365 'description' => __( 'Ensure result set excludes specific IDs.' ), 1366 'type' => 'array', 1367 'items' => array( 1368 'type' => 'integer', 1404 1369 ), 1405 'default' => array(),1370 'default' => array(), 1406 1371 ); 1372 1373 $query_params['hierarchical'] = array( 1374 'description' => __( 'Whether to include comment descendants in the results. Returns a flat array of found comments plus their children.' ), 1375 'type' => 'boolean', 1376 'default' => false, 1377 ); 1407 1378 1379 1408 1380 $query_params['include'] = array( 1409 'description' => __( 'Limit result set to specific IDs.' ),1410 'type' => 'array',1411 'items' => array(1412 'type' => 'integer',1381 'description' => __( 'Limit result set to specific IDs.' ), 1382 'type' => 'array', 1383 'items' => array( 1384 'type' => 'integer', 1413 1385 ), 1414 'default' => array(),1386 'default' => array(), 1415 1387 ); 1416 1388 1417 1389 $query_params['offset'] = array( 1418 'description' => __( 'Offset the result set by a specific number of items.' ),1419 'type' => 'integer',1390 'description' => __( 'Offset the result set by a specific number of items.' ), 1391 'type' => 'integer', 1420 1392 ); 1421 1393 1422 $query_params['order'] = array(1423 'description' => __( 'Order sort attribute ascending or descending.' ),1424 'type' => 'string',1425 'default' => 'desc',1426 'enum' => array(1394 $query_params['order'] = array( 1395 'description' => __( 'Order sort attribute ascending or descending.' ), 1396 'type' => 'string', 1397 'default' => 'desc', 1398 'enum' => array( 1427 1399 'asc', 1428 1400 'desc', 1429 1401 ), 1430 1402 ); 1431 1403 1432 $query_params['orderby'] = array(1433 'description' => __( 'Sort collection by object attribute.' ),1434 'type' => 'string',1435 'default' => 'date_gmt',1436 'enum' => array(1404 $query_params['orderby'] = array( 1405 'description' => __( 'Sort collection by object attribute.' ), 1406 'type' => 'string', 1407 'default' => 'date_gmt', 1408 'enum' => array( 1437 1409 'date', 1438 1410 'date_gmt', 1439 1411 'id', … … 1445 1417 ); 1446 1418 1447 1419 $query_params['parent'] = array( 1448 'default' => array(),1449 'description' => __( 'Limit result set to comments of specific parent IDs.' ),1450 'type' => 'array',1451 'items' => array(1452 'type' => 'integer',1420 'default' => array(), 1421 'description' => __( 'Limit result set to comments of specific parent IDs.' ), 1422 'type' => 'array', 1423 'items' => array( 1424 'type' => 'integer', 1453 1425 ), 1454 1426 ); 1455 1427 1456 1428 $query_params['parent_exclude'] = array( 1457 'default' => array(),1458 'description' => __( 'Ensure result set excludes specific parent IDs.' ),1459 'type' => 'array',1460 'items' => array(1461 'type' => 'integer',1429 'default' => array(), 1430 'description' => __( 'Ensure result set excludes specific parent IDs.' ), 1431 'type' => 'array', 1432 'items' => array( 1433 'type' => 'integer', 1462 1434 ), 1463 1435 ); 1464 1436 1465 $query_params['post'] = array(1466 'default' => array(),1467 'description' => __( 'Limit result set to comments assigned to specific post IDs.' ),1468 'type' => 'array',1469 'items' => array(1470 'type' => 'integer',1437 $query_params['post'] = array( 1438 'default' => array(), 1439 'description' => __( 'Limit result set to comments assigned to specific post IDs.' ), 1440 'type' => 'array', 1441 'items' => array( 1442 'type' => 'integer', 1471 1443 ), 1472 1444 ); 1473 1445 … … 1523 1495 } 1524 1496 1525 1497 switch ( $new_status ) { 1526 case 'approved' :1498 case 'approved' : 1527 1499 case 'approve': 1528 1500 case '1': 1529 1501 $changed = wp_set_comment_status( $comment_id, 'approve' ); … … 1532 1504 case '0': 1533 1505 $changed = wp_set_comment_status( $comment_id, 'hold' ); 1534 1506 break; 1535 case 'spam' :1507 case 'spam' : 1536 1508 $changed = wp_spam_comment( $comment_id ); 1537 1509 break; 1538 case 'unspam' :1510 case 'unspam' : 1539 1511 $changed = wp_unspam_comment( $comment_id ); 1540 1512 break; 1541 case 'trash' :1513 case 'trash' : 1542 1514 $changed = wp_trash_comment( $comment_id ); 1543 1515 break; 1544 case 'untrash' :1516 case 'untrash' : 1545 1517 $changed = wp_untrash_comment( $comment_id ); 1546 1518 break; 1547 default :1519 default : 1548 1520 $changed = false; 1549 1521 break; 1550 1522 } … … 1565 1537 */ 1566 1538 protected function check_read_post_permission( $post, $request ) { 1567 1539 $posts_controller = new WP_REST_Posts_Controller( $post->post_type ); 1568 $post_type 1540 $post_type = get_post_type_object( $post->post_type ); 1569 1541 1570 1542 $has_password_filter = false; 1571 1543 1572 1544 // Only check password if a specific post was queried for or a single comment 1573 $requested_post = ! empty( $request['post'] ) && ( !is_array( $request['post'] ) || 1 === count( $request['post'] ) );1545 $requested_post = ! empty( $request['post'] ) && ( !is_array( $request['post'] ) || 1 === count( $request['post'] ) ); 1574 1546 $requested_comment = ! empty( $request['id'] ); 1575 1547 if ( ( $requested_post || $requested_comment ) && $posts_controller->can_access_password_content( $post, $request ) ) { 1576 1548 add_filter( 'post_password_required', '__return_false' );