/core/fields/user.php

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