Changeset 12121
- Timestamp:
- 10/13/2022 05:57:18 AM (2 years ago)
- 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
r11792 r12121 6 6 License: GPL2 7 7 Version: 1.0 8 Description: Handles the associations sent from other services in the .org ecosystem (WP, WordCamp, WP,etc).8 Description: Handles the associations sent from other services in the .org ecosystem (WP, WordCamp, etc). 9 9 */ 10 10 … … 31 31 add_action( 'wp_ajax_nopriv_wporg_handle_association', array( $this, 'handle_association' ) ); 32 32 33 // Approve users added to groups34 // add_action( 'groups_join_group', array( $this, 'auto_accept_group_invite_for_user' ), 10, 2 );35 36 33 // Disable activity reporting related to groups 37 34 add_filter( 'bp_activity_component_before_save', array( $this, 'disable_group_activity_reporting' ) ); … … 99 96 100 97 return $component; 101 }102 103 /**104 * Accept all invitations to a group on behalf of the user.105 *106 * @param int $group_id Group ID107 * @param int $user_id User ID108 */109 public function auto_accept_group_invite_for_user( $group_id, $user_id ) {110 //TODO111 98 } 112 99 … … 154 141 switch ( $source ) { 155 142 case 'wordcamp': 156 $association_id = $this->handle_wordcamp_association(); 157 break; 158 case 'meetups' : 159 $association_id = $this->handle_meetup_association(); 160 break; 143 case 'meetups': 161 144 case 'polyglots': 162 $association_id = $this->handle_polyglots_association(); 163 break; 145 // These sources may send the field as `user_id`/`association`, so handle that format if not specified otherwise. 146 $_POST['users'] ??= (array) $_POST['user_id']; 147 $_POST['badge'] ??= $_POST['association']; 148 149 // Fall through, the generic-badge uses `users`/`badge` for the fields, as set above. 164 150 case 'generic-badge': 165 151 $association_id = $this->handle_badge_association(); … … 179 165 180 166 /** 181 * Handles incoming associations for WordCamp.182 *183 * Payload: (beyond 'action' and 'source')184 * user_id: User ID185 * association: Slug for group/association186 * command: Either 'add' or 'remove'187 */188 private function handle_wordcamp_association() {189 $user = get_user_by( 'id', $_POST['user_id'] );190 191 if ( ! $user ) {192 return '-1 Association reported for unrecognized user: ' . sanitize_text_field( $_POST['user_id'] );193 }194 195 $association = sanitize_key( $_POST['association'] );196 197 $associated_associations = array( 'wordcamp-organizer', 'wordcamp-speaker' );198 199 if ( ! in_array( $association, $associated_associations ) ) {200 return '-1 Unrecognized association type';201 }202 203 if ( ! $group_id = BP_Groups_Group::group_exists( $association ) ) {204 return '-1 Association does not exist: ' . $association;205 }206 207 if ( 'add' == $_POST['command'] ) {208 groups_join_group( $group_id, $user->ID );209 groups_accept_invite( $user->ID, $group_id );210 } elseif ( 'remove' == $_POST['command'] ) {211 groups_leave_group( $group_id, $user->ID );212 } else {213 return '-1 Unknown association command';214 }215 216 return 1;217 }218 219 /**220 * Handles incoming associations for Meetup.221 *222 * Payload: (beyond 'action' and 'source')223 * users: List of users login224 * association: Group slug225 * @return int|string226 */227 private function handle_meetup_association() {228 229 if ( ( ! isset( $_POST['users'] ) || ( ! is_array( $_POST['users'] ) ) ) ) {230 return '-1 Users does not exist: ';231 }232 233 $association = sanitize_key( $_POST['association'] );234 $associated_associations = array( 'meetup-organizer' );235 236 if ( ! in_array( $association, $associated_associations ) ) {237 return '-1 Unrecognized association type';238 }239 240 if ( ! $group_id = BP_Groups_Group::group_exists( $association ) ) {241 return '-1 Association does not exist: ' . $association;242 }243 244 foreach ( $_POST['users'] as $user ) {245 $user = get_user_by( 'login', $user );246 if ( 'add' == $_POST['command'] ) {247 groups_join_group( $group_id, $user->ID );248 groups_accept_invite( $user->ID, $group_id );249 } elseif ( 'remove' == $_POST['command'] ) {250 groups_leave_group( $group_id, $user->ID );251 } else {252 return '-1 Unknown association command';253 }254 }255 256 return 1;257 258 }259 260 /**261 * Handles incoming associations for Polyglots/translate.wordpress.org.262 *263 * Payload: (beyond 'action' and 'source')264 * user_id: User ID265 * association: Slug for group/association266 * command: Either 'add' or 'remove'267 */268 private function handle_polyglots_association() {269 $user = get_user_by( 'id', $_POST['user_id'] );270 271 if ( ! $user ) {272 return '-1 Association reported for unrecognized user: ' . sanitize_text_field( $_POST['user_id'] );273 }274 275 $association = sanitize_key( $_POST['association'] );276 277 $associated_associations = array( 'translation-editor', 'translation-contributor' );278 279 if ( ! in_array( $association, $associated_associations ) ) {280 return '-1 Unrecognized association type';281 }282 283 if ( ! $group_id = BP_Groups_Group::group_exists( $association ) ) {284 return '-1 Association does not exist: ' . $association;285 }286 287 if ( 'add' == $_POST['command'] ) {288 groups_join_group( $group_id, $user->ID );289 groups_accept_invite( $user->ID, $group_id );290 } elseif ( 'remove' == $_POST['command'] ) {291 groups_leave_group( $group_id, $user->ID );292 } else {293 return '-1 Unknown association command';294 }295 296 return 1;297 }298 299 /**300 167 * Handles incoming associations for the generic '{assign|remove}_badge()' functions. See pub/profile-helpers.php. 301 168 * 302 169 * Payload: (beyond 'action' and 'source') 303 * user : User ID/email/login304 * badge: Slug for group/association 170 * users: User ID(s)/login(s)/nicename(s). 171 * badge: Slug for group/association. 305 172 * command: Either 'add' or 'remove' 306 173 */ 307 174 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 175 $users = wp_unslash( $_POST['users'] ?? [] ); 176 $command = $_POST['command'] ?? ''; 326 177 $badge = sanitize_key( $_POST['badge'] ?? '' ); 327 178 $group_id = BP_Groups_Group::group_exists( $badge ); 179 328 180 if ( ! $badge || ! $group_id ) { 329 181 status_header( 400 ); … … 331 183 } 332 184 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 { 185 if ( 'add' !== $command && 'remove' !== $command ) { 339 186 status_header( 400 ); 340 187 return '-1 Unknown association command'; 341 188 } 342 189 343 // NOTE: Actual response from `groups_join_group()` & `groups_leave_group()` set in cookies. 190 if ( empty( $users ) ) { 191 status_header( 400 ); 192 return '-1 User(s) not specified'; 193 } 194 195 // Validate all users. 196 foreach ( $users as $i => $user ) { 197 if ( is_numeric( $user ) && ( absint( $user ) == $user ) ) { 198 $_user = get_user_by( 'id', $user ); 199 } else { 200 $_user = get_user_by( 'login', $user ); 201 if ( ! $_user ) { 202 $_user = get_user_by( 'slug', $user ); 203 } 204 } 205 206 if ( ! $_user ) { 207 status_header( 400 ); 208 return '-1 Association requested for unrecognized user: ' . sanitize_text_field( $user ); 209 } 210 211 $users[ $i ] = $_user->ID; 212 } 213 214 // Defer group re-counts. 215 if ( function_exists( 'bp_groups_defer_group_members_count' ) ) { 216 bp_groups_defer_group_members_count( true ); 217 } 218 219 foreach ( $users as $user_id ) { 220 if ( 'add' == $command ) { 221 if ( ! groups_is_user_member( $user_id, $group_id ) ) { 222 groups_join_group( $group_id, $user_id ); 223 } 224 } elseif ( 'remove' == $command ) { 225 if ( groups_is_user_member( $group_id, $user_id ) ) { 226 groups_leave_group( $group_id, $user_id ); 227 } 228 } 229 } 230 231 // Perform the group recounts. 232 if ( function_exists( 'bp_groups_defer_group_members_count' ) ) { 233 bp_groups_defer_group_members_count( false, $group_id ); 234 } 235 344 236 return 1; 345 237 }
Note: See TracChangeset
for help on using the changeset viewer.