/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Routes/V1/ProductReviews.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Automattic\WooCommerce\StoreApi\Routes\V1;

use 
WP_Comment_Query;
use 
Automattic\WooCommerce\StoreApi\Utilities\Pagination;

/**
 * ProductReviews class.
 */
class ProductReviews extends AbstractRoute {
    
/**
     * The route identifier.
     *
     * @var string
     */
    
const IDENTIFIER 'product-reviews';

    
/**
     * The routes schema.
     *
     * @var string
     */
    
const SCHEMA_TYPE 'product-review';

    
/**
     * Get the path of this REST route.
     *
     * @return string
     */
    
public function get_path() {
        return 
self::get_path_regex();
    }

    
/**
     * Get the path of this rest route.
     *
     * @return string
     */
    
public static function get_path_regex() {
        return 
'/products/reviews';
    }

    
/**
     * Get method arguments for this REST route.
     *
     * @return array An array of endpoints.
     */
    
public function get_args() {
        return [
            [
                
'methods'             => \WP_REST_Server::READABLE,
                
'callback'            => [ $this'get_response' ],
                
'permission_callback' => '__return_true',
                
'args'                => $this->get_collection_params(),
            ],
            
'schema' => [ $this->schema'get_public_item_schema' ],
        ];
    }

    
/**
     * Get a collection of reviews.
     *
     * @param \WP_REST_Request $request Request object.
     * @return \WP_REST_Response
     */
    
protected function get_route_response\WP_REST_Request $request ) {
        
$prepared_args = array(
            
'type'          => 'review',
            
'status'        => 'approve',
            
'no_found_rows' => false,
            
'offset'        => $request['offset'],
            
'order'         => $request['order'],
            
'number'        => $request['per_page'],
            
'post__in'      => $request['product_id'],
        );

        
/**
         * Map category id to list of product ids.
         */
        
if ( ! empty( $request['category_id'] ) ) {
            
$category_ids $request['category_id'];
            
$child_ids    = [];
            foreach ( 
$category_ids as $category_id ) {
                
$child_ids array_merge$child_idsget_term_children$category_id'product_cat' ) );
            }
            
$category_ids              array_uniquearray_merge$category_ids$child_ids ) );
            
$product_ids               get_objects_in_term$category_ids'product_cat' );
            
$prepared_args['post__in'] = isset( $prepared_args['post__in'] ) ? array_merge$prepared_args['post__in'], $product_ids ) : $product_ids;
        }

        if ( 
'rating' === $request['orderby'] ) {
            
$prepared_args['meta_query'] = array( // phpcs:ignore
                
'relation' => 'OR',
                array(
                    
'key'     => 'rating',
                    
'compare' => 'EXISTS',
                ),
                array(
                    
'key'     => 'rating',
                    
'compare' => 'NOT EXISTS',
                ),
            );
        }
        
$prepared_args['orderby'] = $this->normalize_query_param$request['orderby'] );

        if ( empty( 
$request['offset'] ) ) {
            
$prepared_args['offset'] = $prepared_args['number'] * ( absint$request['page'] ) - );
        }

        
$query            = new WP_Comment_Query();
        
$query_result     $query->query$prepared_args );
        
$response_objects = array();

        foreach ( 
$query_result as $review ) {
            
$data               $this->prepare_item_for_response$review$request );
            
$response_objects[] = $this->prepare_response_for_collection$data );
        }

        
$total_reviews = (int) $query->found_comments;
        
$max_pages     = (int) $query->max_num_pages;

        if ( 
$total_reviews ) {
            
// Out-of-bounds, run the query again without LIMIT for total count.
            
unset( $prepared_args['number'], $prepared_args['offset'] );

            
$query                  = new WP_Comment_Query();
            
$prepared_args['count'] = true;

            
$total_reviews $query->query$prepared_args );
            
$max_pages     $request['per_page'] ? ceil$total_reviews $request['per_page'] ) : 1;
        }

        
$response rest_ensure_response$response_objects );
        
$response = ( new Pagination() )->add_headers$response$request$total_reviews$max_pages );

        return 
$response;
    }

    
/**
     * Prepends internal property prefix to query parameters to match our response fields.
     *
     * @param string $query_param Query parameter.
     * @return string
     */
    
protected function normalize_query_param$query_param ) {
        
$prefix 'comment_';

        switch ( 
$query_param ) {
            case 
'id':
                
$normalized $prefix 'ID';
                break;
            case 
'product':
                
$normalized $prefix 'post_ID';
                break;
            case 
'rating':
                
$normalized 'meta_value_num';
                break;
            default:
                
$normalized $prefix $query_param;
                break;
        }

        return 
$normalized;
    }

    
/**
     * Get the query params for collections of products.
     *
     * @return array
     */
    
public function get_collection_params() {
        
$params                       = array();
        
$params['context']            = $this->get_context_param();
        
$params['context']['default'] = 'view';

        
$params['page'] = array(
            
'description'       => __'Current page of the collection.''woocommerce' ),
            
'type'              => 'integer',
            
'default'           => 1,
            
'sanitize_callback' => 'absint',
            
'validate_callback' => 'rest_validate_request_arg',
            
'minimum'           => 1,
        );

        
$params['per_page'] = array(
            
'description'       => __'Maximum number of items to be returned in result set. Defaults to no limit if left blank.''woocommerce' ),
            
'type'              => 'integer',
            
'default'           => 10,
            
'minimum'           => 0,
            
'maximum'           => 100,
            
'sanitize_callback' => 'absint',
            
'validate_callback' => 'rest_validate_request_arg',
        );

        
$params['offset'] = array(
            
'description'       => __'Offset the result set by a specific number of items.''woocommerce' ),
            
'type'              => 'integer',
            
'sanitize_callback' => 'absint',
            
'validate_callback' => 'rest_validate_request_arg',
        );

        
$params['order'] = array(
            
'description'       => __'Order sort attribute ascending or descending.''woocommerce' ),
            
'type'              => 'string',
            
'default'           => 'desc',
            
'enum'              => array( 'asc''desc' ),
            
'validate_callback' => 'rest_validate_request_arg',
        );

        
$params['orderby'] = array(
            
'description'       => __'Sort collection by object attribute.''woocommerce' ),
            
'type'              => 'string',
            
'default'           => 'date',
            
'enum'              => array(
                
'date',
                
'date_gmt',
                
'id',
                
'rating',
                
'product',
            ),
            
'validate_callback' => 'rest_validate_request_arg',
        );

        
$params['category_id'] = array(
            
'description'       => __'Limit result set to reviews from specific category IDs.''woocommerce' ),
            
'type'              => 'string',
            
'sanitize_callback' => 'wp_parse_id_list',
            
'validate_callback' => 'rest_validate_request_arg',
        );

        
$params['product_id'] = array(
            
'description'       => __'Limit result set to reviews from specific product IDs.''woocommerce' ),
            
'type'              => 'string',
            
'sanitize_callback' => 'wp_parse_id_list',
            
'validate_callback' => 'rest_validate_request_arg',
        );

        return 
$params;
    }
}