Changeset 12122
- Timestamp:
- 10/13/2022 06:00:30 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/profile-helpers.php
r11838 r12122 15 15 * 16 16 * @param $badge string The badge group to assign. 17 * @param $user mixed The userto assign.17 * @param $users mixed The user(s) to assign. A WP_User/ID/Login/Email (or array of) of the user(s) to assign. 18 18 * @return bool 19 19 */ 20 function assign_badge( string $badge, $user ) : bool {21 return badge_api( 'add', $badge, $user );20 function assign_badge( string $badge, $users ) : bool { 21 return badge_api( 'add', $badge, $users ); 22 22 } 23 23 … … 26 26 * 27 27 * @param $badge string The badge group to assign. 28 * @param $user mixed The userto assign.28 * @param $users mixed The user(s) to assign. A WP_User/ID/Login/Email (or array of) of the user(s) to assign. 29 29 * @return bool 30 30 */ 31 function remove_badge( string $badge, $user ) : bool { 32 return badge_api( 'remove', $badge, $user ); 31 function remove_badge( string $badge, $users ) : bool { 32 return badge_api( 'remove', $badge, $users ); 33 } 34 35 /** 36 * Record an activity item for a user. 37 * 38 * @param $component string The component to be used for the acitivity. 39 * @param $type string The type of the activity in that component. 40 * @param $user int|string ID, Login, or Slug of user. 41 * @param $args array The args for the activity item. See `bp_activity_add()`. 42 */ 43 function add_activity( string $component, string $type, $user, array $args ) { 44 $request = api( [ 45 'action' => 'wporg_handle_activity', 46 'source' => 'generic', 47 'component' => $component, 48 'type' => $type, 49 'user' => $user, 50 'args' => $args, 51 ] ); 52 53 return ( 200 === wp_remote_retrieve_response_code( $request ) ); 33 54 } 34 55 … … 36 57 * Assign a badge to a given user. 37 58 * 38 * @param $action string 'Add' or 'Remove'.59 * @param $action string The action to perform; 'add' or 'remove'. 39 60 * @param $badge string The badge group to assign. 40 * @param $user mixed The user to assign to.61 * @param $users mixed The user(s) to assign to. A WP_User/ID/Login/Email/Slug (or array of) of the user(s) to assign. 41 62 * @return bool 42 63 */ 43 function badge_api( string $action, string $badge, $user ) : bool { 44 if ( is_object( $user ) && isset( $user->ID ) ) { 45 $user = $user->ID; 46 } 64 function badge_api( string $action, string $badge, $users ) : bool { 65 $users = (array) $users; 66 $users = array_filter( array_map( function( $user ) { 67 // WP_User-like object. 68 if ( is_object( $user ) ) { 69 return $user->ID ?? false; 70 } 47 71 48 if ( ! $action || ! $badge || ! is_scalar( $user ) ) { 72 // User ID. 73 if ( is_numeric( $user ) && absint( $user ) == $user ) { 74 return (int) $user; 75 } 76 77 // Support user login / email / slug. 78 $_user = get_user_by( 'login', $user ); 79 if ( ! $_user && is_email( $user ) ) { 80 $_user = get_user_by( 'email', $user ); 81 } 82 if ( ! $_user ) { 83 $_user = get_user_by( 'slug', $user ); 84 } 85 86 return $_user->ID ?? false; 87 }, $users ) ); 88 89 if ( ! $action || ! $badge || ! $users ) { 49 90 return false; 50 91 } … … 54 95 'source' => 'generic-badge', 55 96 'command' => $action, 56 'user ' => $user,97 'users' => $users, 57 98 'badge' => $badge, 58 99 ] );
Note: See TracChangeset
for help on using the changeset viewer.