PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.7.2
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.7.2
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.2, at includes/Utils/Options.php

284 lines 6.2 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 * @return int
43 */
44 public function current_user_id(): int {
45 return $this->current_user;
46 }
47
48 /**
49 * Can a user link another templately account in a setup?.
50 * @return boolean
51 */
52 public function link_account(): bool {
53 return $this->who_am_i() === 'link' && ! $this->has_api;
54 }
55
56 public function unlink_account(): bool {
57 return ( $this->who_am_i() === 'link' || $this->who_am_i() === 'local' ) && $this->has_api;
58 }
59
60 /**
61 * Get determined who am I.
62 * @return string
63 */
64 public function who_am_i(): string {
65 $_who_am_i = 'local';
66
67 if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) {
68 $_who_am_i = 'global';
69 }
70
71 if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) {
72 $_who_am_i = 'link';
73 }
74
75 if( $this->is_global() == 0 ) {
76 $_who_am_i = 'local';
77 }
78
79 return $_who_am_i;
80 }
81
82 /**
83 * Pin the acting user as the target for every derived read/write.
84 *
85 * @param bool $force Whether to force the current user.
86 * @return Options
87 */
88 public function use_current_user( bool $force = true ): Options {
89 $this->force_current_user = $force;
90
91 return $this;
92 }
93
94 /**
95 * Get user id determine dynamically
96 * @return integer
97 */
98 private function user_id(): int {
99 if ( $this->force_current_user ) {
100 return $this->current_user;
101 }
102
103 $_who_am_i = $this->who_am_i();
104
105 if( ! empty( $_SERVER['REQUEST_URI'] ) ) {
106 $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) );
107 if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) {
108 return $this->current_user;
109 }
110 }
111
112 if( $_who_am_i === 'link' && $this->has_api ) {
113 return $this->current_user;
114 }
115
116 return $_who_am_i === 'local' ? $this->current_user : $this->is_global();
117 }
118
119 /**
120 * Globally logged in and the User ID of globally logged-in user.
121 * @return integer
122 */
123 public function is_global(): int {
124 return intval( get_option('_templately_global_login', 0) );
125 }
126
127 /**
128 * Set global login flag
129 * @return boolean
130 */
131 public static function set_global_login(): bool {
132 return update_option('_templately_global_login', get_current_user_id(), 'no');
133 }
134
135 /**
136 * Remove global login flag
137 * @return boolean
138 */
139 public function remove_global_login(): bool {
140 return delete_option( '_templately_global_login' );
141 }
142
143 public function is_globally_signed(): bool {
144 return $this->who_am_i() !== 'local';
145 }
146
147 public function signed_as_global(): bool {
148 return $this->current_user === $this->is_global();
149 }
150
151 /**
152 * Set optional user meta or option data
153 *
154 * @param string $key
155 * @param mixed $value
156 * @param null $user_id
157 * @return boolean
158 */
159 public function set( $key, $value, $user_id = null ): bool {
160 $key = '_templately_' . $key;
161
162 $updated = $this->update_user_meta( $user_id, $key, $value );
163
164 if( $key === '_templately_api_key' && $updated ) {
165 $this->has_api = true;
166 }
167
168 return $updated;
169 }
170
171 /**
172 * Get optional user meta or option data
173 *
174 * @param string $key
175 * @param mixed $default
176 * @return mixed
177 */
178 public function get( $key, $default = false, $user_id = null ){
179 $key = '_templately_' . $key;
180 $_user_meta = $this->get_user_meta( $user_id, $key, true );
181 return ! empty( $_user_meta ) ? $_user_meta : $default;
182 }
183
184 /**
185 * Remove options data or user meta
186 *
187 * @param string $key
188 * @return Options
189 */
190 public function remove( string $key ): Options {
191 $key = '_templately_' . $key;
192 $updated = $this->delete_user_meta( $key );
193
194 if( $key === '_templately_api_key' && $updated ) {
195 $this->has_api = false;
196 }
197
198 return $this;
199 }
200
201 public function get_user_meta( $user_id, $key = '', $single = false ) {
202 $user_id = is_null( $user_id ) ? $this->user_id() : $user_id;
203
204 if( ! is_multisite() ) {
205 return get_user_meta( $user_id, $key, $single);
206 }
207
208 return get_user_option( $key, $user_id );
209 }
210
211 public function update_user_meta($user_id, $meta_key, $meta_value) {
212 if ( is_null( $user_id ) ) {
213 if ( ! $this->can_write() ) {
214 return false;
215 }
216
217 $user_id = $this->user_id();
218 }
219
220 if( ! is_multisite() ) {
221 return update_user_meta( $user_id, $meta_key, $meta_value );
222 }
223
224 return update_user_option( $user_id, $meta_key, $meta_value, $this->_is_global() );
225 }
226
227 public function delete_user_meta( $meta_key ): bool {
228 if ( ! $this->can_write() ) {
229 return false;
230 }
231
232 if( ! is_multisite() ) {
233 return delete_user_meta( $this->user_id(), $meta_key );
234 }
235
236 return delete_user_option( $this->user_id(), $meta_key, $this->_is_global() );
237 }
238
239 /**
240 * Whether the current request may write to a derived user target.
241 *
242 * Reads retain the global-login fallback for unauthenticated cloud callbacks,
243 * but an anonymous request must never write through it to the administrator.
244 *
245 * @return bool
246 */
247 private function can_write(): bool {
248 return $this->current_user > 0;
249 }
250
251 private function _is_global() {
252 return apply_filters( 'templately_multisite_is_global', false );
253 }
254
255 /**
256 * Get option data
257 *
258 * @since 2.0.1
259 *
260 * @param string $key
261 * @param mixed $default
262 *
263 * @return mixed
264 */
265 public function get_option( $key, $default = false ){
266 return get_option( $key, $default );
267 }
268
269 /**
270 * Update option data
271 *
272 * @since 2.0.1
273 *
274 * @param string $key
275 * @param mixed $value
276 * @param string $autoload
277 *
278 * @return bool
279 */
280 public function update_option( $key, $value, $autoload = 'no' ): bool {
281 return update_option( $key, $value, $autoload );
282 }
283 }
284