Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WC_REST_Customers_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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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_Customers_Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class WC_REST_Customers_Controller extends WC_REST_Controller { |
||
24 | |||
25 | /** |
||
26 | * Endpoint namespace. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $namespace = 'wc/v1'; |
||
31 | |||
32 | /** |
||
33 | * Route base. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $rest_base = 'customers'; |
||
38 | |||
39 | /** |
||
40 | * Register the routes for customers. |
||
41 | */ |
||
42 | public function register_routes() { |
||
43 | register_rest_route( $this->namespace, '/' . $this->rest_base, array( |
||
44 | array( |
||
45 | 'methods' => WP_REST_Server::READABLE, |
||
46 | 'callback' => array( $this, 'get_items' ), |
||
47 | 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
||
48 | 'args' => $this->get_collection_params(), |
||
49 | ), |
||
50 | array( |
||
51 | 'methods' => WP_REST_Server::CREATABLE, |
||
52 | 'callback' => array( $this, 'create_item' ), |
||
53 | 'permission_callback' => array( $this, 'create_item_permissions_check' ), |
||
54 | 'args' => array_merge( $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), array( |
||
55 | 'email' => array( |
||
56 | 'required' => true, |
||
57 | ), |
||
58 | 'username' => array( |
||
59 | 'required' => 'no' === get_option( 'woocommerce_registration_generate_username', 'yes' ), |
||
60 | ), |
||
61 | 'password' => array( |
||
62 | 'required' => 'no' === get_option( 'woocommerce_registration_generate_password', 'no' ), |
||
63 | ), |
||
64 | ) ), |
||
65 | ), |
||
66 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
67 | ) ); |
||
68 | |||
69 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/(?P<id>[\d]+)', array( |
||
70 | array( |
||
71 | 'methods' => WP_REST_Server::READABLE, |
||
72 | 'callback' => array( $this, 'get_item' ), |
||
73 | 'permission_callback' => array( $this, 'get_item_permissions_check' ), |
||
74 | 'args' => array( |
||
75 | 'context' => $this->get_context_param( array( 'default' => 'view' ) ), |
||
76 | ), |
||
77 | ), |
||
78 | array( |
||
79 | 'methods' => WP_REST_Server::EDITABLE, |
||
80 | 'callback' => array( $this, 'update_item' ), |
||
81 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
||
82 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
83 | ), |
||
84 | array( |
||
85 | 'methods' => WP_REST_Server::DELETABLE, |
||
86 | 'callback' => array( $this, 'delete_item' ), |
||
87 | 'permission_callback' => array( $this, 'delete_item_permissions_check' ), |
||
88 | 'args' => array( |
||
89 | 'force' => array( |
||
90 | 'default' => false, |
||
91 | 'description' => __( 'Required to be true, as resource does not support trashing.', 'woocommerce' ), |
||
92 | ), |
||
93 | 'reassign' => array(), |
||
94 | ), |
||
95 | ), |
||
96 | 'schema' => array( $this, 'get_public_item_schema' ), |
||
97 | ) ); |
||
98 | |||
99 | register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array( |
||
100 | array( |
||
101 | 'methods' => WP_REST_Server::EDITABLE, |
||
102 | 'callback' => array( $this, 'batch_items' ), |
||
103 | 'permission_callback' => array( $this, 'batch_items_permissions_check' ), |
||
104 | 'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ), |
||
105 | ), |
||
106 | 'schema' => array( $this, 'get_public_batch_schema' ), |
||
107 | ) ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Check whether a given request has permission to read customers. |
||
112 | * |
||
113 | * @param WP_REST_Request $request Full details about the request. |
||
114 | * @return WP_Error|boolean |
||
115 | */ |
||
116 | View Code Duplication | public function get_items_permissions_check( $request ) { |
|
123 | |||
124 | /** |
||
125 | * Check if a given request has access create customers. |
||
126 | * |
||
127 | * @param WP_REST_Request $request Full details about the request. |
||
128 | * @return boolean |
||
129 | */ |
||
130 | View Code Duplication | public function create_item_permissions_check( $request ) { |
|
137 | |||
138 | /** |
||
139 | * Check if a given request has access to read a customer. |
||
140 | * |
||
141 | * @param WP_REST_Request $request Full details about the request. |
||
142 | * @return WP_Error|boolean |
||
143 | */ |
||
144 | View Code Duplication | public function get_item_permissions_check( $request ) { |
|
153 | |||
154 | /** |
||
155 | * Check if a given request has access update a customer. |
||
156 | * |
||
157 | * @param WP_REST_Request $request Full details about the request. |
||
158 | * @return boolean |
||
159 | */ |
||
160 | View Code Duplication | public function update_item_permissions_check( $request ) { |
|
169 | |||
170 | /** |
||
171 | * Check if a given request has access delete a customer. |
||
172 | * |
||
173 | * @param WP_REST_Request $request Full details about the request. |
||
174 | * @return boolean |
||
175 | */ |
||
176 | View Code Duplication | public function delete_item_permissions_check( $request ) { |
|
185 | |||
186 | /** |
||
187 | * Check if a given request has access batch create, update and delete items. |
||
188 | * |
||
189 | * @param WP_REST_Request $request Full details about the request. |
||
190 | * @return boolean |
||
191 | */ |
||
192 | View Code Duplication | public function batch_items_permissions_check( $request ) { |
|
193 | if ( ! wc_rest_check_user_permissions( 'batch' ) ) { |
||
194 | return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
||
195 | } |
||
196 | |||
197 | return true; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get all customers. |
||
202 | * |
||
203 | * @param WP_REST_Request $request Full details about the request. |
||
204 | * @return WP_Error|WP_REST_Response |
||
205 | */ |
||
206 | public function get_items( $request ) { |
||
296 | |||
297 | /** |
||
298 | * Create a single customer. |
||
299 | * |
||
300 | * @param WP_REST_Request $request Full details about the request. |
||
301 | * @return WP_Error|WP_REST_Response |
||
302 | */ |
||
303 | public function create_item( $request ) { |
||
344 | |||
345 | /** |
||
346 | * Get a single customer. |
||
347 | * |
||
348 | * @param WP_REST_Request $request Full details about the request. |
||
349 | * @return WP_Error|WP_REST_Response |
||
350 | */ |
||
351 | View Code Duplication | public function get_item( $request ) { |
|
364 | |||
365 | /** |
||
366 | * Update a single user. |
||
367 | * |
||
368 | * @param WP_REST_Request $request Full details about the request. |
||
369 | * @return WP_Error|WP_REST_Response |
||
370 | */ |
||
371 | public function update_item( $request ) { |
||
372 | $id = (int) $request['id']; |
||
373 | $customer = get_userdata( $id ); |
||
374 | |||
375 | if ( ! $customer ) { |
||
376 | return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
377 | } |
||
378 | |||
379 | if ( ! empty( $request['email'] ) && email_exists( $request['email'] ) && $request['email'] !== $customer->user_email ) { |
||
380 | return new WP_Error( 'woocommerce_rest_customer_invalid_email', __( 'Email address is invalid.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
381 | } |
||
382 | |||
383 | if ( ! empty( $request['username'] ) && $request['username'] !== $customer->user_login ) { |
||
384 | return new WP_Error( 'woocommerce_rest_customer_invalid_argument', __( "Username isn't editable.", 'woocommerce' ), array( 'status' => 400 ) ); |
||
385 | } |
||
386 | |||
387 | // Customer email. |
||
388 | View Code Duplication | if ( isset( $request['email'] ) ) { |
|
389 | wp_update_user( array( 'ID' => $customer->ID, 'user_email' => sanitize_email( $request['email'] ) ) ); |
||
390 | } |
||
391 | |||
392 | // Customer password. |
||
393 | View Code Duplication | if ( isset( $request['password'] ) ) { |
|
394 | wp_update_user( array( 'ID' => $customer->ID, 'user_pass' => wc_clean( $request['password'] ) ) ); |
||
395 | } |
||
396 | |||
397 | $this->update_additional_fields_for_object( $customer, $request ); |
||
398 | |||
399 | // Update customer data. |
||
400 | $this->update_customer_meta_fields( $customer, $request ); |
||
401 | |||
402 | /** |
||
403 | * Fires after a customer is created or updated via the REST API. |
||
404 | * |
||
405 | * @param WP_User $customer Data used to create the customer. |
||
406 | * @param WP_REST_Request $request Request object. |
||
407 | * @param boolean $creating True when creating customer, false when updating customer. |
||
408 | */ |
||
409 | do_action( 'woocommerce_rest_insert_customer', $customer, $request, false ); |
||
410 | |||
411 | $request->set_param( 'context', 'edit' ); |
||
412 | $response = $this->prepare_item_for_response( $customer, $request ); |
||
413 | $response = rest_ensure_response( $response ); |
||
414 | return $response; |
||
415 | } |
||
416 | |||
417 | /** |
||
418 | * Delete a single customer. |
||
419 | * |
||
420 | * @param WP_REST_Request $request Full details about the request. |
||
421 | * @return WP_Error|WP_REST_Response |
||
422 | */ |
||
423 | public function delete_item( $request ) { |
||
424 | $id = (int) $request['id']; |
||
425 | $reassign = isset( $request['reassign'] ) ? absint( $request['reassign'] ) : null; |
||
426 | $force = isset( $request['force'] ) ? (bool) $request['force'] : false; |
||
427 | |||
428 | // We don't support trashing for this type, error out. |
||
429 | if ( ! $force ) { |
||
430 | return new WP_Error( 'woocommerce_rest_trash_not_supported', __( 'Customers do not support trashing.', 'woocommerce' ), array( 'status' => 501 ) ); |
||
431 | } |
||
432 | |||
433 | $customer = get_userdata( $id ); |
||
434 | if ( ! $customer ) { |
||
435 | return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
436 | } |
||
437 | |||
438 | View Code Duplication | if ( ! empty( $reassign ) ) { |
|
439 | if ( $reassign === $id || ! get_userdata( $reassign ) ) { |
||
440 | return new WP_Error( 'woocommerce_rest_customer_invalid_reassign', __( 'Invalid resource id for reassignment.', 'woocommerce' ), array( 'status' => 400 ) ); |
||
441 | } |
||
442 | } |
||
443 | |||
444 | $request->set_param( 'context', 'edit' ); |
||
445 | $response = $this->prepare_item_for_response( $customer, $request ); |
||
446 | |||
447 | /** Include admin customer functions to get access to wp_delete_user() */ |
||
448 | require_once ABSPATH . 'wp-admin/includes/user.php'; |
||
449 | |||
450 | $result = wp_delete_user( $id, $reassign ); |
||
451 | |||
452 | if ( ! $result ) { |
||
453 | return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'The resource cannot be deleted.', 'woocommerce' ), array( 'status' => 500 ) ); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Fires after a customer is deleted via the REST API. |
||
458 | * |
||
459 | * @param WP_User $customer The customer data. |
||
460 | * @param WP_REST_Response $response The response returned from the API. |
||
461 | * @param WP_REST_Request $request The request sent to the API. |
||
462 | */ |
||
463 | do_action( 'woocommerce_rest_delete_customer', $customer, $response, $request ); |
||
464 | |||
465 | return $response; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Prepare a single customer output for response. |
||
470 | * |
||
471 | * @param WP_User $customer Customer object. |
||
472 | * @param WP_REST_Request $request Request object. |
||
473 | * @return WP_REST_Response $response Response data. |
||
474 | */ |
||
475 | public function prepare_item_for_response( $customer, $request ) { |
||
476 | $last_order = wc_get_customer_last_order( $customer->ID ); |
||
477 | |||
478 | $data = array( |
||
479 | 'id' => $customer->ID, |
||
480 | 'date_created' => wc_rest_prepare_date_response( $customer->user_registered ), |
||
481 | 'date_modified' => $customer->last_update ? wc_rest_prepare_date_response( date( 'Y-m-d H:i:s', $customer->last_update ) ) : null, |
||
482 | 'email' => $customer->user_email, |
||
483 | 'first_name' => $customer->first_name, |
||
484 | 'last_name' => $customer->last_name, |
||
485 | 'username' => $customer->user_login, |
||
486 | 'last_order' => array( |
||
487 | 'id' => is_object( $last_order ) ? $last_order->id : null, |
||
488 | 'date' => is_object( $last_order ) ? wc_rest_prepare_date_response( $last_order->post->post_date_gmt ) : null |
||
489 | ), |
||
490 | 'orders_count' => wc_get_customer_order_count( $customer->ID ), |
||
491 | 'total_spent' => wc_format_decimal( wc_get_customer_total_spent( $customer->ID ), 2 ), |
||
492 | 'avatar_url' => wc_get_customer_avatar_url( $customer->customer_email ), |
||
493 | 'billing' => array( |
||
494 | 'first_name' => $customer->billing_first_name, |
||
495 | 'last_name' => $customer->billing_last_name, |
||
496 | 'company' => $customer->billing_company, |
||
497 | 'address_1' => $customer->billing_address_1, |
||
498 | 'address_2' => $customer->billing_address_2, |
||
499 | 'city' => $customer->billing_city, |
||
500 | 'state' => $customer->billing_state, |
||
501 | 'postcode' => $customer->billing_postcode, |
||
502 | 'country' => $customer->billing_country, |
||
503 | 'email' => $customer->billing_email, |
||
504 | 'phone' => $customer->billing_phone, |
||
505 | ), |
||
506 | 'shipping' => array( |
||
507 | 'first_name' => $customer->shipping_first_name, |
||
508 | 'last_name' => $customer->shipping_last_name, |
||
509 | 'company' => $customer->shipping_company, |
||
510 | 'address_1' => $customer->shipping_address_1, |
||
511 | 'address_2' => $customer->shipping_address_2, |
||
512 | 'city' => $customer->shipping_city, |
||
513 | 'state' => $customer->shipping_state, |
||
514 | 'postcode' => $customer->shipping_postcode, |
||
515 | 'country' => $customer->shipping_country, |
||
516 | ), |
||
517 | ); |
||
518 | |||
519 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
||
520 | $data = $this->add_additional_fields_to_object( $data, $request ); |
||
521 | $data = $this->filter_response_by_context( $data, $context ); |
||
522 | |||
523 | // Wrap the data in a response object. |
||
524 | $response = rest_ensure_response( $data ); |
||
525 | |||
526 | $response->add_links( $this->prepare_links( $customer ) ); |
||
527 | |||
528 | /** |
||
529 | * Filter customer data returned from the REST API. |
||
530 | * |
||
531 | * @param WP_REST_Response $response The response object. |
||
532 | * @param WP_User $customer User object used to create response. |
||
533 | * @param WP_REST_Request $request Request object. |
||
534 | */ |
||
535 | return apply_filters( 'woocommerce_rest_prepare_customer', $response, $customer, $request ); |
||
536 | } |
||
537 | |||
538 | /** |
||
539 | * Update customer meta fields. |
||
540 | * |
||
541 | * @param WP_User $customer |
||
542 | * @param WP_REST_Request $request |
||
543 | */ |
||
544 | protected function update_customer_meta_fields( $customer, $request ) { |
||
575 | |||
576 | /** |
||
577 | * Prepare links for the request. |
||
578 | * |
||
579 | * @param WP_User $customer Customer object. |
||
580 | * @return array Links for the given customer. |
||
581 | */ |
||
582 | View Code Duplication | protected function prepare_links( $customer ) { |
|
594 | |||
595 | /** |
||
596 | * Get the Customer's schema, conforming to JSON Schema. |
||
597 | * |
||
598 | * @return array |
||
599 | */ |
||
600 | public function get_item_schema() { |
||
601 | $schema = array( |
||
602 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
||
603 | 'title' => 'customer', |
||
604 | 'type' => 'object', |
||
605 | 'properties' => array( |
||
606 | 'id' => array( |
||
607 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
||
608 | 'type' => 'integer', |
||
609 | 'context' => array( 'view', 'edit' ), |
||
610 | 'readonly' => true, |
||
611 | ), |
||
612 | 'date_created' => array( |
||
613 | 'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ), |
||
614 | 'type' => 'date-time', |
||
615 | 'context' => array( 'view', 'edit' ), |
||
616 | 'readonly' => true, |
||
617 | ), |
||
618 | 'date_modified' => array( |
||
619 | 'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ), |
||
620 | 'type' => 'date-time', |
||
621 | 'context' => array( 'view', 'edit' ), |
||
622 | 'readonly' => true, |
||
623 | ), |
||
624 | 'email' => array( |
||
625 | 'description' => __( 'The email address for the customer.', 'woocommerce' ), |
||
626 | 'type' => 'string', |
||
627 | 'format' => 'email', |
||
628 | 'context' => array( 'view', 'edit' ), |
||
629 | ), |
||
630 | 'first_name' => array( |
||
631 | 'description' => __( 'Customer first name.', 'woocommerce' ), |
||
632 | 'type' => 'string', |
||
633 | 'context' => array( 'view', 'edit' ), |
||
634 | 'arg_options' => array( |
||
635 | 'sanitize_callback' => 'sanitize_text_field', |
||
636 | ), |
||
637 | ), |
||
638 | 'last_name' => array( |
||
639 | 'description' => __( 'Customer last name.', 'woocommerce' ), |
||
640 | 'type' => 'string', |
||
641 | 'context' => array( 'view', 'edit' ), |
||
642 | 'arg_options' => array( |
||
643 | 'sanitize_callback' => 'sanitize_text_field', |
||
644 | ), |
||
645 | ), |
||
646 | 'username' => array( |
||
647 | 'description' => __( 'Customer login name.', 'woocommerce' ), |
||
648 | 'type' => 'string', |
||
649 | 'context' => array( 'view', 'edit' ), |
||
650 | 'arg_options' => array( |
||
651 | 'sanitize_callback' => 'sanitize_user', |
||
652 | ), |
||
653 | ), |
||
654 | 'password' => array( |
||
655 | 'description' => __( 'Customer password.', 'woocommerce' ), |
||
656 | 'type' => 'string', |
||
657 | 'context' => array( 'edit' ), |
||
658 | ), |
||
659 | 'last_order' => array( |
||
660 | 'description' => __( 'Last order data.', 'woocommerce' ), |
||
661 | 'type' => 'array', |
||
662 | 'context' => array( 'view', 'edit' ), |
||
663 | 'readonly' => true, |
||
664 | 'properties' => array( |
||
665 | 'id' => array( |
||
666 | 'description' => __( 'Last order ID.', 'woocommerce' ), |
||
667 | 'type' => 'integer', |
||
668 | 'context' => array( 'view', 'edit' ), |
||
669 | 'readonly' => true, |
||
670 | ), |
||
671 | 'date' => array( |
||
672 | 'description' => __( 'UTC DateTime of the customer last order.', 'woocommerce' ), |
||
673 | 'type' => 'date-time', |
||
674 | 'context' => array( 'view', 'edit' ), |
||
675 | 'readonly' => true, |
||
676 | ), |
||
677 | ), |
||
678 | ), |
||
679 | 'orders_count' => array( |
||
680 | 'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ), |
||
681 | 'type' => 'integer', |
||
682 | 'context' => array( 'view', 'edit' ), |
||
683 | 'readonly' => true, |
||
684 | ), |
||
685 | 'total_spent' => array( |
||
686 | 'description' => __( 'Total amount spent.', 'woocommerce' ), |
||
687 | 'type' => 'float', |
||
688 | 'context' => array( 'view', 'edit' ), |
||
689 | 'readonly' => true, |
||
690 | ), |
||
691 | 'avatar_url' => array( |
||
692 | 'description' => __( 'Avatar URL.', 'woocommerce' ), |
||
693 | 'type' => 'string', |
||
694 | 'context' => array( 'view', 'edit' ), |
||
695 | 'readonly' => true, |
||
696 | ), |
||
697 | 'billing' => array( |
||
698 | 'description' => __( 'List of billing address data.', 'woocommerce' ), |
||
699 | 'type' => 'array', |
||
700 | 'context' => array( 'view', 'edit' ), |
||
701 | 'properties' => array( |
||
702 | 'first_name' => array( |
||
703 | 'description' => __( 'First name.', 'woocommerce' ), |
||
704 | 'type' => 'string', |
||
705 | 'context' => array( 'view', 'edit' ), |
||
706 | ), |
||
707 | 'last_name' => array( |
||
708 | 'description' => __( 'Last name.', 'woocommerce' ), |
||
709 | 'type' => 'string', |
||
710 | 'context' => array( 'view', 'edit' ), |
||
711 | ), |
||
712 | 'company' => array( |
||
713 | 'description' => __( 'Company name.', 'woocommerce' ), |
||
714 | 'type' => 'string', |
||
715 | 'context' => array( 'view', 'edit' ), |
||
716 | ), |
||
717 | 'address_1' => array( |
||
718 | 'description' => __( 'Address line 1.', 'woocommerce' ), |
||
719 | 'type' => 'string', |
||
720 | 'context' => array( 'view', 'edit' ), |
||
721 | ), |
||
722 | 'address_2' => array( |
||
723 | 'description' => __( 'Address line 2.', 'woocommerce' ), |
||
724 | 'type' => 'string', |
||
725 | 'context' => array( 'view', 'edit' ), |
||
726 | ), |
||
727 | 'city' => array( |
||
728 | 'description' => __( 'City name.', 'woocommerce' ), |
||
729 | 'type' => 'string', |
||
730 | 'context' => array( 'view', 'edit' ), |
||
731 | ), |
||
732 | 'state' => array( |
||
733 | 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ), |
||
734 | 'type' => 'string', |
||
735 | 'context' => array( 'view', 'edit' ), |
||
736 | ), |
||
737 | 'postcode' => array( |
||
738 | 'description' => __( 'Postal code.', 'woocommerce' ), |
||
739 | 'type' => 'string', |
||
740 | 'context' => array( 'view', 'edit' ), |
||
741 | ), |
||
742 | 'country' => array( |
||
743 | 'description' => __( 'ISO code of the country.', 'woocommerce' ), |
||
744 | 'type' => 'string', |
||
745 | 'context' => array( 'view', 'edit' ), |
||
746 | ), |
||
747 | 'email' => array( |
||
748 | 'description' => __( 'Email address.', 'woocommerce' ), |
||
749 | 'type' => 'string', |
||
750 | 'format' => 'email', |
||
751 | 'context' => array( 'view', 'edit' ), |
||
752 | ), |
||
753 | 'phone' => array( |
||
754 | 'description' => __( 'Phone number.', 'woocommerce' ), |
||
755 | 'type' => 'string', |
||
756 | 'context' => array( 'view', 'edit' ), |
||
757 | ), |
||
758 | ), |
||
759 | ), |
||
760 | 'shipping' => array( |
||
761 | 'description' => __( 'List of shipping address data.', 'woocommerce' ), |
||
762 | 'type' => 'array', |
||
763 | 'context' => array( 'view', 'edit' ), |
||
764 | 'properties' => array( |
||
765 | 'first_name' => array( |
||
766 | 'description' => __( 'First name.', 'woocommerce' ), |
||
767 | 'type' => 'string', |
||
768 | 'context' => array( 'view', 'edit' ), |
||
769 | ), |
||
770 | 'last_name' => array( |
||
771 | 'description' => __( 'Last name.', 'woocommerce' ), |
||
772 | 'type' => 'string', |
||
773 | 'context' => array( 'view', 'edit' ), |
||
774 | ), |
||
775 | 'company' => array( |
||
776 | 'description' => __( 'Company name.', 'woocommerce' ), |
||
777 | 'type' => 'string', |
||
778 | 'context' => array( 'view', 'edit' ), |
||
779 | ), |
||
780 | 'address_1' => array( |
||
781 | 'description' => __( 'Address line 1.', 'woocommerce' ), |
||
782 | 'type' => 'string', |
||
783 | 'context' => array( 'view', 'edit' ), |
||
784 | ), |
||
785 | 'address_2' => array( |
||
786 | 'description' => __( 'Address line 2.', 'woocommerce' ), |
||
787 | 'type' => 'string', |
||
788 | 'context' => array( 'view', 'edit' ), |
||
789 | ), |
||
790 | 'city' => array( |
||
791 | 'description' => __( 'City name.', 'woocommerce' ), |
||
792 | 'type' => 'string', |
||
793 | 'context' => array( 'view', 'edit' ), |
||
794 | ), |
||
795 | 'state' => array( |
||
796 | 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ), |
||
797 | 'type' => 'string', |
||
798 | 'context' => array( 'view', 'edit' ), |
||
799 | ), |
||
800 | 'postcode' => array( |
||
801 | 'description' => __( 'Postal code.', 'woocommerce' ), |
||
802 | 'type' => 'string', |
||
803 | 'context' => array( 'view', 'edit' ), |
||
804 | ), |
||
805 | 'country' => array( |
||
806 | 'description' => __( 'ISO code of the country.', 'woocommerce' ), |
||
807 | 'type' => 'string', |
||
808 | 'context' => array( 'view', 'edit' ), |
||
809 | ), |
||
810 | ), |
||
811 | ), |
||
812 | ), |
||
813 | ); |
||
814 | |||
815 | return $this->add_additional_fields_schema( $schema ); |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * Get role names. |
||
820 | * |
||
821 | * @return array |
||
822 | */ |
||
823 | protected function get_role_names() { |
||
828 | |||
829 | /** |
||
830 | * Get the query params for collections. |
||
831 | * |
||
832 | * @return array |
||
833 | */ |
||
834 | public function get_collection_params() { |
||
893 | } |
||
894 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.