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 0
/
Copy pathwp-teamwork-api.php
680 lines (605 loc) · 15.8 KB
/
wp-teamwork-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
678
679
680
<?php
/**
* Library for accessing the Teamwork API on WordPress
*
* @package WP-API-Libraries\WP-Teamwork-API
*/
/*
* Plugin Name: WP Teamwork API
* Plugin URI: https://wp-api-libraries.com/
* Description: Perform API requests.
* Author: WP API Libraries
* Version: 1.0.0
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-teamwork-ap
* GitHub Branch: master
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'TeamworkAPI' ) ) {
/**
* TeamworkAPI class.
*/
class TeamworkAPI {
/**
* Basic auth username.
*
* @var string
*/
protected $username;
/**
* Basic auth password.
*
* @var string
*/
protected $password;
/**
* BaseAPI Endpoint
*
* @var string
* @access protected
*/
protected $base_uri;
/**
* Route being called.
*
* @var string
*/
protected $route = '';
/**
* Class constructor..
*
* @access public
* @param mixed $base_uri Base URI.
* @param mixed $username Username.
* @param mixed $password Password.
* @return void
*/
public function __construct( $base_uri, $username, $password ) {
$this->base_uri = $base_uri;
$this->username = $username;
$this->password = $password;
}
/**
* 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;
}
$this->args['timeout'] = 20;
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() {
// 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-teamwork-api' ), $code ), $body );
}
return $body;
}
/**
* Set request headers.
*/
protected function set_headers() {
$this->args['headers'] = array(
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode( "{$this->username}:{$this->password}" ),
);
}
/**
* Clear query data.
*/
protected function clear() {
$this->args = array();
$this->query_args = array();
}
/**
* 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 );
}
// AUTHENTICATE.
/**
* authenticate function.
*
* @access public
* @return void
*/
public function authenticate() {
return $this->build_request( '/authenticate.json' )->fetch();
}
// ACCOUNT.
/**
* get_accounts function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_accounts( $args = array() ) {
return $this->build_request( '/accounts.json' )->fetch();
}
// ACTIVITY.
/**
* get_latest_activity function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_latest_activity( $args = array() ) {
return $this->build_request( '/latestActivity.json' )->fetch();
}
/**
* get_projects_latest_activity function.
*
* @access public
* @param mixed $project_id
* @param array $args (default: array())
* @return void
*/
public function get_projects_latest_activity( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '/latestActivity.json' )->fetch();
}
/**
* get_task_activity function.
*
* @access public
* @param mixed $task_id
* @param array $args (default: array())
* @return void
*/
public function get_task_activity( $task_id, $args = array() ) {
return $this->build_request( '/yoursite/tasks/' . $task_id . '/activity.json' )->fetch();
}
/**
* get_task_audit_history function.
*
* @access public
* @param mixed $task_id
* @param array $args (default: array())
* @return void
*/
public function get_task_audit_history( $task_id, $args = array() ) {
return $this->build_request( '/tasks/' . $task_id . '/audit.json' )->fetch();
}
/**
* delete_activity function.
*
* @access public
* @param mixed $activity_id
* @return void
*/
public function delete_activity( $activity_id ) {
return $this->build_request( '/activity/' . $activity_id . '.json', 'DELETE' )->fetch();
}
// PROJECTS.
/**
* Get Projects
*
* @docs https://developer.teamwork.com/projects/projects/retrieve-all-projects
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_projects( $args = array() ) {
return $this->build_request( '/projects.json', $args )->fetch();
}
/**
* get_project function.
*
* @docs https://developer.teamwork.com/projects/projects/retrieve-a-single-project
* @access public
* @param mixed $project_id
* @param array $args (default: array())
* @return void
*/
public function get_project( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '.json', $args )->fetch();
}
/**
* get_company_projects function.
*
* @access public
* @param mixed $company_id
* @param array $args (default: array())
* @return void
*/
public function get_company_projects( $company_id, $args = array() ) {
return $this->build_request( '/companies/' . $company_id . '/projects.json', $args )->fetch();
}
/**
* get_starred_projects function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_starred_projects( $args = array() ) {
return $this->build_request( '/projects/starred.json', $args )->fetch();
}
/**
* create_project function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function create_project( $args = array() ) {
return $this->build_request( '/projects.json', $args, 'POST' )->fetch();
}
/**
* set_project_rates function.
*
* @access public
* @param mixed $project_id
* @param array $args (default: array())
* @return void
*/
public function set_project_rates( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . 'rates.json', $args, 'POST' )->fetch();
}
/**
* update_project function.
*
* @access public
* @param mixed $project_id
* @param array $args (default: array())
* @return void
*/
public function update_project( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '.json', $args, 'PUT' )->fetch();
}
/**
* star_project function.
*
* @access public
* @param mixed $project_id
* @return void
*/
public function star_project( $project_id ) {
return $this->build_request( '/projects/' . $project_id . '/star.json', 'PUT' )->fetch();
}
/**
* unstar_project function.
*
* @access public
* @param mixed $project_id
* @return void
*/
public function unstar_project( $project_id ) {
return $this->build_request( '/projects/' . $project_id . '/unstar.json', 'PUT' )->fetch();
}
/**
* delete_project function.
*
* @access public
* @param mixed $project_id
* @return void
*/
public function delete_project( $project_id ) {
return $this->build_request( '/projects/' . $project_id . '/unstar.json', 'DELETE' )->fetch();
}
// PROJECT CATEGORIES.
/**
* get_project_categories function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_project_categories( $args ) {
return $this->build_request( '/projectsCategories.json', $args )->fetch();
}
/**
* get_project_category_by_id function.
*
* @access public
* @param mixed $project_category_id
* @return void
*/
public function get_project_category_by_id( $project_category_id ) {
return $this->build_request( '/projectsCategories/' . $project_category_id . '.json' )->fetch();
}
/**
* get_tasks_by_project_categories function.
*
* @access public
* @param mixed $project_category_id
* @return void
*/
public function get_tasks_by_project_categories( $project_category_id ) {
return $this->build_request( '/projectsCategories/' . $project_category_id . '/tasks.json' )->fetch();
}
/**
* add_project_category function.
*
* @access public
* @param mixed $args
* @return void
*/
public function add_project_category( $args ) {
return $this->build_request( '/projectsCategories.json', $args, 'POST' )->fetch();
}
/**
* update_project_category function.
*
* @access public
* @param mixed $project_category_id
* @param mixed $args
* @return void
*/
public function update_project_category( $project_category_id, $args ) {
return $this->build_request( '/projectsCategories/' . $project_category_id . '.json', $args, 'PUT' )->fetch();
}
/**
* delete_project_category function.
*
* @access public
* @param mixed $project_category_id
* @return void
*/
public function delete_project_category( $project_category_id ) {
return $this->build_request( '/projectsCategories/' . $project_category_id . '.json', 'DELETE' )->fetch();
}
// PROJECT OWNER.
// SITE OWNER.
// MILESTONES.
/**
* get_milestones function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_milestones( $args ) {
return $this->build_request( '/milestones.json' )->fetch();
}
/**
* get_projects_milestones function.
*
* @access public
* @param mixed $project_id
* @param mixed $args
* @return void
*/
public function get_projects_milestones( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '/milestones.json' )->fetch();
}
// TASK LISTS.
// TASKS.
public function get_tasks( $args ) {
}
public function get_projects_tasks( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '/tasks.json' )->fetch();
}
// TASK REMINDERS.
// COMPANIES.
/**
* get_companies function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_companies( $args = array() ) {
return $this->build_request( '/companies.json', $args )->fetch();
}
/**
* get_project_companies function.
*
* @access public
* @param mixed $project_id
* @param array $args (default: array())
* @return void
*/
public function get_project_companies( $project_id, $args = array() ) {
return $this->build_request( '/projects/' . $project_id . '/companies.json', $args )->fetch();
}
/**
* get_company function.
*
* @access public
* @param mixed $company_id
* @return void
*/
public function get_company( $company_id ) {
return $this->build_request( '/companies/' . $company_id . '.json' )->fetch();
}
/**
* create_company function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function create_company( $args = array() ) {
return $this->build_request( '/companies.json', $args, 'POST' )->fetch();
}
/**
* update_company function.
*
* @access public
* @param mixed $company_id
* @param array $args (default: array())
* @return void
*/
public function update_company( $company_id, $args = array() ) {
return $this->build_request( '/companies/' . $company_id . '.json', $args, 'PUT' )->fetch();
}
/**
* delete_company function.
*
* @access public
* @param mixed $company_id
* @return void
*/
public function delete_company( $company_id ) {
return $this->build_request( '/companies/' . $company_id . '.json', 'DELETE' )->fetch();
}
// PEOPLE.
/**
* get_people function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_people( $args = array() ) {
return $this->build_request( '/people.json', $args )->fetch();
}
/**
* get_people_available_for_calendar_event function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_people_available_for_calendar_event( $args = array() ) {
}
public function get_people_avail_for_message( $args = array() ) {
}
/**
* add_user function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function add_user( $args = array() ) {
return $this->build_request( '/people.json', $args, 'POST' )->fetch();
}
// PEOPLE STATUS.
// CALENDAR EVENT.
// FILES.
/**
* get_project_files function.
*
* @access public
* @param mixed $project_id
* @return void
*/
public function get_project_files( $project_id ) {
return $this->build_request( '/projects/' . $project_id . '/files.json' )->fetch();
}
/**
* get_file function.
*
* @access public
* @param mixed $file_id
* @return void
*/
public function get_file( $file_id ) {
return $this->build_request( '/files/' . $file_id . '.json' )->fetch();
}
// FILE CATEGORIES.
// NOTEBOOKS.
// NOTEBOOK CATEGORIES.
// LINKS.
// LINK CATEGORIES.
// CLOCK IN/ CLOCK OUT.
// TIME TRACKING.
/**
* get_time_entries function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_time_entries( $args ) {
return $this->build_request( '/time_entries.json', $args )->fetch();
}
// MESSAGES.
// MESSAGE REPLIES.
// MESSAGE CATEGORIES.
// COMMENTS.
// INVOICES.
// EXPENSES.
// RISKS.
// BOARDS.
// PORTFOLIO BOARDS.
// FILE UPLOADING.
// PROJECT UPDATES.
// PROJECT ROLES.
// PERMISSIONS.
// LIKES.
// TAGS.
// PROJECT EMAIL ADDRESSES.
// SEARCH.
// WEBHOOKS.
// WORKLOADS.
/**
* get_workload function.
*
* @access public
* @param mixed $args
* @return void
*/
public function get_workload( $args = array() ) {
return $this->build_request( '/workload.json', $args )->fetch();
}
// TRASHCANS.
/**
* get_project_trash function.
*
* @access public
* @param mixed $project_id
* @return void
*/
public function get_project_trash( $project_id ) {
return $this->build_request( '/trashcan/projects/' . $project_id . '.json' )->fetch();
}
/**
* restore_item_from_trash function.
*
* @access public
* @param mixed $resource
* @param mixed $resource_id
* @return void
*/
public function restore_item_from_trash( $resource, $resource_id ) {
return $this->build_request( '/trashcan/' . $resource . '/' . $resource_id . '/restore.json' )->fetch();
}
// TIMEZONES.
/**
* get_timezones function.
*
* @access public
* @param array $args (default: array())
* @return void
*/
public function get_timezones( $args = array() ) {
return $this->build_request( '/timezones.json', $args )->fetch();
}
}
}