Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Addon/Cloudflare/API/Client.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + declare(strict_types=1);
3 +
4 + namespace WP_Rocket\Addon\Cloudflare\API;
5 +
6 + use WP_Error;
7 + use WP_Rocket\Addon\Cloudflare\Auth\AuthInterface;
8 +
9 + class Client {
10 + const CLOUDFLARE_API = 'https://api.cloudflare.com/client/v4/';
11 +
12 + /**
13 + * Auth object
14 + *
15 + * @var AuthInterface
16 + */
17 + private $auth;
18 +
19 + /**
20 + * An array of arguments for wp_remote_request()
21 + *
22 + * @var mixed[]
23 + */
24 + protected $args = [];
25 +
26 + /**
27 + * Constructor.
28 + *
29 + * @param AuthInterface $auth Auth implementation.
30 + */
31 + public function __construct( AuthInterface $auth ) {
32 + $this->auth = $auth;
33 + $this->args = [
34 + 'sslverify' => true,
35 + 'body' => [],
36 + 'headers' => [],
37 + ];
38 + }
39 + /**
40 + * Change client auth.
41 + *
42 + * @param AuthInterface $auth Client auth.
43 + *
44 + * @return void
45 + */
46 + public function set_auth( AuthInterface $auth ) {
47 + $this->auth = $auth;
48 + }
49 +
50 + /**
51 + * API call method for sending requests using GET.
52 + *
53 + * @param string $path Path of the endpoint.
54 + * @param mixed[] $data Data to be sent along with the request.
55 + *
56 + * @return object
57 + */
58 + public function get( $path, array $data = [] ) {
59 + return $this->request( $path, 'get', $data );
60 + }
61 +
62 + /**
63 + * API call method for sending requests using POST.
64 + *
65 + * @param string $path Path of the endpoint.
66 + * @param array $data Data to be sent along with the request.
67 + *
68 + * @return object
69 + */
70 + public function post( $path, array $data = [] ) {
71 + return $this->request( $path, 'post', $data );
72 + }
73 +
74 + /**
75 + * API call method for sending requests using DELETE.
76 + *
77 + * @param string $path Path of the endpoint.
78 + * @param array $data Data to be sent along with the request.
79 + *
80 + * @return object
81 + */
82 + public function delete( $path, array $data = [] ) {
83 + return $this->request( $path, 'delete', $data );
84 + }
85 +
86 + /**
87 + * API call method for sending requests using PATCH.
88 + *
89 + * @param string $path Path of the endpoint.
90 + * @param array $data Data to be sent along with the request.
91 + *
92 + * @return object
93 + */
94 + public function patch( $path, array $data = [] ) {
95 + return $this->request( $path, 'patch', $data );
96 + }
97 +
98 + /**
99 + * API call method for sending requests
100 + *
101 + * @param string $path Path of the endpoint.
102 + * @param string $method Type of method that should be used.
103 + * @param array $data Data to be sent along with the request.
104 + *
105 + * @return object|WP_Error
106 + */
107 + protected function request( $path, $method = 'get', array $data = [] ) {
108 + if ( '/ips' !== $path ) {
109 + $valid = $this->auth->is_valid_credentials();
110 +
111 + if ( is_wp_error( $valid ) ) {
112 + return $valid;
113 + }
114 +
115 + if ( ! $valid ) {
116 + return new WP_Error( 'cloudflare_invalid_credentials', 'Cloudflare credentials are invalid.' );
117 + }
118 + }
119 +
120 + $response = $this->do_remote_request( $path, $method, $data );
121 +
122 + if ( is_wp_error( $response ) ) {
123 + return $response;
124 + }
125 +
126 + $content = wp_remote_retrieve_body( $response );
127 +
128 + if ( empty( $content ) ) {
129 + return new WP_Error( 'cloudflare_no_reply', __( 'Cloudflare did not provide any reply. Please try again later.', 'rocket' ) );
130 + }
131 +
132 + $content = json_decode( $content );
133 +
134 + if ( ! is_object( $content ) ) {
135 + return new WP_Error( 'cloudflare_content_error', __( 'Cloudflare unexpected response', 'rocket' ) );
136 + }
137 +
138 + if ( empty( $content->success ) ) {
139 + return $this->set_request_error( $content );
140 + }
141 +
142 + if ( ! property_exists( $content, 'result' ) ) {
143 + return new WP_Error( 'cloudflare_no_reply', __( 'Missing Cloudflare result.', 'rocket' ) );
144 + }
145 +
146 + return $content->result;
147 + }
148 +
149 + /**
150 + * Does the request remote request.
151 + *
152 + * @param string $path Path of the endpoint.
153 + * @param string $method Type of method that should be used.
154 + * @param array $data Data to be sent along with the request.
155 + *
156 + * @return array|WP_Error
157 + */
158 + private function do_remote_request( string $path, string $method = 'GET', array $data = [] ) {
159 + $this->args['method'] = strtoupper( $method );
160 +
161 + $headers = [
162 + 'User-Agent' => 'wp-rocket/' . rocket_get_constant( 'WP_ROCKET_VERSION' ),
163 + 'Content-Type' => 'application/json',
164 + ];
165 +
166 + if ( '/ips' !== $path ) {
167 + $this->args['headers'] = array_merge( $headers, $this->auth->get_headers() );
168 + }
169 +
170 + $this->args['body'] = [];
171 +
172 + if ( ! empty( $data ) ) {
173 + $this->args['body'] = wp_json_encode( $data );
174 + }
175 +
176 + $response = wp_remote_request( self::CLOUDFLARE_API . $path, $this->args );
177 +
178 + return $response;
179 + }
180 +
181 + /**
182 + * Sets the WP_Error when request is not successful
183 + *
184 + * @param object $content Response object.
185 + *
186 + * @return WP_Error
187 + */
188 + private function set_request_error( $content ) {
189 + $errors = [];
190 +
191 + foreach ( $content->errors as $error ) {
192 + if (
193 + 6003 === $error->code || 9103 === $error->code ) {
194 + $msg = __( 'Incorrect Cloudflare email address or API key.', 'rocket' );
195 +
196 + $msg .= ' ' . sprintf(
197 + /* translators: %1$s = opening link; %2$s = closing link */
198 + __( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
199 + // translators: Documentation exists in EN, FR; use localized URL if applicable.
200 + '<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
201 + '</a>'
202 + );
203 +
204 + return new WP_Error( 'cloudflare_incorrect_credentials', $msg );
205 + }
206 +
207 + if ( 7003 === $error->code ) {
208 + $msg = __( 'Incorrect Cloudflare Zone ID.', 'rocket' );
209 +
210 + $msg .= ' ' . sprintf(
211 + /* translators: %1$s = opening link; %2$s = closing link */
212 + __( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
213 + // translators: Documentation exists in EN, FR; use localized URL if applicable.
214 + '<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
215 + '</a>'
216 + );
217 +
218 + return new WP_Error( 'cloudflare_incorrect_zone_id', $msg );
219 + }
220 +
221 + $errors[] = $error->message;
222 + }
223 +
224 + return new WP_Error( 'cloudflare_request_error', wp_sprintf_l( '%l ', $errors ) );
225 + }
226 + }
227 +