Making WordPress.org


Ignore:
Timestamp:
04/26/2022 05:15:59 AM (4 years ago)
Author:
dd32
Message:

Profiles: Add a generic function available to WordPress.org to assign/remove a badge from a user profile.

See r11791, ​https://github.com/WordPress/pattern-directory/issues/472

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/profiles.wordpress.org/public_html/wp-content/plugins/wporg-profiles-association-handler/wporg-profiles-association-handler.php

    r7683 r11792  
    161161                                case 'polyglots':
    162162                                        $association_id = $this->handle_polyglots_association();
     163                                        break;
     164                                case 'generic-badge':
     165                                        $association_id = $this->handle_badge_association();
    163166                                        break;
    164167                                default:
    … …  
    294297                }
    295298
     299                /**
     300                 * Handles incoming associations for the generic '{assign|remove}_badge()' functions. See pub/profile-helpers.php.
     301                 *
     302                 * Payload:  (beyond 'action' and 'source')
     303                 *  user:    User ID/email/login
     304                 *  badge:   Slug for group/association
     305                 *  command: Either 'add' or 'remove'
     306                 */
     307                private function handle_badge_association() {
     308                        $user = wp_unslash( $_POST['user'] ?? '' );
     309
     310                        // Handle login/email as input..
     311                        if ( ! is_numeric( $user ) ) {
     312                                $_user = get_user_by( 'login', $user );
     313                                if ( ! $_user && is_email( $user ) ) {
     314                                        $_user = get_user_by( 'email', $user );
     315                                }
     316
     317                                $user = $_user->ID ?? $user;
     318                        }
     319
     320                        $user = get_user_by( 'id', $user );
     321                        if ( ! $user ) {
     322                                status_header( 400 );
     323                                return '-1 Association requested for unrecognized user: ' . sanitize_text_field( $_POST['user'] );
     324                        }
     325
     326                        $badge    = sanitize_key( $_POST['badge'] ?? '' );
     327                        $group_id = BP_Groups_Group::group_exists( $badge );
     328                        if ( ! $badge || ! $group_id ) {
     329                                status_header( 400 );
     330                                return '-1 Association does not exist: ' . $badge;
     331                        }
     332
     333                        if ( 'add' == $_POST['command'] ) {
     334                                groups_join_group( $group_id, $user->ID );
     335                                groups_accept_invite( $user->ID, $group_id );
     336                        } elseif ( 'remove' == $_POST['command'] ) {
     337                                groups_leave_group( $group_id, $user->ID );
     338                        } else {
     339                                status_header( 400 );
     340                                return '-1 Unknown association command';
     341                        }
     342
     343                        // NOTE: Actual response from `groups_join_group()` & `groups_leave_group()` set in cookies.
     344                        return 1;
     345                }
     346
    296347        } /* /class WPOrg_Profiles_Association_Handler */
    297348} /* if class_exists */
Note: See TracChangeset for help on using the changeset viewer.