/var/www/html_uk/wp-content/plugins/automatewoo/includes/Remote_Request.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
<?php
// phpcs:ignoreFile

namespace AutomateWoo;

/**
 * @class Remote_Request
 * @since 2.3.1
 */
class Remote_Request {

    
/** @var string */
    
public $method;

    
/** @var string */
    
public $url;

    
/** @var array */
    
public $http_success_codes = [ 200201202203204 ];

    
/**
     * Response from wp_remote_request()
     * @var array|\WP_Error
     */
    
public $request;


    
/**
     * Passes to wp_remote_request()
     *
     * @param $url
     * @param $args
     */
    
function __construct$url$args ) {
        
$domain home_url();
        
$domain str_replace( [ 'http://''https://' ], ''$domain );
        
$domain untrailingslashit$domain );

        
$args wp_parse_args$args, [
            
'user-agent' => 'AutomateWoo ' AW()->version ' - ' $domain
        
]);

        
$this->url $url;
        
$this->method $args['method'];

        
$this->request wp_remote_request$url$args );
    }

    
/**
     * Checks if the remote HTTP request failed.
     *
     * Note: This doesn't check the response, it only checks that some response was received.
     *
     * @return bool
     */
    
function is_http_error() {
        return 
is_wp_error$this->request );
    }

    
/**
     * Checks if a valid HTTP response code was returned.
     *
     * @return bool
     */
    
function is_api_error() {
        if ( 
$this->is_http_error() ) {
            return 
false;
        }
        return ! 
$this->is_http_success_code();
    }

    
/**
     * Checks if the remote request was successful.
     *
     * Checks that the API returned a success HTTP response code e.g. 200.
     *
     * @return bool
     */
    
function is_successful() {
        return 
$this->is_http_success_code();
    }

    
/**
     * Returns the HTTP error message if the request failed and no response was received.
     *
     * @return string|false
     */
    
function get_http_error_message() {
        if ( 
$this->is_http_error() ) {
            return 
$this->request->get_error_message();
        }
        return 
false;
    }

    
/**
     * Returns the HTTP status code of the request.
     *
     * Returns 503 if the request failed.
     *
     * @return int
     */
    
function get_response_code() {
        if ( 
$this->is_http_error() ) {
            return 
503;
        }

        return 
$this->request['response']['code'];
    }

    
/**
     * Returns the HTTP request response message.
     *
     * @return string
     */
    
function get_response_message() {
        if ( 
$this->is_http_error() ) {
            return 
'';
        }

        return 
$this->request['response']['message'];
    }

    
/**
     * Returns the processed request body.
     *
     * JSON will be decoded.
     *
     * @return array|false
     */
    
function get_body() {
        if ( 
$this->is_http_error() ) {
            return 
false;
        }

        
$options PHP_INT_SIZE JSON_BIGINT_AS_STRING 0// fixes rare issue where IDs could be converted to scientific notation
        
return json_decode$this->request['body'], true512$options );
    }

    
/**
     * Returns the unprocessed request body.
     *
     * @return string
     */
    
function get_body_raw() {
        if ( 
$this->is_http_error() ) {
            return 
'';
        }

        return 
$this->request['body'];
    }

    
/**
     * Checks if the HTTP status code is a success code.
     *
     * @return bool
     */
    
function is_http_success_code() {
        return 
in_array$this->get_response_code(), $this->http_success_codes );
    }





    
/**
     * @deprecated
     * @return bool
     */
    
function is_failed() {
        
wc_deprecated_function__METHOD__'5.2.0''is_http_error' );

        return 
$this->is_http_error();
    }


    
/**
     * @deprecated
     * @return bool
     */
    
function get_error_message() {
        
wc_deprecated_function__METHOD__'5.2.0''get_http_error_message' );

        return 
$this->get_http_error_message();
    }


}