PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 2.0.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v2.0.0
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
← All changes | includes/Utils/Options.php +36 -153 3.7.22.0.0 View file →
@@ -9,30 +9,22 @@
9 9 use function get_current_user_id;
10 10
11 11 class Options extends Base {
12 12 /**
13 - * Current User Id
13 + * Currest User Id
14 14 * @var integer
15 15 */
16 - private $current_user;
16 + private $current_user = 0;
17 17
18 18 /**
19 19 * User has any api?
20 20 * @var boolean
21 21 */
22 - private $has_api;
22 + private $has_api = false;
23 23
24 24 /**
25 - * Pin every derived read/write to the acting user, bypassing the
26 - * global-login fallback in user_id().
27 - *
28 - * @var bool
25 + * Autometically invoked and setup the properties.
29 26 */
30 - private $force_current_user = false;
31 -
32 - /**
33 - * Automatically invoked and set up the properties.
34 - */
35 27 public function __construct(){
36 28 $this->current_user = get_current_user_id();
37 29 $this->has_api = ! empty( $this->get( 'api_key', '', $this->current_user ) );
38 30 }
@@ -40,9 +32,9 @@
40 32 /**
41 33 * Get the current user ID.
42 34 * @return int
43 35 */
44 - public function current_user_id(): int {
36 + public function current_user_id(){
45 37 return $this->current_user;
46 38 }
47 39
48 40 /**
@@ -48,120 +40,96 @@
48 40 /**
49 41 * Can a user link another templately account in a setup?.
50 42 * @return boolean
51 43 */
52 - public function link_account(): bool {
53 - return $this->who_am_i() === 'link' && ! $this->has_api;
44 + public function link_account() {
45 + return $this->whoami() === 'link' && ! $this->has_api;
54 46 }
55 47
56 - public function unlink_account(): bool {
57 - return ( $this->who_am_i() === 'link' || $this->who_am_i() === 'local' ) && $this->has_api;
48 + public function unlink_account() {
49 + return ( $this->whoami() === 'link' || $this->whoami() === 'local' ) && $this->has_api;
58 50 }
59 -
60 51 /**
61 52 * Get determined who am I.
62 53 * @return string
63 54 */
64 - public function who_am_i(): string {
65 - $_who_am_i = 'local';
55 + public function whoami(){
56 + $_whoami = 'local';
66 57
67 58 if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) {
68 - $_who_am_i = 'global';
59 + $_whoami = 'global';
69 60 }
70 61
71 62 if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) {
72 - $_who_am_i = 'link';
63 + $_whoami = 'link';
73 64 }
74 65
75 66 if( $this->is_global() == 0 ) {
76 - $_who_am_i = 'local';
67 + $_whoami = 'local';
77 68 }
78 69
79 - return $_who_am_i;
70 + return $_whoami;
80 71 }
81 -
82 72 /**
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 73 * Get user id determine dynamically
96 74 * @return integer
97 75 */
98 - private function user_id(): int {
99 - if ( $this->force_current_user ) {
100 - return $this->current_user;
101 - }
76 + private function user_id(){
77 + $_whoami = $this->whoami();
78 + // Helper::log( '_whoami: ' . $_whoami );
102 79
103 - $_who_am_i = $this->who_am_i();
104 -
105 80 if( ! empty( $_SERVER['REQUEST_URI'] ) ) {
106 81 $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) );
107 - if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) {
82 + if( $_whoami === 'link' && array_pop( $parse_uri ) === 'login' ) {
108 83 return $this->current_user;
109 84 }
110 85 }
111 86
112 - if( $_who_am_i === 'link' && $this->has_api ) {
87 + if( $_whoami === 'link' && $this->has_api ) {
113 88 return $this->current_user;
114 89 }
115 90
116 - return $_who_am_i === 'local' ? $this->current_user : $this->is_global();
91 + return $_whoami === 'local' ? $this->current_user : $this->is_global();
117 92 }
118 -
119 93 /**
120 94 * Globally logged in and the User ID of globally logged-in user.
121 95 * @return integer
122 96 */
123 - public function is_global(): int {
97 + public function is_global(){
124 98 return intval( get_option('_templately_global_login', 0) );
125 99 }
126 -
127 100 /**
128 101 * Set global login flag
129 102 * @return boolean
130 103 */
131 - public static function set_global_login(): bool {
104 + public static function set_global_login() {
132 105 return update_option('_templately_global_login', get_current_user_id(), 'no');
133 106 }
134 -
135 107 /**
136 108 * Remove global login flag
137 109 * @return boolean
138 110 */
139 - public function remove_global_login(): bool {
111 + public function remove_global_login() {
140 112 return delete_option( '_templately_global_login' );
141 113 }
142 114
143 - public function is_globally_signed(): bool {
144 - return $this->who_am_i() !== 'local';
115 + public function is_globally_signed() {
116 + return $this->whoami() !== 'local';
145 117 }
146 -
147 - public function signed_as_global(): bool {
118 + public function signed_as_global() {
148 119 return $this->current_user === $this->is_global();
149 120 }
150 -
151 121 /**
152 - * Set optional user meta or option data
122 + * Set optional usermeta or option data
153 123 *
154 124 * @param string $key
155 125 * @param mixed $value
156 - * @param null $user_id
157 126 * @return boolean
158 127 */
159 - public function set( $key, $value, $user_id = null ): bool {
128 + public function set( $key, $value ){
160 129 $key = '_templately_' . $key;
130 + $updated = update_user_meta( $this->user_id(), $key, $value );
161 131
162 - $updated = $this->update_user_meta( $user_id, $key, $value );
163 -
164 132 if( $key === '_templately_api_key' && $updated ) {
165 133 $this->has_api = true;
166 134 }
167 135
@@ -166,11 +134,10 @@
166 134 }
167 135
168 136 return $updated;
169 137 }
170 -
171 138 /**
172 - * Get optional user meta or option data
139 + * Get optional usermeta or option data
173 140 *
174 141 * @param string $key
175 142 * @param mixed $default
176 143 * @return mixed
@@ -176,21 +143,20 @@
176 143 * @return mixed
177 144 */
178 145 public function get( $key, $default = false, $user_id = null ){
179 146 $key = '_templately_' . $key;
180 - $_user_meta = $this->get_user_meta( $user_id, $key, true );
147 + $_user_meta = get_user_meta( is_null( $user_id ) ? $this->user_id() : $user_id, $key, true );
181 148 return ! empty( $_user_meta ) ? $_user_meta : $default;
182 149 }
183 -
184 150 /**
185 - * Remove options data or user meta
151 + * Remove options data or usermeta
186 152 *
187 153 * @param string $key
188 154 * @return Options
189 155 */
190 - public function remove( string $key ): Options {
156 + public function remove( $key ){
191 157 $key = '_templately_' . $key;
192 - $updated = $this->delete_user_meta( $key );
158 + $updated = delete_user_meta( $this->user_id(), $key ); // delete user meta
193 159
194 160 if( $key === '_templately_api_key' && $updated ) {
195 161 $this->has_api = false;
196 162 }
@@ -196,88 +162,5 @@
196 162 }
197 163
198 164 return $this;
199 165 }
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 -}
166 +}