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

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