| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_REST_Customers' ) ) : |
| 7 |
|
| 8 |
final class WPSC_REST_Customers { |
| 9 |
|
| 10 |
/** |
| 11 |
* Array of customer slugs which we do not want to expose to REST API |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public static $prevent_data = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Ignore fields to modification and sends directly to client |
| 19 |
* |
| 20 |
* @var array |
| 21 |
*/ |
| 22 |
public static $ignore_modification = array(); |
| 23 |
|
| 24 |
/** |
| 25 |
* Initialize this class |
| 26 |
* |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public static function init() { |
| 30 |
|
| 31 |
add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Load class properties |
| 36 |
* |
| 37 |
* @return void |
| 38 |
*/ |
| 39 |
public static function load_properties() { |
| 40 |
|
| 41 |
$current_user = WPSC_Current_User::$current_user; |
| 42 |
|
| 43 |
// prevent fields to send client side. |
| 44 |
$slugs = array( 'user', 'ticket_count' ); |
| 45 |
if ( ! $current_user->is_agent ) { |
| 46 |
$slugs[] = 'email'; |
| 47 |
} |
| 48 |
self::$prevent_data = apply_filters( |
| 49 |
'wpsc_rest_prevent_customer_data', |
| 50 |
$slugs |
| 51 |
); |
| 52 |
|
| 53 |
// ignore fields to mofication. |
| 54 |
self::$ignore_modification = apply_filters( |
| 55 |
'wpsc_rest_ignore_customer_data_modification', |
| 56 |
array( 'name', 'email' ) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Register routes |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public static function register_routes() { |
| 66 |
|
| 67 |
// list categories. |
| 68 |
register_rest_route( |
| 69 |
'supportcandy/v2', |
| 70 |
'/customers', |
| 71 |
array( |
| 72 |
'methods' => 'GET', |
| 73 |
'callback' => array( __CLASS__, 'get_customers' ), |
| 74 |
'args' => array( |
| 75 |
'per_page' => array( |
| 76 |
'default' => 20, |
| 77 |
'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ), |
| 78 |
), |
| 79 |
'page' => array( |
| 80 |
'default' => 1, |
| 81 |
'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ), |
| 82 |
), |
| 83 |
'search' => array( |
| 84 |
'default' => '', |
| 85 |
'sanitize_callback' => 'sanitize_text_field', |
| 86 |
), |
| 87 |
), |
| 88 |
'permission_callback' => array( __CLASS__, 'check_permission' ), |
| 89 |
), |
| 90 |
); |
| 91 |
|
| 92 |
// list individual category. |
| 93 |
register_rest_route( |
| 94 |
'supportcandy/v2', |
| 95 |
'/customers/(?P<id>\d+)', |
| 96 |
array( |
| 97 |
'methods' => 'GET', |
| 98 |
'callback' => array( __CLASS__, 'get_individual_customer' ), |
| 99 |
'args' => array( |
| 100 |
'id' => array( |
| 101 |
'validate_callback' => array( __CLASS__, 'validate_id' ), |
| 102 |
), |
| 103 |
), |
| 104 |
'permission_callback' => 'is_user_logged_in', |
| 105 |
), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Customer collection |
| 111 |
* |
| 112 |
* @param WP_REST_Request $request - request object. |
| 113 |
* @return WP_Error|WP_REST_Response |
| 114 |
*/ |
| 115 |
public static function get_customers( $request ) { |
| 116 |
|
| 117 |
self::load_properties(); |
| 118 |
$search = $request->get_param( 'search' ); |
| 119 |
$data = WPSC_Customer::find( |
| 120 |
array( |
| 121 |
'items_per_page' => $request->get_param( 'per_page' ), |
| 122 |
'page_no' => $request->get_param( 'page' ), |
| 123 |
'search' => $search, |
| 124 |
) |
| 125 |
); |
| 126 |
foreach ( $data['results'] as $key => $customer ) { |
| 127 |
$data['results'][ $key ] = self::modify_response( $customer ); |
| 128 |
} |
| 129 |
return new WP_REST_Response( $data, 200 ); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Single customer |
| 134 |
* |
| 135 |
* @param WP_REST_Request $request - request object. |
| 136 |
* @return WP_Error|WP_REST_Response |
| 137 |
*/ |
| 138 |
public static function get_individual_customer( $request ) { |
| 139 |
|
| 140 |
self::load_properties(); |
| 141 |
$current_user = WPSC_Current_User::$current_user; |
| 142 |
$customer = new WPSC_Customer( $request->get_param( 'id' ) ); |
| 143 |
|
| 144 |
// if customer is an agent, allowed. |
| 145 |
// if current user is not agent and customer is within allowed customers (considering usergroups), allowed. |
| 146 |
$agent = WPSC_Agent::get_by_customer( $customer ); |
| 147 |
if ( ! $agent->id && ! $current_user->is_agent ) { |
| 148 |
$allowed_customers = apply_filters( 'wpsc_non_agent_user_customers_allowed', array( $customer->id ), $customer ); |
| 149 |
if ( ! in_array( $current_user->customer->id, $allowed_customers ) ) { |
| 150 |
return wp_send_json_error( 'Unauthorized!', 401 ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
$data = self::modify_response( $customer ); |
| 155 |
return new WP_REST_Response( $data, 200 ); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Modify response data appropreate for client side |
| 160 |
* |
| 161 |
* @param WPSC_Customer $customer - response array. |
| 162 |
* @return array |
| 163 |
*/ |
| 164 |
public static function modify_response( $customer ) { |
| 165 |
|
| 166 |
$customer = $customer->to_array(); |
| 167 |
$current_user = WPSC_Current_User::$current_user; |
| 168 |
$tff = get_option( 'wpsc-tff' ); |
| 169 |
|
| 170 |
foreach ( $customer as $slug => $value ) { |
| 171 |
|
| 172 |
// remove prevent ticket data. |
| 173 |
if ( in_array( $slug, self::$prevent_data ) ) { |
| 174 |
unset( $customer[ $slug ] ); |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
// ignore modications. |
| 179 |
if ( in_array( $slug, self::$ignore_modification ) ) { |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
// prevent if custom field is not allowed in my profile or ticket form. Applicable for non-agent. |
| 184 |
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug ); |
| 185 |
if ( ! $current_user->is_agent && $cf && ! $cf->type::$is_default && |
| 186 |
! ( |
| 187 |
$cf->allow_my_profile || |
| 188 |
( $cf->allow_ticket_form && isset( $tff[ $slug ] ) ) |
| 189 |
) |
| 190 |
) { |
| 191 |
unset( $customer[ $slug ] ); |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
// convert has multiple values to array. |
| 196 |
if ( WPSC_Customer::$schema[ $slug ]['has_multiple_val'] ) { |
| 197 |
if ( $value ) { |
| 198 |
$customer[ $slug ] = array_filter( |
| 199 |
array_map( |
| 200 |
fn( $val ) => is_numeric( $val ) ? intval( $val ) : $val, |
| 201 |
explode( '|', $value ) |
| 202 |
) |
| 203 |
); |
| 204 |
} else { |
| 205 |
$customer[ $slug ] = array(); |
| 206 |
} |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
// empty date value. |
| 211 |
if ( $value == '0000-00-00 00:00:00' ) { |
| 212 |
$customer[ $slug ] = ''; |
| 213 |
} |
| 214 |
|
| 215 |
// cast numeric fields into integer. |
| 216 |
if ( is_numeric( $value ) ) { |
| 217 |
$customer[ $slug ] = intval( $value ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return apply_filters( 'wpsc_rest_modify_customer_response', $customer ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Validate id |
| 226 |
* |
| 227 |
* @param string $param - parameter value. |
| 228 |
* @param WP_REST_Request $request - request object. |
| 229 |
* @param string $key - filter key. |
| 230 |
* @return boolean |
| 231 |
*/ |
| 232 |
public static function validate_id( $param, $request, $key ) { |
| 233 |
|
| 234 |
$error = new WP_Error( 'invalid_id', 'Invalid customer id', array( 'status' => 400 ) ); |
| 235 |
$customer = new WPSC_Customer( $param ); |
| 236 |
return $customer->id ? true : $error; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Check permission for these routes |
| 241 |
* |
| 242 |
* @return boolean |
| 243 |
*/ |
| 244 |
public static function check_permission() { |
| 245 |
|
| 246 |
$current_user = WPSC_Current_User::$current_user; |
| 247 |
return $current_user->is_agent; |
| 248 |
} |
| 249 |
} |
| 250 |
endif; |
| 251 |
|
| 252 |
WPSC_REST_Customers::init(); |
| 253 |
|