Making WordPress.org

Changeset 12122


Ignore:
Timestamp:
10/13/2022 06:00:30 AM (2 years ago)
Author:
dd32
Message:

Profile Helpers: Allow for assign_badge() to be passed an array of user IDs.

See r12121

File:
1 edited

Legend:

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

    r11838 r12122  
    1515 *
    1616 * @param $badge string The badge group to assign.
    17  * @param $user  mixed  The user to 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.
    1818 * @return bool
    1919 */
    20 function assign_badge( string $badge, $user ) : bool {
    21     return badge_api( 'add', $badge, $user );
     20function assign_badge( string $badge, $users ) : bool {
     21    return badge_api( 'add', $badge, $users );
    2222}
    2323
     
    2626 *
    2727 * @param $badge string The badge group to assign.
    28  * @param $user  mixed  The user to 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.
    2929 * @return bool
    3030 */
    31 function remove_badge( string $badge, $user ) : bool {
    32     return badge_api( 'remove', $badge, $user );
     31function 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 */
     43function 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 ) );
    3354}
    3455
     
    3657 * Assign a badge to a given user.
    3758 *
    38  * @param $action string 'Add' or 'Remove'.
     59 * @param $action string The action to perform; 'add' or 'remove'.
    3960 * @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.
    4162 * @return bool
    4263 */
    43 function badge_api( string $action, string $badge, $user ) : bool {
    44     if ( is_object( $user ) && isset( $user->ID ) ) {
    45         $user = $user->ID;
    46     }
     64function 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        }
    4771
    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 ) {
    4990        return false;
    5091    }
     
    5495        'source'  => 'generic-badge',
    5596        'command' => $action,
    56         'user'    => $user,
     97        'users'   => $users,
    5798        'badge'   => $badge,
    5899    ] );
Note: See TracChangeset for help on using the changeset viewer.