PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.6
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.6
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / elementor / wp-one-package / src / Connect / Classes / Utils.php

Utils.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.6, at vendor/elementor/wp-one-package/src/Connect/Classes/Utils.php

197 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ElementorOne\Connect\Classes;
4
5 use ElementorOne\Connect\Facade;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly
9 }
10
11 /**
12 * Class Utils
13 */
14 class Utils {
15
16 /**
17 * Facade instance
18 * @var Facade
19 */
20 private Facade $facade;
21
22 /**
23 * Utils constructor
24 * @param Facade $facade
25 */
26 public function __construct( Facade $facade ) {
27 $this->facade = $facade;
28 }
29
30 /**
31 * Get clients URL
32 * @param string $client_id
33 * @return string
34 */
35 public function get_clients_url( string $client_id = '' ): string {
36 return $this->get_base_url() . '/api/v1/clients' . ( $client_id ? "/{$client_id}" : '' );
37 }
38
39 /**
40 * Get license info URL
41 * @return string
42 */
43 public function get_license_info_url(): string {
44 return $this->get_base_url() . '/api/v1/license/info';
45 }
46
47 /**
48 * Get redirect URI
49 * @param string $domain
50 * @return string
51 */
52 public function get_admin_url( string $domain = '' ): string {
53 $admin_page = $this->facade->get_config( 'admin_page' );
54
55 if ( false !== strpos( $admin_page, '?page=' ) ) {
56 $admin_url = admin_url( $admin_page );
57 } else {
58 $admin_url = admin_url( 'admin.php?page=' . $admin_page );
59 }
60
61 if ( $domain ) {
62 $parsed_url = wp_parse_url( $admin_url );
63 $path = $parsed_url['path'] . ( isset( $parsed_url['query'] ) ? '?' . $parsed_url['query'] : '' );
64
65 return rtrim( $domain, '/' ) . $path;
66 }
67
68 return $admin_url;
69 }
70
71 /**
72 * Get auth URL
73 * @return string
74 */
75 public function get_auth_url(): string {
76 return $this->get_base_url() . '/v1/oauth2/auth';
77 }
78
79 /**
80 * Get authorize URL
81 * @param string $client_id
82 * @return string
83 */
84 public function get_authorize_url( string $client_id ): string {
85 $scopes = $this->facade->get_config( 'scopes' );
86 $state_nonce = $this->facade->get_config( 'state_nonce' );
87
88 $authorize_url = add_query_arg( [
89 'client_id' => $client_id,
90 'redirect_uri' => rawurlencode( $this->get_admin_url() ),
91 'response_type' => 'code',
92 'scope' => $scopes,
93 'state' => wp_create_nonce( $state_nonce ),
94 ], $this->get_auth_url() );
95
96 // Filter the authorize URL for all apps
97 $authorize_url = apply_filters( 'elementor_one/connect_authorize_url', $authorize_url, $this->facade );
98
99 // Filter the authorize URL for the app prefix
100 $app_prefix = $this->facade->get_config( 'app_prefix' );
101 return apply_filters( 'elementor_one/' . $app_prefix . '_connect_authorize_url', $authorize_url, $this->facade );
102 }
103
104 /**
105 * Get deactivation URL
106 * @param string $client_id
107 * @return string
108 */
109 public function get_deactivation_url( string $client_id ): string {
110 return $this->get_base_url() . "/api/v1/clients/{$client_id}/activation";
111 }
112
113 /**
114 * Get JWKS URL
115 * @return string
116 */
117 public function get_jwks_url(): string {
118 return $this->get_base_url() . '/v1/.well-known/jwks.json';
119 }
120
121 /**
122 * Get sessions URL
123 * @return string
124 */
125 public function get_sessions_url(): string {
126 return $this->get_base_url() . '/api/v1/session';
127 }
128
129 /**
130 * Get token URL
131 * @return string
132 */
133 public function get_token_url(): string {
134 return $this->get_base_url() . '/api/v1/oauth2/token';
135 }
136
137 /**
138 * Patch clients URL
139 *
140 * @param string $client_id
141 *
142 * @return string
143 */
144 public function get_clients_patch_url( string $client_id ): string {
145 return $this->get_clients_url( $client_id );
146 }
147
148 /**
149 * Get base URL
150 * @return string
151 */
152 public function get_base_url(): string {
153 $base_url = $this->facade->get_config( 'base_url' );
154 return apply_filters( 'elementor_one/connect_get_base_url', $base_url, $this->facade );
155 }
156
157 /**
158 * Check if home URL is valid
159 * @return bool
160 */
161 public function is_valid_home_url(): bool {
162 return $this->facade->home_url()->is_valid();
163 }
164
165 /**
166 * Check if connected
167 * @return bool
168 */
169 public function is_connected(): bool {
170 $data = $this->facade->data();
171 return (bool) $data->get_access_token() && $this->is_valid_home_url();
172 }
173
174 /**
175 * Decode JWT and return payload without signature verification
176 * @param string $jwt
177 * @return array|null
178 */
179 public static function decode_jwt( string $jwt ): ?array {
180 $parts = explode( '.', $jwt );
181
182 if ( count( $parts ) !== 3 ) {
183 return null;
184 }
185
186 $payload = base64_decode( strtr( $parts[1], '-_', '+/' ) );
187
188 if ( ! $payload ) {
189 return null;
190 }
191
192 $decoded = json_decode( $payload, true );
193
194 return is_array( $decoded ) ? $decoded : null;
195 }
196 }
197