Skip to content
This repository was archived by the owner on Dec 5, 2023. It is now read-only.

Commit a4097e4

Browse files
author
Santiago Garza
committed
cleanup of new_monitor method
1 parent a044751 commit a4097e4

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

wp-uptime-robot-api.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,15 @@ private function fetch( $request ) {
7373
$request .= '&noJsonCallback=' . static::$no_json_callback;
7474

7575
$response = wp_remote_get( $request );
76+
$code = wp_remote_retrieve_response_code( $response );
77+
78+
if( 200 !== $code ){
79+
return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) );
80+
}
81+
7682
$body = wp_remote_retrieve_body( $response );
7783

78-
if ( static::$no_json_callback ) {
84+
if ( 'json' === static::$format && static::$no_json_callback ) {
7985
return json_decode( $body );
8086
}
8187

@@ -108,17 +114,17 @@ protected function get_monitors( $monitors = null, $types = null, $statuses = nu
108114
* @param [String] $friendly_name Required | Display name.
109115
* @param [String] $url Required | Domain to be monitored.
110116
* @param [String] $type Required | Monitor type.
117+
* @param [type] $alert_contacts Optional | .
111118
* @param [type] $sub_type Optional | .
112119
* @param [type] $port Optional | .
113120
* @param [type] $keyword_type Optional | .
114121
* @param [type] $keyword_value Optional | .
115122
* @param [type] $username Optional | .
116123
* @param [type] $password Optional | .
117-
* @param [type] $alert_contacts Optional | .
118124
* @param [int] $interval Optional | .
119125
* @return [type] [description] .
120126
*/
121-
public function new_monitor( $friendly_name, $url, $type, $sub_type = null, $port = null, $keyword_type = null, $keyword_value = null, $username = null, $password = null, $alert_contacts = null, $interval = 3 ) {
127+
public function new_monitor( $friendly_name, $url, $type, $alert_contacts = null, $sub_type = null, $port = null, $keyword_type = null, $keyword_value = null, $username = null, $password = null, $interval = 3 ) {
122128

123129
if ( empty( $friendly_name ) || empty( $url ) || empty( $type ) ) {
124130
return new WP_Error( 'required-fields', __( "Required fields are empty", 'text-domain' ) );
@@ -127,6 +133,9 @@ public function new_monitor( $friendly_name, $url, $type, $sub_type = null, $por
127133
$friendly_name = urlencode( $friendly_name );
128134
$request = $this->base_uri . '/newMonitor?monitorFriendlyName=' . $friendly_name . '&monitorURL=' . $url . '&monitorType=' . $type;
129135

136+
if ( ! empty( $alert_contacts ) ) {
137+
$request .= '&monitorAlertContacts=' . $this->getImplode( $alert_contacts );
138+
}
130139
if ( ! empty( $sub_type ) ) {
131140
$request .= '&monitorSubType=' . $sub_type;
132141
}
@@ -145,9 +154,6 @@ public function new_monitor( $friendly_name, $url, $type, $sub_type = null, $por
145154
if ( isset( $password ) ) {
146155
$request .= '&monitorHTTPPassword=' . urlencode( $password );
147156
}
148-
if ( ! empty( $alert_contacts ) ) {
149-
$request .= '&monitorAlertContacts=' . $this->getImplode( $alert_contacts );
150-
}
151157
if ( ! empty( $interval ) ) {
152158
$request .= '&monitorInterval=' . $interval;
153159
}
@@ -317,4 +323,19 @@ public function response_code_msg( $code = '' ) {
317323

318324
return $msg;
319325
}
326+
327+
/**
328+
* Array or int to string with separator (-)
329+
*
330+
* @param array|int $var
331+
* @return type string
332+
*/
333+
private function getImplode($var)
334+
{
335+
if (is_array($var))
336+
{
337+
return implode('-', $var);
338+
}
339+
return $var;
340+
}
320341
}

0 commit comments

Comments
 (0)