woocommerce
/
includes
/
rest-api
/
Controllers
/
Version2
/
class-wc-rest-customers-v2-controller.php
class-wc-rest-coupons-v2-controller.php
2 months ago
class-wc-rest-customer-downloads-v2-controller.php
5 years ago
class-wc-rest-customers-v2-controller.php
2 months ago
class-wc-rest-network-orders-v2-controller.php
1 year ago
class-wc-rest-order-notes-v2-controller.php
5 years ago
class-wc-rest-order-refunds-v2-controller.php
2 months ago
class-wc-rest-orders-v2-controller.php
1 month ago
class-wc-rest-payment-gateways-v2-controller.php
11 months ago
class-wc-rest-product-attribute-terms-v2-controller.php
5 years ago
class-wc-rest-product-attributes-v2-controller.php
5 years ago
class-wc-rest-product-brands-v2-controller.php
1 year ago
class-wc-rest-product-categories-v2-controller.php
3 months ago
class-wc-rest-product-reviews-v2-controller.php
4 years ago
class-wc-rest-product-shipping-classes-v2-controller.php
5 years ago
class-wc-rest-product-tags-v2-controller.php
5 years ago
class-wc-rest-product-variations-v2-controller.php
2 months ago
class-wc-rest-products-v2-controller.php
2 months ago
class-wc-rest-report-sales-v2-controller.php
5 years ago
class-wc-rest-report-top-sellers-v2-controller.php
5 years ago
class-wc-rest-reports-v2-controller.php
5 years ago
class-wc-rest-setting-options-v2-controller.php
2 months ago
class-wc-rest-settings-v2-controller.php
5 years ago
class-wc-rest-shipping-methods-v2-controller.php
1 year ago
class-wc-rest-shipping-zone-locations-v2-controller.php
1 year ago
class-wc-rest-shipping-zone-methods-v2-controller.php
1 year ago
class-wc-rest-shipping-zones-v2-controller.php
1 month ago
class-wc-rest-system-status-tools-v2-controller.php
1 month ago
class-wc-rest-system-status-v2-controller.php
1 month ago
class-wc-rest-tax-classes-v2-controller.php
1 year ago
class-wc-rest-taxes-v2-controller.php
5 years ago
class-wc-rest-webhook-deliveries-v2-controller.php
5 years ago
class-wc-rest-webhooks-v2-controller.php
1 year ago
class-wc-rest-customers-v2-controller.php
396 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API Customers controller |
| 4 | * |
| 5 | * Handles requests to the /customers endpoint. |
| 6 | * |
| 7 | * @package WooCommerce\RestApi |
| 8 | * @since 2.6.0 |
| 9 | */ |
| 10 | |
| 11 | use Automattic\WooCommerce\Utilities\MetaDataUtil; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * REST API Customers controller class. |
| 17 | * |
| 18 | * @package WooCommerce\RestApi |
| 19 | * @extends WC_REST_Customers_V1_Controller |
| 20 | */ |
| 21 | class WC_REST_Customers_V2_Controller extends WC_REST_Customers_V1_Controller { |
| 22 | |
| 23 | /** |
| 24 | * Endpoint namespace. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | protected $namespace = 'wc/v2'; |
| 29 | |
| 30 | /** |
| 31 | * Get formatted item data. |
| 32 | * |
| 33 | * @since 3.0.0 |
| 34 | * @param WC_Data $object WC_Data instance. |
| 35 | * @return array |
| 36 | */ |
| 37 | protected function get_formatted_item_data( $object ) { |
| 38 | $formatted_data = $this->get_formatted_item_data_core( $object ); |
| 39 | $formatted_data['orders_count'] = $object->get_order_count(); |
| 40 | $formatted_data['total_spent'] = $object->get_total_spent(); |
| 41 | return $formatted_data; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get formatted item data, not including orders count nor total spent. |
| 46 | * This method is needed because v3 API doesn't return those two fields. |
| 47 | * |
| 48 | * @internal This method could disappear or have its name or signature changed in future releases. |
| 49 | * |
| 50 | * @param WC_Data $object WC_Data instance. |
| 51 | * @return array |
| 52 | */ |
| 53 | protected function get_formatted_item_data_core( $object ) { |
| 54 | $data = $object->get_data(); |
| 55 | $format_date = array( 'date_created', 'date_modified' ); |
| 56 | |
| 57 | // Format date values. |
| 58 | foreach ( $format_date as $key ) { |
| 59 | // Date created is stored UTC, date modified is stored WP local time. |
| 60 | $datetime = 'date_created' === $key && is_subclass_of( $data[ $key ], 'DateTime' ) ? get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $data[ $key ]->getTimestamp() ) ) : $data[ $key ]; |
| 61 | $data[ $key ] = wc_rest_prepare_date_response( $datetime, false ); |
| 62 | $data[ $key . '_gmt' ] = wc_rest_prepare_date_response( $datetime ); |
| 63 | } |
| 64 | |
| 65 | $formatted_data = array( |
| 66 | 'id' => $object->get_id(), |
| 67 | 'date_created' => $data['date_created'], |
| 68 | 'date_created_gmt' => $data['date_created_gmt'], |
| 69 | 'date_modified' => $data['date_modified'], |
| 70 | 'date_modified_gmt' => $data['date_modified_gmt'], |
| 71 | 'email' => $data['email'], |
| 72 | 'first_name' => $data['first_name'], |
| 73 | 'last_name' => $data['last_name'], |
| 74 | 'role' => $data['role'], |
| 75 | 'username' => $data['username'], |
| 76 | 'billing' => $data['billing'], |
| 77 | 'shipping' => $data['shipping'], |
| 78 | 'is_paying_customer' => $data['is_paying_customer'], |
| 79 | 'avatar_url' => $object->get_avatar_url(), |
| 80 | ); |
| 81 | |
| 82 | if ( wc_current_user_has_role( 'administrator' ) ) { |
| 83 | $formatted_data['meta_data'] = array_values( |
| 84 | array_filter( |
| 85 | $data['meta_data'], |
| 86 | function ( $meta ) { |
| 87 | return ! is_protected_meta( $meta->key, 'user' ); |
| 88 | } |
| 89 | ) |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | return $formatted_data; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Prepare a single customer output for response. |
| 98 | * |
| 99 | * @param WP_User $user_data User object. |
| 100 | * @param WP_REST_Request $request Request object. |
| 101 | * @return WP_REST_Response $response Response data. |
| 102 | */ |
| 103 | public function prepare_item_for_response( $user_data, $request ) { |
| 104 | $customer = new WC_Customer( $user_data->ID ); |
| 105 | $data = $this->get_formatted_item_data( $customer ); |
| 106 | $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
| 107 | $data = $this->add_additional_fields_to_object( $data, $request ); |
| 108 | $data = $this->filter_response_by_context( $data, $context ); |
| 109 | $response = rest_ensure_response( $data ); |
| 110 | $response->add_links( $this->prepare_links( $user_data ) ); |
| 111 | |
| 112 | //phpcs:disable WooCommerce.Commenting.CommentHooks.MissingSinceComment |
| 113 | /** |
| 114 | * Filter customer data returned from the REST API. |
| 115 | * |
| 116 | * @param WP_REST_Response $response The response object. |
| 117 | * @param WP_User $user_data User object used to create response. |
| 118 | * @param WP_REST_Request $request Request object. |
| 119 | */ |
| 120 | return apply_filters( 'woocommerce_rest_prepare_customer', $response, $user_data, $request ); |
| 121 | //phpcs:enable WooCommerce.Commenting.CommentHooks.MissingSinceComment |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Update customer meta fields. |
| 126 | * |
| 127 | * @param WC_Customer $customer Customer data. |
| 128 | * @param WP_REST_Request $request Request data. |
| 129 | */ |
| 130 | protected function update_customer_meta_fields( $customer, $request ) { |
| 131 | parent::update_customer_meta_fields( $customer, $request ); |
| 132 | |
| 133 | // Meta data. |
| 134 | if ( isset( $request['meta_data'] ) ) { |
| 135 | $meta_data = array_filter( |
| 136 | MetaDataUtil::normalize( $request['meta_data'] ), |
| 137 | fn( $meta ) => ! is_protected_meta( $meta['key'], 'user' ) |
| 138 | ); |
| 139 | MetaDataUtil::update( $meta_data, $customer ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get the Customer's schema, conforming to JSON Schema. |
| 145 | * |
| 146 | * @return array |
| 147 | */ |
| 148 | public function get_item_schema() { |
| 149 | $schema = array( |
| 150 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 151 | 'title' => 'customer', |
| 152 | 'type' => 'object', |
| 153 | 'properties' => array( |
| 154 | 'id' => array( |
| 155 | 'description' => __( 'Unique identifier for the resource.', 'woocommerce' ), |
| 156 | 'type' => 'integer', |
| 157 | 'context' => array( 'view', 'edit' ), |
| 158 | 'readonly' => true, |
| 159 | ), |
| 160 | 'date_created' => array( |
| 161 | 'description' => __( "The date the customer was created, in the site's timezone.", 'woocommerce' ), |
| 162 | 'type' => 'date-time', |
| 163 | 'context' => array( 'view', 'edit' ), |
| 164 | 'readonly' => true, |
| 165 | ), |
| 166 | 'date_created_gmt' => array( |
| 167 | 'description' => __( 'The date the customer was created, as GMT.', 'woocommerce' ), |
| 168 | 'type' => 'date-time', |
| 169 | 'context' => array( 'view', 'edit' ), |
| 170 | 'readonly' => true, |
| 171 | ), |
| 172 | 'date_modified' => array( |
| 173 | 'description' => __( "The date the customer was last modified, in the site's timezone.", 'woocommerce' ), |
| 174 | 'type' => 'date-time', |
| 175 | 'context' => array( 'view', 'edit' ), |
| 176 | 'readonly' => true, |
| 177 | ), |
| 178 | 'date_modified_gmt' => array( |
| 179 | 'description' => __( 'The date the customer was last modified, as GMT.', 'woocommerce' ), |
| 180 | 'type' => 'date-time', |
| 181 | 'context' => array( 'view', 'edit' ), |
| 182 | 'readonly' => true, |
| 183 | ), |
| 184 | 'email' => array( |
| 185 | 'description' => __( 'The email address for the customer.', 'woocommerce' ), |
| 186 | 'type' => 'string', |
| 187 | 'format' => 'email', |
| 188 | 'context' => array( 'view', 'edit' ), |
| 189 | ), |
| 190 | 'first_name' => array( |
| 191 | 'description' => __( 'Customer first name.', 'woocommerce' ), |
| 192 | 'type' => 'string', |
| 193 | 'context' => array( 'view', 'edit' ), |
| 194 | 'arg_options' => array( |
| 195 | 'sanitize_callback' => 'sanitize_text_field', |
| 196 | ), |
| 197 | ), |
| 198 | 'last_name' => array( |
| 199 | 'description' => __( 'Customer last name.', 'woocommerce' ), |
| 200 | 'type' => 'string', |
| 201 | 'context' => array( 'view', 'edit' ), |
| 202 | 'arg_options' => array( |
| 203 | 'sanitize_callback' => 'sanitize_text_field', |
| 204 | ), |
| 205 | ), |
| 206 | 'role' => array( |
| 207 | 'description' => __( 'Customer role.', 'woocommerce' ), |
| 208 | 'type' => 'string', |
| 209 | 'context' => array( 'view', 'edit' ), |
| 210 | 'readonly' => true, |
| 211 | ), |
| 212 | 'username' => array( |
| 213 | 'description' => __( 'Customer login name.', 'woocommerce' ), |
| 214 | 'type' => 'string', |
| 215 | 'context' => array( 'view', 'edit' ), |
| 216 | 'arg_options' => array( |
| 217 | 'sanitize_callback' => 'sanitize_user', |
| 218 | ), |
| 219 | ), |
| 220 | 'password' => array( |
| 221 | 'description' => __( 'Customer password.', 'woocommerce' ), |
| 222 | 'type' => 'string', |
| 223 | 'context' => array( 'edit' ), |
| 224 | ), |
| 225 | 'billing' => array( |
| 226 | 'description' => __( 'List of billing address data.', 'woocommerce' ), |
| 227 | 'type' => 'object', |
| 228 | 'context' => array( 'view', 'edit' ), |
| 229 | 'properties' => array( |
| 230 | 'first_name' => array( |
| 231 | 'description' => __( 'First name.', 'woocommerce' ), |
| 232 | 'type' => 'string', |
| 233 | 'context' => array( 'view', 'edit' ), |
| 234 | ), |
| 235 | 'last_name' => array( |
| 236 | 'description' => __( 'Last name.', 'woocommerce' ), |
| 237 | 'type' => 'string', |
| 238 | 'context' => array( 'view', 'edit' ), |
| 239 | ), |
| 240 | 'company' => array( |
| 241 | 'description' => __( 'Company name.', 'woocommerce' ), |
| 242 | 'type' => 'string', |
| 243 | 'context' => array( 'view', 'edit' ), |
| 244 | ), |
| 245 | 'address_1' => array( |
| 246 | 'description' => __( 'Address line 1', 'woocommerce' ), |
| 247 | 'type' => 'string', |
| 248 | 'context' => array( 'view', 'edit' ), |
| 249 | ), |
| 250 | 'address_2' => array( |
| 251 | 'description' => __( 'Address line 2', 'woocommerce' ), |
| 252 | 'type' => 'string', |
| 253 | 'context' => array( 'view', 'edit' ), |
| 254 | ), |
| 255 | 'city' => array( |
| 256 | 'description' => __( 'City name.', 'woocommerce' ), |
| 257 | 'type' => 'string', |
| 258 | 'context' => array( 'view', 'edit' ), |
| 259 | ), |
| 260 | 'state' => array( |
| 261 | 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ), |
| 262 | 'type' => 'string', |
| 263 | 'context' => array( 'view', 'edit' ), |
| 264 | ), |
| 265 | 'postcode' => array( |
| 266 | 'description' => __( 'Postal code.', 'woocommerce' ), |
| 267 | 'type' => 'string', |
| 268 | 'context' => array( 'view', 'edit' ), |
| 269 | ), |
| 270 | 'country' => array( |
| 271 | 'description' => __( 'ISO code of the country.', 'woocommerce' ), |
| 272 | 'type' => 'string', |
| 273 | 'context' => array( 'view', 'edit' ), |
| 274 | ), |
| 275 | 'email' => array( |
| 276 | 'description' => __( 'Email address.', 'woocommerce' ), |
| 277 | 'type' => 'string', |
| 278 | 'format' => 'email', |
| 279 | 'context' => array( 'view', 'edit' ), |
| 280 | ), |
| 281 | 'phone' => array( |
| 282 | 'description' => __( 'Phone number.', 'woocommerce' ), |
| 283 | 'type' => 'string', |
| 284 | 'context' => array( 'view', 'edit' ), |
| 285 | ), |
| 286 | ), |
| 287 | ), |
| 288 | 'shipping' => array( |
| 289 | 'description' => __( 'List of shipping address data.', 'woocommerce' ), |
| 290 | 'type' => 'object', |
| 291 | 'context' => array( 'view', 'edit' ), |
| 292 | 'properties' => array( |
| 293 | 'first_name' => array( |
| 294 | 'description' => __( 'First name.', 'woocommerce' ), |
| 295 | 'type' => 'string', |
| 296 | 'context' => array( 'view', 'edit' ), |
| 297 | ), |
| 298 | 'last_name' => array( |
| 299 | 'description' => __( 'Last name.', 'woocommerce' ), |
| 300 | 'type' => 'string', |
| 301 | 'context' => array( 'view', 'edit' ), |
| 302 | ), |
| 303 | 'company' => array( |
| 304 | 'description' => __( 'Company name.', 'woocommerce' ), |
| 305 | 'type' => 'string', |
| 306 | 'context' => array( 'view', 'edit' ), |
| 307 | ), |
| 308 | 'address_1' => array( |
| 309 | 'description' => __( 'Address line 1', 'woocommerce' ), |
| 310 | 'type' => 'string', |
| 311 | 'context' => array( 'view', 'edit' ), |
| 312 | ), |
| 313 | 'address_2' => array( |
| 314 | 'description' => __( 'Address line 2', 'woocommerce' ), |
| 315 | 'type' => 'string', |
| 316 | 'context' => array( 'view', 'edit' ), |
| 317 | ), |
| 318 | 'city' => array( |
| 319 | 'description' => __( 'City name.', 'woocommerce' ), |
| 320 | 'type' => 'string', |
| 321 | 'context' => array( 'view', 'edit' ), |
| 322 | ), |
| 323 | 'state' => array( |
| 324 | 'description' => __( 'ISO code or name of the state, province or district.', 'woocommerce' ), |
| 325 | 'type' => 'string', |
| 326 | 'context' => array( 'view', 'edit' ), |
| 327 | ), |
| 328 | 'postcode' => array( |
| 329 | 'description' => __( 'Postal code.', 'woocommerce' ), |
| 330 | 'type' => 'string', |
| 331 | 'context' => array( 'view', 'edit' ), |
| 332 | ), |
| 333 | 'country' => array( |
| 334 | 'description' => __( 'ISO code of the country.', 'woocommerce' ), |
| 335 | 'type' => 'string', |
| 336 | 'context' => array( 'view', 'edit' ), |
| 337 | ), |
| 338 | ), |
| 339 | ), |
| 340 | 'is_paying_customer' => array( |
| 341 | 'description' => __( 'Is the customer a paying customer?', 'woocommerce' ), |
| 342 | 'type' => 'bool', |
| 343 | 'context' => array( 'view', 'edit' ), |
| 344 | 'readonly' => true, |
| 345 | ), |
| 346 | 'orders_count' => array( |
| 347 | 'description' => __( 'Quantity of orders made by the customer.', 'woocommerce' ), |
| 348 | 'type' => 'integer', |
| 349 | 'context' => array( 'view', 'edit' ), |
| 350 | 'readonly' => true, |
| 351 | ), |
| 352 | 'total_spent' => array( |
| 353 | 'description' => __( 'Total amount spent.', 'woocommerce' ), |
| 354 | 'type' => 'string', |
| 355 | 'context' => array( 'view', 'edit' ), |
| 356 | 'readonly' => true, |
| 357 | ), |
| 358 | 'avatar_url' => array( |
| 359 | 'description' => __( 'Avatar URL.', 'woocommerce' ), |
| 360 | 'type' => 'string', |
| 361 | 'context' => array( 'view', 'edit' ), |
| 362 | 'readonly' => true, |
| 363 | ), |
| 364 | 'meta_data' => array( |
| 365 | 'description' => __( 'Meta data.', 'woocommerce' ), |
| 366 | 'type' => 'array', |
| 367 | 'context' => array( 'view', 'edit' ), |
| 368 | 'items' => array( |
| 369 | 'type' => 'object', |
| 370 | 'properties' => array( |
| 371 | 'id' => array( |
| 372 | 'description' => __( 'Meta ID.', 'woocommerce' ), |
| 373 | 'type' => 'integer', |
| 374 | 'context' => array( 'view', 'edit' ), |
| 375 | 'readonly' => true, |
| 376 | ), |
| 377 | 'key' => array( |
| 378 | 'description' => __( 'Meta key.', 'woocommerce' ), |
| 379 | 'type' => 'string', |
| 380 | 'context' => array( 'view', 'edit' ), |
| 381 | ), |
| 382 | 'value' => array( |
| 383 | 'description' => __( 'Meta value.', 'woocommerce' ), |
| 384 | 'type' => 'mixed', |
| 385 | 'context' => array( 'view', 'edit' ), |
| 386 | ), |
| 387 | ), |
| 388 | ), |
| 389 | ), |
| 390 | ), |
| 391 | ); |
| 392 | |
| 393 | return $this->add_additional_fields_schema( $schema ); |
| 394 | } |
| 395 | } |
| 396 |