PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/advanced-custom-fields-pro/fields/user.php

https://gitlab.com/hschoenburg/tlworks2
PHP | 595 lines | 205 code | 193 blank | 197 comment | 25 complexity | 409ee01d9927760b8b65272d10fd2726 MD5 | raw file
  1. <?php
  2. /*
  3. * ACF User Field Class
  4. *
  5. * All the logic for this field type
  6. *
  7. * @class acf_field_user
  8. * @extends acf_field
  9. * @package ACF
  10. * @subpackage Fields
  11. */
  12. if( ! class_exists('acf_field_user') ) :
  13. class acf_field_user extends acf_field {
  14. /*
  15. * __construct
  16. *
  17. * This function will setup the field type data
  18. *
  19. * @type function
  20. * @date 5/03/2014
  21. * @since 5.0.0
  22. *
  23. * @param n/a
  24. * @return n/a
  25. */
  26. function __construct() {
  27. // vars
  28. $this->name = 'user';
  29. $this->label = __("User",'acf');
  30. $this->category = 'relational';
  31. $this->defaults = array(
  32. 'role' => '',
  33. 'multiple' => 0,
  34. 'allow_null' => 0,
  35. );
  36. // extra
  37. add_action('wp_ajax_acf/fields/user/query', array($this, 'ajax_query'));
  38. add_action('wp_ajax_nopriv_acf/fields/user/query', array($this, 'ajax_query'));
  39. // do not delete!
  40. parent::__construct();
  41. }
  42. /*
  43. * get_choices
  44. *
  45. * This function will return an array of data formatted for use in a select2 AJAX response
  46. *
  47. * @type function
  48. * @date 15/10/2014
  49. * @since 5.0.9
  50. *
  51. * @param $options (array)
  52. * @return (array)
  53. */
  54. function get_choices( $options = array() ) {
  55. // defaults
  56. $options = acf_parse_args($options, array(
  57. 'post_id' => 0,
  58. 's' => '',
  59. 'field_key' => '',
  60. 'paged' => 1,
  61. ));
  62. // vars
  63. $r = array();
  64. $args = array();
  65. // paged
  66. $args['users_per_page'] = 20;
  67. $args['paged'] = $options['paged'];
  68. // load field
  69. $field = acf_get_field( $options['field_key'] );
  70. // bail early if no field
  71. if( !$field ) return false;
  72. // update $args
  73. if( !empty($field['role']) ) {
  74. $args['role'] = acf_get_array( $field['role'] );
  75. }
  76. // search
  77. if( $options['s'] ) {
  78. // append to $args
  79. $args['search'] = '*' . $options['s'] . '*';
  80. // add reference
  81. $this->field = $field;
  82. // add filter to modify search colums
  83. add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 3);
  84. }
  85. // filters
  86. $args = apply_filters("acf/fields/user/query", $args, $field, $options['post_id']);
  87. $args = apply_filters("acf/fields/user/query/name={$field['_name']}", $args, $field, $options['post_id']);
  88. $args = apply_filters("acf/fields/user/query/key={$field['key']}", $args, $field, $options['post_id']);
  89. // get users
  90. $groups = acf_get_grouped_users( $args );
  91. // bail early if no groups
  92. if( empty($groups) ) return false;
  93. // loop
  94. foreach( array_keys($groups) as $group_title ) {
  95. // vars
  96. $users = acf_extract_var( $groups, $group_title );
  97. $data = array(
  98. 'text' => $group_title,
  99. 'children' => array()
  100. );
  101. // append users
  102. foreach( array_keys($users) as $user_id ) {
  103. $users[ $user_id ] = $this->get_result( $users[ $user_id ], $field, $options['post_id'] );
  104. };
  105. // order by search
  106. if( !empty($args['s']) ) {
  107. $users = acf_order_by_search( $users, $args['s'] );
  108. }
  109. // append to $data
  110. foreach( $users as $id => $title ) {
  111. $data['children'][] = array(
  112. 'id' => $id,
  113. 'text' => $title
  114. );
  115. }
  116. // append to $r
  117. $r[] = $data;
  118. }
  119. // optgroup or single
  120. if( !empty($args['role']) && count($args['role']) == 1 ) {
  121. $r = $r[0]['children'];
  122. }
  123. // return
  124. return $r;
  125. }
  126. /*
  127. * ajax_query
  128. *
  129. * description
  130. *
  131. * @type function
  132. * @date 24/10/13
  133. * @since 5.0.0
  134. *
  135. * @param $post_id (int)
  136. * @return $post_id (int)
  137. */
  138. function ajax_query() {
  139. // validate
  140. if( !acf_verify_ajax() ) die();
  141. // get choices
  142. $choices = $this->get_choices( $_POST );
  143. // validate
  144. if( !$choices ) die();
  145. // return JSON
  146. echo json_encode( $choices );
  147. die();
  148. }
  149. /*
  150. * get_result
  151. *
  152. * This function returns the HTML for a result
  153. *
  154. * @type function
  155. * @date 1/11/2013
  156. * @since 5.0.0
  157. *
  158. * @param $post (object)
  159. * @param $field (array)
  160. * @param $post_id (int) the post_id to which this value is saved to
  161. * @return (string)
  162. */
  163. function get_result( $user, $field, $post_id = 0 ) {
  164. // get post_id
  165. if( !$post_id ) {
  166. $post_id = acf_get_setting('form_data/post_id', get_the_ID());
  167. }
  168. // vars
  169. $result = $user->user_login;
  170. // append name
  171. if( $user->first_name ) {
  172. $result .= ' (' . $user->first_name;
  173. if( $user->last_name ) {
  174. $result .= ' ' . $user->last_name;
  175. }
  176. $result .= ')';
  177. }
  178. // filters
  179. $result = apply_filters("acf/fields/user/result", $result, $user, $field, $post_id);
  180. $result = apply_filters("acf/fields/user/result/name={$field['_name']}", $result, $user, $field, $post_id);
  181. $result = apply_filters("acf/fields/user/result/key={$field['key']}", $result, $user, $field, $post_id);
  182. // return
  183. return $result;
  184. }
  185. /*
  186. * user_search_columns
  187. *
  188. * This function will modify the columns which the user AJAX search looks in
  189. *
  190. * @type function
  191. * @date 17/06/2014
  192. * @since 5.0.0
  193. *
  194. * @param $columns (array)
  195. * @return $columns
  196. */
  197. function user_search_columns( $columns, $search, $WP_User_Query ) {
  198. // bail early if no field
  199. if( empty($this->field) ) {
  200. return $columns;
  201. }
  202. // vars
  203. $field = $this->field;
  204. // filter for 3rd party customization
  205. $columns = apply_filters("acf/fields/user/search_columns", $columns, $search, $WP_User_Query, $field);
  206. $columns = apply_filters("acf/fields/user/search_columns/name={$field['_name']}", $columns, $search, $WP_User_Query, $field);
  207. $columns = apply_filters("acf/fields/user/search_columns/key={$field['key']}", $columns, $search, $WP_User_Query, $field);
  208. // return
  209. return $columns;
  210. }
  211. /*
  212. * render_field()
  213. *
  214. * Create the HTML interface for your field
  215. *
  216. * @type action
  217. * @since 3.6
  218. * @date 23/01/13
  219. *
  220. * @param $field - an array holding all the field's data
  221. */
  222. function render_field( $field ) {
  223. // Change Field into a select
  224. $field['type'] = 'select';
  225. $field['ui'] = 1;
  226. $field['ajax'] = 1;
  227. $field['choices'] = array();
  228. // populate choices
  229. if( !empty($field['value']) ) {
  230. // force value to array
  231. $field['value'] = acf_get_array( $field['value'] );
  232. // convert values to int
  233. $field['value'] = array_map('intval', $field['value']);
  234. $users = get_users(array(
  235. 'include' => $field['value']
  236. ));
  237. if( !empty($users) ) {
  238. foreach( $users as $user ) {
  239. $field['choices'][ $user->ID ] = $this->get_result( $user, $field );
  240. }
  241. }
  242. }
  243. // render
  244. acf_render_field( $field );
  245. }
  246. /*
  247. * render_field_settings()
  248. *
  249. * Create extra options for your field. This is rendered when editing a field.
  250. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  251. *
  252. * @type action
  253. * @since 3.6
  254. * @date 23/01/13
  255. *
  256. * @param $field - an array holding all the field's data
  257. */
  258. function render_field_settings( $field ) {
  259. acf_render_field_setting( $field, array(
  260. 'label' => __('Filter by role','acf'),
  261. 'instructions' => '',
  262. 'type' => 'select',
  263. 'name' => 'role',
  264. 'choices' => acf_get_pretty_user_roles(),
  265. 'multiple' => 1,
  266. 'ui' => 1,
  267. 'allow_null' => 1,
  268. 'placeholder' => __("All user roles",'acf'),
  269. ));
  270. // allow_null
  271. acf_render_field_setting( $field, array(
  272. 'label' => __('Allow Null?','acf'),
  273. 'instructions' => '',
  274. 'type' => 'radio',
  275. 'name' => 'allow_null',
  276. 'choices' => array(
  277. 1 => __("Yes",'acf'),
  278. 0 => __("No",'acf'),
  279. ),
  280. 'layout' => 'horizontal',
  281. ));
  282. // multiple
  283. acf_render_field_setting( $field, array(
  284. 'label' => __('Select multiple values?','acf'),
  285. 'instructions' => '',
  286. 'type' => 'radio',
  287. 'name' => 'multiple',
  288. 'choices' => array(
  289. 1 => __("Yes",'acf'),
  290. 0 => __("No",'acf'),
  291. ),
  292. 'layout' => 'horizontal',
  293. ));
  294. }
  295. /*
  296. * update_value()
  297. *
  298. * This filter is appied to the $value before it is updated in the db
  299. *
  300. * @type filter
  301. * @since 3.6
  302. * @date 23/01/13
  303. *
  304. * @param $value - the value which will be saved in the database
  305. * @param $post_id - the $post_id of which the value will be saved
  306. * @param $field - the field array holding all the field options
  307. *
  308. * @return $value - the modified value
  309. */
  310. function update_value( $value, $post_id, $field ) {
  311. // array?
  312. if( is_array($value) && isset($value['ID']) ) {
  313. $value = $value['ID'];
  314. }
  315. // object?
  316. if( is_object($value) && isset($value->ID) ) {
  317. $value = $value->ID;
  318. }
  319. // return
  320. return $value;
  321. }
  322. /*
  323. * load_value()
  324. *
  325. * This filter is applied to the $value after it is loaded from the db
  326. *
  327. * @type filter
  328. * @since 3.6
  329. * @date 23/01/13
  330. *
  331. * @param $value (mixed) the value found in the database
  332. * @param $post_id (mixed) the $post_id from which the value was loaded
  333. * @param $field (array) the field array holding all the field options
  334. * @return $value
  335. */
  336. function load_value( $value, $post_id, $field ) {
  337. // ACF4 null
  338. if( $value === 'null' ) {
  339. return false;
  340. }
  341. // return
  342. return $value;
  343. }
  344. /*
  345. * format_value()
  346. *
  347. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  348. *
  349. * @type filter
  350. * @since 3.6
  351. * @date 23/01/13
  352. *
  353. * @param $value (mixed) the value which was loaded from the database
  354. * @param $post_id (mixed) the $post_id from which the value was loaded
  355. * @param $field (array) the field array holding all the field options
  356. *
  357. * @return $value (mixed) the modified value
  358. */
  359. function format_value( $value, $post_id, $field ) {
  360. // bail early if no value
  361. if( empty($value) ) {
  362. return $value;
  363. }
  364. // force value to array
  365. $value = acf_get_array( $value );
  366. // convert values to int
  367. $value = array_map('intval', $value);
  368. // load users
  369. foreach( array_keys($value) as $i ) {
  370. // vars
  371. $user_id = $value[ $i ];
  372. $user_data = get_userdata( $user_id );
  373. //cope with deleted users by @adampope
  374. if( !is_object($user_data) ) {
  375. unset( $value[ $i ] );
  376. continue;
  377. }
  378. // append to array
  379. $value[ $i ] = array();
  380. $value[ $i ]['ID'] = $user_id;
  381. $value[ $i ]['user_firstname'] = $user_data->user_firstname;
  382. $value[ $i ]['user_lastname'] = $user_data->user_lastname;
  383. $value[ $i ]['nickname'] = $user_data->nickname;
  384. $value[ $i ]['user_nicename'] = $user_data->user_nicename;
  385. $value[ $i ]['display_name'] = $user_data->display_name;
  386. $value[ $i ]['user_email'] = $user_data->user_email;
  387. $value[ $i ]['user_url'] = $user_data->user_url;
  388. $value[ $i ]['user_registered'] = $user_data->user_registered;
  389. $value[ $i ]['user_description'] = $user_data->user_description;
  390. $value[ $i ]['user_avatar'] = get_avatar( $user_id );
  391. }
  392. // convert back from array if neccessary
  393. if( !$field['multiple'] ) {
  394. $value = array_shift($value);
  395. }
  396. // return value
  397. return $value;
  398. }
  399. }
  400. new acf_field_user();
  401. endif;
  402. ?>