Nessuna descrizione

class-acf-field-user.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. if ( ! class_exists( 'ACF_Field_User' ) ) :
  3. class ACF_Field_User extends ACF_Field {
  4. /**
  5. * Initializes the field type.
  6. *
  7. * @date 5/03/2014
  8. * @since 5.0.0
  9. *
  10. * @param void
  11. * @return void
  12. */
  13. function initialize() {
  14. // Props.
  15. $this->name = 'user';
  16. $this->label = __( 'User', 'acf' );
  17. $this->category = 'relational';
  18. $this->defaults = array(
  19. 'role' => '',
  20. 'multiple' => 0,
  21. 'allow_null' => 0,
  22. 'return_format' => 'array',
  23. );
  24. // Register filter variations.
  25. acf_add_filter_variations( 'acf/fields/user/query', array( 'name', 'key' ), 1 );
  26. acf_add_filter_variations( 'acf/fields/user/result', array( 'name', 'key' ), 2 );
  27. acf_add_filter_variations( 'acf/fields/user/search_columns', array( 'name', 'key' ), 3 );
  28. // Add AJAX query.
  29. add_action( 'wp_ajax_acf/fields/user/query', array( $this, 'ajax_query' ) );
  30. add_action( 'wp_ajax_nopriv_acf/fields/user/query', array( $this, 'ajax_query' ) );
  31. }
  32. /**
  33. * Renders the field settings HTML.
  34. *
  35. * @date 23/01/13
  36. * @since 3.6.0
  37. *
  38. * @param array $field The ACF field.
  39. * @return void
  40. */
  41. function render_field_settings( $field ) {
  42. acf_render_field_setting(
  43. $field,
  44. array(
  45. 'label' => __( 'Filter by role', 'acf' ),
  46. 'instructions' => '',
  47. 'type' => 'select',
  48. 'name' => 'role',
  49. 'choices' => acf_get_user_role_labels(),
  50. 'multiple' => 1,
  51. 'ui' => 1,
  52. 'allow_null' => 1,
  53. 'placeholder' => __( 'All user roles', 'acf' ),
  54. )
  55. );
  56. acf_render_field_setting(
  57. $field,
  58. array(
  59. 'label' => __( 'Allow Null?', 'acf' ),
  60. 'instructions' => '',
  61. 'name' => 'allow_null',
  62. 'type' => 'true_false',
  63. 'ui' => 1,
  64. )
  65. );
  66. acf_render_field_setting(
  67. $field,
  68. array(
  69. 'label' => __( 'Select multiple values?', 'acf' ),
  70. 'instructions' => '',
  71. 'name' => 'multiple',
  72. 'type' => 'true_false',
  73. 'ui' => 1,
  74. )
  75. );
  76. acf_render_field_setting(
  77. $field,
  78. array(
  79. 'label' => __( 'Return Format', 'acf' ),
  80. 'instructions' => '',
  81. 'type' => 'radio',
  82. 'name' => 'return_format',
  83. 'choices' => array(
  84. 'array' => __( 'User Array', 'acf' ),
  85. 'object' => __( 'User Object', 'acf' ),
  86. 'id' => __( 'User ID', 'acf' ),
  87. ),
  88. 'layout' => 'horizontal',
  89. )
  90. );
  91. }
  92. /**
  93. * Renders the field input HTML.
  94. *
  95. * @date 23/01/13
  96. * @since 3.6.0
  97. *
  98. * @param array $field The ACF field.
  99. * @return void
  100. */
  101. function render_field( $field ) {
  102. // Change Field into a select.
  103. $field['type'] = 'select';
  104. $field['ui'] = 1;
  105. $field['ajax'] = 1;
  106. $field['choices'] = array();
  107. $field['query_nonce'] = wp_create_nonce( 'acf/fields/user/query' . $field['key'] );
  108. // Populate choices.
  109. if ( $field['value'] ) {
  110. // Clean value into an array of IDs.
  111. $user_ids = array_map( 'intval', acf_array( $field['value'] ) );
  112. // Find users in database (ensures all results are real).
  113. $users = acf_get_users(
  114. array(
  115. 'include' => $user_ids,
  116. )
  117. );
  118. // Append.
  119. if ( $users ) {
  120. foreach ( $users as $user ) {
  121. $field['choices'][ $user->ID ] = $this->get_result( $user, $field );
  122. }
  123. }
  124. }
  125. // Render.
  126. acf_render_field( $field );
  127. }
  128. /**
  129. * Returns the result text for a fiven WP_User object.
  130. *
  131. * @date 1/11/2013
  132. * @since 5.0.0
  133. *
  134. * @param WP_User $user The WP_User object.
  135. * @param array $field The ACF field related to this query.
  136. * @param (int|string) $post_id The post_id being edited.
  137. * @return string
  138. */
  139. function get_result( $user, $field, $post_id = 0 ) {
  140. // Get user result item.
  141. $item = acf_get_user_result( $user );
  142. // Default $post_id to current post being edited.
  143. $post_id = $post_id ? $post_id : acf_get_form_data( 'post_id' );
  144. /**
  145. * Filters the result text.
  146. *
  147. * @date 21/5/19
  148. * @since 5.8.1
  149. *
  150. * @param array $args The query args.
  151. * @param array $field The ACF field related to this query.
  152. * @param (int|string) $post_id The post_id being edited.
  153. */
  154. return apply_filters( 'acf/fields/user/result', $item['text'], $user, $field, $post_id );
  155. }
  156. /**
  157. * Filters the field value after it is loaded from the database.
  158. *
  159. * @date 23/01/13
  160. * @since 3.6.0
  161. *
  162. * @param mixed $value The field value.
  163. * @param mixed $post_id The post ID where the value is saved.
  164. * @param array $field The field array containing all settings.
  165. * @return mixed
  166. */
  167. function load_value( $value, $post_id, $field ) {
  168. // Add compatibility for version 4.
  169. if ( $value === 'null' ) {
  170. return false;
  171. }
  172. return $value;
  173. }
  174. /**
  175. * Filters the field value after it is loaded from the database but before it is returned to the front-end API.
  176. *
  177. * @date 23/01/13
  178. * @since 3.6.0
  179. *
  180. * @param mixed $value The field value.
  181. * @param mixed $post_id The post ID where the value is saved.
  182. * @param array $field The field array containing all settings.
  183. * @return mixed
  184. */
  185. function format_value( $value, $post_id, $field ) {
  186. // Bail early if no value.
  187. if ( ! $value ) {
  188. return false;
  189. }
  190. // Clean value into an array of IDs.
  191. $user_ids = array_map( 'intval', acf_array( $value ) );
  192. // Find users in database (ensures all results are real).
  193. $users = acf_get_users(
  194. array(
  195. 'include' => $user_ids,
  196. )
  197. );
  198. // Bail early if no users found.
  199. if ( ! $users ) {
  200. return false;
  201. }
  202. // Format values using field settings.
  203. $value = array();
  204. foreach ( $users as $user ) {
  205. // Return object.
  206. if ( $field['return_format'] == 'object' ) {
  207. $item = $user;
  208. // Return array.
  209. } elseif ( $field['return_format'] == 'array' ) {
  210. $item = array(
  211. 'ID' => $user->ID,
  212. 'user_firstname' => $user->user_firstname,
  213. 'user_lastname' => $user->user_lastname,
  214. 'nickname' => $user->nickname,
  215. 'user_nicename' => $user->user_nicename,
  216. 'display_name' => $user->display_name,
  217. 'user_email' => $user->user_email,
  218. 'user_url' => $user->user_url,
  219. 'user_registered' => $user->user_registered,
  220. 'user_description' => $user->user_description,
  221. 'user_avatar' => get_avatar( $user->ID ),
  222. );
  223. // Return ID.
  224. } else {
  225. $item = $user->ID;
  226. }
  227. // Append item
  228. $value[] = $item;
  229. }
  230. // Convert to single.
  231. if ( ! $field['multiple'] ) {
  232. $value = array_shift( $value );
  233. }
  234. // Return.
  235. return $value;
  236. }
  237. /**
  238. * Filters the field value before it is saved into the database.
  239. *
  240. * @date 23/01/13
  241. * @since 3.6.0
  242. *
  243. * @param mixed $value The field value.
  244. * @param mixed $post_id The post ID where the value is saved.
  245. * @param array $field The field array containing all settings.
  246. * @return mixed
  247. */
  248. function update_value( $value, $post_id, $field ) {
  249. // Bail early if no value.
  250. if ( empty( $value ) ) {
  251. return $value;
  252. }
  253. // Format array of values.
  254. // - ensure each value is an id.
  255. // - Parse each id as string for SQL LIKE queries.
  256. if ( acf_is_sequential_array( $value ) ) {
  257. $value = array_map( 'acf_idval', $value );
  258. $value = array_map( 'strval', $value );
  259. // Parse single value for id.
  260. } else {
  261. $value = acf_idval( $value );
  262. }
  263. // Return value.
  264. return $value;
  265. }
  266. /**
  267. * Callback for the AJAX query request.
  268. *
  269. * @date 24/10/13
  270. * @since 5.0.0
  271. *
  272. * @param void
  273. * @return void
  274. */
  275. function ajax_query() {
  276. // Modify Request args.
  277. if ( isset( $_REQUEST['s'] ) ) {
  278. $_REQUEST['search'] = $_REQUEST['s'];
  279. }
  280. if ( isset( $_REQUEST['paged'] ) ) {
  281. $_REQUEST['page'] = $_REQUEST['paged'];
  282. }
  283. // Add query hooks.
  284. add_action( 'acf/ajax/query_users/init', array( $this, 'ajax_query_init' ), 10, 2 );
  285. add_filter( 'acf/ajax/query_users/args', array( $this, 'ajax_query_args' ), 10, 3 );
  286. add_filter( 'acf/ajax/query_users/result', array( $this, 'ajax_query_result' ), 10, 3 );
  287. add_filter( 'acf/ajax/query_users/search_columns', array( $this, 'ajax_query_search_columns' ), 10, 4 );
  288. // Simulate AJAX request.
  289. acf_get_instance( 'ACF_Ajax_Query_Users' )->request();
  290. }
  291. /**
  292. * Runs during the AJAX query initialization.
  293. *
  294. * @date 9/3/20
  295. * @since 5.8.8
  296. *
  297. * @param array $request The query request.
  298. * @param ACF_Ajax_Query $query The query object.
  299. * @return void
  300. */
  301. function ajax_query_init( $request, $query ) {
  302. // Require field and make sure it's a user field.
  303. if ( ! $query->field || $query->field['type'] !== $this->name ) {
  304. $query->send( new WP_Error( 'acf_missing_field', __( 'Error loading field.', 'acf' ), array( 'status' => 404 ) ) );
  305. }
  306. // Verify that this is a legitimate request using a separate nonce from the main AJAX nonce.
  307. if ( ! isset( $_REQUEST['user_query_nonce'] ) || ! wp_verify_nonce( $_REQUEST['user_query_nonce'], 'acf/fields/user/query' . $query->field['key']) ) {
  308. $query->send( new WP_Error( 'acf_invalid_request', __( 'Invalid request.', 'acf' ), array( 'status' => 404 ) ) );
  309. }
  310. }
  311. /**
  312. * Filters the AJAX query args.
  313. *
  314. * @date 9/3/20
  315. * @since 5.8.8
  316. *
  317. * @param array $args The query args.
  318. * @param array $request The query request.
  319. * @param ACF_Ajax_Query $query The query object.
  320. * @return array
  321. */
  322. function ajax_query_args( $args, $request, $query ) {
  323. // Add specific roles.
  324. if ( $query->field['role'] ) {
  325. $args['role__in'] = acf_array( $query->field['role'] );
  326. }
  327. /**
  328. * Filters the query args.
  329. *
  330. * @date 21/5/19
  331. * @since 5.8.1
  332. *
  333. * @param array $args The query args.
  334. * @param array $field The ACF field related to this query.
  335. * @param (int|string) $post_id The post_id being edited.
  336. */
  337. return apply_filters( 'acf/fields/user/query', $args, $query->field, $query->post_id );
  338. }
  339. /**
  340. * Filters the WP_User_Query search columns.
  341. *
  342. * @date 9/3/20
  343. * @since 5.8.8
  344. *
  345. * @param array $columns An array of column names to be searched.
  346. * @param string $search The search term.
  347. * @param WP_User_Query $WP_User_Query The WP_User_Query instance.
  348. * @return array
  349. */
  350. function ajax_query_search_columns( $columns, $search, $WP_User_Query, $query ) {
  351. /**
  352. * Filters the column names to be searched.
  353. *
  354. * @date 21/5/19
  355. * @since 5.8.1
  356. *
  357. * @param array $columns An array of column names to be searched.
  358. * @param string $search The search term.
  359. * @param WP_User_Query $WP_User_Query The WP_User_Query instance.
  360. * @param array $field The ACF field related to this query.
  361. */
  362. return apply_filters( 'acf/fields/user/search_columns', $columns, $search, $WP_User_Query, $query->field );
  363. }
  364. /**
  365. * Filters the AJAX Query result.
  366. *
  367. * @date 9/3/20
  368. * @since 5.8.8
  369. *
  370. * @param array $item The choice id and text.
  371. * @param WP_User $user The user object.
  372. * @param ACF_Ajax_Query $query The query object.
  373. * @return array
  374. */
  375. function ajax_query_result( $item, $user, $query ) {
  376. /**
  377. * Filters the result text.
  378. *
  379. * @date 21/5/19
  380. * @since 5.8.1
  381. *
  382. * @param string The result text.
  383. * @param WP_User $user The user object.
  384. * @param array $field The ACF field related to this query.
  385. * @param (int|string) $post_id The post_id being edited.
  386. */
  387. $item['text'] = apply_filters( 'acf/fields/user/result', $item['text'], $user, $query->field, $query->post_id );
  388. return $item;
  389. }
  390. /**
  391. * Return an array of data formatted for use in a select2 AJAX response.
  392. *
  393. * @date 15/10/2014
  394. * @since 5.0.9
  395. * @deprecated 5.8.9
  396. *
  397. * @param array $args An array of query args.
  398. * @return array
  399. */
  400. function get_ajax_query( $options = array() ) {
  401. _deprecated_function( __FUNCTION__, '5.8.9' );
  402. return array();
  403. }
  404. /**
  405. * Filters the WP_User_Query search columns.
  406. *
  407. * @date 15/10/2014
  408. * @since 5.0.9
  409. * @deprecated 5.8.9
  410. *
  411. * @param array $columns An array of column names to be searched.
  412. * @param string $search The search term.
  413. * @param WP_User_Query $WP_User_Query The WP_User_Query instance.
  414. * @return array
  415. */
  416. function user_search_columns( $columns, $search, $WP_User_Query ) {
  417. _deprecated_function( __FUNCTION__, '5.8.9' );
  418. return $columns;
  419. }
  420. /**
  421. * Validates user fields updated via the REST API.
  422. *
  423. * @param bool $valid
  424. * @param int $value
  425. * @param array $field
  426. *
  427. * @return bool|WP_Error
  428. */
  429. public function validate_rest_value( $valid, $value, $field ) {
  430. if ( is_null( $value ) ) {
  431. return $valid;
  432. }
  433. $param = sprintf( '%s[%s]', $field['prefix'], $field['name'] );
  434. $data = array( 'param' => $param );
  435. $value = is_array( $value ) ? $value : array( $value );
  436. $invalid_users = array();
  437. $insufficient_roles = array();
  438. foreach ( $value as $user_id ) {
  439. $user_data = get_userdata( $user_id );
  440. if ( ! $user_data ) {
  441. $invalid_users[] = $user_id;
  442. continue;
  443. }
  444. if ( empty( $field['role'] ) ) {
  445. continue;
  446. }
  447. $has_roles = count( array_intersect( $field['role'], $user_data->roles ) );
  448. if ( ! $has_roles ) {
  449. $insufficient_roles[] = $user_id;
  450. }
  451. }
  452. if ( count( $invalid_users ) ) {
  453. $error = sprintf(
  454. __( '%1$s must have a valid user ID.', 'acf' ),
  455. $param
  456. );
  457. $data['value'] = $invalid_users;
  458. return new WP_Error( 'rest_invalid_param', $error, $data );
  459. }
  460. if ( count( $insufficient_roles ) ) {
  461. $error = sprintf(
  462. _n(
  463. '%1$s must have a user with the %2$s role.',
  464. '%1$s must have a user with one of the following roles: %2$s',
  465. count( $field['role'] ),
  466. 'acf'
  467. ),
  468. $param,
  469. count( $field['role'] ) > 1 ? implode( ', ', $field['role'] ) : $field['role'][0]
  470. );
  471. $data['value'] = $insufficient_roles;
  472. return new WP_Error( 'rest_invalid_param', $error, $data );
  473. }
  474. return $valid;
  475. }
  476. /**
  477. * Return the schema array for the REST API.
  478. *
  479. * @param array $field
  480. * @return array
  481. */
  482. public function get_rest_schema( array $field ) {
  483. $schema = array(
  484. 'type' => array( 'integer', 'array', 'null' ),
  485. 'required' => ! empty( $field['required'] ),
  486. 'items' => array(
  487. 'type' => 'integer',
  488. ),
  489. );
  490. if ( empty( $field['allow_null'] ) ) {
  491. $schema['minItems'] = 1;
  492. }
  493. if ( empty( $field['multiple'] ) ) {
  494. $schema['maxItems'] = 1;
  495. }
  496. return $schema;
  497. }
  498. /**
  499. * @see \acf_field::get_rest_links()
  500. * @param mixed $value The raw (unformatted) field value.
  501. * @param int|string $post_id
  502. * @param array $field
  503. * @return array
  504. */
  505. public function get_rest_links( $value, $post_id, array $field ) {
  506. $links = array();
  507. if ( empty( $value ) ) {
  508. return $links;
  509. }
  510. foreach ( (array) $value as $object_id ) {
  511. $links[] = array(
  512. 'rel' => 'acf:user',
  513. 'href' => rest_url( '/wp/v2/users/' . $object_id ),
  514. 'embeddable' => true,
  515. );
  516. }
  517. return $links;
  518. }
  519. /**
  520. * Apply basic formatting to prepare the value for default REST output.
  521. *
  522. * @param mixed $value
  523. * @param string|int $post_id
  524. * @param array $field
  525. * @return mixed
  526. */
  527. public function format_value_for_rest( $value, $post_id, array $field ) {
  528. return acf_format_numerics( $value );
  529. }
  530. }
  531. // initialize
  532. acf_register_field_type( 'ACF_Field_User' );
  533. endif; // class_exists check