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-uptime-robot-api.php
951 lines (855 loc) · 28.8 KB
/
wp-uptime-robot-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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
<?php
/**
* WP-UptimeRobot-API
*
* @author Santiago Garza <github.com/sfgarza>
* @link https://uptimerobot.com/api APIv2 Documentation.
* @package WP-API-Libraries\WP-UptimeRobot-API
*/
/*
* Plugin Name: WP Uptime Robot API
* Plugin URI: https://github.com/wp-api-libraries/wp-uptime-robot-api
* Description: Perform API requests to Uptime Robot in WordPress.
* Author: WP API Libraries
* Version: 1.0.0
* Text Domain: wp-uptime-robot-api
* Author URI: https://wp-api-libraries.com
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-uptime-robot-api
* GitHub Branch: master
*/
/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! class_exists( 'UptimeRobotApi' ) ) {
/**
* UptimeRobotApi class.
*/
class UptimeRobotApi {
/**
* API key
*
* @var string
*/
static private $api_key;
/**
* Return format. XML or JSON.
*
* @var string
*/
static private $format;
/**
* Indicate if json respone should be wrapped in a callback.
*
* @var int
*/
static private $callback;
/**
* URL to the API.
*
* @var string
*/
private $base_uri = 'https://api.uptimerobot.com/v2';
/**
* Request POST args.
*
* @var array
*/
private $args = array();
/**
* Constructor.
*
* @param string $api_key API key to the account.
* @param string $format XML or JSON.
* @param int $callback If specified, returns json wrapped in a callback with the name passed in.
*/
public function __construct( $api_key, $format = 'json', $callback = null ) {
static::$api_key = $api_key;
static::$format = $format;
static::$callback = $callback;
}
/**
* Fetch the request from the API.
*
* @param string $request Request URL.
* @return mixed Request results.
*/
private function fetch( $request ) {
$this->args['body']['api_key'] = static::$api_key;
$this->args['body']['format'] = static::$format;
if ( null !== static::$callback ) {
$this->args['body']['callback'] = static::$callback;
}
$response = wp_remote_post( $request, $this->args );
$code = wp_remote_retrieve_response_code( $response );
$body = wp_remote_retrieve_body( $response );
if ( 'json' === static::$format && null === static::$callback ) {
$body = json_decode( $body );
}
$this->clear();
if ( 200 !== $code ) {
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'wp-uptime-robot-api' ), $code ),$body );
}
return $body;
}
/**
* Clear query data.
*/
protected function clear() {
$this->args = array();
}
/**
* Account details (max number of monitors that can be added and number of up/down/paused monitors) can be grabbed
* using this method.
*
* @api
*
* @return array Request results.
*/
public function get_account_details() {
$request = $this->base_uri . '/getAccountDetails';
return $this->fetch( $request );
}
/**
* Get monitor info.
*
* @api
*
* @param array $args Array of arguments to send into get_monitors.
* @return array Array of monitor info.
*/
public function get_monitors( $args = array() ) {
// Set route.
$request = $this->base_uri . '/getMonitors';
// Parse and set query args.
if ( isset( $args['monitors'] ) ) {
$this->args['body']['monitors'] = $this->get_implode( $args['monitors'] );
}
if ( isset( $args['types'] ) ) {
$this->args['body']['types'] = $this->get_implode( $args['types'] );
}
if ( isset( $args['statuses'] ) ) {
$this->args['body']['statuses'] = $this->get_implode( $args['statuses'] );
}
if ( isset( $args['custom_uptime_ratios'] ) ) {
$this->args['body']['custom_uptime_ratios'] = $this->get_implode( $args['custom_uptime_ratios'] );
}
if ( isset( $args['custom_uptime_ranges'] ) ) {
$this->args['body']['custom_uptime_ranges'] = $this->get_implode( $args['custom_uptime_ranges'] );
}
if ( isset( $args['all_time_uptime_ratio'] ) ) {
$this->args['body']['all_time_uptime_ratio'] = $args['all_time_uptime_ratio'];
}
if ( isset( $args['all_time_uptime_durations'] ) ) {
$this->args['body']['all_time_uptime_durations'] = $args['all_time_uptime_durations'];
}
if ( isset( $args['logs'] ) ) {
$this->args['body']['logs'] = $args['logs'];
}
if ( isset( $args['logs_start_date'] ) ) {
$this->args['body']['logs_start_date'] = $args['logs_start_date'];
}
if ( isset( $args['logs_end_date'] ) ) {
$this->args['body']['logs_end_date'] = $args['logs_end_date'];
}
if ( isset( $args['logs_limit'] ) ) {
$this->args['body']['logs_limit'] = $args['logs_limit'];
}
if ( isset( $args['response_times'] ) ) {
$this->args['body']['response_times'] = $args['response_times'];
}
if ( isset( $args['response_times_limit'] ) ) {
$this->args['body']['response_times_limit'] = $args['response_times_limit'];
}
if ( isset( $args['response_times_average'] ) ) {
$this->args['body']['response_times_average'] = $args['response_times_average'];
}
if ( isset( $args['response_times_start_date'] ) ) {
$this->args['body']['response_times_start_date'] = $args['response_times_start_date'];
}
if ( isset( $args['response_times_end_date'] ) ) {
$this->args['body']['response_times_end_date'] = $args['response_times_end_date'];
}
if ( isset( $args['alert_contacts'] ) ) {
$this->args['body']['alert_contacts'] = $args['alert_contacts'];
}
if ( isset( $args['mwindows'] ) ) {
$this->args['body']['mwindows'] = $args['mwindows'];
}
if ( isset( $args['timezone'] ) ) {
$this->args['body']['timezone'] = $args['timezone'];
}
if ( isset( $args['offset'] ) ) {
$this->args['body']['offset'] = $args['offset'];
}
if ( isset( $args['limit'] ) ) {
$this->args['body']['limit'] = $args['limit'];
}
if ( isset( $args['search'] ) ) {
$this->args['body']['search'] = htmlspecialchars( $args['search'] );
}
// Make API Call.
$result = $this->fetch( $request );
// Loop until all monitors are retrieved.
if ( isset( $result->pagination->limit ) && isset( $result->pagination->offset ) && isset( $result->pagination->total ) ) {
$limit = $result->pagination->limit;
$offset = $result->pagination->offset;
$total = $result->pagination->total;
while ( ($limit * $offset) + $limit < $total ) {
$result->pagination->limit = ($limit * $offset) + $limit;
$offset++;
$this->args['body']['offset'] = ($offset * $limit);
$append = $this->fetch( $request );
if ( 'fail' === $append->stat ) { break; }
$result->monitors = array_merge( $result->monitors, $append->monitors );
}
$result->pagination->limit = ( $limit * $offset ) + $limit;
}
// Return to sender.
return $result;
}
/**
* New monitors of any type can be created using this method.
*
* @api
*
* @param array $args Args to be sent into newMonitor request.
* @return array Request results.
*/
public function new_monitor( $args ) {
if ( ! isset( $args['friendly_name'] ) || ! isset( $args['url'] ) || ! isset( $args['type'] ) ) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/newMonitor';
$this->args['body']['friendly_name'] = $args['friendly_name'];
$this->args['body']['url'] = $args['url'];
$this->args['body']['type'] = $args['type'];
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['sub_type'] ) ) {
$this->args['body']['sub_type'] = $args['sub_type'];
}
if ( isset( $args['port'] ) ) {
$this->args['body']['port'] = $args['port'];
}
if ( isset( $args['keyword_type'] ) ) {
$this->args['body']['keyword_type'] = $args['keyword_type'];
}
if ( isset( $args['keyword_value'] ) ) {
$this->args['body']['keyword_value'] = $args['keyword_value'];
}
if ( isset( $args['interval'] ) ) {
$this->args['body']['interval'] = $args['interval'];
}
if ( isset( $args['http_username'] ) ) {
$this->args['body']['http_username'] = $args['http_username'];
}
if ( isset( $args['http_password'] ) ) {
$this->args['body']['http_password'] = $args['http_password'];
}
if ( isset( $args['alert_contacts'] ) ) {
$this->args['body']['alert_contacts'] = $this->get_implode( $args['alert_contacts'] );
}
if ( isset( $args['mwindows'] ) ) {
$this->args['body']['mwindows'] = $this->get_implode( $args['mwindows'] );
}
return $this->fetch( $request );
}
/**
* Monitors can be edited using this method.
*
* Important:
* The type of a monitor can not be edited (like changing a HTTP monitor into a Port monitor). For such
* cases, deleting the monitor and re-creating a new one is adviced.
*
* @api
*
* @param array $args Array of arguments to send into get_monitors.
* @return array Array of monitor info.
*/
public function edit_monitor( $args ) {
if ( ! isset( $args['id'] ) ) {
return new WP_Error( 'required-fields', __( 'Monitor id required', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/editMonitor';
$this->args['body']['id'] = $args['id'];
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['url'] ) ) {
$this->args['body']['url'] = $args['url'];
}
if ( isset( $args['sub_type'] ) ) {
$this->args['body']['sub_type'] = $args['sub_type'];
}
if ( isset( $args['port'] ) ) {
$this->args['body']['port'] = $args['port'];
}
if ( isset( $args['keyword_type'] ) ) {
$this->args['body']['keyword_type'] = $args['keyword_type'];
}
if ( isset( $args['keyword_value'] ) ) {
$this->args['body']['keyword_value'] = $args['keyword_value'];
}
if ( isset( $args['interval'] ) ) {
$this->args['body']['interval'] = $args['interval'];
}
if ( isset( $args['status'] ) ) {
$this->args['body']['status'] = $args['status'];
}
if ( isset( $args['http_username'] ) ) {
$this->args['body']['http_username'] = $args['http_username'];
}
if ( isset( $args['http_password'] ) ) {
$this->args['body']['http_password'] = $args['http_password'];
}
if ( isset( $args['alert_contacts'] ) ) {
$this->args['body']['alert_contacts'] = $this->get_implode( $args['alert_contacts'] );
}
if ( isset( $args['mwindows'] ) ) {
$this->args['body']['mwindows'] = $this->get_implode( $args['mwindows'] );
}
return $this->fetch( $request );
}
/**
* Monitors can be deleted using this method.
*
* @api
*
* @param int $monitor_id ID of monitor.
* @return array Request results.
*/
public function delete_monitor( $monitor_id ) {
if ( empty( $monitor_id ) ) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$this->args['body']['id'] = $monitor_id;
$request = $this->base_uri . '/deleteMonitor';
return $this->fetch( $request );
}
/**
* The list of alert contacts can be called with this method.
*
* @api
*
* @param array $args Arguments for getAlertContacts request.
* @return array Request results.
*/
public function get_alert_contacts( $args = array() ) {
$request = $this->base_uri . '/getAlertContacts';
if ( isset( $args['alert_contacts'] ) ) {
$this->args['body']['alert_contacts'] = $this->get_implode( $args['alert_contacts'] );
}
if ( isset( $args['offset'] ) ) {
$this->args['body']['offset'] = $args['offset'];
}
if ( isset( $args['limit'] ) ) {
$this->args['body']['limit'] = $args['limit'];
}
return $this->fetch( $request );
}
/**
* New alert contacts of any type (mobile/SMS alert contacts are not supported
* yet) can be created using this method.
* The alert contacts created using the API are validated with the same way
* as they were created from uptimerobot.com (activation link for e-mails, etc.).
*
* @api
*
* @param array $args Arguments for newAlertContact request.
* @return array Request results.
*/
public function new_alert_contact( $args = array() ) {
if (! isset( $args['type']) || ! isset( $args['value'] )) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/newAlertContact';
$this->args['body']['type'] = $args['type'];
$this->args['body']['value'] = $args['value'];
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['value'] ) ) {
$this->args['body']['value'] = $args['value'];
}
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
return $this->fetch( $request );
}
/**
* Alert contacts can be edited using this method.
*
* @api
*
* @param array $args Arguments for editAlertContact request.
* @return array Request results.
*/
public function edit_alert_contact( $args = array() ) {
if (! isset( $args['id']) ){
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/editAlertContact';
$this->args['body']['id'] = $args['id'];
if ( isset( $args['id'] ) ) {
$this->args['body']['id'] = $args['id'];
}
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['value'] ) ) {
$this->args['body']['value'] = $args['value'];
}
return $this->fetch( $request );
}
/**
* Alert contacts can be deleted using this method.
*
* @api
*
* @param array $args Arguments for deleteAlertContact request.
* @return array Request results.
*/
public function delete_alert_contact( $args = array() ) {
if (! isset( $args['id'])) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/deleteAlertContact';
$this->args['body']['id'] = $args['id'];
if ( isset( $args['id'] ) ) {
$this->args['body']['id'] = $args['id'];
}
return $this->fetch( $request );
}
/**
* The list of maintenance windows can be called with this method.
*
* @api
*
* @param array $args Arguments for getMWindows request.
* @return array Request results.
*/
public function get_mwindows( $args = array() ) {
$request = $this->base_uri . '/getMWindows';
if ( isset( $args['mwindows'] ) ) {
$this->args['body']['mwindows'] = $this->get_implode( $args['mwindows'] );
}
if ( isset( $args['offset'] ) ) {
$this->args['body']['offset'] = $args['offset'];
}
if ( isset( $args['limit'] ) ) {
$this->args['body']['limit'] = $args['limit'];
}
return $this->fetch( $request );
}
/**
* New maintenance windows can be created using this method.
*
* @api
*
* @param array $args Arguments for newMWindows request.
* @return array Request results.
*/
public function new_mwindow( $args = array() ) {
if (! isset( $args['friendly_name']) || ! isset( $args['type'] )|| ! isset( $args['value'] )|| ! isset( $args['start_time'] )|| ! isset( $args['duration'] )) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/newMWindow';
$this->args['body']['friendly_name'] = $args['friendly_name'];
$this->args['body']['type'] = $args['type'];
$this->args['body']['value'] = $args['value'];
$this->args['body']['start_time'] = $args['start_time'];
$this->args['body']['duration'] = $args['duration'];
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['value'] ) ) {
$this->args['body']['value'] = $args['value'];
}
if ( isset( $args['start_time'] ) ) {
$this->args['body']['start_time'] = $args['start_time'];
}
if ( isset( $args['duration'] ) ) {
$this->args['body']['duration'] = $args['duration'];
}
return $this->fetch( $request );
}
/**
* Maintenance windows can be edited using this method.
*
* @api
*
* @param array $args Arguments for editMWindow request.
* @return array Request results.
*/
public function edit_mwindow( $args = array() ) {
if (! isset( $args['friendly_name']) || ! isset( $args['type'] )|| ! isset( $args['value'] )|| ! isset( $args['start_time'] )|| ! isset( $args['duration'] )) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/editMWindow';
$this->args['body']['friendly_name'] = $args['friendly_name'];
$this->args['body']['type'] = $args['type'];
$this->args['body']['value'] = $args['value'];
$this->args['body']['start_time'] = $args['start_time'];
$this->args['body']['duration'] = $args['duration'];
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['value'] ) ) {
$this->args['body']['value'] = $args['value'];
}
if ( isset( $args['start_time'] ) ) {
$this->args['body']['start_time'] = $args['start_time'];
}
if ( isset( $args['duration'] ) ) {
$this->args['body']['duration'] = $args['duration'];
}
return $this->fetch( $request );
}
/**
* Maintenance windows can be deleted using this method.
*
* @api
*
* @param array $args Arguments for deleteMWindow request.
* @return array Request results.
*/
public function delete_mwindow( $args = array() ) {
if (! isset( $args['id'])) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/deleteMWindow';
$this->args['body']['id'] = $args['id'];
if ( isset( $args['id'] ) ) {
$this->args['body']['id'] = $args['id'];
}
return $this->fetch( $request );
}
/**
* The list of alert contacts can be called with this method.
*
* @api
*
* @param array $args Arguments for getPSPs request.
* @return array Request results.
*/
public function get_psps( $args = array() ) {
$request = $this->base_uri . '/getPSPs';
if ( isset( $args['psps'] ) ) {
$this->args['body']['psps'] = $this->get_implode( $args['psps'] );
}
if ( isset( $args['offset'] ) ) {
$this->args['body']['offset'] = $args['offset'];
}
if ( isset( $args['limit'] ) ) {
$this->args['body']['limit'] = $args['limit'];
}
return $this->fetch( $request );
}
/**
* New public status pages can be created using this method.
*
* @api
*
* @param array $args Arguments for newPSP request.
* @return array Request results.
*/
public function new_psp( $args = array() ) {
if (! isset( $args['type']) || ! isset( $args['friendly_name'] )|| ! isset( $args['monitors'] )) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/newPSP';
$this->args['body']['type'] = $args['type'];
$this->args['body']['friendly_name'] = $args['friendly_name'];
$this->args['body']['monitors'] = $this->get_implode($args['monitors']);
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['monitors'] ) ) {
$this->args['body']['monitors'] = $this->get_implode($args['monitors']);
}
if ( isset( $args['custom_domain'] ) ) {
$this->args['body']['custom_domain'] = $args['custom_domain'];
}
if ( isset( $args['sort'] ) ) {
$this->args['body']['sort'] = $args['sort'];
}
if ( isset( $args['hide_url_links'] ) ) {
$this->args['body']['hide_url_links'] = $args['hide_url_links'];
}
if ( isset( $args['status'] ) ) {
$this->args['body']['status'] = $args['status'];
}
return $this->fetch( $request );
}
/**
* Public status pages can be edited using this method.
* @api
*
* @param array $args Arguments for editPSP request.
* @return array Request results.
*/
public function edit_psp( $args = array() ) {
if (! isset( $args['type']) || ! isset( $args['friendly_name'] )|| ! isset( $args['monitors'] )) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/editPSP';
$this->args['body']['type'] = $args['type'];
$this->args['body']['friendly_name'] = $args['friendly_name'];
$this->args['body']['monitors'] = $this->get_implode($args['monitors']);
if ( isset( $args['type'] ) ) {
$this->args['body']['type'] = $args['type'];
}
if ( isset( $args['friendly_name'] ) ) {
$this->args['body']['friendly_name'] = $args['friendly_name'];
}
if ( isset( $args['monitors'] ) ) {
$this->args['body']['monitors'] = $this->get_implode($args['monitors']);
}
if ( isset( $args['custom_domain'] ) ) {
$this->args['body']['custom_domain'] = $args['custom_domain'];
}
if ( isset( $args['sort'] ) ) {
$this->args['body']['sort'] = $args['sort'];
}
if ( isset( $args['hide_url_links'] ) ) {
$this->args['body']['hide_url_links'] = $args['hide_url_links'];
}
if ( isset( $args['status'] ) ) {
$this->args['body']['status'] = $args['status'];
}
return $this->fetch( $request );
}
/**
* Alert contacts can be deleted using this method.
*
* @api
*
* @param array $args Arguments for deletePSP request.
* @return array Request results.
*/
public function delete_psp( $args = array() ) {
if (! isset( $args['id'])) {
return new WP_Error( 'required-fields', __( 'Required fields are empty', 'wp-uptime-robot-api' ) );
}
$request = $this->base_uri . '/delete_psp';
$this->args['body']['id'] = $args['id'];
return $this->fetch( $request );
}
/**
* Converts the status ID to the corresponding status string.
*
* @param int $status Status ID to convert.
* @return string Status in string format ( Up|Down|Seems Down|Paused|Not Checked Yet|Not a Valid Status )
*/
public function status_tostring( Int $status ){
switch ( $status ) {
case 0:
$str_status = __( 'Paused','wp-uptime-robot-api' );
break;
case 1:
$str_status = __( 'Not Checked Yet','wp-uptime-robot-api' );
break;
case 2:
$str_status = __( 'Up','wp-uptime-robot-api' );
break;
case 8:
$str_status = __( 'Seems Down','wp-uptime-robot-api' );
break;
case 9:
$str_status = __( 'Down','wp-uptime-robot-api' );
break;
default:
$str_status = __( 'Not a Valid Status', 'wp-uptime-robot-api' );
break;
}
return $str_status;
}
/**
* Converts the alert type ID to its corresponding messages.
*
* @param Int $type Alert type ID.
* @return String Alert type String (SMS|E-mail|Twitter DM|Boxcar|Web-Hook|Pushbullet|Zapier|Pushover|HipChat|Slack).
*/
public function alert_type_tostring( Int $type ){
switch ( $status ) {
case 1:
$str_type = __( 'SMS','wp-uptime-robot-api' );
break;
case 2:
$str_type = __( 'E-mail','wp-uptime-robot-api' );
break;
case 3:
$str_type = __( 'Twitter DM','wp-uptime-robot-api' );
break;
case 4:
$str_type = __( 'Boxcar','wp-uptime-robot-api' );
break;
case 5:
$str_type = __( 'Web-Hook','wp-uptime-robot-api' );
break;
case 6:
$str_type = __( 'Pushbullet','wp-uptime-robot-api' );
break;
case 7:
$str_type = __( 'Zapier','wp-uptime-robot-api' );
break;
case 9:
$str_type = __( 'Pushover','wp-uptime-robot-api' );
break;
case 10:
$str_type = __( 'HipChat','wp-uptime-robot-api' );
break;
case 11:
$str_type = __( 'Slack','wp-uptime-robot-api' );
break;
default:
$str_type = __( 'Not a Valid Alert Type', 'wp-uptime-robot-api' );
break;
}
return $str_type;
}
/**
* HTTP response code messages.
*
* @api
*
* @param string $code Response code to get message from.
* @return string Message corresponding to response code sent in.
*/
public function response_code_msg( $code = '' ) {
switch ( $code ) {
case 100:
$msg = __( 'ApiKey not mentioned or in a wrong format.','wp-uptime-robot-api' );
break;
case 101:
$msg = __( 'ApiKey is wrong.','wp-uptime-robot-api' );
break;
case 102:
$msg = __( 'Format is wrong (should be xml or json).','wp-uptime-robot-api' );
break;
case 103:
$msg = __( 'No such method exists.','wp-uptime-robot-api' );
break;
case 200:
$msg = __( 'MonitorID(s) should be integers.','wp-uptime-robot-api' );
break;
case 201:
$msg = __( 'MonitorUrl is invalid.','wp-uptime-robot-api' );
break;
case 202:
$msg = __( 'MonitorType is invalid.','wp-uptime-robot-api' );
break;
case 203:
$msg = __( 'MonitorSubType is invalid.','wp-uptime-robot-api' );
break;
case 204:
$msg = __( 'MonitorKeywordType is invalid.','wp-uptime-robot-api' );
break;
case 205:
$msg = __( 'MonitorPort is invalid.','wp-uptime-robot-api' );
break;
case 206:
$msg = __( 'MonitorFriendlyName is required.','wp-uptime-robot-api' );
break;
case 207:
$msg = __( 'The monitor already exists.','wp-uptime-robot-api' );
break;
case 208:
$msg = __( 'MonitorSubType is required for this type of monitors.','wp-uptime-robot-api' );
break;
case 209:
$msg = __( 'The monitorKeyWordType and monitorKeyWordValue are required for this type of monitor.','wp-uptime-robot-api' );
break;
case 210:
$msg = __( 'The monitorID does not exist.','wp-uptime-robot-api' );
break;
case 211:
$msg = __( 'The monitorID is required.','wp-uptime-robot-api' );
break;
case 212:
$msg = __( 'The account has no monitors.','wp-uptime-robot-api' );
break;
case 213:
$msg = __( 'At least one of the parameters to be edited are required.','wp-uptime-robot-api' );
break;
case 214:
$msg = __( 'The monitorHTTPUsername and monitorHTTPPassword should both be empty or have values.','wp-uptime-robot-api' );
break;
case 215:
$msg = __( 'The monitor specific apiKeys can only use getMonitors method.','wp-uptime-robot-api' );
break;
case 216:
$msg = __( 'A user with this e-mail already exists.','wp-uptime-robot-api' );
break;
case 217:
$msg = __( 'The userFirstLastName and userEmail are both required.','wp-uptime-robot-api' );
break;
case 218:
$msg = __( 'The userEmail is not in the right e-mail format.','wp-uptime-robot-api' );
break;
case 219:
$msg = __( 'This account is not authorized to create users.','wp-uptime-robot-api' );
break;
case 220:
$msg = __( 'The monitorAlertContacts value is wrong.','wp-uptime-robot-api' );
break;
case 221:
$msg = __( 'The account has no alert contacts.','wp-uptime-robot-api' );
break;
case 222:
$msg = __( 'The alertcontactID(s) should be integers.','wp-uptime-robot-api' );
break;
case 223:
$msg = __( 'The alertContactType and alertContactValue are both required.','wp-uptime-robot-api' );
break;
case 224:
$msg = __( 'This alertContactType is not supported".','wp-uptime-robot-api' );
break;
case 225:
$msg = __( 'The alert contact already exists.','wp-uptime-robot-api' );
break;
case 226:
$msg = __( 'The alert contact is not following @uptimerobot Twitter user. It is required so that the Twitter direct messages (DM) can be sent.','wp-uptime-robot-api' );
break;
case 227:
$msg = __( 'The Boxcar user mentioned does not exist.','wp-uptime-robot-api' );
break;
case 228:
$msg = __( 'The Boxcar alert contact could not be added, please try again later.','wp-uptime-robot-api' );
break;
case 229:
$msg = __( 'The alertContactID does not exist.','wp-uptime-robot-api' );
break;
case 230:
$msg = __( 'The alertContactValue should be a valid e-mail for this alertContactType.','wp-uptime-robot-api' );
break;
default:
$msg = __( 'Response code unknown.', 'wp-uptime-robot-api' );
break;
}
return $msg;
}
/**
* Implode the array and deliminate the values with '-' or return the variable
* as is.
*
* @param array|string $var Array to be imploded or a string to be sent back.
* @return string Imploded string.
*/
private function get_implode( $var ) {
if ( is_array( $var ) ) {
return implode( '-', $var );
}
return $var;
}
}
}