Making WordPress.org


Ignore:
Timestamp:
10/17/2024 01:37:00 AM (22 months ago)
Author:
dd32
Message:

Profile Helpers: Add extra badge helpers and profile-data getters.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/mu-plugins/pub/profile-helpers.php

    r13675 r14115  
    3333
    3434/**
     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 */
     41function 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 */
     53function 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/**
    3568 * Get a list of badges for a given user.
    3669 *
     
    3871 *
    3972 * @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.
    4174 */
    4275function get_user_badges( $user ) {
     
    4982                        FROM bpmain_wporg_groups_members m
    5083                        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
    5285                        ORDER BY slug",
    5386                $user_id
     
    5588
    5689        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 */
     100function 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;
    57123}
    58124
     
    106172 * @return bool
    107173 */
    108 function badge_api( string $action, string $badge, $users ) : bool {
     174function badge_api( string $action, string $badge, $users = array() ) : bool {
    109175        $users = is_object( $users ) ? [ $users ] : (array) $users;
    110176        $users = array_filter( array_map( __NAMESPACE__ . '\find_user_id', $users ) );
     
    118184                        $users,
    119185                        function( $user ) use ( $badge ) {
    120                                 return isset( get_user_badges( $user )[ $badge ] );
     186                                return has_badge( $badge, $user );
    121187                        }
    122188                );
     
    125191                        $users,
    126192                        function( $user ) use ( $badge ) {
    127                                 return ! isset( get_user_badges( $user )[ $badge ] );
     193                                return ! has_badge( $badge, $user );
    128194                        }
    129195                );
Note: See TracChangeset for help on using the changeset viewer.