acf_field_user

The Advanced Custom Fields acf field user class.

Defined (1)

The class is defined in the following location(s).

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