field-advanced-link.php
3 months ago
field-button-group.php
1 week ago
field-button.php
7 months ago
field-checkbox.php
3 months ago
field-clone.php
2 years ago
field-code-editor.php
3 months ago
field-column.php
3 years ago
field-dynamic-render.php
3 years ago
field-file.php
3 months ago
field-flexible-content-actions-title.php
3 months ago
field-flexible-content-actions-toggle.php
3 months ago
field-flexible-content-actions.php
3 months ago
field-flexible-content-async.php
4 months ago
field-flexible-content-compatibility.php
3 months ago
field-flexible-content-controls.php
9 months ago
field-flexible-content-hide.php
3 months ago
field-flexible-content-hooks.php
9 months ago
field-flexible-content-modal-edit.php
9 months ago
field-flexible-content-modal-select.php
3 months ago
field-flexible-content-modal-settings.php
3 months ago
field-flexible-content-popup.php
3 months ago
field-flexible-content-preview.php
3 months ago
field-flexible-content-state.php
9 months ago
field-flexible-content-thumbnail.php
9 months ago
field-flexible-content-wysiwyg.php
9 months ago
field-flexible-content.php
3 months ago
field-forms.php
3 months ago
field-gallery.php
3 months ago
field-google-map.php
3 months ago
field-group.php
3 months ago
field-hidden.php
3 years ago
field-image.php
3 months ago
field-post-object.php
3 months ago
field-post-statuses.php
3 months ago
field-post-types.php
3 months ago
field-radio.php
3 months ago
field-recaptcha.php
3 months ago
field-relationship.php
3 months ago
field-repeater.php
3 months ago
field-select.php
3 months ago
field-slug.php
1 year ago
field-taxonomies.php
3 months ago
field-taxonomy-terms.php
3 months ago
field-taxonomy.php
3 months ago
field-textarea.php
3 months ago
field-user-roles.php
3 months ago
field-user.php
3 months ago
field-wysiwyg.php
3 months ago
field-user.php
146 lines
| 1 | <?php |
| 2 | |
| 3 | if(!defined('ABSPATH')){ |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | if(!class_exists('acfe_field_user')): |
| 8 | |
| 9 | class acfe_field_user extends acfe_field_extend{ |
| 10 | |
| 11 | /** |
| 12 | * initialize |
| 13 | */ |
| 14 | function initialize(){ |
| 15 | |
| 16 | $this->name = 'user'; |
| 17 | |
| 18 | } |
| 19 | |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * format_front_value |
| 24 | * |
| 25 | * @param $formatted |
| 26 | * @param $unformatted |
| 27 | * @param $post_id |
| 28 | * @param $field |
| 29 | * @param $form |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | function format_front_value($formatted, $unformatted, $post_id, $field, $form){ |
| 34 | |
| 35 | // vars |
| 36 | $value = acfe_as_array($unformatted); |
| 37 | $array = array(); |
| 38 | |
| 39 | // loop values |
| 40 | foreach($value as $user_id){ |
| 41 | |
| 42 | // get user data |
| 43 | $user_data = get_userdata($user_id); |
| 44 | |
| 45 | // validate |
| 46 | if($user_data){ |
| 47 | $array[] = $user_data->user_nicename; |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | // merge |
| 53 | return implode(', ', $array); |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * validate_front_value |
| 60 | * |
| 61 | * @param $valid |
| 62 | * @param $value |
| 63 | * @param $field |
| 64 | * @param $input |
| 65 | * @param $form |
| 66 | * |
| 67 | * @return false |
| 68 | */ |
| 69 | function validate_front_value($valid, $value, $field, $input, $form){ |
| 70 | |
| 71 | // bail early |
| 72 | if(!$this->pre_validate_front_value($valid, $value, $field, $form)){ |
| 73 | return $valid; |
| 74 | } |
| 75 | |
| 76 | // vars |
| 77 | $value = acfe_as_array($value); |
| 78 | |
| 79 | // loop value |
| 80 | foreach($value as $user_id){ |
| 81 | |
| 82 | // check value |
| 83 | if(!$this->is_value_valid($user_id, $field, $form['post_id'])){ |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | } |
| 88 | |
| 89 | // return |
| 90 | return $valid; |
| 91 | |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * is_value_valid |
| 97 | * |
| 98 | * @param $user_id |
| 99 | * @param $field |
| 100 | * @param $post_id |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | function is_value_valid($user_id, $field, $post_id){ |
| 105 | |
| 106 | // bail early |
| 107 | if(empty($user_id)){ |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | // check user exists |
| 112 | $user_data = get_userdata($user_id); |
| 113 | if(!$user_data){ |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // vars |
| 118 | $args = array(); |
| 119 | |
| 120 | // role |
| 121 | if(!empty($field['role'])){ |
| 122 | $args['role__in'] = acfe_as_array($field['role']); |
| 123 | } |
| 124 | |
| 125 | // filter |
| 126 | // this filter might be used by developers to set specific users as choices |
| 127 | $args = apply_filters('acf/fields/user/query', $args, $field, $post_id); |
| 128 | |
| 129 | // override search args |
| 130 | $args['search'] = $user_id; |
| 131 | $args['search_columns'] = array('ID'); |
| 132 | |
| 133 | // query users |
| 134 | $query = new WP_User_Query($args); |
| 135 | $total_users = $query->get_total(); |
| 136 | |
| 137 | // check total found |
| 138 | return ($total_users > 0); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | acf_new_instance('acfe_field_user'); |
| 145 | |
| 146 | endif; |