| 1 |
<?php |
| 2 |
/** |
| 3 |
* Add extra profile fields for users in admin |
| 4 |
* |
| 5 |
* @author PropertyHive |
| 6 |
* @category Admin |
| 7 |
* @package PropertyHive/Admin |
| 8 |
* @version 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly |
| 13 |
} |
| 14 |
|
| 15 |
if ( ! class_exists( 'PH_Admin_Profile', false ) ) : |
| 16 |
|
| 17 |
/** |
| 18 |
* PH_Admin_Profile Class. |
| 19 |
*/ |
| 20 |
class PH_Admin_Profile { |
| 21 |
|
| 22 |
/** |
| 23 |
* Hook in tabs. |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
add_action( 'show_user_profile', array( $this, 'extra_user_meta_fields' ) ); |
| 27 |
add_action( 'edit_user_profile', array( $this, 'extra_user_meta_fields' ) ); |
| 28 |
|
| 29 |
add_action( 'personal_options_update', array( $this, 'save_extra_user_meta_fields' ) ); |
| 30 |
add_action( 'edit_user_profile_update', array( $this, 'save_extra_user_meta_fields' ) ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Get fields for the edit user pages. |
| 35 |
* |
| 36 |
* @return array Fields to display which are filtered through propertyhive_user_meta_fields before being returned |
| 37 |
*/ |
| 38 |
public function get_user_meta_fields() { |
| 39 |
|
| 40 |
$args = array( |
| 41 |
'post_type' => 'office', |
| 42 |
'nopaging' => true, |
| 43 |
'orderby' => 'post_title', |
| 44 |
'order' => 'ASC', |
| 45 |
); |
| 46 |
|
| 47 |
$offices = array(); |
| 48 |
|
| 49 |
$offices_query = new WP_Query( $args ); |
| 50 |
|
| 51 |
if ( $offices_query->have_posts() ) |
| 52 |
{ |
| 53 |
while ( $offices_query->have_posts() ) |
| 54 |
{ |
| 55 |
$offices_query->the_post(); |
| 56 |
|
| 57 |
$offices[get_the_ID()] = get_the_title(); |
| 58 |
} |
| 59 |
} |
| 60 |
wp_reset_postdata(); |
| 61 |
|
| 62 |
$show_fields = apply_filters( |
| 63 |
'propertyhive_user_meta_fields', |
| 64 |
array( |
| 65 |
'negotiator' => array( |
| 66 |
'title' => __( 'Additional Negotiator Information', 'propertyhive' ), |
| 67 |
'fields' => array( |
| 68 |
'office_id' => array( |
| 69 |
'label' => __( 'Office', 'propertyhive' ), |
| 70 |
'description' => '', |
| 71 |
'type' => 'select', |
| 72 |
'options' => array( '' => __( 'Select an office', 'property' ) ) + $offices, |
| 73 |
), |
| 74 |
), |
| 75 |
), |
| 76 |
) |
| 77 |
); |
| 78 |
return $show_fields; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Show fields on edit user pages. |
| 83 |
* |
| 84 |
* @param WP_User $user |
| 85 |
*/ |
| 86 |
public function extra_user_meta_fields( $user ) { |
| 87 |
|
| 88 |
if ( ! current_user_can( 'manage_propertyhive' ) ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$user_meta = get_userdata($user->ID); |
| 93 |
$user_roles = $user_meta->roles; |
| 94 |
|
| 95 |
if ( ! in_array("administrator", $user_roles) && ! in_array("editor", $user_roles) ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$show_fields = $this->get_user_meta_fields(); |
| 100 |
|
| 101 |
foreach ( $show_fields as $fieldset_key => $fieldset ) : |
| 102 |
?> |
| 103 |
<h2><?php echo $fieldset['title']; ?></h2> |
| 104 |
<table class="form-table" id="<?php echo esc_attr( 'fieldset-' . $fieldset_key ); ?>"> |
| 105 |
<?php foreach ( $fieldset['fields'] as $key => $field ) : ?> |
| 106 |
<tr> |
| 107 |
<th> |
| 108 |
<label for="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $field['label'] ); ?></label> |
| 109 |
</th> |
| 110 |
<td> |
| 111 |
<?php if ( ! empty( $field['type'] ) && 'select' === $field['type'] ) : ?> |
| 112 |
<select name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" class="<?php echo esc_attr( $field['class'] ); ?>" style="width: 25em;"> |
| 113 |
<?php |
| 114 |
$selected = esc_attr( get_user_meta( $user->ID, $key, true ) ); |
| 115 |
foreach ( $field['options'] as $option_key => $option_value ) : |
| 116 |
?> |
| 117 |
<option value="<?php echo esc_attr( $option_key ); ?>" <?php selected( $selected, $option_key, true ); ?>><?php echo esc_html( $option_value ); ?></option> |
| 118 |
<?php endforeach; ?> |
| 119 |
</select> |
| 120 |
<?php elseif ( ! empty( $field['type'] ) && 'checkbox' === $field['type'] ) : ?> |
| 121 |
<input type="checkbox" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="1" class="<?php echo esc_attr( $field['class'] ); ?>" <?php checked( (int) get_user_meta( $user->ID, $key, true ), 1, true ); ?> /> |
| 122 |
<?php elseif ( ! empty( $field['type'] ) && 'button' === $field['type'] ) : ?> |
| 123 |
<button type="button" id="<?php echo esc_attr( $key ); ?>" class="button <?php echo esc_attr( $field['class'] ); ?>"><?php echo esc_html( $field['text'] ); ?></button> |
| 124 |
<?php else : ?> |
| 125 |
<input type="text" name="<?php echo esc_attr( $key ); ?>" id="<?php echo esc_attr( $key ); ?>" value="<?php echo esc_attr( $this->get_user_meta( $user->ID, $key ) ); ?>" class="<?php echo ( ! empty( $field['class'] ) ? esc_attr( $field['class'] ) : 'regular-text' ); ?>" /> |
| 126 |
<?php endif; ?> |
| 127 |
<p class="description"><?php echo wp_kses_post( $field['description'] ); ?></p> |
| 128 |
</td> |
| 129 |
</tr> |
| 130 |
<?php endforeach; ?> |
| 131 |
</table> |
| 132 |
<?php |
| 133 |
endforeach; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Save Address Fields on edit user pages. |
| 138 |
* |
| 139 |
* @param int $user_id User ID of the user being saved |
| 140 |
*/ |
| 141 |
public function save_extra_user_meta_fields( $user_id ) { |
| 142 |
|
| 143 |
if ( ! current_user_can( 'manage_propertyhive' ) ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
$user_meta = get_userdata($user_id); |
| 148 |
$user_roles = $user_meta->roles; |
| 149 |
|
| 150 |
if ( ! in_array("administrator", $user_roles) && ! in_array("editor", $user_roles) ) { |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
$save_fields = $this->get_user_meta_fields(); |
| 155 |
|
| 156 |
foreach ( $save_fields as $fieldset ) { |
| 157 |
|
| 158 |
foreach ( $fieldset['fields'] as $key => $field ) { |
| 159 |
|
| 160 |
if ( isset( $field['type'] ) && 'checkbox' === $field['type'] ) { |
| 161 |
update_user_meta( $user_id, $key, isset( $_POST[ $key ] ) ); |
| 162 |
} elseif ( isset( $_POST[ $key ] ) ) { |
| 163 |
update_user_meta( $user_id, $key, ph_clean( $_POST[ $key ] ) ); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get user meta for a given key, with fallbacks to core user info for pre-existing fields. |
| 171 |
* |
| 172 |
* @param int $user_id User ID of the user being edited |
| 173 |
* @param string $key Key for user meta field |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
protected function get_user_meta( $user_id, $key ) { |
| 177 |
$value = get_user_meta( $user_id, $key, true ); |
| 178 |
|
| 179 |
return $value; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
endif; |
| 184 |
|
| 185 |
return new PH_Admin_Profile(); |
| 186 |
|