PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.2
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.2
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 / trunk / modules / connect / classes / data.php

data.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.2, at trunk/modules/connect/classes/data.php

344 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Modules\Connect\Classes;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly
7 }
8
9 /**
10 * Class Data
11 */
12 class Data {
13 const CLIENT_ID = '_client_id';
14 const CLIENT_SECRET = '_client_secret';
15 const ACCESS_TOKEN = '_access_token';
16 const REFRESH_TOKEN = '_refresh_token';
17 const TOKEN_ID = '_token_id';
18 const SUBSCRIPTION_ID = '_subscription_id';
19 const OPTION_OWNER_USER_ID = '_owner_user_id';
20 const HOME_URL = '_home_url';
21
22 /**
23 * get_option
24 * @param $option_name
25 * @param $default
26 *
27 * @return false|mixed|null
28 */
29 public static function get_option( $option_name, $default ) {
30 return get_option( Config::APP_PREFIX . $option_name, $default );
31 }
32
33 /**
34 * set_option
35 * @param $option_name
36 * @param $option_value
37 * @param $auto_load
38 *
39 * @return bool
40 */
41 public static function set_option( $option_name, $option_value, $auto_load = false ): bool {
42 return update_option( Config::APP_PREFIX . $option_name, $option_value, $auto_load );
43 }
44
45 /**
46 * delete_option
47 * @param $option_name
48 *
49 * @return bool
50 */
51 public static function delete_option( $option_name ) : bool {
52 return delete_option( Config::APP_PREFIX . $option_name );
53 }
54
55 /**
56 * get_user_data
57 * @param $user_id
58 * @param $data_name
59 * @param mixed|bool $default
60 *
61 * @return false|mixed
62 */
63 public static function get_user_data( $user_id, $data_name, $default = false ) {
64 $data = get_user_meta( $user_id, Config::APP_PREFIX . $data_name, true );
65
66 return empty( $data ) ? $default : $data;
67 }
68
69 /**
70 * set_user_data
71 * @param $user_id
72 * @param $data_name
73 * @param $value
74 *
75 * @return bool|int
76 */
77 public static function set_user_data( $user_id, $data_name, $value ) {
78 return update_user_meta( $user_id, Config::APP_PREFIX . $data_name, $value );
79 }
80
81 /**
82 * delete_user_data
83 * @param $user_id
84 * @param $data_name
85 *
86 * @return bool
87 */
88 public static function delete_user_data( $user_id, $data_name ): bool {
89 return delete_user_meta( $user_id, Config::APP_PREFIX . $data_name );
90 }
91
92 /**
93 * get_connect_mode_data
94 * @param ...$data
95 *
96 * @return false|mixed|null|string
97 */
98 public static function get_connect_mode_data( ...$data ) {
99 if ( Config::CONNECT_MODE === 'site' ) {
100 return self::get_option( ...$data );
101 }
102 $user_id = get_current_user_id();
103 return self::get_user_data( ...( [ $user_id, ...$data ] ) );
104 }
105
106 /**
107 * set_connect_mode_data
108 * @param ...$data
109 *
110 * @return bool|int
111 */
112 public static function set_connect_mode_data( ...$data ) {
113 if ( Config::CONNECT_MODE === 'site' ) {
114 return self::set_option( ...$data );
115 }
116 $user_id = get_current_user_id();
117 return self::set_user_data( ...( [ $user_id, ...$data ] ) );
118 }
119
120 /**
121 * get_client_id
122 *
123 * @return string|bool|mixed
124 */
125 public static function get_client_id() {
126 return self::get_connect_mode_data( self::CLIENT_ID, false );
127 }
128
129 /**
130 * get_client_secret
131 * @return false|mixed|string|null
132 */
133 public static function get_client_secret() {
134 return self::get_connect_mode_data( self::CLIENT_SECRET, false );
135 }
136
137 /**
138 * set_client_id
139 * @param $value
140 *
141 * @return bool
142 */
143 public static function set_client_id( $value ): bool {
144 return self::set_connect_mode_data( self::CLIENT_ID, $value );
145 }
146
147 public static function get_subscription_id() {
148 return self::get_connect_mode_data( self::SUBSCRIPTION_ID, false );
149 }
150
151 public static function set_subscription_id( $value ): bool {
152 return self::set_connect_mode_data( self::SUBSCRIPTION_ID, $value );
153 }
154
155 /**
156 * set_client_secret
157 * @param $value
158 *
159 * @return bool
160 */
161 public static function set_client_secret( $value ): bool {
162 return self::set_connect_mode_data( self::CLIENT_SECRET, $value );
163 }
164
165 /**
166 * get_access_token
167 * @return false|mixed|string|null
168 */
169 public static function get_access_token() {
170 return self::get_connect_mode_data( self::ACCESS_TOKEN, false );
171 }
172
173 public static function get_token_id() {
174 return self::get_connect_mode_data( self::TOKEN_ID, false );
175 }
176
177 /**
178 * get_refresh_token
179 * @return false|mixed|string|null
180 */
181 public static function get_refresh_token() {
182 return self::get_connect_mode_data( self::REFRESH_TOKEN, false );
183 }
184
185 public static function get_home_url() {
186 return self::get_connect_mode_data( self::HOME_URL, false );
187 }
188
189 public static function set_home_url( ?string $home_url = null ): bool {
190 return self::set_connect_mode_data( self::HOME_URL, $home_url ?? home_url() );
191 }
192
193 /**
194 * set_user_is_owner_option
195 */
196 public static function set_user_is_owner_option( $value ) {
197 return self::set_connect_mode_data( self::OPTION_OWNER_USER_ID, $value );
198 }
199
200 /**
201 * get_user_is_owner_option
202 */
203 public static function get_user_is_owner_option() {
204 return self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
205 }
206
207 /**
208 * fetch_option
209 * direct query to avoid cache and race condition issues
210 *
211 * @param $option_name
212 * @param $default
213 *
214 * @return mixed|null
215 */
216 public static function fetch_option( $option_name, $default = null ) {
217 global $wpdb;
218 $cache_buster = wp_generate_uuid4();
219 $option = $wpdb->get_col(
220 $wpdb->prepare(
221 "SELECT option_value
222 FROM $wpdb->options
223 WHERE option_name = %s
224 AND %s = %s
225 LIMIT 1",
226 $option_name,
227 $cache_buster,
228 $cache_buster
229 )
230 );
231 if ( ! empty( $option ) ) {
232 return $option[0];
233 }
234 return $default;
235 }
236
237 /**
238 * insert_option_uniquely
239 * used to insert option if not there already
240 * direct query to avoid cache and race condition issues
241 * @param $option_name
242 * @param $option_value
243 *
244 * @return bool
245 */
246 public static function insert_option_uniquely( $option_name, $option_value ): bool {
247 global $wpdb;
248 $cache_buster = wp_generate_uuid4();
249 $result = $wpdb->query(
250 $wpdb->prepare(
251 "INSERT INTO $wpdb->options (option_name, option_value, autoload)
252 SELECT * FROM (SELECT %s, %s, 'no') AS tmp
253 WHERE NOT EXISTS (
254 SELECT option_name
255 FROM $wpdb->options
256 WHERE option_name = %s
257 AND option_value = %s
258 AND %s = %s
259 ) LIMIT 1",
260 $option_name,
261 $option_value,
262 $option_name,
263 $option_value,
264 $cache_buster,
265 $cache_buster
266 )
267 );
268
269 if ( false === $result || 0 === $result ) {
270 // false means query failed, 0 means no row inserted because it exists
271 return false;
272 }
273
274 return true;
275 }
276
277 /**
278 * User is subscription owner.
279 *
280 * Check if current user is subscription owner.
281 *
282 * @return boolean
283 */
284 public static function user_is_subscription_owner(): bool {
285 $owner_id = (int) self::get_connect_mode_data( self::OPTION_OWNER_USER_ID, false );
286
287 return get_current_user_id() === $owner_id;
288 }
289
290 public static function ensure_reset_connect() {
291 if ( Config::CONNECT_MODE === 'site' ) {
292 self::delete_option( self::ACCESS_TOKEN );
293 self::delete_option( self::REFRESH_TOKEN );
294 self::delete_option( self::TOKEN_ID );
295 self::delete_option( self::SUBSCRIPTION_ID );
296 self::delete_option( self::OPTION_OWNER_USER_ID );
297 self::delete_option( self::HOME_URL );
298
299 delete_transient( 'image_optimizer_status_check' );
300 } else {
301 $user_id = get_current_user_id();
302 self::delete_user_data( $user_id, self::ACCESS_TOKEN );
303 self::delete_user_data( $user_id, self::REFRESH_TOKEN );
304 self::delete_user_data( $user_id, self::TOKEN_ID );
305 self::delete_user_data( $user_id, self::SUBSCRIPTION_ID );
306 self::delete_user_data( $user_id, self::OPTION_OWNER_USER_ID );
307 self::delete_user_data( $user_id, self::HOME_URL );
308
309 delete_transient( 'image_optimizer_status_check' );
310 }
311 }
312
313 /**
314 * clear_session
315 */
316 public static function clear_session() {
317 if ( Config::CONNECT_MODE === 'site' ) {
318 self::delete_option( self::CLIENT_ID );
319 self::delete_option( self::CLIENT_SECRET );
320 self::delete_option( self::ACCESS_TOKEN );
321 self::delete_option( self::REFRESH_TOKEN );
322 self::delete_option( self::TOKEN_ID );
323 self::delete_option( self::SUBSCRIPTION_ID );
324 self::delete_option( self::OPTION_OWNER_USER_ID );
325 self::delete_option( self::HOME_URL );
326
327 delete_transient( 'image_optimizer_status_check' );
328 } else {
329 $user_id = get_current_user_id();
330
331 self::delete_user_data( $user_id, self::CLIENT_ID );
332 self::delete_user_data( $user_id, self::CLIENT_SECRET );
333 self::delete_user_data( $user_id, self::ACCESS_TOKEN );
334 self::delete_user_data( $user_id, self::REFRESH_TOKEN );
335 self::delete_user_data( $user_id, self::TOKEN_ID );
336 self::delete_user_data( $user_id, self::SUBSCRIPTION_ID );
337 self::delete_user_data( $user_id, self::OPTION_OWNER_USER_ID );
338 self::delete_user_data( $user_id, self::HOME_URL );
339
340 delete_transient( 'image_optimizer_status_check' );
341 }
342 }
343 }
344