This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-aircall-api.php
677 lines (594 loc) · 15.6 KB
/
wp-aircall-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
<?php
/**
* WP-Aircall-API
*
* @package WP-API-Libraries\WP-Aircall-API
*/
/*
* Plugin Name: WP Aircall API
* Plugin URI: https://github.com/wp-api-libraries/wp-aircall-api
* Description: Perform API requests to Aircall in WordPress.
* Author: imFORZA
* Version: 1.0.0
* Author URI: https://www.imforza.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-aircall-api
* GitHub Branch: master
*/
/* Exit if accessed directly. */
if ( ! defined( 'ABSPATH' ) ) {
exit; }
/* Check if class exists. */
if ( ! class_exists( 'AircallAPI' ) ) {
/**
* Aircall API Class.
*/
class AircallAPI {
/**
* API ID.
*
* @var string
*/
protected $api_id;
/**
* API Token.
*
* @var string
*/
protected $api_token;
/**
* is_debug
*
* @var mixed
* @access protected
*/
protected $is_debug;
/**
* BaseAPI Endpoint
*
* @var string
* @access protected
*/
protected $base_uri = 'https://api.aircall.io/v1/';
/**
* Class constructor.
*
* @param string $api_id API ID.
* @param string $api_token API Token.
*/
public function __construct( $api_id, $api_token, $is_debug = false ) {
$this->api_id = $api_id;
$this->api_token = $api_token;
$this->is_debug = $is_debug;
}
/**
* Prepares API request.
*
* @param string $route API route to make the call to.
* @param array $args Arguments to pass into the API call.
* @param array $method HTTP Method to use for request.
* @return self Returns an instance of itself so it can be chained to the fetch method.
*/
protected function build_request( $route, $args = array(), $method = 'GET' ) {
// Start building query.
$this->set_headers();
$this->args['method'] = $method;
$this->route = $route;
// Generate query string for GET requests.
if ( 'GET' === $method ) {
$this->route = add_query_arg( array_filter( $args ), $route );
} elseif ( 'application/json' === $this->args['headers']['Content-Type'] ) {
$this->args['body'] = wp_json_encode( $args );
} else {
$this->args['body'] = $args;
}
return $this;
}
/**
* Fetch the request from the API.
*
* @access private
* @return array|WP_Error Request results or WP_Error on request failure.
*/
protected function fetch() {
// pp( $this->base_uri . $this->route, $this->args );
// Make the request.
$response = wp_remote_request( $this->base_uri . $this->route, $this->args );
// Retrieve Status code & body.
$code = wp_remote_retrieve_response_code( $response );
$body = json_decode( wp_remote_retrieve_body( $response ) );
$this->clear();
// Return WP_Error if request is not successful.
if ( ! $this->is_status_ok( $code ) ) {
return new WP_Error( 'response-error', sprintf( __( 'Status: %d', 'wp-aircall-api' ), $code ), $body );
}
return $body;
}
/**
* Check if HTTP status code is a success.
*
* @param int $code HTTP status code.
* @return boolean True if status is within valid range.
*/
protected function is_status_ok( $code ) {
return ( 200 <= $code && 300 > $code );
}
/**
* set_headers function.
*
* @access protected
* @return void
*/
protected function set_headers() {
$this->args['headers'] = array(
'Authorization' => 'Basic ' . base64_encode( $this->api_id . ':' . $this->api_token ),
'Content-Type' => 'application/json',
);
}
/**
* Private wrapper function (for simpler coding), prepares the request then fetches it.
*
* @param [type] $route [description]
* @param array $body [description]
* @param bool $method The method of the request.
* @return [type] [description]
*/
private function run( $route, $body = array(), $method = 'GET' ) {
return $this->build_request( $route, $body, $method )->fetch();
}
/**
* Clear query data.
*/
protected function clear() {
$this->args = array();
}
/* RATE LIMITING. */
/**
* get_rate_limit function.
*
* @access public
* @return void
*/
public function get_api_limit_header() {
$rate_limit = $_SERVER['X-AircallApi-Limit'];
return $rate_limit;
}
/**
* get_api_remaining_header function.
*
* @access public
* @return void
*/
public function get_api_remaining_header() {
$remaining = $_SERVER['X-AircallApi-Remaining'];
return $remaining;
}
/**
* get_api_reset_header function.
*
* @access public
* @return void
*/
public function get_api_reset_header() {
$reset_header = $_SERVER['X-AircallApi-Reset'];
return $reset_header;
}
/* AUTH. */
/**
* An ancient tradition in the world of establishing communications.
*
* @return [type] [description]
*/
public function ping() {
return $this->run( 'ping' );
}
/* COMPANY. */
public function get_company() {
return $this->run( 'company' );
}
/* USERS. */
/**
* Get users.
*
* A list of users
*
* @return [type] [description]
*/
public function get_users() {
return $this->run( 'users' );
}
/**
* Get a specific user.
*
* @param [type] $user_id [description]
* @return [type] [description]
*/
public function get_user( $user_id ) {
return $this->run( "users/$user_id" );
}
/**
* Update data for a specific user.
*
* @param [type] $user_id [description]
* @param [type] $data [description]
* @return [type] [description]
*/
public function update_user( $user_id, $data ) {
return $this->run( "users/$user_id", $data, 'PUT' );
}
/* TEAMS. */
/**
* Get a list of teams.
*
* @return [type] [description]
*/
public function get_teams() {
return $this->run( 'teams' );
}
/**
* Get a specific team.
*
* @param [type] $team_id [description]
* @return [type] [description]
*/
public function get_team( $team_id ) {
return $this->run( "teams/$team_id" );
}
/**
* Add a user to a team.
*
* @param [type] $user_id [description]
* @param array $user_obj The User object.
* @return object The results.
*/
public function add_user_to_team( $team_id, $user_obj ) {
return $this->run( "teams/$team_id/users", $user_obj, 'POST' );
}
/**
* Remove a user from a team.
*
* Huh, not 100% sure how this is used, their API is a little vague on whether
* you pass an object or reference it by ID.
*
* @param [type] $user_id [description]
* @return [type] [description]
*/
public function remove_user_from_team( $user_id ) {
}
/* NUMBERS. */
/**
* Get a list of numbers.
*
* @return object A pagination object (with results).
*/
public function get_numbers() {
return $this->run( 'numbers' );
}
/**
* Get a specific number.
*
* @param int/string $number_id The ID of the number.
* @return object The number.
*/
public function get_number( $number_id ) {
return $this->run( "numbers/$number_id" );
}
/**
* Update a number.
*
* @param int/string $number_id The ID of the number.
* @param array $number_obj The number object.
* @return object The response.
*/
public function update_number( $number_id, $number_obj ) {
return $this->run( "numbers/$number_id", $number_obj, 'PUT' );
}
/* CALL ROUTES. */
public function get_calls( $page = 1, $per_page = 50 ) {
$args = array(
'page' => $page,
'per_page' => $per_page,
);
return $this->run( 'calls', $args );
}
/**
* search_calls function.
*
* @access public
* @param array $params (default: array())
* @return void
*/
public function search_calls( $params = array() ) {
return $this->run( 'calls/search', $params );
}
/**
* get_call function.
*
* @access public
* @param mixed $call_id
* @return void
*/
public function get_call( $call_id ) {
return $this->run( "calls/$call_id" );
}
/**
* Transfer a call to a user.
*
* @param [type] $call_id [description]
* @param [type] $user_id [description]
* @return [type] [description]
*/
public function transfer_call( $call_id, $user_id ) {
return $this->run( "calls/$call_id/transfers", array( 'user_id' => $user_id ), 'POST' );
}
/**
* See a link in the requests body.
*
* @param [type] $call_id [description]
* @return [type] [description]
*/
public function display_call_link( $call_id ) {
return $this->run( "calls/$call_id/link" );
}
/**
* Get the custom data for a call.
*
* @param int $call_id The ID of the call.
* @param array $args Additional arguments to pass.
*/
public function set_custom_call_data( $call_id, $args ) {
return $this->run( "calls/$call_id/metadata", $args, 'POST' );
}
/**
* Delete a recording.
*
* @param int $call_id The ID of the call.
* @return object Hopefully an empty 200 OK response.
*/
public function delete_recording( $call_id ) {
return $this->run( "calls/$call_id/recording", array(), 'DELETE' );
}
/**
* Delete a voicemail.
*
* @param int $call_id The ID of the call.
* @return object Hopefully an empty 200 OK response.
*/
public function delete_voicemail( $call_id ) {
return $this->run( "calls/$call_id/voicemail", array(), 'DELETE' );
}
/* CONTACTS. */
/**
* Get a list of contacts.
*
* @param int $page (Default: 1) The first page to start at.
* @param int $per_page (Default: 50) The number of results to display per page.
* @return object Hopefully a pagination object with results.
*/
public function get_contacts( $page = 1, $per_page = 50 ) {
$args = array(
'page' => $page,
'per_page' => $per_page,
);
return $this->run( 'contacts', $args );
}
/**
* Get a specific contact.
*
* @param int $contact_id The ID of the contat.
* @return object The contact.
*/
public function get_contact( $contact_id ) {
return $this->run( "contacts/$contact_id" );
}
/**
* Search through contacts.
*
* $params accepts these (all optional) key => vals.
* page
* Pagination for list of objects 1
* per_page
* Number of objects fetched per request 20
* order
* Reorder entries per order_by value, asc or desc asc
* order_by
* Set the order field (only for contacts), created_at or updated_at created_at
* from
* Set a minimal creation date for objects (UNIX timestamp) (none)
* to
* Set a maximal creation date for objects (UNIX timestamp) (none)
*
* @param array $params [desciption]
* @return [type] [description]
*/
public function search_contacts( $params = array() ) {
return $this->run( 'contacts/search', $params );
}
/**
* Create a contact.
*
* @param array $contact The contact.
* @return object The hopefully created contact.
*/
public function add_contact( $contact ) {
return $this->run( 'contacts', $contact, 'POST' );
}
/**
* Get a list of webhooks.
*
* @return array
*/
public function get_webhooks() {
return $this->run( 'webhooks' );
}
/**
* get_webhook function.
*
* @access public
* @param mixed $webhook_id
* @return void
*/
public function get_webhook( $webhook_id ) {
return $this->run( "webhooks/$webhook_id" );
}
/**
* create_webhook function.
*
* @access public
* @param mixed $webhook
* @return void
*/
public function create_webhook( $webhook ) {
return $this->run( 'webhooks', $webhook, 'POST' );
}
/**
* update_webhook function.
*
* @access public
* @param mixed $webhook
* @param mixed $webhook_id
* @return void
*/
public function update_webhook( $webhook_id, $webhook ) {
return $this->run( "webhooks/$webhook_id", $webhook, 'PUT' );
}
/**
* delete_webhook function.
*
* @access public
* @param mixed $webhook_id
* @return void
*/
public function delete_webhook( $webhook_id ) {
return $this->run( "webooks/$webhook_id", array(), 'DELETE' );
}
/**
* Update a contact.
*
* $obj = array(
* 'emails' => array(
* array(
* 'label' => 'Work',
* 'value' => '[email protected]'
* )
* )
* );
*
* @param [type] $contact_id [description]
* @param array $contact [description]
* @return [type] [description]
*/
public function update_contact( $contact_id, $contact = array() ) {
return $this->run( "contacts/$contact_id", $contact, 'POST' );
}
/**
* delete_contact function.
*
* @access public
* @param mixed $contact_id
* @return void
*/
public function delete_contact( $contact_id ) {
return $this->run( "contacts/$contact_id", array(), 'DELETE' );
}
/**
* add_contact_number function.
*
* @access public
* @param mixed $contact_id
* @param mixed $value
* @param string $label (default: 'Alternate')
* @return void
*/
public function add_contact_number( $contact_id, $value, $label = 'Alternate' ) {
$number = array(
'label' => $label,
'value' => $value,
);
return $this->run( "contacts/$contact_id/phone_details/", $number, 'POST' );
}
/**
* update_contact_number function.
*
* @access public
* @param mixed $contact_id
* @param mixed $phone_id
* @param mixed $label (default: null)
* @param mixed $value (default: null)
* @return void
*/
public function update_contact_number( $contact_id, $phone_id, $label = null, $value = null ) {
if ( $label === $value && $value === null ) {
return new WP_Error( 'invalid-data', __( 'You must submit either $label or $value.', 'wp-aircall-api' ) );
}
$args = array();
if ( $value !== null ) {
$args['value'] = $value;
}
if ( $label !== null ) {
$args['label'] = $label;
}
return $this->run( "contacts/$contact_id/phone_details/$phone_id", $args, 'PUT' );
}
/**
* delete_contact_number function.
*
* @access public
* @param mixed $contact_id
* @param mixed $phone_id
* @return void
*/
public function delete_contact_number( $contact_id, $phone_id ) {
return $this->run( "contacts/$contact_id/phone_details/$phone_id", array(), 'DELETE' );
}
/**
* add_contact_email function.
*
* @access public
* @param mixed $contact_id
* @param mixed $value
* @param string $label (default: 'Alternate')
* @return void
*/
public function add_contact_email( $contact_id, $value, $label = 'Alternate' ) {
$email = array(
'label' => $label,
'value' => $value,
);
return $this->run( "contacts/$contact_id/email_details/", $email, 'POST' );
}
/**
* update_contact_email function.
*
* @access public
* @param mixed $contact_id
* @param mixed $email_id
* @param mixed $label (default: null)
* @param mixed $value (default: null)
* @return void
*/
public function update_contact_email( $contact_id, $email_id, $label = null, $value = null ) {
if ( $label === $value && $value === null ) {
return new WP_Error( 'invalid-data', __( 'You must submit either $label or $value.', 'wp-aircall-api' ) );
}
$args = array();
if ( $value !== null ) {
$args['value'] = $value;
}
if ( $label !== null ) {
$args['label'] = $label;
}
return $this->run( "contacts/$contact_id/email_details/$email_id", $args, 'PUT' );
}
/**
* delete_contact_email function.
*
* @access public
* @param mixed $contact_id
* @param mixed $email_id
* @return void
*/
public function delete_contact_email( $contact_id, $email_id ) {
return $this->run( "contacts/$contact_id/email_details/$email_id", array(), 'DELETE' );
}
}
}