PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 5.6.2 All 33 releases
automatorwp / integrations / clickup / includes / functions.php

functions.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 6.0.2, at integrations/clickup/includes/functions.php

813 lines 17.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions
4 *
5 * @package AutomatorWP\ClickUp\Functions
6 * @since 1.0.0
7 */
8 // Exit if accessed directly
9 if( !defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Helper function to get the ClickUp url
13 *
14 * @since 1.0.0
15 *
16 * @return string
17 */
18 function automatorwp_clickup_get_url() {
19
20 return 'https://api.clickup.com/api/v2';
21
22 }
23
24 /**
25 * Helper function to get the ClickUp API parameters
26 *
27 * @since 1.0.0
28 *
29 * @return array|false
30 */
31 function automatorwp_clickup_get_api() {
32
33 $url = automatorwp_clickup_get_url();
34 $token = automatorwp_clickup_get_option( 'token', '' );
35
36 if( empty( $token ) ) {
37 return false;
38 }
39
40 return array(
41 'url' => $url,
42 'token' => $token,
43 );
44
45 }
46
47 /**
48 * Get teams from ClickUp
49 *
50 * @since 1.0.0
51 *
52 * @return array
53 */
54 function automatorwp_clickup_get_teams( ) {
55
56 $teams = array();
57
58 $api = automatorwp_clickup_get_api();
59
60 if( ! $api ) {
61 return $options;
62 }
63
64 $response = wp_remote_get( $api['url'] . '/team', array(
65 'headers' => array(
66 'Authorization' => $api['token'],
67 'Accept' => 'application/json',
68 'Content-Type' => 'application/json'
69 )
70 ) );
71
72 $response = json_decode( wp_remote_retrieve_body( $response ), true );
73
74 foreach ( $response['teams'] as $team ){
75
76 $teams[] = array(
77 'id' => $team['id'],
78 'name' => $team['name'],
79 );
80
81 }
82
83 return $teams;
84
85 }
86
87 /**
88 * Get team from ClickUp
89 *
90 * @since 1.0.0
91 *
92 * @param stdClass $field
93 *
94 * @return array
95 */
96 function automatorwp_clickup_options_cb_team( $field ) {
97
98 // Setup vars
99 $value = $field->escaped_value;
100 $none_value = 'any';
101 $none_label = __( 'any team', 'automatorwp' );
102 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
103
104 if( ! empty( $value ) ) {
105 if( ! is_array( $value ) ) {
106 $value = array( $value );
107 }
108
109 foreach( $value as $team_id ) {
110
111 // Skip option none
112 if( $team_id === $none_value ) {
113 continue;
114 }
115
116 $options[$team_id] = automatorwp_clickup_get_team_name( $team_id );
117 }
118 }
119
120 return $options;
121
122 }
123
124 /**
125 * Get the team name
126 *
127 * @since 1.0.0
128 *
129 * @param string $team_id
130 *
131 * @return array
132 */
133 function automatorwp_clickup_get_team_name( $team_id ) {
134
135 $api = automatorwp_clickup_get_api();
136
137 if( ! $api ) {
138 return $options;
139 }
140
141 $response = wp_remote_get( 'https://api.clickup.com/api/v2/team/' . $team_id, array(
142 'headers' => array(
143 'Authorization' => $api['token'],
144 ),
145 ) );
146
147 $response = json_decode( wp_remote_retrieve_body( $response ), true );
148
149 if ( isset ( $response['error']['code'] ) === 404 || !isset ( $response['team']['name'] ) ){
150 return;
151 }
152
153 return $response['team']['name'];
154 }
155
156 /**
157 * Get spaces from ClickUp
158 *
159 * @since 1.0.0
160 *
161 * @return array
162 */
163 function automatorwp_clickup_get_spaces( $team_id ) {
164
165 $spaces = array();
166
167 $api = automatorwp_clickup_get_api();
168
169 if( ! $api ) {
170 return $options;
171 }
172
173 //$response = wp_remote_get( $api['url'] . '/team/9008180486/space', array(
174 $response = wp_remote_get( $api['url'] . '/team/' . $team_id . '/space', array(
175 'headers' => array(
176 'Authorization' => $api['token'],
177 'Accept' => 'application/json',
178 'Content-Type' => 'application/json'
179 )
180 ) );
181
182 $response = json_decode( wp_remote_retrieve_body( $response ), true );
183
184 foreach ( $response['spaces'] as $space ){
185
186 $spaces[] = array(
187 'id' => $space['id'],
188 'name' => $space['name'],
189 );
190
191 }
192
193 return $spaces;
194
195 }
196
197 /**
198 * Get space from ClickUp
199 *
200 * @since 1.0.0
201 *
202 * @param stdClass $field
203 *
204 * @return array
205 */
206 function automatorwp_clickup_options_cb_space( $field ) {
207
208 // Setup vars
209 $value = $field->escaped_value;
210 $none_value = 'any';
211 $none_label = __( 'any space', 'automatorwp' );
212 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
213
214 $team_id = ct_get_object_meta( $field->object_id, 'team', true );
215
216 if( ! empty( $value ) ) {
217 if( ! is_array( $value ) ) {
218 $value = array( $value );
219 }
220
221 foreach( $value as $space_id ) {
222
223 // Skip option none
224 if( $space_id === $none_value ) {
225 continue;
226 }
227
228 $options[$space_id] = automatorwp_clickup_get_space_name( $space_id );
229 }
230 }
231
232 return $options;
233
234 }
235
236 /**
237 * Get the space name
238 *
239 * @since 1.0.0
240 *
241 * @param string $space_id
242 *
243 * @return array
244 */
245 function automatorwp_clickup_get_space_name( $space_id ) {
246
247 $api = automatorwp_clickup_get_api();
248
249 if( ! $api ) {
250 return $options;
251 }
252
253 $response = wp_remote_get( 'https://api.clickup.com/api/v2/space/' . $space_id, array(
254 'headers' => array(
255 'Authorization' => $api['token'],
256 ),
257 ) );
258
259 $response = json_decode( wp_remote_retrieve_body( $response ), true );
260
261 if ( isset ( $response['error']['code'] ) === 404 || !isset ( $response['name'] ) ){
262 return;
263 }
264
265 return $response['name'];
266 }
267
268 /**
269 * Get folders from ClickUp
270 *
271 * @since 1.0.0
272 *
273 * @return array
274 */
275 function automatorwp_clickup_get_folders( $space_id ) {
276
277 $folders = array();
278
279 $folders[] = array(
280 'id' => 0,
281 'name' => __( 'Folderless lists', 'automatorwp' ),
282 );
283
284 $api = automatorwp_clickup_get_api();
285
286 if( ! $api ) {
287 return $options;
288 }
289
290 $response = wp_remote_get( $api['url'] . '/space/'. $space_id . '/folder', array(
291 'headers' => array(
292 'Authorization' => $api['token'],
293 'Accept' => 'application/json',
294 'Content-Type' => 'application/json'
295 )
296 ) );
297
298 $response = json_decode( wp_remote_retrieve_body( $response ), true );
299
300 if ( empty( $response['folders'] ) ) {
301 return $folders;
302 }
303
304 foreach ( $response['folders'] as $folder ) {
305
306 $folders[] = array(
307 'id' => $folder['id'],
308 'name' => $folder['name'],
309 );
310
311 }
312
313 return $folders;
314
315 }
316
317 /**
318 * Get folder from ClickUp
319 *
320 * @since 1.0.0
321 *
322 * @param stdClass $field
323 *
324 * @return array
325 */
326 function automatorwp_clickup_options_cb_folder( $field ) {
327
328 // Setup vars
329 $value = $field->escaped_value;
330 $none_value = 'any';
331 $none_label = __( 'any folder', 'automatorwp' );
332 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
333
334 $space_id = ct_get_object_meta( $field->object_id, 'space', true );
335
336 if( ! empty( $value ) ) {
337 if( ! is_array( $value ) ) {
338 $value = array( $value );
339 }
340
341 foreach( $value as $folder_id ) {
342
343 // Skip option none
344 if( $folder_id === $none_value ) {
345 continue;
346 }
347
348 $options[$folder_id] = automatorwp_clickup_get_folder_name( $folder_id );
349 }
350 }
351
352 return $options;
353
354 }
355
356 /**
357 * Get the folder name
358 *
359 * @since 1.0.0
360 *
361 * @param string $folder_id
362 *
363 * @return array
364 */
365 function automatorwp_clickup_get_folder_name( $folder_id ) {
366
367 $api = automatorwp_clickup_get_api();
368
369 if( ! $api ) {
370 return $options;
371 }
372
373 if ( $folder_id === 0 ) {
374 return __( 'Folderless lists', 'automatorwp' );
375 }
376
377 $response = wp_remote_get( 'https://api.clickup.com/api/v2/folder/' . $folder_id, array(
378 'headers' => array(
379 'Authorization' => $api['token'],
380 ),
381 ) );
382
383 $response = json_decode( wp_remote_retrieve_body( $response ), true );
384
385 if ( isset ( $response['error']['code'] ) === 404 ){
386 return;
387 }
388
389 return $response['name'];
390 }
391
392
393 /**
394 * Get lists from ClickUp
395 *
396 * @since 1.0.0
397 *
398 * @return array
399 */
400 function automatorwp_clickup_get_lists( $folder_id ) {
401
402 $lists = array();
403
404 $api = automatorwp_clickup_get_api();
405
406 if( ! $api ) {
407 return $options;
408 }
409
410 $response = wp_remote_get( $api['url'] . '/folder/'. $folder_id . '/list', array(
411 'headers' => array(
412 'Authorization' => $api['token'],
413 )
414 ) );
415
416 $response = json_decode( wp_remote_retrieve_body( $response ), true );
417
418 if ( empty( $response['lists'] ) ) {
419 return $lists;
420 }
421
422 foreach ( $response['lists'] as $list ) {
423
424 $lists[] = array(
425 'id' => $list['id'],
426 'name' => $list['name'],
427 );
428
429 }
430
431 return $lists;
432
433 }
434
435 /**
436 * Get list from ClickUp
437 *
438 * @since 1.0.0
439 *
440 * @param stdClass $field
441 *
442 * @return array
443 */
444 function automatorwp_clickup_options_cb_list( $field ) {
445
446 // Setup vars
447 $value = $field->escaped_value;
448 $none_value = 'any';
449 $none_label = __( 'any list', 'automatorwp' );
450 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
451
452 $folder_id = ct_get_object_meta( $field->object_id, 'folder', true );
453
454 if( ! empty( $value ) ) {
455 if( ! is_array( $value ) ) {
456 $value = array( $value );
457 }
458
459 foreach( $value as $list_id ) {
460
461 // Skip option none
462 if( $list_id === $none_value ) {
463 continue;
464 }
465
466 $options[$list_id] = automatorwp_clickup_get_list_name( $list_id );
467 }
468 }
469
470 return $options;
471
472 }
473
474 /**
475 * Get the list name
476 *
477 * @since 1.0.0
478 *
479 * @param string $list_id
480 *
481 * @return array
482 */
483 function automatorwp_clickup_get_list_name( $list_id ) {
484
485 $api = automatorwp_clickup_get_api();
486
487 if( ! $api ) {
488 return $options;
489 }
490
491 $response = wp_remote_get( 'https://api.clickup.com/api/v2/list/' . $list_id, array(
492 'headers' => array(
493 'Authorization' => $api['token'],
494 ),
495 ) );
496
497 $response = json_decode( wp_remote_retrieve_body( $response ), true );
498
499 if ( isset ( $response['error']['code'] ) === 404 ){
500 return;
501 }
502
503 return $response['name'];
504 }
505
506
507 /**
508 * Get tasks from ClickUp
509 *
510 * @since 1.0.0
511 *
512 * @return array
513 */
514 function automatorwp_clickup_get_tasks( $list_id ) {
515
516 $tasks = array();
517
518 $api = automatorwp_clickup_get_api();
519
520 if( ! $api ) {
521 return $options;
522 }
523
524 $response = wp_remote_get( $api['url'] . '/list/'. $list_id . '/task', array(
525 'headers' => array(
526 'Authorization' => $api['token'],
527 )
528 ) );
529
530 $response = json_decode( wp_remote_retrieve_body( $response ), true );
531
532 if ( empty( $response['tasks'] ) ) {
533 return $tasks;
534 }
535
536 foreach ( $response['tasks'] as $task ) {
537
538 $tasks[] = array(
539 'id' => $task['id'],
540 'name' => $task['name'],
541 );
542
543 }
544
545 return $tasks;
546
547 }
548
549 /**
550 * Get task from ClickUp
551 *
552 * @since 1.0.0
553 *
554 * @param stdClass $field
555 *
556 * @return array
557 */
558 function automatorwp_clickup_options_cb_task( $field ) {
559
560 // Setup vars
561 $value = $field->escaped_value;
562 $none_value = 'any';
563 $none_label = __( 'any task', 'automatorwp' );
564 $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
565
566 $list_id = ct_get_object_meta( $field->object_id, 'list', true );
567
568 if( ! empty( $value ) ) {
569 if( ! is_array( $value ) ) {
570 $value = array( $value );
571 }
572
573 foreach( $value as $task_id ) {
574
575 // Skip option none
576 if( $task_id === $none_value ) {
577 continue;
578 }
579
580 $options[$task_id] = automatorwp_clickup_get_task_name( $task_id );
581 }
582 }
583
584 return $options;
585
586 }
587
588 /**
589 * Get the task name
590 *
591 * @since 1.0.0
592 *
593 * @param string $task_id
594 *
595 * @return array
596 */
597 function automatorwp_clickup_get_task_name( $task_id ) {
598
599 $api = automatorwp_clickup_get_api();
600
601 if( ! $api ) {
602 return $options;
603 }
604
605 $response = wp_remote_get( 'https://api.clickup.com/api/v2/task/' . $task_id, array(
606 'headers' => array(
607 'Authorization' => $api['token'],
608 ),
609 ) );
610
611 $response = json_decode( wp_remote_retrieve_body( $response ), true );
612
613 if ( isset ( $response['error']['code'] ) === 404 ) {
614 return;
615 }
616
617 if ( !isset ( $response['name'] ) ) {
618 return;
619 }
620
621 return $response['name'];
622 }
623
624 /**
625 * Create list to ClickUp
626 *
627 * @since 1.0.0
628 *
629 * @param string $list The list name to create
630 * @param double $space_id Space ID
631 * @param double $folder_id Folder ID
632 */
633 function automatorwp_clickup_create_list( $list, $space_id, $folder_id ) {
634
635 $api = automatorwp_clickup_get_api();
636
637 if( ! $api ) {
638 return;
639 }
640
641 if ( $folder_id === 0 ) {
642 $url_request = $api['url'] . '/space/' . $space_id . '/list';
643 } else {
644 $url_request = $api['url'] . '/folder/' . $folder_id . '/list';
645 }
646
647 $response = wp_remote_post( $url_request, array(
648 'headers' => array(
649 'Authorization' => $api['token'],
650 'Accept' => 'application/json',
651 'Content-Type' => 'application/json'
652 ),
653 'body' => json_encode( array(
654 'name' => $list,
655 ) )
656 ) );
657
658 return $response['response']['code'];
659 }
660
661 /**
662 * Add comment to a task
663 *
664 * @since 1.0.0
665 *
666 * @param double $task_id Space ID
667 * @param string $comment_text Comment text
668 *
669 * @return int
670 */
671 function automatorwp_clickup_add_comment( $task_id, $comment_text ) {
672
673 $api = automatorwp_clickup_get_api();
674
675 if( ! $api ) {
676 return;
677 }
678
679 $response = wp_remote_post( $api['url'] . '/task/' . $task_id . '/comment', array(
680 'headers' => array(
681 'Authorization' => $api['token'],
682 'Accept' => 'application/json',
683 'Content-Type' => 'application/json'
684 ),
685 'body' => json_encode( array(
686 'comment_text' => $comment_text,
687 ) )
688 ) );
689
690 return $response['response']['code'];
691 }
692
693 /**
694 * Add tag to a task
695 *
696 * @since 1.0.0
697 *
698 * @param double $task_id Space ID
699 * @param string $tag Tag
700 *
701 * @return int
702 */
703 function automatorwp_clickup_add_tag( $task_id, $tag ) {
704
705 $api = automatorwp_clickup_get_api();
706
707 if( ! $api ) {
708 return;
709 }
710
711 $response = wp_remote_post( $api['url'] . '/task/' . $task_id . '/tag/' . $tag, array(
712 'headers' => array(
713 'Authorization' => $api['token'],
714 'Accept' => 'application/json',
715 'Content-Type' => 'application/json'
716 )
717 ) );
718
719 return $response['response']['code'];
720 }
721
722 /**
723 * Remove tag from a task
724 *
725 * @since 1.0.0
726 *
727 * @param double $task_id Space ID
728 * @param string $tag Tag
729 *
730 * @return int
731 */
732 function automatorwp_clickup_remove_tag( $task_id, $tag ) {
733
734 $api = automatorwp_clickup_get_api();
735
736 if( ! $api ) {
737 return;
738 }
739
740 $response = wp_remote_request( $api['url'] . '/task/' . $task_id . '/tag/' . $tag, array(
741 'method' => 'DELETE',
742 'headers' => array(
743 'Authorization' => $api['token'],
744 'Accept' => 'application/json',
745 'Content-Type' => 'application/json'
746 )
747 ) );
748
749 return $response['response']['code'];
750 }
751
752 /**
753 * Add comment to a task
754 *
755 * @since 1.0.0
756 *
757 * @param double $list_id List ID
758 * @param string $name_task Name task
759 * @param string $description_task Description task
760 *
761 * @return int
762 */
763 function automatorwp_clickup_create_task( $list_id, $name_task, $description_task = '' ) {
764
765 $api = automatorwp_clickup_get_api();
766
767 if( ! $api ) {
768 return;
769 }
770
771 $response = wp_remote_post( $api['url'] . '/list/' . $list_id . '/task', array(
772 'headers' => array(
773 'Authorization' => $api['token'],
774 'Accept' => 'application/json',
775 'Content-Type' => 'application/json'
776 ),
777 'body' => json_encode( array(
778 'name' => $name_task,
779 'description' => $description_task
780 ) )
781 ) );
782
783 return $response['response']['code'];
784 }
785
786 /**
787 * Remove a task
788 *
789 * @since 1.0.0
790 *
791 * @param double $task_id Task ID
792 *
793 * @return int
794 */
795 function automatorwp_clickup_remove_task( $task_id ) {
796
797 $api = automatorwp_clickup_get_api();
798
799 if( ! $api ) {
800 return;
801 }
802
803 $response = wp_remote_request( $api['url'] . '/task/' . $task_id, array(
804 'method' => 'DELETE',
805 'headers' => array(
806 'Authorization' => $api['token'],
807 'Accept' => 'application/json',
808 'Content-Type' => 'application/json'
809 )
810 ) );
811
812 return $response['response']['code'];
813 }