| Total Complexity | 140 |
| Total Lines | 1144 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WC_REST_Product_Reviews_Controller often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WC_REST_Product_Reviews_Controller, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class WC_REST_Product_Reviews_Controller extends WC_REST_Controller { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Endpoint namespace. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $namespace = 'wc/v3'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Route base. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $rest_base = 'products/reviews'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Register the routes for product reviews. |
||
| 37 | */ |
||
| 38 | public function register_routes() { |
||
| 39 | register_rest_route( |
||
| 40 | $this->namespace, '/' . $this->rest_base, array( |
||
| 41 | array( |
||
| 42 | 'methods' => WP_REST_Server::READABLE, |
||
| 43 | 'callback' => array( $this, 'get_items' ), |
||
| 44 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
| 45 | 'args' => $this->get_collection_params(), |
||
| 46 | ), |
||
| 47 | array( |
||
| 48 | 'methods' => WP_REST_Server::CREATABLE, |
||
| 49 | 'callback' => array( $this, 'create_item' ), |
||
| 50 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
| 51 | 'args' => array_merge( |
||
| 52 | $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( |
||
| 53 | 'product_id' => array( |
||
| 54 | 'required' => true, |
||
| 55 | 'description' => __( 'Unique identifier for the product.', 'woocommerce' ), |
||
| 56 | 'type' => 'integer', |
||
| 57 | ), |
||
| 58 | 'review' => array( |
||
| 59 | 'required' => true, |
||
| 60 | 'type' => 'string', |
||
| 61 | 'description' => __( 'Review content.', 'woocommerce' ), |
||
| 62 | ), |
||
| 63 | 'reviewer' => array( |
||
| 64 | 'required' => true, |
||
| 65 | 'type' => 'string', |
||
| 66 | 'description' => __( 'Name of the reviewer.', 'woocommerce' ), |
||
| 67 | ), |
||
| 68 | 'reviewer_email' => array( |
||
| 69 | 'required' => true, |
||
| 70 | 'type' => 'string', |
||
| 71 | 'description' => __( 'Email of the reviewer.', 'woocommerce' ), |
||
| 72 | ), |
||
| 73 | ) |
||
| 74 | ), |
||
| 75 | ), |
||
| 76 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 77 | ) |
||
| 78 | ); |
||
| 79 | |||
| 80 | register_rest_route( |
||
| 81 | $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
||
| 82 | 'args' => array( |
||
| 83 | 'id' => array( |
||
| 84 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
| 85 | 'type' => 'integer', |
||
| 86 | ), |
||
| 87 | ), |
||
| 88 | array( |
||
| 89 | 'methods' => WP_REST_Server::READABLE, |
||
| 90 | 'callback' => array( $this, 'get_item' ), |
||
| 91 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
| 92 | 'args' => array( |
||
| 93 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
| 94 | ), |
||
| 95 | ), |
||
| 96 | array( |
||
| 97 | 'methods' => WP_REST_Server::EDITABLE, |
||
| 98 | 'callback' => array( $this, 'update_item' ), |
||
| 99 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
| 100 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
| 101 | ), |
||
| 102 | array( |
||
| 103 | 'methods' => WP_REST_Server::DELETABLE, |
||
| 104 | 'callback' => array( $this, 'delete_item' ), |
||
| 105 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
| 106 | 'args' => array( |
||
| 107 | 'force' => array( |
||
| 108 | 'default' => false, |
||
| 109 | 'type' => 'boolean', |
||
| 110 | 'description' => __( 'Whether to bypass trash and force deletion.', 'woocommerce' ), |
||
| 111 | ), |
||
| 112 | ), |
||
| 113 | ), |
||
| 114 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | |||
| 118 | register_rest_route( |
||
| 119 | $this->namespace, '/' . $this->rest_base . '/batch', array( |
||
| 120 | array( |
||
| 121 | 'methods' => WP_REST_Server::EDITABLE, |
||
| 122 | 'callback' => array( $this, 'batch_items' ), |
||
| 123 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
| 124 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
| 125 | ), |
||
| 126 | 'schema' => array( $this, 'get_public_batch_schema' ), |
||
| 127 | ) |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Check whether a given request has permission to read webhook deliveries. |
||
| 133 | * |
||
| 134 | * @param WP_REST_Request $request Full details about the request. |
||
| 135 | * @return WP_Error|boolean |
||
| 136 | */ |
||
| 137 | public function get_items_permissions_check( $request ) { |
||
| 138 | if ( ! wc_rest_check_product_reviews_permissions( 'read' ) ) { |
||
| 139 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 140 | } |
||
| 141 | |||
| 142 | return true; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Check if a given request has access to read a product review. |
||
| 147 | * |
||
| 148 | * @param WP_REST_Request $request Full details about the request. |
||
| 149 | * @return WP_Error|boolean |
||
| 150 | */ |
||
| 151 | public function get_item_permissions_check( $request ) { |
||
| 152 | $id = (int) $request['id']; |
||
| 153 | $review = get_comment( $id ); |
||
| 154 | |||
| 155 | if ( $review && ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) { |
||
| 156 | return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 157 | } |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Check if a given request has access to create a new product review. |
||
| 164 | * |
||
| 165 | * @param WP_REST_Request $request Full details about the request. |
||
| 166 | * @return WP_Error|boolean |
||
| 167 | */ |
||
| 168 | public function create_item_permissions_check( $request ) { |
||
| 169 | if ( ! wc_rest_check_product_reviews_permissions( 'create' ) ) { |
||
| 170 | return new WP_Error( 'woocommerce_rest_cannot_create', __( 'Sorry, you are not allowed to create resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 171 | } |
||
| 172 | |||
| 173 | return true; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Check if a given request has access to update a product review. |
||
| 178 | * |
||
| 179 | * @param WP_REST_Request $request Full details about the request. |
||
| 180 | * @return WP_Error|boolean |
||
| 181 | */ |
||
| 182 | public function update_item_permissions_check( $request ) { |
||
| 183 | $id = (int) $request['id']; |
||
| 184 | $review = get_comment( $id ); |
||
| 185 | |||
| 186 | if ( $review && ! wc_rest_check_product_reviews_permissions( 'edit', $review->comment_ID ) ) { |
||
| 187 | return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 188 | } |
||
| 189 | |||
| 190 | return true; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Check if a given request has access to delete a product review. |
||
| 195 | * |
||
| 196 | * @param WP_REST_Request $request Full details about the request. |
||
| 197 | * @return WP_Error|boolean |
||
| 198 | */ |
||
| 199 | public function delete_item_permissions_check( $request ) { |
||
| 200 | $id = (int) $request['id']; |
||
| 201 | $review = get_comment( $id ); |
||
| 202 | |||
| 203 | if ( $review && ! wc_rest_check_product_reviews_permissions( 'delete', $review->comment_ID ) ) { |
||
| 204 | return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 205 | } |
||
| 206 | |||
| 207 | return true; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Check if a given request has access batch create, update and delete items. |
||
| 212 | * |
||
| 213 | * @param WP_REST_Request $request Full details about the request. |
||
| 214 | * @return boolean|WP_Error |
||
| 215 | */ |
||
| 216 | public function batch_items_permissions_check( $request ) { |
||
| 217 | if ( ! wc_rest_check_product_reviews_permissions( 'create' ) ) { |
||
| 218 | return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to batch manipulate this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
| 219 | } |
||
| 220 | |||
| 221 | return true; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get all reviews. |
||
| 226 | * |
||
| 227 | * @param WP_REST_Request $request Full details about the request. |
||
| 228 | * @return array|WP_Error |
||
| 229 | */ |
||
| 230 | public function get_items( $request ) { |
||
| 231 | // Retrieve the list of registered collection query parameters. |
||
| 232 | $registered = $this->get_collection_params(); |
||
| 233 | |||
| 234 | /* |
||
| 235 | * This array defines mappings between public API query parameters whose |
||
| 236 | * values are accepted as-passed, and their internal WP_Query parameter |
||
| 237 | * name equivalents (some are the same). Only values which are also |
||
| 238 | * present in $registered will be set. |
||
| 239 | */ |
||
| 240 | $parameter_mappings = array( |
||
| 241 | 'reviewer' => 'author__in', |
||
| 242 | 'reviewer_email' => 'author_email', |
||
| 243 | 'reviewer_exclude' => 'author__not_in', |
||
| 244 | 'exclude' => 'comment__not_in', |
||
| 245 | 'include' => 'comment__in', |
||
| 246 | 'offset' => 'offset', |
||
| 247 | 'order' => 'order', |
||
| 248 | 'per_page' => 'number', |
||
| 249 | 'product' => 'post__in', |
||
| 250 | 'search' => 'search', |
||
| 251 | 'status' => 'status', |
||
| 252 | ); |
||
| 253 | |||
| 254 | $prepared_args = array(); |
||
| 255 | |||
| 256 | /* |
||
| 257 | * For each known parameter which is both registered and present in the request, |
||
| 258 | * set the parameter's value on the query $prepared_args. |
||
| 259 | */ |
||
| 260 | foreach ( $parameter_mappings as $api_param => $wp_param ) { |
||
| 261 | if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { |
||
| 262 | $prepared_args[ $wp_param ] = $request[ $api_param ]; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | // Ensure certain parameter values default to empty strings. |
||
| 267 | foreach ( array( 'author_email', 'search' ) as $param ) { |
||
| 268 | if ( ! isset( $prepared_args[ $param ] ) ) { |
||
| 269 | $prepared_args[ $param ] = ''; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | if ( isset( $registered['orderby'] ) ) { |
||
| 274 | $prepared_args['orderby'] = $this->normalize_query_param( $request['orderby'] ); |
||
| 275 | } |
||
| 276 | |||
| 277 | if ( isset( $prepared_args['status'] ) ) { |
||
| 278 | $prepared_args['status'] = 'approved' === $prepared_args['status'] ? 'approve' : $prepared_args['status']; |
||
| 279 | } |
||
| 280 | |||
| 281 | $prepared_args['no_found_rows'] = false; |
||
| 282 | $prepared_args['date_query'] = array(); |
||
| 283 | |||
| 284 | // Set before into date query. Date query must be specified as an array of an array. |
||
| 285 | if ( isset( $registered['before'], $request['before'] ) ) { |
||
| 286 | $prepared_args['date_query'][0]['before'] = $request['before']; |
||
| 287 | } |
||
| 288 | |||
| 289 | // Set after into date query. Date query must be specified as an array of an array. |
||
| 290 | if ( isset( $registered['after'], $request['after'] ) ) { |
||
| 291 | $prepared_args['date_query'][0]['after'] = $request['after']; |
||
| 292 | } |
||
| 293 | |||
| 294 | if ( isset( $registered['page'] ) && empty( $request['offset'] ) ) { |
||
| 295 | $prepared_args['offset'] = $prepared_args['number'] * ( absint( $request['page'] ) - 1 ); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Filters arguments, before passing to WP_Comment_Query, when querying reviews via the REST API. |
||
| 300 | * |
||
| 301 | * @since 3.5.0 |
||
| 302 | * @link https://developer.wordpress.org/reference/classes/wp_comment_query/ |
||
| 303 | * @param array $prepared_args Array of arguments for WP_Comment_Query. |
||
| 304 | * @param WP_REST_Request $request The current request. |
||
| 305 | */ |
||
| 306 | $prepared_args = apply_filters( 'woocommerce_rest_product_review_query', $prepared_args, $request ); |
||
| 307 | |||
| 308 | // Make sure that returns only reviews. |
||
| 309 | $prepared_args['type'] = 'review'; |
||
| 310 | |||
| 311 | // Query reviews. |
||
| 312 | $query = new WP_Comment_Query(); |
||
| 313 | $query_result = $query->query( $prepared_args ); |
||
| 314 | $reviews = array(); |
||
| 315 | |||
| 316 | foreach ( $query_result as $review ) { |
||
| 317 | if ( ! wc_rest_check_product_reviews_permissions( 'read', $review->comment_ID ) ) { |
||
| 318 | continue; |
||
| 319 | } |
||
| 320 | |||
| 321 | $data = $this->prepare_item_for_response( $review, $request ); |
||
| 322 | $reviews[] = $this->prepare_response_for_collection( $data ); |
||
| 323 | } |
||
| 324 | |||
| 325 | $total_reviews = (int) $query->found_comments; |
||
| 326 | $max_pages = (int) $query->max_num_pages; |
||
| 327 | |||
| 328 | if ( $total_reviews < 1 ) { |
||
| 329 | // Out-of-bounds, run the query again without LIMIT for total count. |
||
| 330 | unset( $prepared_args['number'], $prepared_args['offset'] ); |
||
| 331 | |||
| 332 | $query = new WP_Comment_Query(); |
||
| 333 | $prepared_args['count'] = true; |
||
| 334 | |||
| 335 | $total_reviews = $query->query( $prepared_args ); |
||
| 336 | $max_pages = ceil( $total_reviews / $request['per_page'] ); |
||
| 337 | } |
||
| 338 | |||
| 339 | $response = rest_ensure_response( $reviews ); |
||
| 340 | $response->header( 'X-WP-Total', $total_reviews ); |
||
| 341 | $response->header( 'X-WP-TotalPages', $max_pages ); |
||
| 342 | |||
| 343 | $base = add_query_arg( $request->get_query_params(), rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ) ); |
||
| 344 | |||
| 345 | if ( $request['page'] > 1 ) { |
||
| 346 | $prev_page = $request['page'] - 1; |
||
| 347 | |||
| 348 | if ( $prev_page > $max_pages ) { |
||
| 349 | $prev_page = $max_pages; |
||
| 350 | } |
||
| 351 | |||
| 352 | $prev_link = add_query_arg( 'page', $prev_page, $base ); |
||
| 353 | $response->link_header( 'prev', $prev_link ); |
||
| 354 | } |
||
| 355 | |||
| 356 | if ( $max_pages > $request['page'] ) { |
||
| 357 | $next_page = $request['page'] + 1; |
||
| 358 | $next_link = add_query_arg( 'page', $next_page, $base ); |
||
| 359 | |||
| 360 | $response->link_header( 'next', $next_link ); |
||
| 361 | } |
||
| 362 | |||
| 363 | return $response; |
||
| 364 | } |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Create a single review. |
||
| 368 | * |
||
| 369 | * @param WP_REST_Request $request Full details about the request. |
||
| 370 | * @return WP_Error|WP_REST_Response |
||
| 371 | */ |
||
| 372 | public function create_item( $request ) { |
||
| 373 | if ( ! empty( $request['id'] ) ) { |
||
| 374 | return new WP_Error( 'woocommerce_rest_review_exists', __( 'Cannot create existing product review.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
| 375 | } |
||
| 376 | |||
| 377 | $product_id = (int) $request['product_id']; |
||
| 378 | |||
| 379 | if ( 'product' !== get_post_type( $product_id ) ) { |
||
| 380 | return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 381 | } |
||
| 382 | |||
| 383 | $prepared_review = $this->prepare_item_for_database( $request ); |
||
| 384 | if ( is_wp_error( $prepared_review ) ) { |
||
| 385 | return $prepared_review; |
||
| 386 | } |
||
| 387 | |||
| 388 | $prepared_review['comment_type'] = 'review'; |
||
| 389 | |||
| 390 | /* |
||
| 391 | * Do not allow a comment to be created with missing or empty comment_content. See wp_handle_comment_submission(). |
||
| 392 | */ |
||
| 393 | if ( empty( $prepared_review['comment_content'] ) ) { |
||
| 394 | return new WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
| 395 | } |
||
| 396 | |||
| 397 | // Setting remaining values before wp_insert_comment so we can use wp_allow_comment(). |
||
| 398 | if ( ! isset( $prepared_review['comment_date_gmt'] ) ) { |
||
| 399 | $prepared_review['comment_date_gmt'] = current_time( 'mysql', true ); |
||
| 400 | } |
||
| 401 | |||
| 402 | if ( ! empty( $_SERVER['REMOTE_ADDR'] ) && rest_is_ip_address( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ) { // WPCS: input var ok, sanitization ok. |
||
| 403 | $prepared_review['comment_author_IP'] = wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ); // WPCS: input var ok. |
||
| 404 | } else { |
||
| 405 | $prepared_review['comment_author_IP'] = '127.0.0.1'; |
||
| 406 | } |
||
| 407 | |||
| 408 | if ( ! empty( $request['author_user_agent'] ) ) { |
||
| 409 | $prepared_review['comment_agent'] = $request['author_user_agent']; |
||
| 410 | } elseif ( $request->get_header( 'user_agent' ) ) { |
||
| 411 | $prepared_review['comment_agent'] = $request->get_header( 'user_agent' ); |
||
| 412 | } else { |
||
| 413 | $prepared_review['comment_agent'] = ''; |
||
| 414 | } |
||
| 415 | |||
| 416 | $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_review ); |
||
| 417 | if ( is_wp_error( $check_comment_lengths ) ) { |
||
| 418 | $error_code = str_replace( array( 'comment_author', 'comment_content' ), array( 'reviewer', 'review_content' ), $check_comment_lengths->get_error_code() ); |
||
| 419 | return new WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
| 420 | } |
||
| 421 | |||
| 422 | $prepared_review['comment_parent'] = 0; |
||
| 423 | $prepared_review['comment_author_url'] = ''; |
||
| 424 | $prepared_review['comment_approved'] = wp_allow_comment( $prepared_review, true ); |
||
| 425 | |||
| 426 | if ( is_wp_error( $prepared_review['comment_approved'] ) ) { |
||
| 427 | $error_code = $prepared_review['comment_approved']->get_error_code(); |
||
| 428 | $error_message = $prepared_review['comment_approved']->get_error_message(); |
||
| 429 | |||
| 430 | if ( 'comment_duplicate' === $error_code ) { |
||
| 431 | return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 409 ) ); |
||
| 432 | } |
||
| 433 | |||
| 434 | if ( 'comment_flood' === $error_code ) { |
||
| 435 | return new WP_Error( 'woocommerce_rest_' . $error_code, $error_message, array( 'status' => 400 ) ); |
||
| 436 | } |
||
| 437 | |||
| 438 | return $prepared_review['comment_approved']; |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Filters a review before it is inserted via the REST API. |
||
| 443 | * |
||
| 444 | * Allows modification of the review right before it is inserted via wp_insert_comment(). |
||
| 445 | * Returning a WP_Error value from the filter will shortcircuit insertion and allow |
||
| 446 | * skipping further processing. |
||
| 447 | * |
||
| 448 | * @since 3.5.0 |
||
| 449 | * @param array|WP_Error $prepared_review The prepared review data for wp_insert_comment(). |
||
| 450 | * @param WP_REST_Request $request Request used to insert the review. |
||
| 451 | */ |
||
| 452 | $prepared_review = apply_filters( 'woocommerce_rest_pre_insert_product_review', $prepared_review, $request ); |
||
| 453 | if ( is_wp_error( $prepared_review ) ) { |
||
| 454 | return $prepared_review; |
||
| 455 | } |
||
| 456 | |||
| 457 | $review_id = wp_insert_comment( wp_filter_comment( wp_slash( (array) $prepared_review ) ) ); |
||
| 458 | |||
| 459 | if ( ! $review_id ) { |
||
| 460 | return new WP_Error( 'woocommerce_rest_review_failed_create', __( 'Creating product review failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
||
| 461 | } |
||
| 462 | |||
| 463 | if ( isset( $request['status'] ) ) { |
||
| 464 | $this->handle_status_param( $request['status'], $review_id ); |
||
| 465 | } |
||
| 466 | |||
| 467 | update_comment_meta( $review_id, 'rating', ! empty( $request['rating'] ) ? $request['rating'] : '0' ); |
||
| 468 | |||
| 469 | $review = get_comment( $review_id ); |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Fires after a comment is created or updated via the REST API. |
||
| 473 | * |
||
| 474 | * @param WP_Comment $review Inserted or updated comment object. |
||
| 475 | * @param WP_REST_Request $request Request object. |
||
| 476 | * @param bool $creating True when creating a comment, false when updating. |
||
| 477 | */ |
||
| 478 | do_action( 'woocommerce_rest_insert_product_review', $review, $request, true ); |
||
| 479 | |||
| 480 | $fields_update = $this->update_additional_fields_for_object( $review, $request ); |
||
| 481 | if ( is_wp_error( $fields_update ) ) { |
||
| 482 | return $fields_update; |
||
| 483 | } |
||
| 484 | |||
| 485 | $context = current_user_can( 'moderate_comments' ) ? 'edit' : 'view'; |
||
| 486 | $request->set_param( 'context', $context ); |
||
| 487 | |||
| 488 | $response = $this->prepare_item_for_response( $review, $request ); |
||
| 489 | $response = rest_ensure_response( $response ); |
||
| 490 | |||
| 491 | $response->set_status( 201 ); |
||
| 492 | $response->header( 'Location', rest_url( sprintf( '%s/%s/%d', $this->namespace, $this->rest_base, $review_id ) ) ); |
||
| 493 | |||
| 494 | return $response; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Get a single product review. |
||
| 499 | * |
||
| 500 | * @param WP_REST_Request $request Full details about the request. |
||
| 501 | * @return WP_Error|WP_REST_Response |
||
| 502 | */ |
||
| 503 | public function get_item( $request ) { |
||
| 504 | $review = $this->get_review( $request['id'] ); |
||
| 505 | if ( is_wp_error( $review ) ) { |
||
| 506 | return $review; |
||
| 507 | } |
||
| 508 | |||
| 509 | $data = $this->prepare_item_for_response( $review, $request ); |
||
| 510 | $response = rest_ensure_response( $data ); |
||
| 511 | |||
| 512 | return $response; |
||
| 513 | } |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Updates a review. |
||
| 517 | * |
||
| 518 | * @param WP_REST_Request $request Full details about the request. |
||
| 519 | * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. |
||
| 520 | */ |
||
| 521 | public function update_item( $request ) { |
||
| 522 | $review = $this->get_review( $request['id'] ); |
||
| 523 | if ( is_wp_error( $review ) ) { |
||
| 524 | return $review; |
||
| 525 | } |
||
| 526 | |||
| 527 | $id = (int) $review->comment_ID; |
||
| 528 | |||
| 529 | if ( isset( $request['type'] ) && 'review' !== get_comment_type( $id ) ) { |
||
| 530 | return new WP_Error( 'woocommerce_rest_review_invalid_type', __( 'Sorry, you are not allowed to change the comment type.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 531 | } |
||
| 532 | |||
| 533 | $prepared_args = $this->prepare_item_for_database( $request ); |
||
| 534 | if ( is_wp_error( $prepared_args ) ) { |
||
| 535 | return $prepared_args; |
||
| 536 | } |
||
| 537 | |||
| 538 | if ( ! empty( $prepared_args['comment_post_ID'] ) ) { |
||
| 539 | if ( 'product' !== get_post_type( (int) $prepared_args['comment_post_ID'] ) ) { |
||
| 540 | return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 541 | } |
||
| 542 | } |
||
| 543 | |||
| 544 | if ( empty( $prepared_args ) && isset( $request['status'] ) ) { |
||
| 545 | // Only the comment status is being changed. |
||
| 546 | $change = $this->handle_status_param( $request['status'], $id ); |
||
| 547 | |||
| 548 | if ( ! $change ) { |
||
| 549 | return new WP_Error( 'woocommerce_rest_review_failed_edit', __( 'Updating review status failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
||
| 550 | } |
||
| 551 | } elseif ( ! empty( $prepared_args ) ) { |
||
| 552 | if ( is_wp_error( $prepared_args ) ) { |
||
| 553 | return $prepared_args; |
||
| 554 | } |
||
| 555 | |||
| 556 | if ( isset( $prepared_args['comment_content'] ) && empty( $prepared_args['comment_content'] ) ) { |
||
| 557 | return new WP_Error( 'woocommerce_rest_review_content_invalid', __( 'Invalid review content.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
| 558 | } |
||
| 559 | |||
| 560 | $prepared_args['comment_ID'] = $id; |
||
| 561 | |||
| 562 | $check_comment_lengths = wp_check_comment_data_max_lengths( $prepared_args ); |
||
| 563 | if ( is_wp_error( $check_comment_lengths ) ) { |
||
| 564 | $error_code = str_replace( array( 'comment_author', 'comment_content' ), array( 'reviewer', 'review_content' ), $check_comment_lengths->get_error_code() ); |
||
| 565 | return new WP_Error( 'woocommerce_rest_' . $error_code, __( 'Product review field exceeds maximum length allowed.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
| 566 | } |
||
| 567 | |||
| 568 | $updated = wp_update_comment( wp_slash( (array) $prepared_args ) ); |
||
| 569 | |||
| 570 | if ( false === $updated ) { |
||
| 571 | return new WP_Error( 'woocommerce_rest_comment_failed_edit', __( 'Updating review failed.', 'woocommerce' ), array( 'status' => 500 ) ); |
||
| 572 | } |
||
| 573 | |||
| 574 | if ( isset( $request['status'] ) ) { |
||
| 575 | $this->handle_status_param( $request['status'], $id ); |
||
| 576 | } |
||
| 577 | } |
||
| 578 | |||
| 579 | if ( ! empty( $request['rating'] ) ) { |
||
| 580 | update_comment_meta( $id, 'rating', $request['rating'] ); |
||
| 581 | } |
||
| 582 | |||
| 583 | $review = get_comment( $id ); |
||
| 584 | |||
| 585 | /** This action is documented in includes/api/class-wc-rest-product-reviews-controller.php */ |
||
| 586 | do_action( 'woocommerce_rest_insert_product_review', $review, $request, false ); |
||
| 587 | |||
| 588 | $fields_update = $this->update_additional_fields_for_object( $review, $request ); |
||
| 589 | |||
| 590 | if ( is_wp_error( $fields_update ) ) { |
||
| 591 | return $fields_update; |
||
| 592 | } |
||
| 593 | |||
| 594 | $request->set_param( 'context', 'edit' ); |
||
| 595 | |||
| 596 | $response = $this->prepare_item_for_response( $review, $request ); |
||
| 597 | |||
| 598 | return rest_ensure_response( $response ); |
||
| 599 | } |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Deletes a review. |
||
| 603 | * |
||
| 604 | * @param WP_REST_Request $request Full details about the request. |
||
| 605 | * @return WP_Error|WP_REST_Response Response object on success, or error object on failure. |
||
| 606 | */ |
||
| 607 | public function delete_item( $request ) { |
||
| 608 | $review = $this->get_review( $request['id'] ); |
||
| 609 | if ( is_wp_error( $review ) ) { |
||
| 610 | return $review; |
||
| 611 | } |
||
| 612 | |||
| 613 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Filters whether a review can be trashed. |
||
| 617 | * |
||
| 618 | * Return false to disable trash support for the post. |
||
| 619 | * |
||
| 620 | * @since 3.5.0 |
||
| 621 | * @param bool $supports_trash Whether the post type support trashing. |
||
| 622 | * @param WP_Comment $review The review object being considered for trashing support. |
||
| 623 | */ |
||
| 624 | $supports_trash = apply_filters( 'woocommerce_rest_product_review_trashable', ( EMPTY_TRASH_DAYS > 0 ), $review ); |
||
| 625 | |||
| 626 | $request->set_param( 'context', 'edit' ); |
||
| 627 | |||
| 628 | if ( $force ) { |
||
| 629 | $previous = $this->prepare_item_for_response( $review, $request ); |
||
| 630 | $result = wp_delete_comment( $review->comment_ID, true ); |
||
| 631 | $response = new WP_REST_Response(); |
||
| 632 | $response->set_data( |
||
| 633 | array( |
||
| 634 | 'deleted' => true, |
||
| 635 | 'previous' => $previous->get_data(), |
||
| 636 | ) |
||
| 637 | ); |
||
| 638 | } else { |
||
| 639 | // If this type doesn't support trashing, error out. |
||
| 640 | if ( ! $supports_trash ) { |
||
| 641 | /* translators: %s: force=true */ |
||
| 642 | return new WP_Error( 'woocommerce_rest_trash_not_supported', sprintf( __( "The object does not support trashing. Set '%s' to delete.", 'woocommerce' ), 'force=true' ), array( 'status' => 501 ) ); |
||
| 643 | } |
||
| 644 | |||
| 645 | if ( 'trash' === $review->comment_approved ) { |
||
| 646 | return new WP_Error( 'woocommerce_rest_already_trashed', __( 'The object has already been trashed.', 'woocommerce' ), array( 'status' => 410 ) ); |
||
| 647 | } |
||
| 648 | |||
| 649 | $result = wp_trash_comment( $review->comment_ID ); |
||
| 650 | $review = get_comment( $review->comment_ID ); |
||
| 651 | $response = $this->prepare_item_for_response( $review, $request ); |
||
| 652 | } |
||
| 653 | |||
| 654 | if ( ! $result ) { |
||
| 655 | return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The object cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); |
||
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Fires after a review is deleted via the REST API. |
||
| 660 | * |
||
| 661 | * @param WP_Comment $review The deleted review data. |
||
| 662 | * @param WP_REST_Response $response The response returned from the API. |
||
| 663 | * @param WP_REST_Request $request The request sent to the API. |
||
| 664 | */ |
||
| 665 | do_action( 'woocommerce_rest_delete_review', $review, $response, $request ); |
||
| 666 | |||
| 667 | return $response; |
||
| 668 | } |
||
| 669 | |||
| 670 | /** |
||
| 671 | * Prepare a single product review output for response. |
||
| 672 | * |
||
| 673 | * @param WP_Comment $review Product review object. |
||
| 674 | * @param WP_REST_Request $request Request object. |
||
| 675 | * @return WP_REST_Response $response Response data. |
||
| 676 | */ |
||
| 677 | public function prepare_item_for_response( $review, $request ) { |
||
| 678 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
| 679 | $fields = $this->get_fields_for_response( $request ); |
||
| 680 | $data = array(); |
||
| 681 | |||
| 682 | if ( in_array( 'id', $fields, true ) ) { |
||
| 683 | $data['id'] = (int) $review->comment_ID; |
||
| 684 | } |
||
| 685 | if ( in_array( 'date_created', $fields, true ) ) { |
||
| 686 | $data['date_created'] = wc_rest_prepare_date_response( $review->comment_date ); |
||
| 687 | } |
||
| 688 | if ( in_array( 'date_created_gmt', $fields, true ) ) { |
||
| 689 | $data['date_created_gmt'] = wc_rest_prepare_date_response( $review->comment_date_gmt ); |
||
| 690 | } |
||
| 691 | if ( in_array( 'product_id', $fields, true ) ) { |
||
| 692 | $data['product_id'] = (int) $review->comment_post_ID; |
||
| 693 | } |
||
| 694 | if ( in_array( 'status', $fields, true ) ) { |
||
| 695 | $data['status'] = $this->prepare_status_response( (string) $review->comment_approved ); |
||
| 696 | } |
||
| 697 | if ( in_array( 'reviewer', $fields, true ) ) { |
||
| 698 | $data['reviewer'] = $review->comment_author; |
||
| 699 | } |
||
| 700 | if ( in_array( 'reviewer_email', $fields, true ) ) { |
||
| 701 | $data['reviewer_email'] = $review->comment_author_email; |
||
| 702 | } |
||
| 703 | if ( in_array( 'review', $fields, true ) ) { |
||
| 704 | $data['review'] = 'view' === $context ? wpautop( $review->comment_content ) : $review->comment_content; |
||
| 705 | } |
||
| 706 | if ( in_array( 'rating', $fields, true ) ) { |
||
| 707 | $data['rating'] = (int) get_comment_meta( $review->comment_ID, 'rating', true ); |
||
| 708 | } |
||
| 709 | if ( in_array( 'verified', $fields, true ) ) { |
||
| 710 | $data['verified'] = wc_review_is_from_verified_owner( $review->comment_ID ); |
||
| 711 | } |
||
| 712 | if ( in_array( 'reviewer_avatar_urls', $fields, true ) ) { |
||
| 713 | $data['reviewer_avatar_urls'] = rest_get_avatar_urls( $review->comment_author_email ); |
||
| 714 | } |
||
| 715 | |||
| 716 | $data = $this->add_additional_fields_to_object( $data, $request ); |
||
| 717 | $data = $this->filter_response_by_context( $data, $context ); |
||
| 718 | |||
| 719 | // Wrap the data in a response object. |
||
| 720 | $response = rest_ensure_response( $data ); |
||
| 721 | |||
| 722 | $response->add_links( $this->prepare_links( $review ) ); |
||
| 723 | |||
| 724 | /** |
||
| 725 | * Filter product reviews object returned from the REST API. |
||
| 726 | * |
||
| 727 | * @param WP_REST_Response $response The response object. |
||
| 728 | * @param WP_Comment $review Product review object used to create response. |
||
| 729 | * @param WP_REST_Request $request Request object. |
||
| 730 | */ |
||
| 731 | return apply_filters( 'woocommerce_rest_prepare_product_review', $response, $review, $request ); |
||
| 732 | } |
||
| 733 | |||
| 734 | /** |
||
| 735 | * Prepare a single product review to be inserted into the database. |
||
| 736 | * |
||
| 737 | * @param WP_REST_Request $request Request object. |
||
| 738 | * @return array|WP_Error $prepared_review |
||
| 739 | */ |
||
| 740 | protected function prepare_item_for_database( $request ) { |
||
| 741 | if ( isset( $request['id'] ) ) { |
||
| 742 | $prepared_review['comment_ID'] = (int) $request['id']; |
||
| 743 | } |
||
| 744 | |||
| 745 | if ( isset( $request['review'] ) ) { |
||
| 746 | $prepared_review['comment_content'] = $request['review']; |
||
| 747 | } |
||
| 748 | |||
| 749 | if ( isset( $request['product_id'] ) ) { |
||
| 750 | $prepared_review['comment_post_ID'] = (int) $request['product_id']; |
||
| 751 | } |
||
| 752 | |||
| 753 | if ( isset( $request['reviewer'] ) ) { |
||
| 754 | $prepared_review['comment_author'] = $request['reviewer']; |
||
| 755 | } |
||
| 756 | |||
| 757 | if ( isset( $request['reviewer_email'] ) ) { |
||
| 758 | $prepared_review['comment_author_email'] = $request['reviewer_email']; |
||
| 759 | } |
||
| 760 | |||
| 761 | if ( ! empty( $request['date_created'] ) ) { |
||
| 762 | $date_data = rest_get_date_with_gmt( $request['date_created'] ); |
||
| 763 | |||
| 764 | if ( ! empty( $date_data ) ) { |
||
| 765 | list( $prepared_review['comment_date'], $prepared_review['comment_date_gmt'] ) = $date_data; |
||
| 766 | } |
||
| 767 | } elseif ( ! empty( $request['date_created_gmt'] ) ) { |
||
| 768 | $date_data = rest_get_date_with_gmt( $request['date_created_gmt'], true ); |
||
| 769 | |||
| 770 | if ( ! empty( $date_data ) ) { |
||
| 771 | list( $prepared_review['comment_date'], $prepared_review['comment_date_gmt'] ) = $date_data; |
||
| 772 | } |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Filters a review after it is prepared for the database. |
||
| 777 | * |
||
| 778 | * Allows modification of the review right after it is prepared for the database. |
||
| 779 | * |
||
| 780 | * @since 3.5.0 |
||
| 781 | * @param array $prepared_review The prepared review data for `wp_insert_comment`. |
||
| 782 | * @param WP_REST_Request $request The current request. |
||
| 783 | */ |
||
| 784 | return apply_filters( 'woocommerce_rest_preprocess_product_review', $prepared_review, $request ); |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Prepare links for the request. |
||
| 789 | * |
||
| 790 | * @param WP_Comment $review Product review object. |
||
| 791 | * @return array Links for the given product review. |
||
| 792 | */ |
||
| 793 | protected function prepare_links( $review ) { |
||
| 794 | $links = array( |
||
| 795 | 'self' => array( |
||
| 796 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $review->comment_ID ) ), |
||
| 797 | ), |
||
| 798 | 'collection' => array( |
||
| 799 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), |
||
| 800 | ), |
||
| 801 | ); |
||
| 802 | |||
| 803 | if ( 0 !== (int) $review->comment_post_ID ) { |
||
| 804 | $links['up'] = array( |
||
| 805 | 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $review->comment_post_ID ) ), |
||
| 806 | ); |
||
| 807 | } |
||
| 808 | |||
| 809 | if ( 0 !== (int) $review->user_id ) { |
||
| 810 | $links['reviewer'] = array( |
||
| 811 | 'href' => rest_url( 'wp/v2/users/' . $review->user_id ), |
||
| 812 | 'embeddable' => true, |
||
| 813 | ); |
||
| 814 | } |
||
| 815 | |||
| 816 | return $links; |
||
| 817 | } |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Get the Product Review's schema, conforming to JSON Schema. |
||
| 821 | * |
||
| 822 | * @return array |
||
| 823 | */ |
||
| 824 | public function get_item_schema() { |
||
| 825 | $schema = array( |
||
| 826 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
| 827 | 'title' => 'product_review', |
||
| 828 | 'type' => 'object', |
||
| 829 | 'properties' => array( |
||
| 830 | 'id' => array( |
||
| 831 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
| 832 | 'type' => 'integer', |
||
| 833 | 'context' => array( 'view', 'edit' ), |
||
| 834 | 'readonly' => true, |
||
| 835 | ), |
||
| 836 | 'date_created' => array( |
||
| 837 | 'description' => __( "The date the review was created, in the site's timezone.", 'woocommerce' ), |
||
| 838 | 'type' => 'date-time', |
||
| 839 | 'context' => array( 'view', 'edit' ), |
||
| 840 | 'readonly' => true, |
||
| 841 | ), |
||
| 842 | 'date_created_gmt' => array( |
||
| 843 | 'description' => __( 'The date the review was created, as GMT.', 'woocommerce' ), |
||
| 844 | 'type' => 'date-time', |
||
| 845 | 'context' => array( 'view', 'edit' ), |
||
| 846 | 'readonly' => true, |
||
| 847 | ), |
||
| 848 | 'product_id' => array( |
||
| 849 | 'description' => __( 'Unique identifier for the product that the review belongs to.', 'woocommerce' ), |
||
| 850 | 'type' => 'integer', |
||
| 851 | 'context' => array( 'view', 'edit' ), |
||
| 852 | ), |
||
| 853 | 'status' => array( |
||
| 854 | 'description' => __( 'Status of the review.', 'woocommerce' ), |
||
| 855 | 'type' => 'string', |
||
| 856 | 'default' => 'approved', |
||
| 857 | 'enum' => array( 'approved', 'hold', 'spam', 'unspam', 'trash', 'untrash' ), |
||
| 858 | 'context' => array( 'view', 'edit' ), |
||
| 859 | ), |
||
| 860 | 'reviewer' => array( |
||
| 861 | 'description' => __( 'Reviewer name.', 'woocommerce' ), |
||
| 862 | 'type' => 'string', |
||
| 863 | 'context' => array( 'view', 'edit' ), |
||
| 864 | ), |
||
| 865 | 'reviewer_email' => array( |
||
| 866 | 'description' => __( 'Reviewer email.', 'woocommerce' ), |
||
| 867 | 'type' => 'string', |
||
| 868 | 'format' => 'email', |
||
| 869 | 'context' => array( 'view', 'edit' ), |
||
| 870 | ), |
||
| 871 | 'review' => array( |
||
| 872 | 'description' => __( 'The content of the review.', 'woocommerce' ), |
||
| 873 | 'type' => 'string', |
||
| 874 | 'context' => array( 'view', 'edit' ), |
||
| 875 | 'arg_options' => array( |
||
| 876 | 'sanitize_callback' => 'wp_filter_post_kses', |
||
| 877 | ), |
||
| 878 | ), |
||
| 879 | 'rating' => array( |
||
| 880 | 'description' => __( 'Review rating (0 to 5).', 'woocommerce' ), |
||
| 881 | 'type' => 'integer', |
||
| 882 | 'context' => array( 'view', 'edit' ), |
||
| 883 | ), |
||
| 884 | 'verified' => array( |
||
| 885 | 'description' => __( 'Shows if the reviewer bought the product or not.', 'woocommerce' ), |
||
| 886 | 'type' => 'boolean', |
||
| 887 | 'context' => array( 'view', 'edit' ), |
||
| 888 | 'readonly' => true, |
||
| 889 | ), |
||
| 890 | ), |
||
| 891 | ); |
||
| 892 | |||
| 893 | if ( get_option( 'show_avatars' ) ) { |
||
| 894 | $avatar_properties = array(); |
||
| 895 | $avatar_sizes = rest_get_avatar_sizes(); |
||
| 896 | |||
| 897 | foreach ( $avatar_sizes as $size ) { |
||
| 898 | $avatar_properties[ $size ] = array( |
||
| 899 | /* translators: %d: avatar image size in pixels */ |
||
| 900 | 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.', 'woocommerce' ), $size ), |
||
| 901 | 'type' => 'string', |
||
| 902 | 'format' => 'uri', |
||
| 903 | 'context' => array( 'embed', 'view', 'edit' ), |
||
| 904 | ); |
||
| 905 | } |
||
| 906 | $schema['properties']['reviewer_avatar_urls'] = array( |
||
| 907 | 'description' => __( 'Avatar URLs for the object reviewer.', 'woocommerce' ), |
||
| 908 | 'type' => 'object', |
||
| 909 | 'context' => array( 'view', 'edit' ), |
||
| 910 | 'readonly' => true, |
||
| 911 | 'properties' => $avatar_properties, |
||
| 912 | ); |
||
| 913 | } |
||
| 914 | |||
| 915 | return $this->add_additional_fields_schema( $schema ); |
||
| 916 | } |
||
| 917 | |||
| 918 | /** |
||
| 919 | * Get the query params for collections. |
||
| 920 | * |
||
| 921 | * @return array |
||
| 922 | */ |
||
| 923 | public function get_collection_params() { |
||
| 924 | $params = parent::get_collection_params(); |
||
| 925 | |||
| 926 | $params['context']['default'] = 'view'; |
||
| 927 | |||
| 928 | $params['after'] = array( |
||
| 929 | 'description' => __( 'Limit response to resources published after a given ISO8601 compliant date.', 'woocommerce' ), |
||
| 930 | 'type' => 'string', |
||
| 931 | 'format' => 'date-time', |
||
| 932 | ); |
||
| 933 | $params['before'] = array( |
||
| 934 | 'description' => __( 'Limit response to reviews published before a given ISO8601 compliant date.', 'woocommerce' ), |
||
| 935 | 'type' => 'string', |
||
| 936 | 'format' => 'date-time', |
||
| 937 | ); |
||
| 938 | $params['exclude'] = array( |
||
| 939 | 'description' => __( 'Ensure result set excludes specific IDs.', 'woocommerce' ), |
||
| 940 | 'type' => 'array', |
||
| 941 | 'items' => array( |
||
| 942 | 'type' => 'integer', |
||
| 943 | ), |
||
| 944 | 'default' => array(), |
||
| 945 | ); |
||
| 946 | $params['include'] = array( |
||
| 947 | 'description' => __( 'Limit result set to specific IDs.', 'woocommerce' ), |
||
| 948 | 'type' => 'array', |
||
| 949 | 'items' => array( |
||
| 950 | 'type' => 'integer', |
||
| 951 | ), |
||
| 952 | 'default' => array(), |
||
| 953 | ); |
||
| 954 | $params['offset'] = array( |
||
| 955 | 'description' => __( 'Offset the result set by a specific number of items.', 'woocommerce' ), |
||
| 956 | 'type' => 'integer', |
||
| 957 | ); |
||
| 958 | $params['order'] = array( |
||
| 959 | 'description' => __( 'Order sort attribute ascending or descending.', 'woocommerce' ), |
||
| 960 | 'type' => 'string', |
||
| 961 | 'default' => 'desc', |
||
| 962 | 'enum' => array( |
||
| 963 | 'asc', |
||
| 964 | 'desc', |
||
| 965 | ), |
||
| 966 | ); |
||
| 967 | $params['orderby'] = array( |
||
| 968 | 'description' => __( 'Sort collection by object attribute.', 'woocommerce' ), |
||
| 969 | 'type' => 'string', |
||
| 970 | 'default' => 'date_gmt', |
||
| 971 | 'enum' => array( |
||
| 972 | 'date', |
||
| 973 | 'date_gmt', |
||
| 974 | 'id', |
||
| 975 | 'include', |
||
| 976 | 'product', |
||
| 977 | ), |
||
| 978 | ); |
||
| 979 | $params['reviewer'] = array( |
||
| 980 | 'description' => __( 'Limit result set to reviews assigned to specific user IDs.', 'woocommerce' ), |
||
| 981 | 'type' => 'array', |
||
| 982 | 'items' => array( |
||
| 983 | 'type' => 'integer', |
||
| 984 | ), |
||
| 985 | ); |
||
| 986 | $params['reviewer_exclude'] = array( |
||
| 987 | 'description' => __( 'Ensure result set excludes reviews assigned to specific user IDs.', 'woocommerce' ), |
||
| 988 | 'type' => 'array', |
||
| 989 | 'items' => array( |
||
| 990 | 'type' => 'integer', |
||
| 991 | ), |
||
| 992 | ); |
||
| 993 | $params['reviewer_email'] = array( |
||
| 994 | 'default' => null, |
||
| 995 | 'description' => __( 'Limit result set to that from a specific author email.', 'woocommerce' ), |
||
| 996 | 'format' => 'email', |
||
| 997 | 'type' => 'string', |
||
| 998 | ); |
||
| 999 | $params['product'] = array( |
||
| 1000 | 'default' => array(), |
||
| 1001 | 'description' => __( 'Limit result set to reviews assigned to specific product IDs.', 'woocommerce' ), |
||
| 1002 | 'type' => 'array', |
||
| 1003 | 'items' => array( |
||
| 1004 | 'type' => 'integer', |
||
| 1005 | ), |
||
| 1006 | ); |
||
| 1007 | $params['status'] = array( |
||
| 1008 | 'default' => 'approved', |
||
| 1009 | 'description' => __( 'Limit result set to reviews assigned a specific status.', 'woocommerce' ), |
||
| 1010 | 'sanitize_callback' => 'sanitize_key', |
||
| 1011 | 'type' => 'string', |
||
| 1012 | 'enum' => array( |
||
| 1013 | 'all', |
||
| 1014 | 'hold', |
||
| 1015 | 'approved', |
||
| 1016 | 'spam', |
||
| 1017 | 'trash', |
||
| 1018 | ), |
||
| 1019 | ); |
||
| 1020 | |||
| 1021 | /** |
||
| 1022 | * Filter collection parameters for the reviews controller. |
||
| 1023 | * |
||
| 1024 | * This filter registers the collection parameter, but does not map the |
||
| 1025 | * collection parameter to an internal WP_Comment_Query parameter. Use the |
||
| 1026 | * `wc_rest_review_query` filter to set WP_Comment_Query parameters. |
||
| 1027 | * |
||
| 1028 | * @since 3.5.0 |
||
| 1029 | * @param array $params JSON Schema-formatted collection parameters. |
||
| 1030 | */ |
||
| 1031 | return apply_filters( 'woocommerce_rest_product_review_collection_params', $params ); |
||
| 1032 | } |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * Get the reivew, if the ID is valid. |
||
| 1036 | * |
||
| 1037 | * @since 3.5.0 |
||
| 1038 | * @param int $id Supplied ID. |
||
| 1039 | * @return WP_Comment|WP_Error Comment object if ID is valid, WP_Error otherwise. |
||
| 1040 | */ |
||
| 1041 | protected function get_review( $id ) { |
||
| 1042 | $id = (int) $id; |
||
| 1043 | $error = new WP_Error( 'woocommerce_rest_review_invalid_id', __( 'Invalid review ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 1044 | |||
| 1045 | if ( 0 >= $id ) { |
||
| 1046 | return $error; |
||
| 1047 | } |
||
| 1048 | |||
| 1049 | $review = get_comment( $id ); |
||
| 1050 | if ( empty( $review ) ) { |
||
| 1051 | return $error; |
||
| 1052 | } |
||
| 1053 | |||
| 1054 | if ( ! empty( $review->comment_post_ID ) ) { |
||
| 1055 | $post = get_post( (int) $review->comment_post_ID ); |
||
| 1056 | |||
| 1057 | if ( 'product' !== get_post_type( (int) $review->comment_post_ID ) ) { |
||
| 1058 | return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) ); |
||
| 1059 | } |
||
| 1060 | } |
||
| 1061 | |||
| 1062 | return $review; |
||
| 1063 | } |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * Prepends internal property prefix to query parameters to match our response fields. |
||
| 1067 | * |
||
| 1068 | * @since 3.5.0 |
||
| 1069 | * @param string $query_param Query parameter. |
||
| 1070 | * @return string |
||
| 1071 | */ |
||
| 1072 | protected function normalize_query_param( $query_param ) { |
||
| 1073 | $prefix = 'comment_'; |
||
| 1074 | |||
| 1075 | switch ( $query_param ) { |
||
| 1076 | case 'id': |
||
| 1077 | $normalized = $prefix . 'ID'; |
||
| 1078 | break; |
||
| 1079 | case 'product': |
||
| 1080 | $normalized = $prefix . 'post_ID'; |
||
| 1081 | break; |
||
| 1082 | case 'include': |
||
| 1083 | $normalized = 'comment__in'; |
||
| 1084 | break; |
||
| 1085 | default: |
||
| 1086 | $normalized = $prefix . $query_param; |
||
| 1087 | break; |
||
| 1088 | } |
||
| 1089 | |||
| 1090 | return $normalized; |
||
| 1091 | } |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Checks comment_approved to set comment status for single comment output. |
||
| 1095 | * |
||
| 1096 | * @since 3.5.0 |
||
| 1097 | * @param string|int $comment_approved comment status. |
||
| 1098 | * @return string Comment status. |
||
| 1099 | */ |
||
| 1100 | protected function prepare_status_response( $comment_approved ) { |
||
| 1101 | switch ( $comment_approved ) { |
||
| 1102 | case 'hold': |
||
| 1103 | case '0': |
||
| 1104 | $status = 'hold'; |
||
| 1105 | break; |
||
| 1106 | case 'approve': |
||
| 1107 | case '1': |
||
| 1108 | $status = 'approved'; |
||
| 1109 | break; |
||
| 1110 | case 'spam': |
||
| 1111 | case 'trash': |
||
| 1112 | default: |
||
| 1113 | $status = $comment_approved; |
||
| 1114 | break; |
||
| 1115 | } |
||
| 1116 | |||
| 1117 | return $status; |
||
| 1118 | } |
||
| 1119 | |||
| 1120 | /** |
||
| 1121 | * Sets the comment_status of a given review object when creating or updating a review. |
||
| 1122 | * |
||
| 1123 | * @since 3.5.0 |
||
| 1124 | * @param string|int $new_status New review status. |
||
| 1125 | * @param int $id Review ID. |
||
| 1126 | * @return bool Whether the status was changed. |
||
| 1127 | */ |
||
| 1128 | protected function handle_status_param( $new_status, $id ) { |
||
| 1129 | $old_status = wp_get_comment_status( $id ); |
||
| 1130 | |||
| 1131 | if ( $new_status === $old_status ) { |
||
| 1132 | return false; |
||
| 1133 | } |
||
| 1134 | |||
| 1135 | switch ( $new_status ) { |
||
| 1136 | case 'approved': |
||
| 1137 | case 'approve': |
||
| 1138 | case '1': |
||
| 1139 | $changed = wp_set_comment_status( $id, 'approve' ); |
||
| 1140 | break; |
||
| 1141 | case 'hold': |
||
| 1142 | case '0': |
||
| 1143 | $changed = wp_set_comment_status( $id, 'hold' ); |
||
| 1144 | break; |
||
| 1145 | case 'spam': |
||
| 1146 | $changed = wp_spam_comment( $id ); |
||
| 1147 | break; |
||
| 1148 | case 'unspam': |
||
| 1149 | $changed = wp_unspam_comment( $id ); |
||
| 1150 | break; |
||
| 1151 | case 'trash': |
||
| 1152 | $changed = wp_trash_comment( $id ); |
||
| 1153 | break; |
||
| 1154 | case 'untrash': |
||
| 1155 | $changed = wp_untrash_comment( $id ); |
||
| 1156 | break; |
||
| 1157 | default: |
||
| 1158 | $changed = false; |
||
| 1159 | break; |
||
| 1160 | } |
||
| 1161 | |||
| 1162 | return $changed; |
||
| 1163 | } |
||
| 1165 |