| @@ -9,64 +9,8 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | class UsersWP_Meta { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Gets UsersWP setting value using key. | |
| 14 | - * | |
| 15 | - * @since 1.0.0 | |
| 16 | - * @package userswp | |
| 17 | - * | |
| 18 | - * @param string $key Setting Key. | |
| 19 | - * @param bool|string $default Default value. | |
| 20 | - * @param bool $cache Use cache to retrieve the value?. | |
| 21 | - * | |
| 22 | - * @return string Setting Value. | |
| 23 | - */ | |
| 24 | - public function get_option( $key = '', $default = false, $cache = true ) { | |
| 25 | - if ($cache) { | |
| 26 | - global $uwp_options; | |
| 27 | - } else { | |
| 28 | - $uwp_options = get_option( 'uwp_settings' ); | |
| 29 | - } | |
| 30 | - $value = ! empty( $uwp_options[ $key ] ) ? $uwp_options[ $key ] : $default; | |
| 31 | - $value = apply_filters( 'uwp_get_option', $value, $key, $default ); | |
| 32 | - return apply_filters( 'uwp_get_option_' . $key, $value, $key, $default ); | |
| 33 | - } | |
| 34 | - | |
| 35 | - /** | |
| 36 | - * Updates UsersWP setting value using key. | |
| 37 | - * | |
| 38 | - * @since 1.0.0 | |
| 39 | - * @package userswp | |
| 40 | - * | |
| 41 | - * @param string|bool $key Setting Key. | |
| 42 | - * @param string $value Setting Value. | |
| 43 | - * | |
| 44 | - * @return bool Update success or not?. | |
| 45 | - */ | |
| 46 | - public function update_option( $key = false, $value = '') { | |
| 47 | - | |
| 48 | - if (!$key ) { | |
| 49 | - return false; | |
| 50 | - } | |
| 51 | - | |
| 52 | - $settings = get_option( 'uwp_settings', array()); | |
| 53 | - | |
| 54 | - if( !is_array( $settings ) ) { | |
| 55 | - $settings = array(); | |
| 56 | - } | |
| 57 | - | |
| 58 | - $settings[ $key ] = $value; | |
| 59 | - | |
| 60 | - $settings = apply_filters( 'uwp_update_option', $settings, $key, $value ); | |
| 61 | - $settings = apply_filters( 'uwp_update_option_' . $key, $settings, $key, $value ); | |
| 62 | - | |
| 63 | - update_option( 'uwp_settings', $settings ); | |
| 64 | - | |
| 65 | - return true; | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | 13 | * Gets UsersWP user meta value using key. |
| 70 | 14 | * |
| 71 | 15 | * @since 1.0.0 |
| 72 | 16 | * @package userswp |
| @@ -81,33 +25,75 @@ | ||
| 81 | 25 | if (!$user_id) { |
| 82 | 26 | return $default; |
| 83 | 27 | } |
| 84 | 28 | |
| 29 | + if(!$key){ | |
| 30 | + return $default; | |
| 31 | + } | |
| 32 | + | |
| 85 | 33 | global $wpdb; |
| 86 | 34 | $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 87 | 35 | |
| 88 | 36 | if (uwp_str_ends_with($key, '_privacy')) { |
| 89 | - $value = 'yes'; | |
| 90 | - $row = $wpdb->get_row($wpdb->prepare("SELECT user_privacy FROM {$meta_table} WHERE user_id = %d", $user_id), ARRAY_A); | |
| 91 | - if (!empty($row)) { | |
| 92 | - $output = isset($row['user_privacy']) ? $row['user_privacy'] : $default; | |
| 93 | - $public_fields = explode(',', $output); | |
| 94 | - if (in_array($key, $public_fields)) { | |
| 95 | - $value = 'no'; | |
| 96 | - } | |
| 97 | - } | |
| 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 | + } | |
| 98 | 70 | } else { |
| 99 | 71 | $value = null; |
| 100 | 72 | $user_data = get_userdata($user_id); |
| 101 | 73 | |
| 74 | + if (!$user_data) { | |
| 75 | + return $value; | |
| 76 | + } | |
| 77 | + | |
| 102 | 78 | switch ($key){ |
| 103 | - case 'uwp_account_email': $value = $user_data->user_email; break; | |
| 104 | - case 'uwp_account_username': $value = $user_data->user_login; break; | |
| 105 | - case 'uwp_account_bio': $value = $user_data->description; break; | |
| 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; | |
| 106 | 84 | default : |
| 107 | - $row = $wpdb->get_row($wpdb->prepare("SELECT {$key} FROM {$meta_table} WHERE user_id = %d", $user_id), ARRAY_A); | |
| 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 | + | |
| 108 | 94 | if (!empty($row)) { |
| 109 | - $value = !empty($row[$key]) ? $row[$key] : $default; | |
| 95 | + $value = isset($row[$key]) ? $row[$key] : $default; | |
| 110 | 96 | } else { |
| 111 | 97 | $value = $default; |
| 112 | 98 | } |
| 113 | 99 | break; |
| @@ -131,9 +117,9 @@ | ||
| 131 | 117 | * @param string $value User meta Value. |
| 132 | 118 | * |
| 133 | 119 | * @return bool Update success or not?. |
| 134 | 120 | */ |
| 135 | - public function update_usermeta( $user_id = false, $key, $value ) { | |
| 121 | + public function update_usermeta( $user_id, $key, $value ) { | |
| 136 | 122 | |
| 137 | 123 | if (!$user_id || !$key ) { |
| 138 | 124 | return false; |
| 139 | 125 | } |
| @@ -139,8 +125,21 @@ | ||
| 139 | 125 | } |
| 140 | 126 | |
| 141 | 127 | global $wpdb; |
| 142 | 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 | + | |
| 143 | 142 | $user_meta_info = $wpdb->get_col( $wpdb->prepare( "SELECT $key FROM $meta_table WHERE user_id = %d", $user_id ) ); |
| 144 | 143 | |
| 145 | 144 | $value = apply_filters( 'uwp_update_usermeta', $value, $user_id, $key, $user_meta_info ); |
| 146 | 145 | $value = apply_filters( 'uwp_update_usermeta_' . $key, $value, $user_id, $key, $user_meta_info ); |
| @@ -149,9 +148,9 @@ | ||
| 149 | 148 | |
| 150 | 149 | $value = uwp_maybe_serialize($key, $value); |
| 151 | 150 | |
| 152 | 151 | if (!empty($user_meta_info)) { |
| 153 | - $wpdb->update( | |
| 152 | + $result = $wpdb->update( | |
| 154 | 153 | $meta_table, |
| 155 | 154 | array($key => $value), |
| 156 | 155 | array('user_id' => $user_id), |
| 157 | 156 | array('%s'), |
| @@ -156,15 +155,26 @@ | ||
| 156 | 155 | array('user_id' => $user_id), |
| 157 | 156 | array('%s'), |
| 158 | 157 | array('%d') |
| 159 | 158 | ); |
| 159 | + | |
| 160 | + if ( ! $result ) { | |
| 161 | + return false; | |
| 162 | + } | |
| 163 | + | |
| 160 | 164 | } else { |
| 161 | - $wpdb->insert( | |
| 165 | + $result = $wpdb->insert( | |
| 162 | 166 | $meta_table, |
| 163 | 167 | array('user_id' => $user_id, $key => $value) |
| 164 | 168 | ); |
| 169 | + | |
| 170 | + if ( ! $result ) { | |
| 171 | + return false; | |
| 172 | + } | |
| 165 | 173 | } |
| 166 | 174 | |
| 175 | + wp_cache_delete( $obj_key, $cache_group ); | |
| 176 | + | |
| 167 | 177 | return true; |
| 168 | 178 | } |
| 169 | 179 | |
| 170 | 180 | /** |
| @@ -184,9 +194,13 @@ | ||
| 184 | 194 | |
| 185 | 195 | global $wpdb; |
| 186 | 196 | $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 187 | 197 | |
| 188 | - $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$meta_table} WHERE user_id = %d", $user_id)); | |
| 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 | + } | |
| 189 | 203 | |
| 190 | 204 | return $row; |
| 191 | 205 | } |
| 192 | 206 | |
| @@ -206,9 +220,17 @@ | ||
| 206 | 220 | } |
| 207 | 221 | |
| 208 | 222 | global $wpdb; |
| 209 | 223 | $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 210 | - $wpdb->query($wpdb->prepare("DELETE FROM {$meta_table} WHERE user_id = %d", $user_id)); | |
| 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 | + | |
| 211 | 233 | } |
| 212 | 234 | |
| 213 | 235 | /** |
| 214 | 236 | * Syncs WP usermeta with UsersWP usermeta. |
| @@ -226,15 +248,21 @@ | ||
| 226 | 248 | $user_data = get_userdata($user_id); |
| 227 | 249 | $meta_table = get_usermeta_table_prefix() . 'uwp_usermeta'; |
| 228 | 250 | |
| 229 | 251 | $user_meta = array( |
| 230 | - 'uwp_account_username' => $user_data->user_login, | |
| 231 | - 'uwp_account_email' => $user_data->user_email, | |
| 232 | - 'uwp_account_display_name' => $user_data->display_name, | |
| 233 | - 'uwp_account_first_name' => $user_data->first_name, | |
| 234 | - 'uwp_account_last_name' => $user_data->last_name, | |
| 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, | |
| 235 | 259 | ); |
| 236 | 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 | + | |
| 237 | 265 | $users = $wpdb->get_var($wpdb->prepare("SELECT COUNT(user_id) FROM {$meta_table} WHERE user_id = %d", $user_id)); |
| 238 | 266 | |
| 239 | 267 | if($users){ |
| 240 | 268 | $wpdb->update( |
| @@ -312,11 +340,10 @@ | ||
| 312 | 340 | * |
| 313 | 341 | * @return void |
| 314 | 342 | */ |
| 315 | 343 | public function save_user_ip_on_login( $user_login, $user ) { |
| 316 | - | |
| 317 | - $ip = uwp_get_ip(); | |
| 318 | 344 | if (isset($user->ID)) { |
| 345 | + $ip = uwp_get_ip(); | |
| 319 | 346 | uwp_update_usermeta($user->ID, 'user_ip', $ip); |
| 320 | 347 | } |
| 321 | 348 | } |
| 322 | 349 | |
| @@ -356,112 +383,38 @@ | ||
| 356 | 383 | * @return int Unix Timestamp. |
| 357 | 384 | */ |
| 358 | 385 | public function modify_datepicker_value_on_get($value, $user_id, $key, $default) { |
| 359 | 386 | // modify date to timestamp |
| 360 | - if (is_string($value) && (strpos($value, '-') !== false)) { | |
| 361 | - $field_info = uwp_get_custom_field_info($key); | |
| 362 | - if (isset($field_info->field_type) && $field_info->field_type == 'datepicker') { | |
| 363 | - $value = strtotime($value); | |
| 364 | - } | |
| 365 | - } | |
| 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 | + | |
| 366 | 394 | return $value; |
| 367 | 395 | } |
| 368 | 396 | |
| 369 | - public function uwp_user_row_actions($actions, $user_object){ | |
| 370 | - $user_id = $user_object->ID; | |
| 371 | - $mod_value = get_user_meta( $user_id, 'uwp_mod', true ); | |
| 372 | - $delete_link = add_query_arg( | |
| 373 | - array( | |
| 374 | - 'user_id' => $user_id, | |
| 375 | - 'action' => 'uwp_resend', | |
| 376 | - '_nonce' => wp_create_nonce('uwp_resend'), | |
| 377 | - ), | |
| 378 | - admin_url( 'users.php' ) | |
| 379 | - ); | |
| 380 | - if ($mod_value == 'email_unconfirmed') { | |
| 381 | - $actions['uwp_resend_activation'] = "<a class='' href='" . $delete_link . "'>" . __( 'Resend Activation','ultimate-member') . "</a>"; | |
| 382 | - } | |
| 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 ) { | |
| 383 | 408 | |
| 384 | - return $actions; | |
| 385 | - } | |
| 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 | + } | |
| 386 | 416 | |
| 387 | - public function uwp_process_user_actions(){ | |
| 388 | - $user_id = isset($_REQUEST['user_id']) ? (int)$_REQUEST['user_id'] : 0; | |
| 389 | - $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : false; | |
| 390 | - $nonce = isset($_REQUEST['_nonce']) ? $_REQUEST['_nonce'] : false; | |
| 391 | - | |
| 392 | - if($user_id && 'uwp_resend' == $action && wp_verify_nonce( $nonce,'uwp_resend')){ | |
| 393 | - $send_result = $this->uwp_resend_activation_mail($user_id); | |
| 394 | - if(!is_admin()){ | |
| 395 | - global $uwp_notices; | |
| 396 | - | |
| 397 | - if($send_result) { | |
| 398 | - $message = __('Activation email has been sent!', 'userswp'); | |
| 399 | - } else { | |
| 400 | - $message = __('Error while processing request. Please contact site admin.', 'userswp'); | |
| 401 | - } | |
| 402 | - $uwp_notices[] = '<div class="uwp-alert-success text-center">'.$message.'</div>'; | |
| 403 | - return; | |
| 404 | - } | |
| 405 | - if(!$send_result){ | |
| 406 | - wp_redirect( add_query_arg( 'update', 'err_uwp_resend', admin_url('users.php') ) ); | |
| 407 | - } | |
| 408 | - wp_redirect( add_query_arg( 'update', 'uwp_resend', admin_url('users.php') ) ); | |
| 409 | - exit(); | |
| 410 | - } | |
| 411 | - } | |
| 412 | - | |
| 413 | - public function uwp_users_bulk_actions($bulk_actions){ | |
| 414 | - $bulk_actions['uwp_resend'] = __( 'Resend Activation', 'userswp'); | |
| 415 | - return $bulk_actions; | |
| 416 | - } | |
| 417 | - | |
| 418 | - public function uwp_handle_users_bulk_actions($redirect_to, $doaction, $user_ids){ | |
| 419 | - if ( 'uwp_resend' !== $doaction ) { | |
| 420 | - return $redirect_to; | |
| 421 | - } | |
| 422 | - | |
| 423 | - foreach ( $user_ids as $user_id ) { | |
| 424 | - $this->uwp_resend_activation_mail($user_id); | |
| 425 | - } | |
| 426 | - | |
| 427 | - $redirect_to = add_query_arg( 'update', 'uwp_resend', $redirect_to ); | |
| 428 | - return $redirect_to; | |
| 429 | - } | |
| 430 | - | |
| 431 | - public function uwp_resend_activation_mail($user_id = 0){ | |
| 432 | - if(!$user_id){ | |
| 433 | - return false; | |
| 434 | - } | |
| 435 | - if( 'email_unconfirmed' == get_user_meta( $user_id, 'uwp_mod', true )){ | |
| 436 | - $email = new UsersWP_Mails(); | |
| 437 | - $send_result = $email->send( 'activate', $user_id ); | |
| 438 | - return $send_result; | |
| 439 | - } | |
| 440 | - return true; | |
| 441 | - } | |
| 442 | - | |
| 443 | - public function uwp_show_update_messages(){ | |
| 444 | - if ( !isset($_REQUEST['update']) ) return; | |
| 445 | - | |
| 446 | - $update = $_REQUEST['update']; | |
| 447 | - $messages = array(); | |
| 448 | - | |
| 449 | - switch($update) { | |
| 450 | - case 'uwp_resend': | |
| 451 | - $messages['msg'] = __('Activation email has been sent!','userswp'); | |
| 452 | - break; | |
| 453 | - case 'err_uwp_resend': | |
| 454 | - $messages['err_msg'] = __('Error while sending activation email. Please try again.','userswp'); | |
| 455 | - break; | |
| 456 | - } | |
| 457 | - | |
| 458 | - if ( !empty( $messages ) ) { | |
| 459 | - if ( isset($messages['err_content'])) { | |
| 460 | - echo '<div class="notice notice-error"><p>' . $messages['err_msg'] . '</p></div>'; | |
| 461 | - } else { | |
| 462 | - echo '<div class="notice notice-success is-dismissible"><p>' . $messages['msg'] . '</p></div>'; | |
| 463 | - } | |
| 464 | - } | |
| 465 | - } | |
| 417 | + return $metadata; | |
| 418 | + } | |
| 466 | 419 | |
| 467 | 420 | } |