| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles requests to the /wc/shiprocket/v1/. |
| 4 |
* |
| 5 |
* @author Shiprocket |
| 6 |
* |
| 7 |
* @category API |
| 8 |
* @package Shiprocket |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'Shiprocket_User_List_API' ) ) { |
| 16 |
/** |
| 17 |
* Tracking REST API controller class. |
| 18 |
* |
| 19 |
* @package WooCommerce/API |
| 20 |
* |
| 21 |
* @extends WC_REST_Controller |
| 22 |
*/ |
| 23 |
class Shiprocket_User_List_API extends WC_REST_Controller { |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Endpoint namespace. |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
protected $namespace = 'wc/shiprocket/v1'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Route base. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
protected $rest_base = '/user_list'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Post type. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
protected $post_type = 'users'; |
| 46 |
|
| 47 |
/** |
| 48 |
* Register the routes for order notes. |
| 49 |
*/ |
| 50 |
public function register_routes() { |
| 51 |
register_rest_route( |
| 52 |
$this->namespace, |
| 53 |
'/' . $this->rest_base, |
| 54 |
array( |
| 55 |
array( |
| 56 |
'methods' => WP_REST_Server::READABLE, |
| 57 |
'callback' => array( $this, 'get_customers' ), |
| 58 |
'permission_callback' => array( $this, 'get_items_permissions_check' ), |
| 59 |
'args' => array(), |
| 60 |
), |
| 61 |
'schema' => array( $this, 'get_public_item_schema' ), |
| 62 |
) |
| 63 |
); |
| 64 |
} |
| 65 |
/** |
| 66 |
* Check if a given request has write access. |
| 67 |
* |
| 68 |
* @param WP_REST_Request $request Full details about the request. |
| 69 |
* |
| 70 |
* @return bool|WP_Error |
| 71 |
*/ |
| 72 |
public function get_items_permissions_check( $request ) { |
| 73 |
if ( ! wc_rest_check_user_permissions( 'read' ) ) { |
| 74 |
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) ); |
| 75 |
} |
| 76 |
|
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Gets Customers. |
| 82 |
* |
| 83 |
* @param array|NULL $fields Fields of customer. |
| 84 |
* @param array $filter Filters to be applied. |
| 85 |
* @param int $page Pagination on customers. |
| 86 |
* |
| 87 |
* @return array |
| 88 |
**/ |
| 89 |
public function get_customers( $fields = null, $filter = array(), $page = 1 ) { |
| 90 |
|
| 91 |
$filter['page'] = $page; |
| 92 |
|
| 93 |
$query = $this->query_customers( $filter ); |
| 94 |
|
| 95 |
$customers = array(); |
| 96 |
|
| 97 |
foreach ( $query->get_results() as $user_id ) { |
| 98 |
$customers[] = current( $this->get_customer( $user_id, $fields ) ); |
| 99 |
} |
| 100 |
return array( 'customers' => $customers ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Gets Customer by id. |
| 105 |
* |
| 106 |
* @param int $id ID of customer. |
| 107 |
* @param array|NULL $fields Fields of customer. |
| 108 |
* |
| 109 |
* @return array |
| 110 |
**/ |
| 111 |
public function get_customer( $id, $fields = null ) { |
| 112 |
|
| 113 |
global $wpdb; |
| 114 |
|
| 115 |
$customer = new WC_Customer( $id ); |
| 116 |
$last_order = $customer->get_last_order(); |
| 117 |
$customer_data = array( |
| 118 |
'id' => $customer->get_id(), |
| 119 |
'created_at' => $this->format_datetime( $customer->get_date_created() ? $customer->get_date_created()->getTimestamp() : 0 ), // API gives UTC times. |
| 120 |
'last_update' => $this->format_datetime( $customer->get_date_modified() ? $customer->get_date_modified()->getTimestamp() : 0 ), // API gives UTC times. |
| 121 |
'email' => $customer->get_email(), |
| 122 |
'first_name' => $customer->get_first_name(), |
| 123 |
'last_name' => $customer->get_last_name(), |
| 124 |
'username' => $customer->get_username(), |
| 125 |
'role' => $customer->get_role(), |
| 126 |
'last_order_id' => is_object( $last_order ) ? $last_order->get_id() : null, |
| 127 |
'last_order_date' => is_object( $last_order ) ? $this->format_datetime( $last_order->get_date_created() ? $last_order->get_date_created()->getTimestamp() : 0 ) : null, // API gives UTC times. |
| 128 |
'orders_count' => $customer->get_order_count(), |
| 129 |
'total_spent' => wc_format_decimal( $customer->get_total_spent(), 2 ), |
| 130 |
'avatar_url' => $customer->get_avatar_url(), |
| 131 |
'billing_address' => array( |
| 132 |
'first_name' => $customer->get_billing_first_name(), |
| 133 |
'last_name' => $customer->get_billing_last_name(), |
| 134 |
'company' => $customer->get_billing_company(), |
| 135 |
'address_1' => $customer->get_billing_address_1(), |
| 136 |
'address_2' => $customer->get_billing_address_2(), |
| 137 |
'city' => $customer->get_billing_city(), |
| 138 |
'state' => $customer->get_billing_state(), |
| 139 |
'postcode' => $customer->get_billing_postcode(), |
| 140 |
'country' => $customer->get_billing_country(), |
| 141 |
'email' => $customer->get_billing_email(), |
| 142 |
'phone' => $customer->get_billing_phone(), |
| 143 |
), |
| 144 |
'shipping_address' => array( |
| 145 |
'first_name' => $customer->get_shipping_first_name(), |
| 146 |
'last_name' => $customer->get_shipping_last_name(), |
| 147 |
'company' => $customer->get_shipping_company(), |
| 148 |
'address_1' => $customer->get_shipping_address_1(), |
| 149 |
'address_2' => $customer->get_shipping_address_2(), |
| 150 |
'city' => $customer->get_shipping_city(), |
| 151 |
'state' => $customer->get_shipping_state(), |
| 152 |
'postcode' => $customer->get_shipping_postcode(), |
| 153 |
'country' => $customer->get_shipping_country(), |
| 154 |
), |
| 155 |
); |
| 156 |
|
| 157 |
return array( 'customer' => apply_filters( 'woocommerce_api_customer_response', $customer_data, $customer, $fields ) ); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Formats datetime. |
| 162 |
* |
| 163 |
* @param int $timestamp Timestamp. |
| 164 |
* @param bool $convert_to_utc Convert to UTC. |
| 165 |
* @param bool $convert_to_gmt Convert to GMT. |
| 166 |
* |
| 167 |
* @return DateTime |
| 168 |
**/ |
| 169 |
public function format_datetime( $timestamp, $convert_to_utc = false, $convert_to_gmt = false ) { |
| 170 |
if ( $convert_to_gmt ) { |
| 171 |
if ( is_numeric( $timestamp ) ) { |
| 172 |
$timestamp = gmdate( 'Y-m-d H:i:s', $timestamp ); |
| 173 |
} |
| 174 |
|
| 175 |
$timestamp = get_gmt_from_date( $timestamp ); |
| 176 |
} |
| 177 |
|
| 178 |
if ( $convert_to_utc ) { |
| 179 |
$timezone = new DateTimeZone( wc_timezone_string() ); |
| 180 |
} else { |
| 181 |
$timezone = new DateTimeZone( 'UTC' ); |
| 182 |
} |
| 183 |
|
| 184 |
try { |
| 185 |
if ( is_numeric( $timestamp ) ) { |
| 186 |
$date = new DateTime( "@{$timestamp}" ); |
| 187 |
} else { |
| 188 |
$date = new DateTime( $timestamp, $timezone ); |
| 189 |
} |
| 190 |
|
| 191 |
// convert to UTC by adjusting the time based on the offset of the site's timezone. |
| 192 |
if ( $convert_to_utc ) { |
| 193 |
$date->modify( -1 * $date->getOffset() . ' seconds' ); |
| 194 |
} |
| 195 |
} catch ( Exception $e ) { |
| 196 |
$date = new DateTime( '@0' ); |
| 197 |
} |
| 198 |
|
| 199 |
return $date->format( 'Y-m-d\TH:i:s\Z' ); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Get customers query. |
| 205 |
* |
| 206 |
* @param array $args Array for arguments. |
| 207 |
* |
| 208 |
* @return WP_User_Query |
| 209 |
**/ |
| 210 |
public function query_customers( $args = array() ) { |
| 211 |
|
| 212 |
// default users per page. |
| 213 |
$users_per_page = get_option( 'posts_per_page' ); |
| 214 |
|
| 215 |
// Set base query arguments. |
| 216 |
$query_args = array( |
| 217 |
'fields' => 'ID', |
| 218 |
'role' => 'customer', |
| 219 |
'orderby' => 'registered', |
| 220 |
'number' => $users_per_page, |
| 221 |
); |
| 222 |
|
| 223 |
$query = new WP_User_Query( $query_args ); |
| 224 |
|
| 225 |
// Helper members for pagination headers. |
| 226 |
$query->total_pages = ( -1 === $args['limit'] ) ? 1 : ceil( $query->get_total() / $users_per_page ); |
| 227 |
$query->page = $args['page']; |
| 228 |
|
| 229 |
return $query; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
} |
| 235 |
|