Total Complexity | 52 |
Total Lines | 596 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Products 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 Products, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Products extends AbstractObjectsController { |
||
20 | |||
21 | /** |
||
22 | * Route base. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $rest_base = 'products'; |
||
27 | |||
28 | /** |
||
29 | * Post type. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $post_type = 'product'; |
||
34 | |||
35 | /** |
||
36 | * If object is hierarchical. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $hierarchical = true; |
||
41 | |||
42 | /** |
||
43 | * Get the Product's schema, conforming to JSON Schema. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | public function get_item_schema() { |
||
48 | return $this->add_additional_fields_schema( ProductSchema::get_schema() ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Get the query params for collections of attachments. |
||
53 | * |
||
54 | * @return array |
||
55 | */ |
||
56 | public function get_collection_params() { |
||
57 | $params = parent::get_collection_params(); |
||
58 | |||
59 | $params['slug'] = array( |
||
60 | 'description' => __( 'Limit result set to products with a specific slug.', 'woocommerce' ), |
||
61 | 'type' => 'string', |
||
62 | 'validate_callback' => 'rest_validate_request_arg', |
||
63 | ); |
||
64 | |||
65 | $params['status'] = array( |
||
66 | 'default' => 'any', |
||
67 | 'description' => __( 'Limit result set to products assigned a specific status.', 'woocommerce' ), |
||
68 | 'type' => 'string', |
||
69 | 'enum' => array_merge( array( 'any', 'future' ), array_keys( get_post_statuses() ) ), |
||
70 | 'sanitize_callback' => 'sanitize_key', |
||
71 | 'validate_callback' => 'rest_validate_request_arg', |
||
72 | ); |
||
73 | |||
74 | $params['type'] = array( |
||
75 | 'description' => __( 'Limit result set to products assigned a specific type.', 'woocommerce' ), |
||
76 | 'type' => 'string', |
||
77 | 'enum' => array_keys( wc_get_product_types() ), |
||
78 | 'sanitize_callback' => 'sanitize_key', |
||
79 | 'validate_callback' => 'rest_validate_request_arg', |
||
80 | ); |
||
81 | |||
82 | $params['sku'] = array( |
||
83 | 'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'woocommerce' ), |
||
84 | 'type' => 'string', |
||
85 | 'sanitize_callback' => 'sanitize_text_field', |
||
86 | 'validate_callback' => 'rest_validate_request_arg', |
||
87 | ); |
||
88 | |||
89 | $params['featured'] = array( |
||
90 | 'description' => __( 'Limit result set to featured products.', 'woocommerce' ), |
||
91 | 'type' => 'boolean', |
||
92 | 'sanitize_callback' => 'wc_string_to_bool', |
||
93 | 'validate_callback' => 'rest_validate_request_arg', |
||
94 | ); |
||
95 | |||
96 | $params['category'] = array( |
||
97 | 'description' => __( 'Limit result set to products assigned a specific category ID.', 'woocommerce' ), |
||
98 | 'type' => 'string', |
||
99 | 'sanitize_callback' => 'wp_parse_id_list', |
||
100 | 'validate_callback' => 'rest_validate_request_arg', |
||
101 | ); |
||
102 | |||
103 | $params['tag'] = array( |
||
104 | 'description' => __( 'Limit result set to products assigned a specific tag ID.', 'woocommerce' ), |
||
105 | 'type' => 'string', |
||
106 | 'sanitize_callback' => 'wp_parse_id_list', |
||
107 | 'validate_callback' => 'rest_validate_request_arg', |
||
108 | ); |
||
109 | |||
110 | $params['shipping_class'] = array( |
||
111 | 'description' => __( 'Limit result set to products assigned a specific shipping class ID.', 'woocommerce' ), |
||
112 | 'type' => 'string', |
||
113 | 'sanitize_callback' => 'wp_parse_id_list', |
||
114 | 'validate_callback' => 'rest_validate_request_arg', |
||
115 | ); |
||
116 | |||
117 | $params['attribute'] = array( |
||
118 | 'description' => __( 'Limit result set to products with a specific attribute. Use the taxonomy name/attribute slug.', 'woocommerce' ), |
||
119 | 'type' => 'string', |
||
120 | 'sanitize_callback' => 'sanitize_text_field', |
||
121 | 'validate_callback' => 'rest_validate_request_arg', |
||
122 | ); |
||
123 | |||
124 | $params['attribute_term'] = array( |
||
125 | 'description' => __( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'woocommerce' ), |
||
126 | 'type' => 'string', |
||
127 | 'sanitize_callback' => 'wp_parse_id_list', |
||
128 | 'validate_callback' => 'rest_validate_request_arg', |
||
129 | ); |
||
130 | |||
131 | if ( wc_tax_enabled() ) { |
||
132 | $params['tax_class'] = array( |
||
133 | 'description' => __( 'Limit result set to products with a specific tax class.', 'woocommerce' ), |
||
134 | 'type' => 'string', |
||
135 | 'enum' => array_merge( array( 'standard' ), \WC_Tax::get_tax_class_slugs() ), |
||
136 | 'sanitize_callback' => 'sanitize_text_field', |
||
137 | 'validate_callback' => 'rest_validate_request_arg', |
||
138 | ); |
||
139 | } |
||
140 | |||
141 | $params['on_sale'] = array( |
||
142 | 'description' => __( 'Limit result set to products on sale.', 'woocommerce' ), |
||
143 | 'type' => 'boolean', |
||
144 | 'sanitize_callback' => 'wc_string_to_bool', |
||
145 | 'validate_callback' => 'rest_validate_request_arg', |
||
146 | ); |
||
147 | |||
148 | $params['min_price'] = array( |
||
149 | 'description' => __( 'Limit result set to products based on a minimum price.', 'woocommerce' ), |
||
150 | 'type' => 'string', |
||
151 | 'sanitize_callback' => 'sanitize_text_field', |
||
152 | 'validate_callback' => 'rest_validate_request_arg', |
||
153 | ); |
||
154 | |||
155 | $params['max_price'] = array( |
||
156 | 'description' => __( 'Limit result set to products based on a maximum price.', 'woocommerce' ), |
||
157 | 'type' => 'string', |
||
158 | 'sanitize_callback' => 'sanitize_text_field', |
||
159 | 'validate_callback' => 'rest_validate_request_arg', |
||
160 | ); |
||
161 | |||
162 | $params['stock_status'] = array( |
||
163 | 'description' => __( 'Limit result set to products with specified stock status.', 'woocommerce' ), |
||
164 | 'type' => 'string', |
||
165 | 'enum' => array_keys( wc_get_product_stock_status_options() ), |
||
166 | 'sanitize_callback' => 'sanitize_text_field', |
||
167 | 'validate_callback' => 'rest_validate_request_arg', |
||
168 | ); |
||
169 | |||
170 | $params['low_in_stock'] = array( |
||
171 | 'description' => __( 'Limit result set to products that are low or out of stock.', 'woocommerce' ), |
||
172 | 'type' => 'boolean', |
||
173 | 'default' => false, |
||
174 | 'sanitize_callback' => 'wc_string_to_bool', |
||
175 | ); |
||
176 | |||
177 | $params['search'] = array( |
||
178 | 'description' => __( 'Search by similar product name or sku.', 'woocommerce' ), |
||
179 | 'type' => 'string', |
||
180 | 'validate_callback' => 'rest_validate_request_arg', |
||
181 | ); |
||
182 | |||
183 | $params['orderby']['enum'] = array_merge( $params['orderby']['enum'], array( 'price', 'popularity', 'rating' ) ); |
||
184 | |||
185 | return $params; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Get object. |
||
190 | * |
||
191 | * @param int $id Object ID. |
||
192 | * |
||
193 | * @since 3.0.0 |
||
194 | * @return \WC_Data|bool |
||
195 | */ |
||
196 | protected function get_object( $id ) { |
||
197 | return wc_get_product( $id ); |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Prepare a single product output for response. |
||
202 | * |
||
203 | * @param \WC_Data $object Object data. |
||
204 | * @param \WP_REST_Request $request Request object. |
||
205 | * |
||
206 | * @since 3.0.0 |
||
207 | * @return \WP_REST_Response |
||
208 | */ |
||
209 | public function prepare_object_for_response( $object, $request ) { |
||
210 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
211 | $data = ProductSchema::object_to_schema( $object, $context ); |
||
212 | $data = $this->add_additional_fields_to_object( $data, $request ); |
||
213 | $data = $this->filter_response_by_context( $data, $context ); |
||
214 | $response = rest_ensure_response( $data ); |
||
215 | $response->add_links( $this->prepare_links( $object, $request ) ); |
||
216 | |||
217 | /** |
||
218 | * Filter the data for a response. |
||
219 | * |
||
220 | * The dynamic portion of the hook name, $this->post_type, |
||
221 | * refers to object type being prepared for the response. |
||
222 | * |
||
223 | * @param \WP_REST_Response $response The response object. |
||
224 | * @param \WC_Data $object Object data. |
||
225 | * @param \WP_REST_Request $request Request object. |
||
226 | */ |
||
227 | return apply_filters( "woocommerce_rest_prepare_{$this->post_type}_object", $response, $object, $request ); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Prepare a single product for create or update. |
||
232 | * |
||
233 | * @param \WP_REST_Request $request Request object. |
||
234 | * @param bool $creating If is creating a new object. |
||
235 | * @return \WP_Error|\WC_Data |
||
236 | */ |
||
237 | protected function prepare_object_for_database( $request, $creating = false ) { |
||
238 | $product = ProductSchema::schema_to_object( $request ); |
||
239 | |||
240 | /** |
||
241 | * Filters an object before it is inserted via the REST API. |
||
242 | * |
||
243 | * The dynamic portion of the hook name, `$this->post_type`, |
||
244 | * refers to the object type slug. |
||
245 | * |
||
246 | * @param \WC_Data $product Object object. |
||
247 | * @param \WP_REST_Request $request Request object. |
||
248 | * @param bool $creating If is creating a new object. |
||
249 | */ |
||
250 | return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $product, $request, $creating ); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get a collection of posts and add the post title filter option to \WP_Query. |
||
255 | * |
||
256 | * @param \WP_REST_Request $request Full details about the request. |
||
257 | * @return \WP_Error|\WP_REST_Response |
||
258 | */ |
||
259 | public function get_items( $request ) { |
||
260 | add_filter( 'posts_clauses', array( $this, 'get_items_query_clauses' ), 10, 2 ); |
||
261 | $response = parent::get_items( $request ); |
||
262 | remove_filter( 'posts_clauses', array( $this, 'get_items_query_clauses' ), 10 ); |
||
263 | return $response; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Add in conditional search filters for products. |
||
268 | * |
||
269 | * @param array $args Query args. |
||
270 | * @param \WC_Query $wp_query WC_Query object. |
||
271 | * @return array |
||
272 | */ |
||
273 | public function get_items_query_clauses( $args, $wp_query ) { |
||
274 | global $wpdb; |
||
275 | |||
276 | if ( $wp_query->get( 'search' ) ) { |
||
|
|||
277 | $search = "'%" . $wpdb->esc_like( $wp_query->get( 'search' ) ) . "%'"; |
||
278 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
279 | $args['where'] .= " AND ({$wpdb->posts}.post_title LIKE {$search}"; |
||
280 | $args['where'] .= wc_product_sku_enabled() ? ' OR wc_product_meta_lookup.sku LIKE ' . $search . ')' : ')'; |
||
281 | } |
||
282 | |||
283 | if ( $wp_query->get( 'sku' ) ) { |
||
284 | $skus = explode( ',', $wp_query->get( 'sku' ) ); |
||
285 | // Include the current string as a SKU too. |
||
286 | if ( 1 < count( $skus ) ) { |
||
287 | $skus[] = $wp_query->get( 'sku' ); |
||
288 | } |
||
289 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
290 | $args['where'] .= ' AND wc_product_meta_lookup.sku IN ("' . implode( '","', array_map( 'esc_sql', $skus ) ) . '")'; |
||
291 | } |
||
292 | |||
293 | if ( $wp_query->get( 'min_price' ) ) { |
||
294 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
295 | $args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.min_price >= %f ', floatval( $wp_query->get( 'min_price' ) ) ); |
||
296 | } |
||
297 | |||
298 | if ( $wp_query->get( 'max_price' ) ) { |
||
299 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
300 | $args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.max_price <= %f ', floatval( $wp_query->get( 'max_price' ) ) ); |
||
301 | } |
||
302 | |||
303 | if ( $wp_query->get( 'stock_status' ) ) { |
||
304 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
305 | $args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.stock_status = %s ', $wp_query->get( 'stock_status' ) ); |
||
306 | } |
||
307 | |||
308 | if ( $wp_query->get( 'low_in_stock' ) ) { |
||
309 | $low_stock = absint( max( get_option( 'woocommerce_notify_low_stock_amount' ), 1 ) ); |
||
310 | $args['join'] = $this->append_product_sorting_table_join( $args['join'] ); |
||
311 | $args['where'] .= $wpdb->prepare( ' AND wc_product_meta_lookup.stock_quantity <= %d', $low_stock ); |
||
312 | } |
||
313 | |||
314 | return $args; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Join wc_product_meta_lookup to posts if not already joined. |
||
319 | * |
||
320 | * @param string $sql SQL join. |
||
321 | * @return string |
||
322 | */ |
||
323 | protected function append_product_sorting_table_join( $sql ) { |
||
324 | global $wpdb; |
||
325 | |||
326 | if ( ! strstr( $sql, 'wc_product_meta_lookup' ) ) { |
||
327 | $sql .= " LEFT JOIN {$wpdb->wc_product_meta_lookup} wc_product_meta_lookup ON $wpdb->posts.ID = wc_product_meta_lookup.product_id "; |
||
328 | } |
||
329 | return $sql; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Make extra product orderby features supported by WooCommerce available to the WC API. |
||
334 | * This includes 'price', 'popularity', and 'rating'. |
||
335 | * |
||
336 | * @param \WP_REST_Request $request Request data. |
||
337 | * @return array |
||
338 | */ |
||
339 | protected function prepare_objects_query( $request ) { |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Prepare links for the request. |
||
464 | * |
||
465 | * @param \WC_Data $object Object data. |
||
466 | * @param \WP_REST_Request $request Request object. |
||
467 | * |
||
468 | * @return array Links for the given post. |
||
469 | */ |
||
470 | protected function prepare_links( $object, $request ) { |
||
471 | $links = array( |
||
472 | 'self' => array( |
||
473 | 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $object->get_id() ) ), // @codingStandardsIgnoreLine. |
||
474 | ), |
||
475 | 'collection' => array( |
||
476 | 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), // @codingStandardsIgnoreLine. |
||
477 | ), |
||
478 | ); |
||
479 | |||
480 | if ( $object->get_parent_id() ) { |
||
481 | $links['up'] = array( |
||
482 | 'href' => rest_url( sprintf( '/%s/products/%d', $this->namespace, $object->get_parent_id() ) ), // @codingStandardsIgnoreLine. |
||
483 | ); |
||
484 | } |
||
485 | |||
486 | return $links; |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * Delete a single item. |
||
491 | * |
||
492 | * @param \WP_REST_Request $request Full details about the request. |
||
493 | * |
||
494 | * @return \WP_REST_Response|\WP_Error |
||
495 | */ |
||
496 | public function delete_item( $request ) { |
||
615 | } |
||
616 | } |
||
617 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.