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