PluginProbe
Image Optimization – Compress Images and Convert to WebP or AVIF / 1.6.6
Image Optimization – Compress Images and Convert to WebP or AVIF v1.6.6
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 1.6.6 All 32 releases
image-optimization / modules / connect-manager / components / connect.php

connect.php in Image Optimization – Compress Images and Convert to WebP or AVIF 1.6.6, at modules/connect-manager/components/connect.php

192 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 ImageOptimization\Modules\ConnectManager\Components;
3
4 use ImageOptimization\Modules\Connect\{
5 Module as ConnectModule,
6 Classes\Data,
7 Classes\Service,
8 Classes\Utils as ConnectUtils,
9 Rest\Authorize,
10 Rest\Version
11 };
12
13 use ImageOptimization\Classes\Logger;
14 use ImageOptimization\Classes\Utils;
15 use Throwable;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit; // exit if accessed directly
19 }
20
21 class Connect implements Connect_Interface {
22
23 const STATUS_CHECK_TRANSIENT = 'image_optimizer_status_check';
24
25 public function is_connected() : bool {
26 return ConnectModule::is_connected();
27 }
28
29 public function is_activated() : bool {
30 return ConnectModule::is_connected();
31 }
32
33 public function is_valid_home_url() : bool {
34 return ConnectUtils::is_valid_home_url();
35 }
36
37 public function get_connect_status() {
38 if ( ! $this->is_connected() ) {
39 Logger::log( Logger::LEVEL_INFO, 'Status check error. Reason: User is not connected' );
40 return null;
41 }
42
43 $cached_status = get_transient( self::STATUS_CHECK_TRANSIENT );
44
45 if ( $cached_status ) {
46 return $cached_status;
47 }
48
49 try {
50 $response = Utils::get_api_client()->make_request(
51 'POST',
52 'status/check'
53 );
54 } catch ( Throwable $t ) {
55 Logger::log(
56 Logger::LEVEL_ERROR,
57 'Status check error. Reason: ' . $t->getMessage()
58 );
59
60 return null;
61 }
62
63 if ( ! isset( $response->status ) ) {
64 Logger::log( Logger::LEVEL_ERROR, 'Invalid response from server' );
65
66 return null;
67 }
68
69 if ( ! empty( $response->site_url ) && Data::get_home_url() !== $response->site_url ) {
70 Data::set_home_url( $response->site_url );
71 }
72
73 set_transient( self::STATUS_CHECK_TRANSIENT, $response, MINUTE_IN_SECONDS * 5 );
74
75 return $response;
76 }
77
78 public function get_connect_data( bool $force = false ): array {
79 $data = get_transient( self::STATUS_CHECK_TRANSIENT );
80
81 $user = [];
82
83 // Return empty array if transient does not exist or is expired.
84 if ( ! $data ) {
85 return $user;
86 }
87
88 // Return if user property does not exist in the data object.
89 if ( ! property_exists( $data, 'user' ) ) {
90 return $user;
91 }
92
93 if ( $data->user->email ) {
94 $user = [
95 'user' => [
96 'email' => $data->user->email,
97 ],
98 ];
99 }
100
101 return $user;
102 }
103
104 public function update_usage_data( $new_usage_data ): void {
105 $connect_status = $this->get_connect_status();
106
107 if ( ! isset( $new_usage_data->allowed ) || ! isset( $new_usage_data->used ) ) {
108 return;
109 }
110
111 if ( 0 === $new_usage_data->allowed - $new_usage_data->used ) {
112 $connect_status->status = 'expired';
113 }
114
115 $connect_status->quota = $new_usage_data->allowed;
116 $connect_status->used_quota = $new_usage_data->used;
117
118 set_transient( self::STATUS_CHECK_TRANSIENT, $connect_status, MINUTE_IN_SECONDS * 5 );
119 }
120
121 public function get_activation_state() : string {
122 /**
123 * Returning true because the license key is
124 * not used for deactivation in connect.
125 */
126 return true;
127 }
128
129 public function get_access_token() {
130 return Data::get_access_token();
131
132 }
133
134 public function get_client_id(): string {
135 return Data::get_client_id();
136 }
137
138 public function get_client_secret(): string {
139 return Data::get_client_secret();
140 }
141
142 public function images_left(): int {
143 $plan_data = $this->get_connect_status();
144
145 if ( empty( $plan_data ) ) {
146 return 0;
147 }
148
149 $quota = $plan_data->quota;
150 $used_quota = $plan_data->used_quota;
151
152 return max( $quota - $used_quota, 0 );
153 }
154
155 public function user_is_subscription_owner(): bool {
156 return Data::user_is_subscription_owner();
157 }
158
159 // Nonces.
160 public function connect_init_nonce(): string {
161 return Authorize::NONCE_NAME;
162 }
163
164 public function disconnect_nonce(): string {
165 return Authorize::NONCE_NAME;
166 }
167
168 public function deactivate_nonce(): string {
169 return Authorize::NONCE_NAME;
170 }
171
172 public function get_subscriptions_nonce(): string {
173 return Authorize::NONCE_NAME;
174 }
175
176 public function activate_nonce(): string {
177 return Authorize::NONCE_NAME;
178 }
179
180 public function version_nonce(): string {
181 return Version::NONCE_NAME;
182 }
183
184 public function get_is_connect_on_fly(): bool {
185 return true;
186 }
187
188 public function refresh_token(): void {
189 Service::refresh_token();
190 }
191 }
192