user.php
| 1 | <?php |
| 2 | |
| 3 | class acf_field_user extends acf_field |
| 4 | { |
| 5 | |
| 6 | var $defaults; |
| 7 | |
| 8 | |
| 9 | /* |
| 10 | * __construct |
| 11 | * |
| 12 | * Set name / label needed for actions / filters |
| 13 | * |
| 14 | * @since 3.6 |
| 15 | * @date 23/01/13 |
| 16 | */ |
| 17 | |
| 18 | function __construct() |
| 19 | { |
| 20 | // vars |
| 21 | $this->name = 'user'; |
| 22 | $this->label = __("User",'acf'); |
| 23 | $this->category = __("Relational",'acf'); |
| 24 | |
| 25 | |
| 26 | // settings |
| 27 | $this->defaults = array( |
| 28 | 'role' => 'all', |
| 29 | 'field_type' => 'select', |
| 30 | 'allow_null' => 0, |
| 31 | ); |
| 32 | |
| 33 | |
| 34 | // do not delete! |
| 35 | parent::__construct(); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * format_value_for_api() |
| 42 | * |
| 43 | * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field |
| 44 | * |
| 45 | * @type filter |
| 46 | * @since 3.6 |
| 47 | * @date 23/01/13 |
| 48 | * |
| 49 | * @param $value - the value which was loaded from the database |
| 50 | * @param $field - the field array holding all the field options |
| 51 | * |
| 52 | * @return $value - the modified value |
| 53 | */ |
| 54 | |
| 55 | function format_value_for_api( $value, $post_id, $field ) |
| 56 | { |
| 57 | |
| 58 | // format value |
| 59 | if( !$value || $value == 'null' ) |
| 60 | { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | // temp convert to array |
| 66 | $is_array = true; |
| 67 | |
| 68 | if( !is_array($value) ) |
| 69 | { |
| 70 | $is_array = false; |
| 71 | $value = array( $value ); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | foreach( $value as $k => $v ) |
| 76 | { |
| 77 | $user_data = get_userdata( $v ); |
| 78 | |
| 79 | //cope with deleted users by @adampope |
| 80 | if( !is_object($user_data) ) |
| 81 | { |
| 82 | unset( $value[$k] ); |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | $value[ $k ] = array(); |
| 88 | $value[ $k ]['ID'] = $v; |
| 89 | $value[ $k ]['user_firstname'] = $user_data->user_firstname; |
| 90 | $value[ $k ]['user_lastname'] = $user_data->user_lastname; |
| 91 | $value[ $k ]['nickname'] = $user_data->nickname; |
| 92 | $value[ $k ]['user_nicename'] = $user_data->user_nicename; |
| 93 | $value[ $k ]['display_name'] = $user_data->display_name; |
| 94 | $value[ $k ]['user_email'] = $user_data->user_email; |
| 95 | $value[ $k ]['user_url'] = $user_data->user_url; |
| 96 | $value[ $k ]['user_registered'] = $user_data->user_registered; |
| 97 | $value[ $k ]['user_description'] = $user_data->user_description; |
| 98 | $value[ $k ]['user_avatar'] = get_avatar( $v ); |
| 99 | |
| 100 | } |
| 101 | |
| 102 | |
| 103 | // de-convert from array |
| 104 | if( !$is_array && isset($value[0]) ) |
| 105 | { |
| 106 | $value = $value[0]; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | // return value |
| 111 | return $value; |
| 112 | |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | * input_admin_head() |
| 118 | * |
| 119 | * This action is called in the admin_head action on the edit screen where your field is created. |
| 120 | * Use this action to add css and javascript to assist your create_field() action. |
| 121 | * |
| 122 | * @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head |
| 123 | * @type action |
| 124 | * @since 3.6 |
| 125 | * @date 23/01/13 |
| 126 | */ |
| 127 | |
| 128 | function input_admin_head() |
| 129 | { |
| 130 | if( ! function_exists( 'get_editable_roles' ) ) |
| 131 | { |
| 132 | // if using front-end forms then we need to add this core file |
| 133 | require_once( ABSPATH . '/wp-admin/includes/user.php' ); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /* |
| 139 | * create_field() |
| 140 | * |
| 141 | * Create the HTML interface for your field |
| 142 | * |
| 143 | * @type action |
| 144 | * @since 3.6 |
| 145 | * @date 23/01/13 |
| 146 | * |
| 147 | * @param $field - an array holding all the field's data |
| 148 | */ |
| 149 | |
| 150 | function create_field( $field ) |
| 151 | { |
| 152 | // vars |
| 153 | $field = array_merge($this->defaults, $field); |
| 154 | $field['choices'] = array(); |
| 155 | $args = array(); |
| 156 | $editable_roles = get_editable_roles(); |
| 157 | |
| 158 | |
| 159 | // roles |
| 160 | if( !$field['role'] || !is_array( $field['role'] ) || $field['role'][0] == 'all' ) |
| 161 | { |
| 162 | $field['role'] = array(); |
| 163 | |
| 164 | |
| 165 | foreach( $editable_roles as $role => $details ) |
| 166 | { |
| 167 | // only translate the output not the value |
| 168 | $field['role'][] = $role; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | // choices |
| 174 | foreach( $field['role'] as $role ) |
| 175 | { |
| 176 | $label = translate_user_role( $editable_roles[ $role ]['name'] ); |
| 177 | |
| 178 | // get users |
| 179 | $users = get_users(array( |
| 180 | 'role' => $role |
| 181 | )); |
| 182 | |
| 183 | |
| 184 | if( $users ) |
| 185 | { |
| 186 | $field['choices'][ $label ] = array(); |
| 187 | |
| 188 | foreach( $users as $user ) |
| 189 | { |
| 190 | $field['choices'][ $label ][ $user->ID ] = ucfirst( $user->display_name ); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | // modify field |
| 197 | if( $field['field_type'] == 'multi_select' ) |
| 198 | { |
| 199 | $field['multiple'] = 1; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | $field['type'] = 'select'; |
| 204 | |
| 205 | |
| 206 | do_action('acf/create_field', $field); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /* |
| 212 | * create_options() |
| 213 | * |
| 214 | * Create extra options for your field. This is rendered when editing a field. |
| 215 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 216 | * |
| 217 | * @type action |
| 218 | * @since 3.6 |
| 219 | * @date 23/01/13 |
| 220 | * |
| 221 | * @param $field - an array holding all the field's data |
| 222 | */ |
| 223 | |
| 224 | function create_options( $field ) |
| 225 | { |
| 226 | // vars |
| 227 | $field = array_merge($this->defaults, $field); |
| 228 | $key = $field['name']; |
| 229 | |
| 230 | ?> |
| 231 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 232 | <td class="label"> |
| 233 | <label><?php _e( "Filter by role", 'acf' ); ?></label> |
| 234 | </td> |
| 235 | <td> |
| 236 | <?php |
| 237 | |
| 238 | $choices = array('all' => __('All', 'acf')); |
| 239 | $editable_roles = get_editable_roles(); |
| 240 | |
| 241 | foreach( $editable_roles as $role => $details ) |
| 242 | { |
| 243 | // only translate the output not the value |
| 244 | $choices[$role] = translate_user_role( $details['name'] ); |
| 245 | } |
| 246 | |
| 247 | do_action('acf/create_field', array( |
| 248 | 'type' => 'select', |
| 249 | 'name' => 'fields[' . $key . '][role]', |
| 250 | 'value' => $field['role'], |
| 251 | 'choices' => $choices, |
| 252 | 'multiple' => '1', |
| 253 | )); |
| 254 | |
| 255 | ?> |
| 256 | </td> |
| 257 | </tr> |
| 258 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 259 | <td class="label"> |
| 260 | <label><?php _e("Field Type",'acf'); ?></label> |
| 261 | </td> |
| 262 | <td> |
| 263 | <?php |
| 264 | do_action('acf/create_field', array( |
| 265 | 'type' => 'select', |
| 266 | 'name' => 'fields['.$key.'][field_type]', |
| 267 | 'value' => $field['field_type'], |
| 268 | 'choices' => array( |
| 269 | __("Multiple Values",'acf') => array( |
| 270 | //'checkbox' => __('Checkbox', 'acf'), |
| 271 | 'multi_select' => __('Multi Select', 'acf') |
| 272 | ), |
| 273 | __("Single Value",'acf') => array( |
| 274 | //'radio' => __('Radio Buttons', 'acf'), |
| 275 | 'select' => __('Select', 'acf') |
| 276 | ) |
| 277 | ) |
| 278 | )); |
| 279 | ?> |
| 280 | </td> |
| 281 | </tr> |
| 282 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 283 | <td class="label"> |
| 284 | <label><?php _e("Allow Null?",'acf'); ?></label> |
| 285 | </td> |
| 286 | <td> |
| 287 | <?php |
| 288 | do_action('acf/create_field', array( |
| 289 | 'type' => 'radio', |
| 290 | 'name' => 'fields['.$key.'][allow_null]', |
| 291 | 'value' => $field['allow_null'], |
| 292 | 'choices' => array( |
| 293 | 1 => __("Yes",'acf'), |
| 294 | 0 => __("No",'acf'), |
| 295 | ), |
| 296 | 'layout' => 'horizontal', |
| 297 | )); |
| 298 | ?> |
| 299 | </td> |
| 300 | </tr> |
| 301 | <?php |
| 302 | |
| 303 | } |
| 304 | |
| 305 | |
| 306 | /* |
| 307 | * update_value() |
| 308 | * |
| 309 | * This filter is appied to the $value before it is updated in the db |
| 310 | * |
| 311 | * @type filter |
| 312 | * @since 3.6 |
| 313 | * @date 23/01/13 |
| 314 | * |
| 315 | * @param $value - the value which will be saved in the database |
| 316 | * @param $post_id - the $post_id of which the value will be saved |
| 317 | * @param $field - the field array holding all the field options |
| 318 | * |
| 319 | * @return $value - the modified value |
| 320 | */ |
| 321 | |
| 322 | function update_value( $value, $post_id, $field ) |
| 323 | { |
| 324 | // array? |
| 325 | if( is_array($value) && isset($value['ID']) ) |
| 326 | { |
| 327 | $value = $value['ID']; |
| 328 | } |
| 329 | |
| 330 | // object? |
| 331 | if( is_object($value) && isset($value->ID) ) |
| 332 | { |
| 333 | $value = $value->ID; |
| 334 | } |
| 335 | |
| 336 | return $value; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | } |
| 341 | |
| 342 | new acf_field_user(); |
| 343 | |
| 344 | ?> |