acf-user-functions.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * acf_get_users
  4. *
  5. * Similar to the get_users() function but with extra functionality.
  6. *
  7. * @date 9/1/19
  8. * @since 5.7.10
  9. *
  10. * @param array $args The query args.
  11. * @return array
  12. */
  13. function acf_get_users( $args = array() ) {
  14. // Get users.
  15. $users = get_users( $args );
  16. // Maintain order.
  17. if ( $users && $args['include'] ) {
  18. // Generate order array.
  19. $order = array();
  20. foreach ( $users as $i => $user ) {
  21. $order[ $i ] = array_search( $user->ID, $args['include'] );
  22. }
  23. // Sort results.
  24. array_multisort( $order, $users );
  25. }
  26. // Return
  27. return $users;
  28. }
  29. /**
  30. * acf_get_user_result
  31. *
  32. * Returns a result containing "id" and "text" for the given user.
  33. *
  34. * @date 21/5/19
  35. * @since 5.8.1
  36. *
  37. * @param WP_User $user The user object.
  38. * @return array
  39. */
  40. function acf_get_user_result( $user ) {
  41. // Vars.
  42. $id = $user->ID;
  43. $text = $user->user_login;
  44. // Add name.
  45. if ( $user->first_name && $user->last_name ) {
  46. $text .= " ({$user->first_name} {$user->last_name})";
  47. } elseif ( $user->first_name ) {
  48. $text .= " ({$user->first_name})";
  49. }
  50. return compact( 'id', 'text' );
  51. }
  52. /**
  53. * acf_get_user_role_labels
  54. *
  55. * Returns an array of user roles in the format "name => label".
  56. *
  57. * @date 20/5/19
  58. * @since 5.8.1
  59. *
  60. * @param array $roles A specific array of roles.
  61. * @return array
  62. */
  63. function acf_get_user_role_labels( $roles = array() ) {
  64. $all_roles = wp_roles()->get_names();
  65. // Load all roles if none provided.
  66. if ( empty( $roles ) ) {
  67. $roles = array_keys( $all_roles );
  68. }
  69. // Loop over roles and populare labels.
  70. $lables = array();
  71. foreach ( $roles as $role ) {
  72. if ( isset( $all_roles[ $role ] ) ) {
  73. $lables[ $role ] = translate_user_role( $all_roles[ $role ] );
  74. }
  75. }
  76. // Return labels.
  77. return $lables;
  78. }
  79. /**
  80. * acf_allow_unfiltered_html
  81. *
  82. * Returns true if the current user is allowed to save unfiltered HTML.
  83. *
  84. * @date 9/1/19
  85. * @since 5.7.10
  86. *
  87. * @param void
  88. * @return bool
  89. */
  90. function acf_allow_unfiltered_html() {
  91. // Check capability.
  92. $allow_unfiltered_html = current_user_can( 'unfiltered_html' );
  93. /**
  94. * Filters whether the current user is allowed to save unfiltered HTML.
  95. *
  96. * @date 9/1/19
  97. * @since 5.7.10
  98. *
  99. * @param bool allow_unfiltered_html The result.
  100. */
  101. return apply_filters( 'acf/allow_unfiltered_html', $allow_unfiltered_html );
  102. }