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

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