PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.53
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.53
1.2.72 1.2.71 1.2.70 1.2.69 1.2.68 1.2.67 1.2.66 1.2.65 1.2.64 1.2.63 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 All 172 releases
userswp / includes / class-meta.php

class-meta.php in UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP 1.2.53, at includes/class-meta.php

434 lines 12.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UsersWP Meta functions
4 *
5 * All UsersWP related User Settings and Site settings can be created, updated and accessed via this class.
6 *
7 * @since 1.0.0
8 * @author GeoDirectory Team <info@wpgeodirectory.com>
9 */
10 class UsersWP_Meta {
11
12 /**
13 * Gets UsersWP user meta value using key.
14 *
15 * @since 1.0.0
16 * @package userswp
17 *
18 * @param int|bool $user_id User ID.
19 * @param string $key User meta Key.
20 * @param bool|string $default Default value.
21 *
22 * @return string User meta Value.
23 */
24 public function get_usermeta( $user_id = false, $key = '', $default = false ) {
25 global $wpdb;
26
27 if ( ! $user_id ) {
28 return $default;
29 }
30
31 if ( ! $key ) {
32 return $default;
33 }
34
35 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
36
37 if ( uwp_str_ends_with( $key, '_privacy' ) ) {
38 if ( uwp_str_ends_with( $key, '_tab_privacy' ) ) {
39 $obj_key = $user_id.'_tabs_privacy';
40 $row = wp_cache_get( $obj_key, 'uwp_usermeta_tabs_privacy' );
41
42 if ( ! $row ) {
43 $row = $wpdb->get_row( $wpdb->prepare( "SELECT tabs_privacy FROM `{$meta_table}` WHERE user_id = %d", $user_id ), ARRAY_A );
44 wp_cache_set( $obj_key, $row, 'uwp_usermeta_tabs_privacy' );
45 }
46
47 $value = false;
48
49 if ( ! empty( $row ) ) {
50 $public_fields = isset( $row['tabs_privacy'] ) ? maybe_unserialize( $row['tabs_privacy'] ) : $default;
51 $public_fields_keys = is_array( $public_fields ) ? array_keys( $public_fields ) : $public_fields;
52
53 if ( is_array( $public_fields ) && in_array( $key, $public_fields_keys ) ) {
54 $value = $public_fields[ $key ];
55 }
56 }
57 } else {
58 $obj_key = $user_id.'_user_privacy';
59 $row = wp_cache_get( $obj_key, 'uwp_usermeta_user_privacy' );
60
61 if ( ! $row ) {
62 $row = $wpdb->get_row( $wpdb->prepare("SELECT user_privacy FROM `{$meta_table}` WHERE user_id = %d", $user_id ), ARRAY_A );
63 wp_cache_set( $obj_key, $row, 'uwp_usermeta_user_privacy' );
64 }
65
66 $value = 'yes';
67
68 if ( ! empty( $row ) ) {
69 $output = isset( $row['user_privacy'] ) ? $row['user_privacy'] : $default;
70 $public_fields = explode( ',', $output );
71
72 if ( in_array( $key, $public_fields ) ) {
73 $value = 'no';
74 }
75 }
76 }
77 } else {
78 $value = null;
79 $user_data = get_userdata( $user_id );
80
81 if ( ! $user_data ) {
82 return $value;
83 }
84
85 switch ( $key ) {
86 case 'email': $value = $user_data->user_email; break;
87 case 'username': $value = $user_data->user_login; break;
88 case 'user_nicename': $value = $user_data->user_nicename; break;
89 case 'bio': $value = $user_data->description; break;
90 case 'uwp_language': $value = $user_data->locale; break;
91 default :
92 $obj_key = $user_id.'_'.$key;
93 $row = wp_cache_get( $obj_key, 'uwp_usermeta' );
94
95 if ( ! $row ) {
96 if ( in_array( $key, array( 'user_id', 'user_ip', 'user_privacy', 'tabs_privacy', 'username', 'email', 'first_name', 'last_name', 'avatar_thumb', 'banner_thumb', 'display_name', 'user_url', 'bio' ) ) || uwp_column_exist( $meta_table, $key ) ) {
97 $row = $wpdb->get_row( $wpdb->prepare( "SELECT `{$key}` FROM `{$meta_table}` WHERE user_id = %d", $user_id ), ARRAY_A );
98 wp_cache_set( $obj_key, $row, 'uwp_usermeta' );
99 }
100 }
101
102 if ( ! empty( $row ) ) {
103 $value = isset( $row[ $key ] ) ? $row[ $key ] : $default;
104 } else {
105 $value = $default;
106 }
107 break;
108 }
109 }
110
111 $value = uwp_maybe_unserialize($key, $value);
112 $value = wp_unslash($value);
113 $value = apply_filters( 'uwp_get_usermeta', $value, $user_id, $key, $default );
114
115 return apply_filters( 'uwp_get_usermeta_' . $key, $value, $user_id, $key, $default );
116 }
117
118 /**
119 * Updates UsersWP user meta value using key.
120 *
121 * @since 1.0.0
122 * @package userswp
123 *
124 * @param int|bool $user_id User ID.
125 * @param string|bool $key User meta Key.
126 * @param string $value User meta Value.
127 *
128 * @return bool Update success or not?.
129 */
130 public function update_usermeta( $user_id, $key, $value ) {
131 global $wpdb;
132
133 if ( ! $user_id || ! $key ) {
134 return false;
135 }
136
137 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
138 $cache_group = 'uwp_usermeta';
139 $obj_key = $user_id . '_' . $key;
140
141 if ( ! in_array( $key, array( 'user_id', 'user_ip', 'user_privacy', 'tabs_privacy', 'username', 'email', 'first_name', 'last_name', 'avatar_thumb', 'banner_thumb', 'display_name', 'user_url', 'bio' ) ) && ! uwp_column_exist( $meta_table, $key ) ) {
142 return false;
143 }
144
145 if ( uwp_str_ends_with( $key, '_privacy' ) ) {
146 if ( 'tabs_privacy' == $key ) {
147 $obj_key = $user_id . '_tabs_privacy';
148 $cache_group = 'uwp_usermeta_tab_privacy';
149 } elseif('user_privacy' == $key) {
150 $obj_key = $user_id . '_user_privacy';
151 $cache_group = 'uwp_usermeta_user_privacy';
152 }
153 }
154
155 $user_meta_info = $wpdb->get_col( $wpdb->prepare( "SELECT `{$key}` FROM `{$meta_table}` WHERE user_id = %d", $user_id ) );
156
157 $value = apply_filters( 'uwp_update_usermeta', $value, $user_id, $key, $user_meta_info );
158 $value = apply_filters( 'uwp_update_usermeta_' . $key, $value, $user_id, $key, $user_meta_info );
159
160 do_action( 'uwp_before_update_usermeta', $user_id, $key, $value, $user_meta_info );
161
162 $value = uwp_maybe_serialize( $key, $value );
163
164 if ( ! empty( $user_meta_info ) ) {
165 $result = $wpdb->update(
166 $meta_table,
167 array( $key => $value ),
168 array( 'user_id' => $user_id ),
169 array('%s'),
170 array('%d')
171 );
172
173 if ( ! $result ) {
174 return false;
175 }
176 } else {
177 $result = $wpdb->insert(
178 $meta_table,
179 array( 'user_id' => $user_id, $key => $value )
180 );
181
182 if ( ! $result ) {
183 return false;
184 }
185 }
186
187 wp_cache_delete( $obj_key, $cache_group );
188
189 return true;
190 }
191
192 /**
193 * Gets UsersWP user meta row using user ID.
194 *
195 * @since 1.0.0
196 * @package userswp
197 *
198 * @param int|bool $user_id User ID.
199 *
200 * @return object|bool User meta row object.
201 */
202 public function get_usermeta_row($user_id = false) {
203 if (!$user_id) {
204 return false;
205 }
206
207 global $wpdb;
208 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
209
210 $row = wp_cache_get( $user_id, 'uwp_usermeta_row' );
211 if ( ! $row ) {
212 $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$meta_table} WHERE user_id = %d", $user_id));
213 wp_cache_set( $user_id, $row, 'uwp_usermeta_row' );
214 }
215
216 return $row;
217 }
218
219 /**
220 * Deletes a UsersWP meta row using the user ID.
221 *
222 * @since 1.0.5
223 * @package UsersWP
224 *
225 * @param int|bool $user_id User ID.
226 *
227 * @return void
228 */
229 public function delete_usermeta_row($user_id = false) {
230 if (!$user_id) {
231 return;
232 }
233
234 global $wpdb;
235 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
236
237 $count = $wpdb->query($wpdb->prepare("DELETE FROM {$meta_table} WHERE user_id = %d", $user_id));
238
239 if ( ! $count ) {
240 return false;
241 }
242
243 wp_cache_delete( $user_id, 'uwp_usermeta_row' );
244
245 }
246
247 /**
248 * Syncs WP usermeta with UsersWP usermeta.
249 *
250 * @since 1.0.0
251 * @package userswp
252 *
253 * @param int $user_id User ID.
254 *
255 * @return void
256 */
257 public function sync_usermeta($user_id) {
258
259 global $wpdb;
260 $user_data = get_userdata($user_id);
261 $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta';
262
263 $user_meta = array(
264 'username' => $user_data->user_login,
265 'email' => $user_data->user_email,
266 'display_name' => $user_data->display_name,
267 'first_name' => $user_data->first_name,
268 'last_name' => $user_data->last_name,
269 'user_url' => $user_data->user_url,
270 'bio' => $user_data->description,
271 );
272
273 foreach ($user_meta as $key => $meta){
274 do_action('sync_usermeta_on_register', $user_id, $key, $meta); // for adding points via mycred add on.
275 }
276
277 $users = $wpdb->get_var($wpdb->prepare("SELECT COUNT(user_id) FROM {$meta_table} WHERE user_id = %d", $user_id));
278
279 if($users){
280 $wpdb->update(
281 $meta_table,
282 $user_meta,
283 array('user_id' => $user_id)
284 );
285 } else {
286 $user_meta['user_id'] = $user_id;
287 $wpdb->insert(
288 $meta_table,
289 $user_meta
290 );
291 }
292
293 }
294
295 /**
296 * Delete UsersWP meta when user get deleted.
297 *
298 * @since 1.0.5
299 * @package UsersWP
300 *
301 * @param int $user_id User ID.
302 *
303 * @return void
304 */
305 public function delete_usermeta_for_user($user_id) {
306 $this->delete_usermeta_row($user_id);
307 }
308
309 /**
310 * Delete UsersWP meta when user get deleted from subsite of multisite network.
311 *
312 * @package UsersWP
313 *
314 * @param int $user_id User ID.
315 *
316 * @return void
317 */
318 public function remove_user_from_blog($user_id, $blog_id) {
319 switch_to_blog( $blog_id );
320 $this->delete_usermeta_row($user_id);
321 restore_current_blog();
322 }
323
324 /**
325 * Saves User IP during registration.
326 *
327 * @since 1.0.5
328 * @package userswp
329 *
330 * @param array $result Validated form result.
331 * @param string $type Form Type.
332 * @param int $user_id User ID.
333 *
334 * @return array Validated form result.
335 */
336 public function save_user_ip_on_register($result, $type, $user_id) {
337 if ($type == 'register') {
338 $ip = uwp_get_ip();
339 uwp_update_usermeta($user_id, 'user_ip', $ip);
340 }
341 return $result;
342 }
343
344 /**
345 * Saves the users IP on login.
346 *
347 * @since 1.0.0
348 * @package userswp
349 *
350 * @param string $user_login The users username.
351 * @param object $user The user object WP_User.
352 *
353 * @return void
354 */
355 public function save_user_ip_on_login( $user_login, $user ) {
356 if (isset($user->ID)) {
357 $ip = uwp_get_ip();
358 uwp_update_usermeta($user->ID, 'user_ip', $ip);
359 }
360 }
361
362 /**
363 * Modifies date value from unix timestamp to string while updating into the db.
364 *
365 * @since 1.0.0
366 * @package userswp
367 *
368 * @param int $value Unix Timestamp.
369 * @param int $user_id The User ID.
370 * @param string $key Custom field key.
371 *
372 * @return string Date string.
373 */
374 public function modify_datepicker_value_on_update( $value, $user_id, $key ) {
375 // Modify timestamp to date
376 if ( is_int( $value ) ) {
377 $field_info = uwp_get_custom_field_info( $key );
378
379 if ( ! empty( $field_info ) && $field_info->field_type == 'datepicker' ) {
380 $value = date( 'Y-m-d', $value );
381 }
382 }
383
384 return $value;
385 }
386
387 /**
388 * Modifies date value from string to unix timestamp while fetching from the db.
389 *
390 * @since 1.0.0
391 * @package userswp
392 *
393 * @param string $value Date string.
394 * @param int $user_id The User ID.
395 * @param string $key Custom field key.
396 *
397 * @return int Unix Timestamp.
398 */
399 public function modify_datepicker_value_on_get($value, $user_id, $key, $default) {
400 // modify date to timestamp
401 $field_info = uwp_get_custom_field_info($key);
402 if (isset($field_info->field_type) && $field_info->field_type == 'datepicker') {
403 if (is_string($value) && (strpos($value, '-') !== false) && $value != '0000-00-00') {
404 $value = strtotime( $value );
405 } elseif($value === '0000-00-00'){$value = '';}
406 }
407
408 return $value;
409 }
410
411 /**
412 * Make UWP user meta available through the standard get_user_meta() function if prefixed with `user_`
413 *
414 * @param $metadata
415 * @param $object_id
416 * @param $meta_key
417 * @param $single
418 *
419 * @return bool|mixed|null|string
420 */
421 public static function dynamically_add_user_meta( $metadata, $object_id, $meta_key, $single ) {
422
423 if ( strpos( $meta_key, 'uwp_meta_' ) === 0 ) {
424 $meta_key = substr( $meta_key, 9 );
425 $maybe_meta = uwp_get_usermeta( $object_id, $meta_key, $single );
426 if ( ! empty( $maybe_meta ) ) {
427 $metadata = $maybe_meta;
428 }
429 }
430
431 return $metadata;
432 }
433
434 }