| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_REST_Agents' ) ) : |
| 7 |
|
| 8 |
final class WPSC_REST_Agents { |
| 9 |
|
| 10 |
/** |
| 11 |
* Prevent data from sending |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public static $prevent_data = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize this class |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
|
| 24 |
add_action( 'init', array( __CLASS__, 'load_prevent_data' ) ); |
| 25 |
add_action( 'wpsc_rest_register_routes', array( __CLASS__, 'register_routes' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Load prevent slugs |
| 30 |
* |
| 31 |
* @return void |
| 32 |
*/ |
| 33 |
public static function load_prevent_data() { |
| 34 |
|
| 35 |
self::$prevent_data = apply_filters( 'wpsc_rest_prevent_agent_data', array( 'user', 'customer', 'role', 'workload', 'unresolved_count', 'is_active' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Register routes |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public static function register_routes() { |
| 44 |
|
| 45 |
// list categories. |
| 46 |
register_rest_route( |
| 47 |
'supportcandy/v2', |
| 48 |
'/agents', |
| 49 |
array( |
| 50 |
'methods' => 'GET', |
| 51 |
'callback' => array( __CLASS__, 'get_agents' ), |
| 52 |
'args' => array( |
| 53 |
'page' => array( |
| 54 |
'default' => 1, |
| 55 |
'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ), |
| 56 |
), |
| 57 |
'per_page' => array( |
| 58 |
'default' => 20, |
| 59 |
'validate_callback' => array( 'WPSC_REST_API', 'validate_integer_value' ), |
| 60 |
), |
| 61 |
'search' => array( |
| 62 |
'default' => '', |
| 63 |
'sanitize_callback' => 'sanitize_text_field', |
| 64 |
), |
| 65 |
), |
| 66 |
'permission_callback' => 'is_user_logged_in', |
| 67 |
), |
| 68 |
); |
| 69 |
|
| 70 |
// list individual category. |
| 71 |
register_rest_route( |
| 72 |
'supportcandy/v2', |
| 73 |
'/agents/(?P<id>\d+)', |
| 74 |
array( |
| 75 |
'methods' => 'GET', |
| 76 |
'callback' => array( __CLASS__, 'get_individual_agent' ), |
| 77 |
'args' => array( |
| 78 |
'id' => array( |
| 79 |
'validate_callback' => array( __CLASS__, 'validate_id' ), |
| 80 |
), |
| 81 |
), |
| 82 |
'permission_callback' => 'is_user_logged_in', |
| 83 |
), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Agengs collection |
| 89 |
* |
| 90 |
* @param WP_REST_Request $request - request object. |
| 91 |
* @return WP_Error|WP_REST_Response |
| 92 |
*/ |
| 93 |
public static function get_agents( $request ) { |
| 94 |
|
| 95 |
$agents = WPSC_Agent::find( |
| 96 |
array( |
| 97 |
'page_no' => $request->get_param( 'page' ), |
| 98 |
'items_per_page' => $request->get_param( 'per_page' ), |
| 99 |
'search' => $request->get_param( 'search' ), |
| 100 |
'meta_query' => array( |
| 101 |
'relation' => 'AND', |
| 102 |
array( |
| 103 |
'slug' => 'is_active', |
| 104 |
'compare' => '=', |
| 105 |
'val' => 1, |
| 106 |
), |
| 107 |
), |
| 108 |
), |
| 109 |
false |
| 110 |
); |
| 111 |
|
| 112 |
if ( $agents['results'] ) { |
| 113 |
$agents['results'] = array_map( |
| 114 |
fn( $agent ) => self::modify_response( $agent ), |
| 115 |
$agents['results'] |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
return new WP_REST_Response( $agents, 200 ); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Single agent |
| 124 |
* |
| 125 |
* @param WP_REST_Request $request - request object. |
| 126 |
* @return WP_Error|WP_REST_Response |
| 127 |
*/ |
| 128 |
public static function get_individual_agent( $request ) { |
| 129 |
|
| 130 |
$agent = new WPSC_Agent( $request->get_param( 'id' ) ); |
| 131 |
$data = self::modify_response( $agent->to_array() ); |
| 132 |
return new WP_REST_Response( $data, 200 ); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Modify response data appropreate for client side |
| 137 |
* |
| 138 |
* @param array $agent - agent array. |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
public static function modify_response( $agent ) { |
| 142 |
|
| 143 |
foreach ( $agent as $key => $value ) { |
| 144 |
|
| 145 |
if ( in_array( $key, self::$prevent_data ) ) { |
| 146 |
unset( $agent[ $key ] ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $agent; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Validate id |
| 155 |
* |
| 156 |
* @param string $param - parameter value. |
| 157 |
* @param WP_REST_Request $request - request object. |
| 158 |
* @param string $key - filter key. |
| 159 |
* @return boolean |
| 160 |
*/ |
| 161 |
public static function validate_id( $param, $request, $key ) { |
| 162 |
|
| 163 |
$error = new WP_Error( 'invalid_id', 'Invalid agent id', array( 'status' => 400 ) ); |
| 164 |
$agent = new WPSC_Agent( $param ); |
| 165 |
return $agent->id ? true : $error; |
| 166 |
} |
| 167 |
} |
| 168 |
endif; |
| 169 |
|
| 170 |
WPSC_REST_Agents::init(); |
| 171 |
|