PluginProbe
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder / 2.13.0
aBlocks – Gutenberg Blocks, User Dashboard Builder, Popup Builder, Form Builder & Animation Builder v2.13.0
2.13.0 2.13.1 2.12.0 2.11.1 2.11.0 2.10.0 2.9.0 2.7.4 2.7.5 2.7.6 2.7.7 2.8.0 2.8.1 2.9.1 trunk 1.0 1.0-beta1 1.0-beta2 1.0-beta3 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 All 80 releases
← All changes | includes/classes/request.php +54 -0 2.7.42.13.0 View file →
@@ -1,0 +1,54 @@
1 +<?php
2 +namespace ABlocks\Classes;
3 +
4 +use Exception;
5 +/**
6 + * @class Request
7 + */
8 +class Request {
9 + private string $base_url;
10 + private array $headers = [];
11 +
12 + public function __construct( string $base_url = '', array $headers = [] ) {
13 + $this->base_url = $base_url;
14 + $this->headers = $headers;
15 + }
16 +
17 + public function get( string $pathOrQuery = '' ) {
18 + return $this->send( $pathOrQuery );
19 + }
20 +
21 + public function post( string $pathOrQuery = '', $body = null ) {
22 + return $this->send( $pathOrQuery, 'POST', $body );
23 + }
24 +
25 + public function put( string $pathOrQuery = '', $body = null ) {
26 + return $this->send( $pathOrQuery, 'PUT', $body );
27 + }
28 +
29 + public function send( string $pathOrQuery = '', string $method = 'GET', $body = null ) {
30 + $args = [
31 + 'method' => $method,
32 + 'headers' => $this->headers,
33 + ];
34 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
35 + if ( in_array( $method, [ 'PUT', 'POST' ] ) && $body ) {
36 + $args['body'] = $body;
37 + }
38 +
39 + $url = $this->base_url . $pathOrQuery;
40 +
41 + if ( empty( $url ) ) {
42 + return false;
43 + }
44 +
45 + $res = wp_remote_request( $url, $args );
46 + return [
47 + 'error' => is_wp_error( $res ),
48 + 'status_code' => intval( wp_remote_retrieve_response_code( $res ) ),
49 + 'body' => json_decode( wp_remote_retrieve_body( $res ), true ),
50 + 'result' => $res,
51 + ];
52 + }
53 +
54 +}