/var/www/hkosl.com/aga/wp-content/plugins/wordpress-seo/admin/class-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
<?php
/**
 * WPSEO plugin file.
 *
 * @package WPSEO\Admin
 */

/**
 * This class handles a post request being send to a given endpoint.
 */
class WPSEO_Remote_Request {

    
/**
     * @var string
     */
    
const METHOD_POST 'post';

    
/**
     * @var string
     */
    
const METHOD_GET 'get';

    
/**
     * @var string
     */
    
protected $endpoint '';

    
/**
     * @var array
     */
    
protected $args = array(
        
'blocking'  => false,
        
'sslverify' => false,
        
'timeout'   => 2,
    );

    
/**
     * @var WP_Error|null
     */
    
protected $response_error;

    
/**
     * @var mixed
     */
    
protected $response_body;

    
/**
     * Sets the endpoint and arguments.
     *
     * @param string $endpoint The endpoint to send the request to.
     * @param array  $args     The arguments to use in this request.
     */
    
public function __construct$endpoint, array $args = array() ) {
        
$this->endpoint $endpoint;
        
$this->args     wp_parse_args$this->args$args );
    }

    
/**
     * Sets the request body.
     *
     * @param mixed $body The body to set.
     */
    
public function set_body$body ) {
        
$this->args['body'] = $body;
    }

    
/**
     * Sends the data to the given endpoint.
     *
     * @param string $method The type of request to send.
     *
     * @return bool True when sending data has been successful.
     */
    
public function send$method self::METHOD_POST ) {
        switch ( 
$method ) {
            case 
self::METHOD_POST:
                
$response $this->post();
                break;
            case 
self::METHOD_GET:
                
$response $this->get();
                break;
            default:
                
/* translators: %1$s expands to the request method  */
                
$response = new WP_Error1sprintf__'Request method %1$s is not valid.''wordpress-seo' ), $method ) );
                break;
        }

        return 
$this->process_response$response );
    }

    
/**
     * Returns the value of the response error.
     *
     * @return null|WP_Error The response error.
     */
    
public function get_response_error() {
        return 
$this->response_error;
    }

    
/**
     * Returns the response body.
     *
     * @return mixed The response body.
     */
    
public function get_response_body() {
        return 
$this->response_body;
    }

    
/**
     * Processes the given response.
     *
     * @param mixed $response The response to process.
     *
     * @return bool True when response is valid.
     */
    
protected function process_response$response ) {
        if ( 
$response instanceof WP_Error ) {
            
$this->response_error $response;

            return 
false;
        }

        
$this->response_body wp_remote_retrieve_body$response );

        return ( 
wp_remote_retrieve_response_code$response ) === 200 );
    }

    
/**
     * Performs a post request to the specified endpoint with set arguments.
     *
     * @return WP_Error|array The response or WP_Error on failure.
     */
    
protected function post() {
        return 
wp_remote_post$this->endpoint$this->args );
    }

    
/**
     * Performs a post request to the specified endpoint with set arguments.
     *
     * @return WP_Error|array The response or WP_Error on failure.
     */
    
protected function get() {
        return 
wp_remote_get$this->endpoint$this->args );
    }
}