PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 3.8.2
Social Media Auto Poster – Schedule & Publish to Buffer v3.8.2
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 3.8.8 All 124 releases
wp-to-buffer / vendor / includes / admin / api.php

api.php in Social Media Auto Poster – Schedule & Publish to Buffer 3.8.2, at vendor/includes/admin/api.php

203 lines 6.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. Used by other classes to perform POST and GET requests.
4 *
5 * @package WP_To_Social_Pro
6 * @author Tim Carr
7 * @version 1.0.0
8 */
9 class WP_To_Social_Pro_API {
10
11 /**
12 * Sanitizes API arguments, by removing false or empty
13 * arguments in the array.
14 *
15 * @since 1.0.0
16 *
17 * @param array $args Arguments
18 * @return array Sanitized Arguments
19 */
20 public function sanitize_arguments( $args ) {
21
22 foreach ( $args as $key => $value ) {
23 if ( empty( $value ) || ! $value ) {
24 unset( $args[ $key ] );
25 }
26 }
27
28 return $args;
29
30 }
31
32 /**
33 * Private function to perform a GET request
34 *
35 * @since 1.0.0
36 *
37 * @param string $cmd Command (required)
38 * @param array $params Params (optional)
39 * @return mixed WP_Error | object
40 */
41 public function get( $cmd, $params = array() ) {
42
43 return $this->request( $cmd, 'get', $params );
44
45 }
46
47 /**
48 * Private function to perform a POST request
49 *
50 * @since 1.0.0
51 *
52 * @param string $cmd Command (required)
53 * @param array $params Params (optional)
54 * @return mixed WP_Error | object
55 */
56 public function post( $cmd, $params = array() ) {
57
58 return $this->request( $cmd, 'post', $params );
59
60 }
61
62 /**
63 * Main function which handles sending requests to an API
64 *
65 * @since 1.0.0
66 *
67 * @param string $cmd Command
68 * @param string $method Method (get|post)
69 * @param array $params Parameters (optional)
70 * @return mixed WP_Error | object
71 */
72 private function request( $cmd, $method = 'get', $params = array() ) {
73
74 // Set timeout
75 $timeout = 20;
76
77 /**
78 * Defines the number of seconds before timing out a request to the remote API.
79 *
80 * @since 3.0.0
81 *
82 * @param int $timeout Timeout, in seconds
83 */
84 $timeout = apply_filters( 'wp_to_social_pro_api_request_timeout', $timeout );
85
86 // Send request
87 $result = $this->request_curl( $this->api_endpoint, $cmd, $method, $params, $timeout );
88
89 // Result will be WP_Error or the data we expect
90 return $result;
91
92 }
93
94 /**
95 * Performs POST and GET requests through PHP's curl_exec() function.
96 *
97 * If this function is called, request_wordpress() failed, most likely
98 * due to a DNS lookup failure or CloudFlare failing to respond.
99 *
100 * @since 1.7.1
101 *
102 * @param string $url URL
103 * @param string $cmd API Command
104 * @param string $method Method (post|get)
105 * @param array $params Parameters
106 * @param int $timeout Timeout, in seconds (default: 10)
107 * @return mixed WP_Error | object
108 */
109 private function request_curl( $url, $cmd, $method, $params, $timeout = 20 ) {
110
111 // Bail if cURL isn't installed
112 if ( ! function_exists( 'curl_init' ) ) {
113 return new WP_Error(
114 $this->base->plugin->name . '_api_request_curl',
115 sprintf(
116 /* translators: Plugin Name */
117 __( '%s requires the PHP cURL extension to be installed and enabled by your web host.', 'wp-to-social-pro' ),
118 $this->base->plugin->displayName
119 )
120 );
121 }
122
123 // Init
124 $ch = curl_init();
125
126 // Set request specific options
127 switch ( $method ) {
128 /**
129 * GET
130 */
131 case 'get':
132 case 'GET':
133 curl_setopt_array( $ch, array(
134 CURLOPT_URL => $url . '&' . http_build_query( array(
135 'endpoint' => $cmd,
136 'params' => $params,
137 ) ),
138 CURLOPT_RESOLVE => $this->api_endpoint_resolutions,
139 ) );
140 break;
141
142 /**
143 * POST
144 */
145 case 'post':
146 case 'POST':
147 curl_setopt_array( $ch, array(
148 CURLOPT_URL => $url,
149 CURLOPT_POST => true,
150 CURLOPT_POSTFIELDS => http_build_query( array(
151 'endpoint' => $cmd,
152 'params' => $params,
153 ) ),
154 CURLOPT_RESOLVE => $this->api_endpoint_resolutions,
155 ) );
156 break;
157 }
158
159 // Set shared options
160 curl_setopt_array( $ch, array(
161 CURLOPT_RETURNTRANSFER => true,
162 CURLOPT_HEADER => false,
163 CURLOPT_FOLLOWLOCATION => true,
164 CURLOPT_MAXREDIRS => 10,
165 CURLOPT_CONNECTTIMEOUT => $timeout,
166 CURLOPT_TIMEOUT => $timeout,
167 ) );
168
169 // Execute
170 $result = curl_exec( $ch );
171 $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
172 $error = curl_error( $ch );
173 curl_close( $ch );
174
175 // If our error string isn't empty, something went wrong
176 if ( ! empty( $error ) ) {
177 return new WP_Error( 'wp_to_social_pro_api_request_curl', $error );
178 }
179
180 // Decode the result
181 $result = json_decode( $result );
182
183 // If the response is empty or missing the data payload, return a generic error
184 if ( is_null( $result ) || ! isset( $result->data ) ) {
185 return new WP_Error(
186 $http_code,
187 'API Error: HTTP Code ' . $http_code . '. Sorry, we don\'t have any more information about this error. Please try again.'
188 );
189 }
190
191 // If the response's success flag is false, return the data as an error
192 if ( ! $result->success ) {
193 return new WP_Error( $http_code, $result->data );
194 }
195
196 // All OK - return the data
197 unset( $result->data->status ); // This is from the originating API request, and we no longer need it
198
199 return $result->data; // object comprising of data, links + meta
200
201 }
202
203 }