PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.3
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Utils / Options.php

Options.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.7.3, at includes/Utils/Options.php

296 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Utils;
4
5 use function get_option;
6 use function update_option;
7 use function get_user_meta;
8 use function update_user_meta;
9 use function get_current_user_id;
10
11 class Options extends Base {
12 /**
13 * Current User Id
14 * @var integer
15 */
16 private $current_user;
17
18 /**
19 * User has any api?
20 * @var boolean
21 */
22 private $has_api;
23
24 /**
25 * Pin every derived read/write to the acting user, bypassing the
26 * global-login fallback in user_id().
27 *
28 * @var bool
29 */
30 private $force_current_user = false;
31
32 /**
33 * Automatically invoked and set up the properties.
34 */
35 public function __construct(){
36 $this->current_user = get_current_user_id();
37 $this->has_api = ! empty( $this->get( 'api_key', '', $this->current_user ) );
38 }
39
40 /**
41 * Get the current user ID.
42 *
43 * `get_current_user_id()` can still be 0 when this singleton is built — a
44 * wp-cli run before `wp_set_current_user()`, or a request whose auth has not
45 * been resolved yet. Caching that 0 for the rest of the request made every
46 * derived write bail in `can_write()`, so a connect could answer "Site
47 * connected successfully" and persist nothing. Re-read while it is still
48 * unknown; a resolved id is never overwritten.
49 *
50 * @return int
51 */
52 public function current_user_id(): int {
53 if ( $this->current_user <= 0 ) {
54 $this->current_user = get_current_user_id();
55 }
56
57 return $this->current_user;
58 }
59
60 /**
61 * Can a user link another templately account in a setup?.
62 * @return boolean
63 */
64 public function link_account(): bool {
65 return $this->who_am_i() === 'link' && ! $this->has_api;
66 }
67
68 public function unlink_account(): bool {
69 return ( $this->who_am_i() === 'link' || $this->who_am_i() === 'local' ) && $this->has_api;
70 }
71
72 /**
73 * Get determined who am I.
74 * @return string
75 */
76 public function who_am_i(): string {
77 $_who_am_i = 'local';
78
79 if( $this->is_global() > 0 && $this->is_global() === $this->current_user_id() ) {
80 $_who_am_i = 'global';
81 }
82
83 if( $this->is_global() > 0 && $this->is_global() !== $this->current_user_id() ) {
84 $_who_am_i = 'link';
85 }
86
87 if( $this->is_global() == 0 ) {
88 $_who_am_i = 'local';
89 }
90
91 return $_who_am_i;
92 }
93
94 /**
95 * Pin the acting user as the target for every derived read/write.
96 *
97 * @param bool $force Whether to force the current user.
98 * @return Options
99 */
100 public function use_current_user( bool $force = true ): Options {
101 $this->force_current_user = $force;
102
103 return $this;
104 }
105
106 /**
107 * Get user id determine dynamically
108 * @return integer
109 */
110 private function user_id(): int {
111 if ( $this->force_current_user ) {
112 return $this->current_user_id();
113 }
114
115 $_who_am_i = $this->who_am_i();
116
117 if( ! empty( $_SERVER['REQUEST_URI'] ) ) {
118 $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) );
119 if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) {
120 return $this->current_user_id();
121 }
122 }
123
124 if( $_who_am_i === 'link' && $this->has_api ) {
125 return $this->current_user_id();
126 }
127
128 return $_who_am_i === 'local' ? $this->current_user_id() : $this->is_global();
129 }
130
131 /**
132 * Globally logged in and the User ID of globally logged-in user.
133 * @return integer
134 */
135 public function is_global(): int {
136 return intval( get_option('_templately_global_login', 0) );
137 }
138
139 /**
140 * Set global login flag
141 * @return boolean
142 */
143 public static function set_global_login(): bool {
144 return update_option('_templately_global_login', get_current_user_id(), 'no');
145 }
146
147 /**
148 * Remove global login flag
149 * @return boolean
150 */
151 public function remove_global_login(): bool {
152 return delete_option( '_templately_global_login' );
153 }
154
155 public function is_globally_signed(): bool {
156 return $this->who_am_i() !== 'local';
157 }
158
159 public function signed_as_global(): bool {
160 return $this->current_user_id() === $this->is_global();
161 }
162
163 /**
164 * Set optional user meta or option data
165 *
166 * @param string $key
167 * @param mixed $value
168 * @param null $user_id
169 * @return boolean
170 */
171 public function set( $key, $value, $user_id = null ): bool {
172 $key = '_templately_' . $key;
173
174 $updated = $this->update_user_meta( $user_id, $key, $value );
175
176 if( $key === '_templately_api_key' && $updated ) {
177 $this->has_api = true;
178 }
179
180 return $updated;
181 }
182
183 /**
184 * Get optional user meta or option data
185 *
186 * @param string $key
187 * @param mixed $default
188 * @return mixed
189 */
190 public function get( $key, $default = false, $user_id = null ){
191 $key = '_templately_' . $key;
192 $_user_meta = $this->get_user_meta( $user_id, $key, true );
193 return ! empty( $_user_meta ) ? $_user_meta : $default;
194 }
195
196 /**
197 * Remove options data or user meta
198 *
199 * @param string $key
200 * @return Options
201 */
202 public function remove( string $key ): Options {
203 $key = '_templately_' . $key;
204 $updated = $this->delete_user_meta( $key );
205
206 if( $key === '_templately_api_key' && $updated ) {
207 $this->has_api = false;
208 }
209
210 return $this;
211 }
212
213 public function get_user_meta( $user_id, $key = '', $single = false ) {
214 $user_id = is_null( $user_id ) ? $this->user_id() : $user_id;
215
216 if( ! is_multisite() ) {
217 return get_user_meta( $user_id, $key, $single);
218 }
219
220 return get_user_option( $key, $user_id );
221 }
222
223 public function update_user_meta($user_id, $meta_key, $meta_value) {
224 if ( is_null( $user_id ) ) {
225 if ( ! $this->can_write() ) {
226 return false;
227 }
228
229 $user_id = $this->user_id();
230 }
231
232 if( ! is_multisite() ) {
233 return update_user_meta( $user_id, $meta_key, $meta_value );
234 }
235
236 return update_user_option( $user_id, $meta_key, $meta_value, $this->_is_global() );
237 }
238
239 public function delete_user_meta( $meta_key ): bool {
240 if ( ! $this->can_write() ) {
241 return false;
242 }
243
244 if( ! is_multisite() ) {
245 return delete_user_meta( $this->user_id(), $meta_key );
246 }
247
248 return delete_user_option( $this->user_id(), $meta_key, $this->_is_global() );
249 }
250
251 /**
252 * Whether the current request may write to a derived user target.
253 *
254 * Reads retain the global-login fallback for unauthenticated cloud callbacks,
255 * but an anonymous request must never write through it to the administrator.
256 *
257 * @return bool
258 */
259 private function can_write(): bool {
260 return $this->current_user_id() > 0;
261 }
262
263 private function _is_global() {
264 return apply_filters( 'templately_multisite_is_global', false );
265 }
266
267 /**
268 * Get option data
269 *
270 * @since 2.0.1
271 *
272 * @param string $key
273 * @param mixed $default
274 *
275 * @return mixed
276 */
277 public function get_option( $key, $default = false ){
278 return get_option( $key, $default );
279 }
280
281 /**
282 * Update option data
283 *
284 * @since 2.0.1
285 *
286 * @param string $key
287 * @param mixed $value
288 * @param string $autoload
289 *
290 * @return bool
291 */
292 public function update_option( $key, $value, $autoload = 'no' ): bool {
293 return update_option( $key, $value, $autoload );
294 }
295 }
296