PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/sihabudinahmad/asppi
PHP | 556 lines | 201 code | 181 blank | 174 comment | 26 complexity | 7328a4ba9a0b7c76ef5f3a1336dbc6ba 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. ));
  61. // vars
  62. $r = array();
  63. $args = array();
  64. // load field
  65. $field = acf_get_field( $options['field_key'] );
  66. if( !$field ) {
  67. return false;
  68. }
  69. // editable roles
  70. $editable_roles = get_editable_roles();
  71. if( !empty($field['role']) ) {
  72. foreach( $editable_roles as $role => $role_info ) {
  73. if( !in_array($role, $field['role']) ) {
  74. unset( $editable_roles[ $role ] );
  75. }
  76. }
  77. }
  78. // search
  79. if( $options['s'] ) {
  80. // append to $args
  81. $args['search'] = '*' . $options['s'] . '*';
  82. // add filter to modify search colums
  83. add_filter('user_search_columns', array($this, 'user_search_columns'), 10, 1);
  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. $users = get_users( $args );
  91. if( !empty($users) && !empty($editable_roles) ) {
  92. foreach( $editable_roles as $role => $role_info ) {
  93. // vars
  94. $this_users = array();
  95. $this_json = array();
  96. // loop over users
  97. foreach( array_keys($users) as $key ) {
  98. if( in_array($role, $users[ $key ]->roles) ) {
  99. // extract user
  100. $user = acf_extract_var( $users, $key );
  101. // append to $this_users
  102. $this_users[ $user->ID ] = ucfirst( $user->display_name ) . ' (' . $user->user_login . ')';
  103. }
  104. }
  105. // bail early if no users for this role
  106. if( empty($this_users) ) {
  107. continue;
  108. }
  109. // order by search
  110. if( !empty($args['s']) ) {
  111. $this_users = acf_order_by_search( $this_users, $args['s'] );
  112. }
  113. // append to json
  114. foreach( array_keys($this_users) as $user_id ) {
  115. // add to json
  116. $this_json[] = array(
  117. 'id' => $user_id,
  118. 'text' => $this_users[ $user_id ]
  119. );
  120. }
  121. // add as optgroup or results
  122. if( count($editable_roles) == 1 ) {
  123. $r = $this_json;
  124. } else {
  125. $r[] = array(
  126. 'text' => translate_user_role( $role_info['name'] ),
  127. 'children' => $this_json
  128. );
  129. }
  130. }
  131. }
  132. // return
  133. return $r;
  134. }
  135. /*
  136. * ajax_query
  137. *
  138. * description
  139. *
  140. * @type function
  141. * @date 24/10/13
  142. * @since 5.0.0
  143. *
  144. * @param $post_id (int)
  145. * @return $post_id (int)
  146. */
  147. function ajax_query() {
  148. // validate
  149. if( empty($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'acf_nonce') ) {
  150. die();
  151. }
  152. // get choices
  153. $choices = $this->get_choices( $_POST );
  154. // validate
  155. if( !$choices ) {
  156. die();
  157. }
  158. // return JSON
  159. echo json_encode( $choices );
  160. die();
  161. }
  162. /*
  163. * user_search_columns
  164. *
  165. * This function will modify the columns which the user AJAX search looks in
  166. *
  167. * @type function
  168. * @date 17/06/2014
  169. * @since 5.0.0
  170. *
  171. * @param $columns (array)
  172. * @return $columns
  173. */
  174. function user_search_columns( $columns ) {
  175. return array('user_login', 'display_name');
  176. }
  177. /*
  178. * render_field()
  179. *
  180. * Create the HTML interface for your field
  181. *
  182. * @type action
  183. * @since 3.6
  184. * @date 23/01/13
  185. *
  186. * @param $field - an array holding all the field's data
  187. */
  188. function render_field( $field ) {
  189. // Change Field into a select
  190. $field['type'] = 'select';
  191. $field['ui'] = 1;
  192. $field['ajax'] = 1;
  193. $field['choices'] = array();
  194. // populate choices
  195. if( !empty($field['value']) ) {
  196. // force value to array
  197. $field['value'] = acf_force_type_array( $field['value'] );
  198. // convert values to int
  199. $field['value'] = array_map('intval', $field['value']);
  200. $users = get_users(array(
  201. 'include' => $field['value']
  202. ));
  203. if( !empty($users) ) {
  204. foreach( $users as $user ) {
  205. $field['choices'][ $user->ID ] = ucfirst( $user->display_name );
  206. }
  207. }
  208. }
  209. // render
  210. acf_render_field( $field );
  211. }
  212. /*
  213. * render_field_settings()
  214. *
  215. * Create extra options for your field. This is rendered when editing a field.
  216. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  217. *
  218. * @type action
  219. * @since 3.6
  220. * @date 23/01/13
  221. *
  222. * @param $field - an array holding all the field's data
  223. */
  224. function render_field_settings( $field ) {
  225. // role
  226. $choices = array();
  227. $editable_roles = get_editable_roles();
  228. foreach( $editable_roles as $role => $details ) {
  229. // only translate the output not the value
  230. $choices[ $role ] = translate_user_role( $details['name'] );
  231. }
  232. acf_render_field_setting( $field, array(
  233. 'label' => __('Filter by role','acf'),
  234. 'instructions' => '',
  235. 'type' => 'select',
  236. 'name' => 'role',
  237. 'choices' => $choices,
  238. 'multiple' => 1,
  239. 'ui' => 1,
  240. 'allow_null' => 1,
  241. 'placeholder' => __("All user roles",'acf'),
  242. ));
  243. // allow_null
  244. acf_render_field_setting( $field, array(
  245. 'label' => __('Allow Null?','acf'),
  246. 'instructions' => '',
  247. 'type' => 'radio',
  248. 'name' => 'allow_null',
  249. 'choices' => array(
  250. 1 => __("Yes",'acf'),
  251. 0 => __("No",'acf'),
  252. ),
  253. 'layout' => 'horizontal',
  254. ));
  255. // multiple
  256. acf_render_field_setting( $field, array(
  257. 'label' => __('Select multiple values?','acf'),
  258. 'instructions' => '',
  259. 'type' => 'radio',
  260. 'name' => 'multiple',
  261. 'choices' => array(
  262. 1 => __("Yes",'acf'),
  263. 0 => __("No",'acf'),
  264. ),
  265. 'layout' => 'horizontal',
  266. ));
  267. }
  268. /*
  269. * update_value()
  270. *
  271. * This filter is appied to the $value before it is updated in the db
  272. *
  273. * @type filter
  274. * @since 3.6
  275. * @date 23/01/13
  276. *
  277. * @param $value - the value which will be saved in the database
  278. * @param $post_id - the $post_id of which the value will be saved
  279. * @param $field - the field array holding all the field options
  280. *
  281. * @return $value - the modified value
  282. */
  283. function update_value( $value, $post_id, $field ) {
  284. // array?
  285. if( is_array($value) && isset($value['ID']) ) {
  286. $value = $value['ID'];
  287. }
  288. // object?
  289. if( is_object($value) && isset($value->ID) ) {
  290. $value = $value->ID;
  291. }
  292. // return
  293. return $value;
  294. }
  295. /*
  296. * load_value()
  297. *
  298. * This filter is applied to the $value after it is loaded from the db
  299. *
  300. * @type filter
  301. * @since 3.6
  302. * @date 23/01/13
  303. *
  304. * @param $value (mixed) the value found in the database
  305. * @param $post_id (mixed) the $post_id from which the value was loaded
  306. * @param $field (array) the field array holding all the field options
  307. * @return $value
  308. */
  309. function load_value( $value, $post_id, $field ) {
  310. // ACF4 null
  311. if( $value === 'null' ) {
  312. return false;
  313. }
  314. // return
  315. return $value;
  316. }
  317. /*
  318. * format_value()
  319. *
  320. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  321. *
  322. * @type filter
  323. * @since 3.6
  324. * @date 23/01/13
  325. *
  326. * @param $value (mixed) the value which was loaded from the database
  327. * @param $post_id (mixed) the $post_id from which the value was loaded
  328. * @param $field (array) the field array holding all the field options
  329. *
  330. * @return $value (mixed) the modified value
  331. */
  332. function format_value( $value, $post_id, $field ) {
  333. // bail early if no value
  334. if( empty($value) ) {
  335. return $value;
  336. }
  337. // force value to array
  338. $value = acf_force_type_array( $value );
  339. // convert values to int
  340. $value = array_map('intval', $value);
  341. // load users
  342. foreach( array_keys($value) as $i ) {
  343. // vars
  344. $user_id = $value[ $i ];
  345. $user_data = get_userdata( $user_id );
  346. //cope with deleted users by @adampope
  347. if( !is_object($user_data) ) {
  348. unset( $value[ $i ] );
  349. continue;
  350. }
  351. // append to array
  352. $value[ $i ] = array();
  353. $value[ $i ]['ID'] = $user_id;
  354. $value[ $i ]['user_firstname'] = $user_data->user_firstname;
  355. $value[ $i ]['user_lastname'] = $user_data->user_lastname;
  356. $value[ $i ]['nickname'] = $user_data->nickname;
  357. $value[ $i ]['user_nicename'] = $user_data->user_nicename;
  358. $value[ $i ]['display_name'] = $user_data->display_name;
  359. $value[ $i ]['user_email'] = $user_data->user_email;
  360. $value[ $i ]['user_url'] = $user_data->user_url;
  361. $value[ $i ]['user_registered'] = $user_data->user_registered;
  362. $value[ $i ]['user_description'] = $user_data->user_description;
  363. $value[ $i ]['user_avatar'] = get_avatar( $user_id );
  364. }
  365. // convert back from array if neccessary
  366. if( !$field['multiple'] ) {
  367. $value = array_shift($value);
  368. }
  369. // return value
  370. return $value;
  371. }
  372. }
  373. new acf_field_user();
  374. endif;
  375. ?>