| 1 |
<?php |
| 2 |
|
| 3 |
namespace King_Addons\Widgets\Login_Register_Form; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
/** |
| 10 |
* User Profile Fields Handler for Login Register Form widget |
| 11 |
*/ |
| 12 |
class User_Profile_Fields |
| 13 |
{ |
| 14 |
/** |
| 15 |
* Initialize hooks |
| 16 |
*/ |
| 17 |
public static function init() |
| 18 |
{ |
| 19 |
add_action('show_user_profile', [__CLASS__, 'show_extra_profile_fields']); |
| 20 |
add_action('edit_user_profile', [__CLASS__, 'show_extra_profile_fields']); |
| 21 |
add_action('personal_options_update', [__CLASS__, 'save_extra_profile_fields']); |
| 22 |
add_action('edit_user_profile_update', [__CLASS__, 'save_extra_profile_fields']); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Show extra profile fields |
| 27 |
*/ |
| 28 |
public static function show_extra_profile_fields($user) |
| 29 |
{ |
| 30 |
?> |
| 31 |
<h3><?php esc_html_e('Additional Information', 'king-addons'); ?></h3> |
| 32 |
<table class="form-table"> |
| 33 |
<tr> |
| 34 |
<th><label for="phone"><?php esc_html_e('Phone', 'king-addons'); ?></label></th> |
| 35 |
<td> |
| 36 |
<input type="text" name="phone" id="phone" value="<?php echo esc_attr(get_user_meta($user->ID, 'phone', true)); ?>" class="regular-text" /> |
| 37 |
<br /> |
| 38 |
<span class="description"><?php esc_html_e('Please enter your phone number.', 'king-addons'); ?></span> |
| 39 |
</td> |
| 40 |
</tr> |
| 41 |
<?php |
| 42 |
// Show social login provider info |
| 43 |
$social_provider = get_user_meta($user->ID, 'king_addons_social_provider', true); |
| 44 |
if (!empty($social_provider)) { |
| 45 |
$provider_id = get_user_meta($user->ID, 'king_addons_social_provider_id', true); |
| 46 |
$social_picture = get_user_meta($user->ID, 'king_addons_social_picture', true); |
| 47 |
?> |
| 48 |
<tr> |
| 49 |
<th><?php esc_html_e('Social Login', 'king-addons'); ?></th> |
| 50 |
<td> |
| 51 |
<div style="display: flex; align-items: center; gap: 10px;"> |
| 52 |
<?php if (!empty($social_picture)): ?> |
| 53 |
<img src="<?php echo esc_url($social_picture); ?>" alt="Profile" style="width: 32px; height: 32px; border-radius: 50%;" /> |
| 54 |
<?php endif; ?> |
| 55 |
<div> |
| 56 |
<strong><?php echo esc_html(ucfirst($social_provider)); ?></strong><br /> |
| 57 |
<small><?php printf(esc_html__('Connected via %s', 'king-addons'), esc_html(ucfirst($social_provider))); ?></small> |
| 58 |
</div> |
| 59 |
</div> |
| 60 |
</td> |
| 61 |
</tr> |
| 62 |
<?php |
| 63 |
} |
| 64 |
|
| 65 |
// Show custom fields from King Addons |
| 66 |
$custom_fields = self::get_king_addons_custom_fields($user->ID); |
| 67 |
if (!empty($custom_fields)) { |
| 68 |
foreach ($custom_fields as $meta_key => $field_data) { |
| 69 |
$field_label = $field_data['label']; |
| 70 |
$field_value = $field_data['value']; |
| 71 |
?> |
| 72 |
<tr> |
| 73 |
<th><label for="<?php echo esc_attr($meta_key); ?>"><?php echo esc_html($field_label); ?></label></th> |
| 74 |
<td> |
| 75 |
<?php if (is_array($field_value)): ?> |
| 76 |
<textarea name="<?php echo esc_attr($meta_key); ?>" id="<?php echo esc_attr($meta_key); ?>" class="regular-text" rows="3"><?php echo esc_textarea(implode(', ', $field_value)); ?></textarea> |
| 77 |
<?php else: ?> |
| 78 |
<input type="text" name="<?php echo esc_attr($meta_key); ?>" id="<?php echo esc_attr($meta_key); ?>" value="<?php echo esc_attr($field_value); ?>" class="regular-text" /> |
| 79 |
<?php endif; ?> |
| 80 |
<br /> |
| 81 |
<span class="description"><?php printf(esc_html__('Custom field: %s (added by King Addons)', 'king-addons'), esc_html($field_label)); ?></span> |
| 82 |
</td> |
| 83 |
</tr> |
| 84 |
<?php |
| 85 |
} |
| 86 |
} |
| 87 |
?> |
| 88 |
</table> |
| 89 |
<?php |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Save extra profile fields |
| 94 |
*/ |
| 95 |
public static function save_extra_profile_fields($user_id) |
| 96 |
{ |
| 97 |
if (!current_user_can('edit_user', $user_id)) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
if (isset($_POST['phone'])) { |
| 102 |
update_user_meta($user_id, 'phone', sanitize_text_field($_POST['phone'])); |
| 103 |
} |
| 104 |
|
| 105 |
// Save King Addons custom fields |
| 106 |
$custom_fields = self::get_king_addons_custom_fields($user_id); |
| 107 |
if (!empty($custom_fields)) { |
| 108 |
foreach ($custom_fields as $meta_key => $field_data) { |
| 109 |
if (isset($_POST[$meta_key])) { |
| 110 |
$sanitized_value = sanitize_textarea_field($_POST[$meta_key]); |
| 111 |
update_user_meta($user_id, $meta_key, $sanitized_value); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Get King Addons custom fields for a user |
| 119 |
*/ |
| 120 |
private static function get_king_addons_custom_fields($user_id) |
| 121 |
{ |
| 122 |
$custom_fields = []; |
| 123 |
$all_meta = get_user_meta($user_id); |
| 124 |
|
| 125 |
foreach ($all_meta as $meta_key => $meta_value) { |
| 126 |
// Check if it's a King Addons custom field (but not a label) |
| 127 |
if (strpos($meta_key, 'king_addons_custom_field_') === 0 && strpos($meta_key, '_label') === false) { |
| 128 |
$field_value = is_array($meta_value) && count($meta_value) === 1 ? $meta_value[0] : $meta_value; |
| 129 |
|
| 130 |
// Try to get the field label |
| 131 |
$label_key = $meta_key . '_label'; |
| 132 |
$field_label = get_user_meta($user_id, $label_key, true); |
| 133 |
|
| 134 |
$custom_fields[$meta_key] = [ |
| 135 |
'value' => $field_value, |
| 136 |
'label' => !empty($field_label) ? $field_label : ucwords(str_replace(['king_addons_custom_field_', '_'], ['', ' '], $meta_key)) |
| 137 |
]; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $custom_fields; |
| 142 |
} |
| 143 |
} |