PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.0
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.0
6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 All 125 releases
wp-to-buffer / lib / social / includes / class-api.php

class-api.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.0, at lib/social/includes/class-api.php

190 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Used by other classes which interact with APIs to perform POST and GET requests.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 1.0.0
17 */
18 class API {
19
20 /**
21 * Sanitizes API arguments, by removing false or empty
22 * arguments in the array.
23 *
24 * @since 1.0.0
25 *
26 * @param array $args Arguments.
27 * @return array Sanitized Arguments
28 */
29 public function sanitize_arguments( $args ) {
30
31 foreach ( $args as $key => $value ) {
32 if ( empty( $value ) || ! $value ) {
33 unset( $args[ $key ] );
34 }
35 }
36
37 return $args;
38
39 }
40
41 /**
42 * Private function to perform a GET request
43 *
44 * @since 1.0.0
45 *
46 * @param string $cmd Command (required).
47 * @param array $params Params (optional).
48 * @return mixed WP_Error | object
49 */
50 public function get( $cmd, $params = array() ) {
51
52 return $this->request( $cmd, 'get', $params );
53
54 }
55
56 /**
57 * Private function to perform a POST request
58 *
59 * @since 1.0.0
60 *
61 * @param string $cmd Command (required).
62 * @param array $params Params (optional).
63 * @return mixed WP_Error | object
64 */
65 public function post( $cmd, $params = array() ) {
66
67 return $this->request( $cmd, 'post', $params );
68
69 }
70
71 /**
72 * Main function which handles sending requests to an API
73 *
74 * @since 1.0.0
75 *
76 * @param string $cmd Command.
77 * @param string $method Method (get|post).
78 * @param array $params Parameters (optional).
79 * @return mixed WP_Error | object
80 */
81 private function request( $cmd, $method = 'get', $params = array() ) {
82
83 // Set timeout.
84 $timeout = 20;
85
86 /**
87 * Defines the number of seconds before timing out a request to the remote API.
88 *
89 * @since 3.0.0
90 *
91 * @param int $timeout Timeout, in seconds.
92 */
93 $timeout = apply_filters( $this->base->plugin->filter_name . '_api_request_timeout', $timeout );
94
95 // Send request.
96 $result = $this->request_wordpress( $this->api_endpoint, $cmd, $method, $params, $timeout );
97
98 // Result will be WP_Error or the data we expect.
99 return $result;
100
101 }
102
103 /**
104 * Performs POST and GET requests.
105 *
106 * @since 1.7.1
107 *
108 * @param string $url URL.
109 * @param string $cmd API Command.
110 * @param string $method Method (post|get).
111 * @param array $params Parameters.
112 * @param int $timeout Timeout, in seconds (default: 10).
113 * @return mixed WP_Error | object
114 */
115 private function request_wordpress( $url, $cmd, $method, $params, $timeout = 20 ) {
116
117 // Send request.
118 switch ( $method ) {
119 /**
120 * GET
121 */
122 case 'get':
123 case 'GET':
124 $response = wp_remote_get(
125 $url . '&' . http_build_query(
126 array(
127 'endpoint' => $cmd,
128 'params' => $params,
129 )
130 ),
131 array(
132 'timeout' => $timeout,
133 )
134 );
135 break;
136
137 /**
138 * POST
139 */
140 case 'post':
141 case 'POST':
142 $response = wp_remote_post(
143 $url,
144 array(
145 'body' => array(
146 'endpoint' => $cmd,
147 'params' => $params,
148 ),
149 'timeout' => $timeout,
150 )
151 );
152 break;
153 }
154
155 // If an error occured, return it now.
156 if ( is_wp_error( $response ) ) {
157 return $response;
158 }
159
160 // Fetch HTTP code and body.
161 $http_code = wp_remote_retrieve_response_code( $response );
162 $response = wp_remote_retrieve_body( $response );
163
164 // Decode the result.
165 $result = json_decode( $response );
166
167 // If the response is empty or missing the data payload, return a generic error.
168 if ( is_null( $result ) || ! isset( $result->data ) ) {
169 return new \WP_Error(
170 $http_code,
171 'API Error: HTTP Code ' . $http_code . '. Sorry, we don\'t have any more information about this error. Please try again.'
172 );
173 }
174
175 // If the response's success flag is false, return the data as an error.
176 if ( ! $result->success ) {
177 return new \WP_Error( $http_code, $result->data );
178 }
179
180 // All OK - return the data.
181 // This is from the originating API request, and we no longer need it.
182 unset( $result->data->status );
183
184 // object comprising of data, links + meta.
185 return $result->data;
186
187 }
188
189 }
190