PluginProbe
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP / 1.2.29
UsersWP – Front-end login form, User Registration, User Profile & Members Directory plugin for WP v1.2.29
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.29, at includes/class-meta.php

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