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

237 lines 5.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 * Automatically invoked and set up the properties.
26 */
27 public function __construct(){
28 $this->current_user = get_current_user_id();
29 $this->has_api = ! empty( $this->get( 'api_key', '', $this->current_user ) );
30 }
31
32 /**
33 * Get the current user ID.
34 * @return int
35 */
36 public function current_user_id(): int {
37 return $this->current_user;
38 }
39
40 /**
41 * Can a user link another templately account in a setup?.
42 * @return boolean
43 */
44 public function link_account(): bool {
45 return $this->who_am_i() === 'link' && ! $this->has_api;
46 }
47
48 public function unlink_account(): bool {
49 return ( $this->who_am_i() === 'link' || $this->who_am_i() === 'local' ) && $this->has_api;
50 }
51
52 /**
53 * Get determined who am I.
54 * @return string
55 */
56 public function who_am_i(): string {
57 $_who_am_i = 'local';
58
59 if( $this->is_global() > 0 && $this->is_global() === $this->current_user ) {
60 $_who_am_i = 'global';
61 }
62
63 if( $this->is_global() > 0 && $this->is_global() !== $this->current_user ) {
64 $_who_am_i = 'link';
65 }
66
67 if( $this->is_global() == 0 ) {
68 $_who_am_i = 'local';
69 }
70
71 return $_who_am_i;
72 }
73
74 /**
75 * Get user id determine dynamically
76 * @return integer
77 */
78 private function user_id(): int {
79 $_who_am_i = $this->who_am_i();
80
81 if( ! empty( $_SERVER['REQUEST_URI'] ) ) {
82 $parse_uri = explode( '/', substr( $_SERVER['REQUEST_URI'], 0, strpos( $_SERVER['REQUEST_URI'], '?' ) ) );
83 if( $_who_am_i === 'link' && array_pop( $parse_uri ) === 'login' ) {
84 return $this->current_user;
85 }
86 }
87
88 if( $_who_am_i === 'link' && $this->has_api ) {
89 return $this->current_user;
90 }
91
92 return $_who_am_i === 'local' ? $this->current_user : $this->is_global();
93 }
94
95 /**
96 * Globally logged in and the User ID of globally logged-in user.
97 * @return integer
98 */
99 public function is_global(): int {
100 return intval( get_option('_templately_global_login', 0) );
101 }
102
103 /**
104 * Set global login flag
105 * @return boolean
106 */
107 public static function set_global_login(): bool {
108 return update_option('_templately_global_login', get_current_user_id(), 'no');
109 }
110
111 /**
112 * Remove global login flag
113 * @return boolean
114 */
115 public function remove_global_login(): bool {
116 return delete_option( '_templately_global_login' );
117 }
118
119 public function is_globally_signed(): bool {
120 return $this->who_am_i() !== 'local';
121 }
122
123 public function signed_as_global(): bool {
124 return $this->current_user === $this->is_global();
125 }
126
127 /**
128 * Set optional user meta or option data
129 *
130 * @param string $key
131 * @param mixed $value
132 * @param null $user_id
133 * @return boolean
134 */
135 public function set( $key, $value, $user_id = null ): bool {
136 $key = '_templately_' . $key;
137
138 $updated = $this->update_user_meta( $user_id, $key, $value );
139
140 if( $key === '_templately_api_key' && $updated ) {
141 $this->has_api = true;
142 }
143
144 return $updated;
145 }
146
147 /**
148 * Get optional user meta or option data
149 *
150 * @param string $key
151 * @param mixed $default
152 * @return mixed
153 */
154 public function get( $key, $default = false, $user_id = null ){
155 $key = '_templately_' . $key;
156 $_user_meta = $this->get_user_meta( $user_id, $key, true );
157 return ! empty( $_user_meta ) ? $_user_meta : $default;
158 }
159
160 /**
161 * Remove options data or user meta
162 *
163 * @param string $key
164 * @return Options
165 */
166 public function remove( string $key ): Options {
167 $key = '_templately_' . $key;
168 $updated = $this->delete_user_meta( $key );
169
170 if( $key === '_templately_api_key' && $updated ) {
171 $this->has_api = false;
172 }
173
174 return $this;
175 }
176
177 public function get_user_meta( $user_id, $key = '', $single = false ) {
178 $user_id = is_null( $user_id ) ? $this->user_id() : $user_id;
179
180 if( ! is_multisite() ) {
181 return get_user_meta( $user_id, $key, $single);
182 }
183
184 return get_user_option( $key, $user_id );
185 }
186
187 public function update_user_meta($user_id, $meta_key, $meta_value) {
188 $user_id = is_null( $user_id ) ? $this->user_id() : $user_id;
189
190 if( ! is_multisite() ) {
191 return update_user_meta( $user_id, $meta_key, $meta_value );
192 }
193
194 return update_user_option( $user_id, $meta_key, $meta_value, $this->_is_global() );
195 }
196
197 public function delete_user_meta( $meta_key ): bool {
198 if( ! is_multisite() ) {
199 return delete_user_meta( $this->user_id(), $meta_key );
200 }
201
202 return delete_user_option( $this->user_id(), $meta_key, $this->_is_global() );
203 }
204
205 private function _is_global() {
206 return apply_filters( 'templately_multisite_is_global', false );
207 }
208
209 /**
210 * Get option data
211 *
212 * @since 2.0.1
213 *
214 * @param string $key
215 * @param mixed $default
216 *
217 * @return mixed
218 */
219 public function get_option( $key, $default = false ){
220 return get_option( $key, $default );
221 }
222
223 /**
224 * Update option data
225 *
226 * @since 2.0.1
227 *
228 * @param string $key
229 * @param mixed $value
230 * @param string $autoload
231 *
232 * @return bool
233 */
234 public function update_option( $key, $value, $autoload = 'no' ): bool {
235 return update_option( $key, $value, $autoload );
236 }
237 }