templates
1 year ago
SendinblueAccount.php
1 year ago
SendinblueApiClient.php
1 year ago
function.wp_mail.php
8 years ago
http-build-url.php
1 year ago
index.php
8 years ago
mailin.php
3 years ago
push-admin.php
1 year ago
push-amp.php
1 year ago
push-api.php
1 year ago
push-httpclient.php
1 year ago
push-public.php
1 year ago
push-settings.php
1 year ago
push-utils.php
1 year ago
push-woocommerce.php
1 year ago
sendinblue.php
3 years ago
sib-api-manager.php
1 year ago
sib-form-preview.php
2 years ago
sib-sms-code.php
3 years ago
table-forms.php
1 year ago
push-httpclient.php
119 lines
| 1 | <?php |
| 2 | |
| 3 | use WonderPush\Net\Request; |
| 4 | use WonderPush\Net\Response; |
| 5 | |
| 6 | /** |
| 7 | * An implementation of \WonderPush\Net\HttpClientInterface that uses the Curl shipped with WordPress |
| 8 | */ |
| 9 | |
| 10 | class SIB_Push_HttpClient implements \WonderPush\Net\HttpClientInterface { |
| 11 | |
| 12 | /** @var WP_Http */ |
| 13 | var $http; |
| 14 | /** @var \WonderPush\WonderPush */ |
| 15 | var $wp; |
| 16 | public function __construct(\WonderPush\WonderPush $wonderPush) { |
| 17 | $this->http = new WP_Http(); |
| 18 | $this->wp = $wonderPush; |
| 19 | } |
| 20 | |
| 21 | public function execute(Request $request) { |
| 22 | // Construct absolute URL |
| 23 | $root = $request->getRoot() ?: $this->wp->getApiRoot(); |
| 24 | $path = $request->getPath(); |
| 25 | if (!\WonderPush\Util\StringUtil::beginsWith($path, '/')) { |
| 26 | $path = '/' . $path; |
| 27 | } |
| 28 | $url = $root . $path; |
| 29 | $qsParams = $request->getQsParams(); |
| 30 | $headers = $request->getHeaders(); |
| 31 | $body = null; |
| 32 | |
| 33 | // Construct $qsParams and $body, and honors $request->getParams() too |
| 34 | switch ($request->getMethod()) { |
| 35 | case Request::GET: |
| 36 | case Request::DELETE: |
| 37 | $qsParams = array_merge($qsParams, $request->getParams()); |
| 38 | break; |
| 39 | case Request::PUT: |
| 40 | case Request::POST: |
| 41 | case Request::PATCH: |
| 42 | $body = $request->getParams(); |
| 43 | $files = $request->getFiles(); |
| 44 | if (count($files)) { |
| 45 | $body = $request->getParams() ?: array(); |
| 46 | foreach ($files as $name => $file) { |
| 47 | $body[$name] = new \CURLFile($file['tmp_name'], $file['type'], $file['name']); |
| 48 | } |
| 49 | } else if (empty($body)) { |
| 50 | $body = null; |
| 51 | } else { |
| 52 | $headers['Content-Type'] = 'application/json'; |
| 53 | $options = 0; |
| 54 | if (defined('JSON_UNESCAPED_SLASHES')) { |
| 55 | $options |= JSON_UNESCAPED_SLASHES; |
| 56 | } |
| 57 | if (defined('JSON_INVALID_UTF8_SUBSTITUTE')) { |
| 58 | $options |= JSON_INVALID_UTF8_SUBSTITUTE; |
| 59 | } else if (defined('JSON_PARTIAL_OUTPUT_ON_ERROR')) { |
| 60 | $options |= JSON_PARTIAL_OUTPUT_ON_ERROR; |
| 61 | } |
| 62 | $body = json_encode($body, $options); |
| 63 | } |
| 64 | break; |
| 65 | } |
| 66 | |
| 67 | // Incorporate query string into URL |
| 68 | if (!empty($qsParams)) { |
| 69 | $prevQs = \WonderPush\Util\UrlUtil::parseQueryString(parse_url($url, PHP_URL_QUERY)); |
| 70 | $qsParams = array_merge($prevQs, $qsParams); |
| 71 | $url = \WonderPush\Util\UrlUtil::replaceQueryStringInUrl($url, $qsParams); |
| 72 | } |
| 73 | |
| 74 | if (!isset($headers['User-Agent'])) { |
| 75 | $curlVersion = array(); |
| 76 | if (function_exists("curl_version")) { |
| 77 | $curlVersion = curl_version(); |
| 78 | } |
| 79 | $headers['User-Agent'] = 'BrevoPushApi/' . \WonderPush\WonderPush::API_VERSION |
| 80 | . ' WonderPushPhpLib/' . \WonderPush\WonderPush::VERSION |
| 81 | . ' curl/' . \WonderPush\Util\ArrayUtil::getIfSet($curlVersion, 'version', 'na') |
| 82 | . ' ' . \WonderPush\Util\ArrayUtil::getIfSet($curlVersion, 'ssl_version', 'curlssl/na') |
| 83 | ; |
| 84 | } |
| 85 | |
| 86 | $rawResponse = $this->http->request($url, array( |
| 87 | 'method' => $request->getMethod() ? strtoupper($request->getMethod()) : 'GET', |
| 88 | 'headers' => $headers, |
| 89 | 'body' => $body, |
| 90 | )); |
| 91 | |
| 92 | // Parse response |
| 93 | $response = new Response(); |
| 94 | $response->setRequest($request); |
| 95 | |
| 96 | if (is_wp_error($rawResponse)) { |
| 97 | $response->setStatusCode(0); |
| 98 | } else { |
| 99 | $response->setStatusCode( |
| 100 | isset($rawResponse['response']) |
| 101 | && is_array($rawResponse['response']) |
| 102 | && isset($rawResponse['response']['code']) |
| 103 | ? $rawResponse['response']['code'] : 0 |
| 104 | ); |
| 105 | $response->setRawBody(isset($rawResponse['body']) ? $rawResponse['body'] : null); |
| 106 | $responseHeaders = array(); |
| 107 | // Make a copy of the headers, which is not a regular array but a Requests_Utility_CaseInsensitiveDictionary |
| 108 | if (isset($rawResponse['headers'])) { |
| 109 | foreach ($rawResponse['headers'] as $key => $val) { |
| 110 | $responseHeaders[$key] = $val; |
| 111 | } |
| 112 | } |
| 113 | $response->setHeaders($responseHeaders); |
| 114 | } |
| 115 | return $response; |
| 116 | } |
| 117 | |
| 118 | } |
| 119 |