wp/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
   242 			$prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] );
   242 			$prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] );
   243 		}
   243 		}
   244 
   244 
   245 		$prepared_args['no_found_rows'] = false;
   245 		$prepared_args['no_found_rows'] = false;
   246 
   246 
       
   247 		$prepared_args['update_comment_post_cache'] = true;
       
   248 
   247 		$prepared_args['date_query'] = array();
   249 		$prepared_args['date_query'] = array();
   248 
   250 
   249 		// Set before into date query. Date query must be specified as an array of an array.
   251 		// Set before into date query. Date query must be specified as an array of an array.
   250 		if ( isset( $registered['before'], $request['before'] ) ) {
   252 		if ( isset( $registered['before'], $request['before'] ) ) {
   251 			$prepared_args['date_query'][0]['before'] = $request['before'];
   253 			$prepared_args['date_query'][0]['before'] = $request['before'];
   270 		 * @param array           $prepared_args Array of arguments for WP_Comment_Query.
   272 		 * @param array           $prepared_args Array of arguments for WP_Comment_Query.
   271 		 * @param WP_REST_Request $request       The REST API request.
   273 		 * @param WP_REST_Request $request       The REST API request.
   272 		 */
   274 		 */
   273 		$prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request );
   275 		$prepared_args = apply_filters( 'rest_comment_query', $prepared_args, $request );
   274 
   276 
   275 		$query        = new WP_Comment_Query;
   277 		$query        = new WP_Comment_Query();
   276 		$query_result = $query->query( $prepared_args );
   278 		$query_result = $query->query( $prepared_args );
   277 
   279 
   278 		$comments = array();
   280 		$comments = array();
   279 
   281 
   280 		foreach ( $query_result as $comment ) {
   282 		foreach ( $query_result as $comment ) {
   291 
   293 
   292 		if ( $total_comments < 1 ) {
   294 		if ( $total_comments < 1 ) {
   293 			// Out-of-bounds, run the query again without LIMIT for total count.
   295 			// Out-of-bounds, run the query again without LIMIT for total count.
   294 			unset( $prepared_args['number'], $prepared_args['offset'] );
   296 			unset( $prepared_args['number'], $prepared_args['offset'] );
   295 
   297 
   296 			$query                  = new WP_Comment_Query;
   298 			$query                    = new WP_Comment_Query();
   297 			$prepared_args['count'] = true;
   299 			$prepared_args['count']   = true;
       
   300 			$prepared_args['orderby'] = 'none';
   298 
   301 
   299 			$total_comments = $query->query( $prepared_args );
   302 			$total_comments = $query->query( $prepared_args );
   300 			$max_pages      = ceil( $total_comments / $request['per_page'] );
   303 			$max_pages      = (int) ceil( $total_comments / $request['per_page'] );
   301 		}
   304 		}
   302 
   305 
   303 		$response = rest_ensure_response( $comments );
   306 		$response = rest_ensure_response( $comments );
   304 		$response->header( 'X-WP-Total', $total_comments );
   307 		$response->header( 'X-WP-Total', $total_comments );
   305 		$response->header( 'X-WP-TotalPages', $max_pages );
   308 		$response->header( 'X-WP-TotalPages', $max_pages );
  1035 	 * @return WP_REST_Response Response object.
  1038 	 * @return WP_REST_Response Response object.
  1036 	 */
  1039 	 */
  1037 	public function prepare_item_for_response( $item, $request ) {
  1040 	public function prepare_item_for_response( $item, $request ) {
  1038 		// Restores the more descriptive, specific name for use within this method.
  1041 		// Restores the more descriptive, specific name for use within this method.
  1039 		$comment = $item;
  1042 		$comment = $item;
  1040 		$fields  = $this->get_fields_for_response( $request );
  1043 
  1041 		$data    = array();
  1044 		$fields = $this->get_fields_for_response( $request );
       
  1045 		$data   = array();
  1042 
  1046 
  1043 		if ( in_array( 'id', $fields, true ) ) {
  1047 		if ( in_array( 'id', $fields, true ) ) {
  1044 			$data['id'] = (int) $comment->comment_ID;
  1048 			$data['id'] = (int) $comment->comment_ID;
  1045 		}
  1049 		}
  1046 
  1050 
  1085 		}
  1089 		}
  1086 
  1090 
  1087 		if ( in_array( 'content', $fields, true ) ) {
  1091 		if ( in_array( 'content', $fields, true ) ) {
  1088 			$data['content'] = array(
  1092 			$data['content'] = array(
  1089 				/** This filter is documented in wp-includes/comment-template.php */
  1093 				/** This filter is documented in wp-includes/comment-template.php */
  1090 				'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment ),
  1094 				'rendered' => apply_filters( 'comment_text', $comment->comment_content, $comment, array() ),
  1091 				'raw'      => $comment->comment_content,
  1095 				'raw'      => $comment->comment_content,
  1092 			);
  1096 			);
  1093 		}
  1097 		}
  1094 
  1098 
  1095 		if ( in_array( 'link', $fields, true ) ) {
  1099 		if ( in_array( 'link', $fields, true ) ) {
  1117 		$data    = $this->filter_response_by_context( $data, $context );
  1121 		$data    = $this->filter_response_by_context( $data, $context );
  1118 
  1122 
  1119 		// Wrap the data in a response object.
  1123 		// Wrap the data in a response object.
  1120 		$response = rest_ensure_response( $data );
  1124 		$response = rest_ensure_response( $data );
  1121 
  1125 
  1122 		$response->add_links( $this->prepare_links( $comment ) );
  1126 		if ( rest_is_field_included( '_links', $fields ) || rest_is_field_included( '_embedded', $fields ) ) {
       
  1127 			$response->add_links( $this->prepare_links( $comment ) );
       
  1128 		}
  1123 
  1129 
  1124 		/**
  1130 		/**
  1125 		 * Filters a comment returned from the REST API.
  1131 		 * Filters a comment returned from the REST API.
  1126 		 *
  1132 		 *
  1127 		 * Allows modification of the comment right before it is returned.
  1133 		 * Allows modification of the comment right before it is returned.
  1181 		}
  1187 		}
  1182 
  1188 
  1183 		// Only grab one comment to verify the comment has children.
  1189 		// Only grab one comment to verify the comment has children.
  1184 		$comment_children = $comment->get_children(
  1190 		$comment_children = $comment->get_children(
  1185 			array(
  1191 			array(
  1186 				'number' => 1,
  1192 				'count'   => true,
  1187 				'count'  => true,
  1193 				'orderby' => 'none',
  1188 			)
  1194 			)
  1189 		);
  1195 		);
  1190 
  1196 
  1191 		if ( ! empty( $comment_children ) ) {
  1197 		if ( ! empty( $comment_children ) ) {
  1192 			$args = array(
  1198 			$args = array(
  1755 			return false;
  1761 			return false;
  1756 		}
  1762 		}
  1757 
  1763 
  1758 		$posts_controller = $post_type->get_rest_controller();
  1764 		$posts_controller = $post_type->get_rest_controller();
  1759 
  1765 
  1760 		// Ensure the posts controller is specifically a WP_REST_Posts_Controller instance
  1766 		/*
  1761 		// before using methods specific to that controller.
  1767 		 * Ensure the posts controller is specifically a WP_REST_Posts_Controller instance
       
  1768 		 * before using methods specific to that controller.
       
  1769 		 */
  1762 		if ( ! $posts_controller instanceof WP_REST_Posts_Controller ) {
  1770 		if ( ! $posts_controller instanceof WP_REST_Posts_Controller ) {
  1763 			$posts_controller = new WP_REST_Posts_Controller( $post->post_type );
  1771 			$posts_controller = new WP_REST_Posts_Controller( $post->post_type );
  1764 		}
  1772 		}
  1765 
  1773 
  1766 		$has_password_filter = false;
  1774 		$has_password_filter = false;
  1881 	protected function check_is_comment_content_allowed( $prepared_comment ) {
  1889 	protected function check_is_comment_content_allowed( $prepared_comment ) {
  1882 		$check = wp_parse_args(
  1890 		$check = wp_parse_args(
  1883 			$prepared_comment,
  1891 			$prepared_comment,
  1884 			array(
  1892 			array(
  1885 				'comment_post_ID'      => 0,
  1893 				'comment_post_ID'      => 0,
  1886 				'comment_parent'       => 0,
       
  1887 				'user_ID'              => 0,
       
  1888 				'comment_author'       => null,
  1894 				'comment_author'       => null,
  1889 				'comment_author_email' => null,
  1895 				'comment_author_email' => null,
  1890 				'comment_author_url'   => null,
  1896 				'comment_author_url'   => null,
       
  1897 				'comment_parent'       => 0,
       
  1898 				'user_id'              => 0,
  1891 			)
  1899 			)
  1892 		);
  1900 		);
  1893 
  1901 
  1894 		/** This filter is documented in wp-includes/comment.php */
  1902 		/** This filter is documented in wp-includes/comment.php */
  1895 		$allow_empty = apply_filters( 'allow_empty_comment', false, $check );
  1903 		$allow_empty = apply_filters( 'allow_empty_comment', false, $check );