| 1 |
<?php |
| 2 |
/** |
| 3 |
* User Metabox |
| 4 |
* |
| 5 |
* @since 1.0.5 |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Whols\Admin; |
| 9 |
|
| 10 |
/** |
| 11 |
* User Metabox |
| 12 |
*/ |
| 13 |
class User_Metabox { |
| 14 |
|
| 15 |
/** |
| 16 |
* Metabox constructor. |
| 17 |
* |
| 18 |
* @since 1.0.0 |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
// Generate additional custom fields for user |
| 22 |
add_action( 'show_user_profile', array( $this, 'additional_fields_for_user' ) ); |
| 23 |
add_action( 'edit_user_profile', array( $this, 'additional_fields_for_user' ) ); |
| 24 |
|
| 25 |
// Save fields |
| 26 |
add_action( 'personal_options_update', array( $this, 'save_additional_fields' ) ); |
| 27 |
add_action( 'edit_user_profile_update', array( $this, 'save_additional_fields' ) ); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Generate custom fields for user |
| 32 |
*/ |
| 33 |
public function additional_fields_for_user() { |
| 34 |
if( empty($_GET['user_id']) ){ |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$user_id = absint($_GET['user_id']); |
| 39 |
$user_meta = get_user_meta( $user_id, '', false ); |
| 40 |
?> |
| 41 |
<h3><?php echo esc_html__("Whols - Additional Fields", "whols"); ?></h3> |
| 42 |
|
| 43 |
<table class="form-table whols-additional-fields"> |
| 44 |
|
| 45 |
<?php |
| 46 |
$has_additional_fields = false; |
| 47 |
foreach($user_meta as $key => $value){ |
| 48 |
if( str_starts_with($key, '_whols_') ){ |
| 49 |
$key = str_replace('_whols_', '', $key); |
| 50 |
$fields = whols_get_registration_fields(); |
| 51 |
$field_info = !empty($fields[$key]) ? $fields[$key] : array(); |
| 52 |
|
| 53 |
if($field_info){ |
| 54 |
$has_additional_fields = true; |
| 55 |
// add input class |
| 56 |
$field_info['input_class'] = array('regular-text'); |
| 57 |
?> |
| 58 |
<tr> |
| 59 |
<th><label for="<?php echo esc_attr($key) ?>"><?php echo esc_html($field_info['label']); ?></label></th> |
| 60 |
<td> |
| 61 |
<?php whols_form_field( $key, $field_info, $value[0] ); ?> |
| 62 |
</td> |
| 63 |
</tr> |
| 64 |
<?php |
| 65 |
} |
| 66 |
} |
| 67 |
} ?> |
| 68 |
|
| 69 |
<?php |
| 70 |
if( !$has_additional_fields ){ |
| 71 |
echo esc_html__('No additional fields found!', 'whols'); |
| 72 |
} |
| 73 |
?> |
| 74 |
</table> |
| 75 |
|
| 76 |
<?php } |
| 77 |
|
| 78 |
/** |
| 79 |
* Save custom fields |
| 80 |
*/ |
| 81 |
public function save_additional_fields( $user_id ) { |
| 82 |
$posted_data = wp_unslash($_POST); |
| 83 |
|
| 84 |
if ( empty( $posted_data['_wpnonce'] ) || ! wp_verify_nonce( $posted_data['_wpnonce'], 'update-user_' . $user_id ) ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if ( !current_user_can( 'edit_user', $user_id ) ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
$user_meta = get_user_meta( $user_id, '', false ); |
| 93 |
foreach($user_meta as $key => $value){ |
| 94 |
if( str_starts_with($key, '_whols_') ){ |
| 95 |
update_user_meta( $user_id, $key, $posted_data[$key] ); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |