PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
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 / modules / connect-manager / components / connect.php

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

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