Changeset 14115 for sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/profile-helpers.php
- Timestamp:
- 10/17/2024 01:37:00 AM (22 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/profile-helpers.php
r13675 r14115 33 33 34 34 /** 35 * Determine if a user has a given badge 36 * 37 * @param $badge string The badge slug to check for. 38 * @param $user mixed The user to check. A WP_User/ID/Login/Email. 39 * @return bool 40 */ 41 function has_badge( string $badge, $user ) : bool { 42 return isset( get_user_badges( $user )[ $badge ] ); 43 } 44 45 /** 46 * Get a list of users with a given badge. 47 * 48 * WARNING: Uncached. Excludes dynamically allocated badges. 49 * 50 * @param $badge string The badge group we're looking for. 51 * @return array An array of user IDs. 52 */ 53 function get_users_with_badge( string $badge ) : array { 54 global $wpdb; 55 56 $users = $wpdb->get_col( $wpdb->prepare( 57 "SELECT user_id 58 FROM bpmain_wporg_groups_members m 59 JOIN bpmain_wporg_groups g ON m.group_id = g.id 60 WHERE g.slug = %s AND m.is_confirmed = 1 AND m.is_banned = 0", 61 $badge 62 ) ); 63 64 return array_map( 'intval', $users ); 65 } 66 67 /** 35 68 * Get a list of badges for a given user. 36 69 * … … 38 71 * 39 72 * @param $users mixed The user to fetch the badged for. A WP_User/ID/Login/Email. 40 * @return array 73 * @return array An array of badge names keyed by slug. 41 74 */ 42 75 function get_user_badges( $user ) { … … 49 82 FROM bpmain_wporg_groups_members m 50 83 JOIN bpmain_wporg_groups g ON m.group_id = g.id 51 WHERE m.user_id = %d 84 WHERE m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0 52 85 ORDER BY slug", 53 86 $user_id … … 55 88 56 89 return array_column( $badges, 'name', 'slug' ); 90 } 91 92 /** 93 * Fetch the profiles data for a given user. 94 * 95 * NOTE: The returned array is currently uncached and if a field is empty/blank it's not set here. 96 * 97 * @param $user mixed The user to fetch the profile for. A WP_User/ID/Login/Email. 98 * @return array|WP_Error An array of profile fields keyed by field name. 99 */ 100 function get_user_details( $user ) { 101 global $wpdb; 102 103 $user_id = find_user_id( $user ); 104 105 $data = $wpdb->get_results( $wpdb->prepare( 106 "SELECT f.name, d.value, f.type, d.last_updated 107 FROM bpmain_bp_xprofile_data d 108 JOIN bpmain_bp_xprofile_fields f ON d.field_id = f.id 109 WHERE d.user_id = %d", 110 $user_id 111 ) ); 112 113 $result = []; 114 foreach ( $data as $field ) { 115 $name = sanitize_title_with_dashes( $field->name ); 116 if ( str_contains( $field->type, 'multi' ) || $field->type === 'checkbox' ) { 117 $field->value = maybe_unserialize( $field->value ); 118 } 119 $result[ $name ] = $field; 120 } 121 122 return $result; 57 123 } 58 124 … … 106 172 * @return bool 107 173 */ 108 function badge_api( string $action, string $badge, $users ) : bool {174 function badge_api( string $action, string $badge, $users = array() ) : bool { 109 175 $users = is_object( $users ) ? [ $users ] : (array) $users; 110 176 $users = array_filter( array_map( __NAMESPACE__ . '\find_user_id', $users ) ); … … 118 184 $users, 119 185 function( $user ) use ( $badge ) { 120 return isset( get_user_badges( $user )[ $badge ]);186 return has_badge( $badge, $user ); 121 187 } 122 188 ); … … 125 191 $users, 126 192 function( $user ) use ( $badge ) { 127 return ! isset( get_user_badges( $user )[ $badge ]);193 return ! has_badge( $badge, $user ); 128 194 } 129 195 );
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)