PluginProbe
Manage – Centralized site maintenance and monitoring / trunk
Manage – Centralized site maintenance and monitoring vtrunk
1.0.9 1.0.8 1.0.7 1.0.6 1.0.5 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4
manage / classes / platform-client.php

platform-client.php in Manage – Centralized site maintenance and monitoring trunk, at classes/platform-client.php

142 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Manage\Classes;
3
4 use Manage\Modules\Connect\Module as Connect;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 abstract class Platform_Client {
11 public const BASE_URL = 'https://my.elementor.com/';
12
13 protected bool $refreshed = false;
14
15 abstract protected static function get_api_path(): string;
16
17 abstract protected static function get_grant_type(): string;
18
19 protected static function get_base_url(): string {
20 return trailingslashit( self::BASE_URL ) . ltrim( static::get_api_path(), '/' );
21 }
22
23 protected function get_remote_url( string $endpoint ): string {
24 return trailingslashit( static::get_base_url() ) . ltrim( $endpoint, '/' );
25 }
26
27 protected static function is_connected(): bool {
28 return Connect::is_connected();
29 }
30
31 protected function add_bearer_token( array $headers ): array {
32 if ( static::is_connected() ) {
33 $headers['Authorization'] = 'Bearer ' . Connect::get_connect()->data()->get_access_token( static::get_grant_type() );
34 }
35
36 return $headers;
37 }
38
39 protected function renew_access_token(): void {
40 Connect::get_connect()->service()->renew_access_token( static::get_grant_type() );
41 $this->refreshed = true;
42 }
43
44 protected function make_request( string $method, string $endpoint, array $body = [], array $headers = [], bool $send_json = false ) {
45 $headers = array_replace_recursive(
46 $headers,
47 static::is_connected() ? $this->add_bearer_token( [] ) : []
48 );
49
50 if ( $send_json ) {
51 $headers['Content-Type'] = 'application/json';
52 $body = wp_json_encode( $body );
53 }
54
55 return $this->request(
56 $method,
57 $endpoint,
58 [
59 'timeout' => 100,
60 'headers' => $headers,
61 'body' => $body,
62 ]
63 );
64 }
65
66 protected function should_retry_after_unauthorized(): bool {
67 return true;
68 }
69
70 protected function handle_empty_body( string $raw_body, int $response_code, $decoded_body, array $response ) {
71 if ( empty( $decoded_body ) && \WP_Http::OK !== $response_code ) {
72 return new \WP_Error( $response_code, wp_remote_retrieve_response_message( $response ) );
73 }
74
75 return null;
76 }
77
78 protected function handle_non_ok_response( $decoded_body, int $response_code, array $response ): ?\WP_Error {
79 return null;
80 }
81
82 protected function build_non_ok_error( $decoded_body, int $response_code, array $response ): \WP_Error {
83 $message = $decoded_body->message ?? wp_remote_retrieve_response_message( $response );
84 $message = is_array( $message ) ? join( ', ', $message ) : $message;
85 $code = isset( $decoded_body->code ) ? (int) $decoded_body->code : $response_code;
86
87 return new \WP_Error( $code, $message, $decoded_body );
88 }
89
90 protected function request( string $method, string $endpoint, array $args = [] ) {
91 $args['method'] = $method;
92
93 $response = wp_remote_request( $this->get_remote_url( $endpoint ), $args );
94
95 if ( is_wp_error( $response ) ) {
96 $message = $response->get_error_message();
97
98 return new \WP_Error( $response->get_error_code(), is_array( $message ) ? join( ', ', $message ) : $message );
99 }
100
101 $body = wp_remote_retrieve_body( $response );
102 $response_code = (int) wp_remote_retrieve_response_code( $response );
103
104 if ( ! $response_code ) {
105 return new \WP_Error( \WP_Http::INTERNAL_SERVER_ERROR, 'No Response' );
106 }
107
108 if ( 'null' === $body || 'OK' === $body ) {
109 return true;
110 }
111
112 if ( in_array( $response_code, [ \WP_Http::CREATED, \WP_Http::NO_CONTENT ], true ) ) {
113 return true;
114 }
115
116 $decoded_body = json_decode( $body );
117
118 $empty_body_result = $this->handle_empty_body( $body, $response_code, $decoded_body, $response );
119 if ( is_wp_error( $empty_body_result ) ) {
120 return $empty_body_result;
121 }
122
123 if ( ! $this->refreshed && \WP_Http::UNAUTHORIZED === $response_code && $this->should_retry_after_unauthorized() ) {
124 $this->renew_access_token();
125 $args['headers'] = $this->add_bearer_token( $args['headers'] ?? [] );
126
127 return $this->request( $method, $endpoint, $args );
128 }
129
130 $hook_result = $this->handle_non_ok_response( $decoded_body, $response_code, $response );
131 if ( is_wp_error( $hook_result ) ) {
132 return $hook_result;
133 }
134
135 if ( \WP_Http::OK !== $response_code ) {
136 return $this->build_non_ok_error( $decoded_body, $response_code, $response );
137 }
138
139 return $decoded_body;
140 }
141 }
142