PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.4.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.4.0
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 / classes / data.php

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

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