| | 197 | /** |
| | 198 | * Handles incoming associations for Polyglots/translate.wordpress.org. |
| | 199 | * |
| | 200 | * Payload: (beyond 'action' and 'source') |
| | 201 | * user_id: User ID |
| | 202 | * association: Slug for group/association |
| | 203 | * command: Either 'add' or 'remove' |
| | 204 | */ |
| | 205 | private function handle_polyglots_association() { |
| | 206 | $user = get_user_by( 'id', $_POST['user_id'] ); |
| | 207 | |
| | 208 | if ( ! $user ) { |
| | 209 | return '-1 Association reported for unrecognized user: ' . sanitize_text_field( $_POST['user_id'] ); |
| | 210 | } |
| | 211 | |
| | 212 | $association = sanitize_key( $_POST['association'] ); |
| | 213 | |
| | 214 | $associated_associations = array( 'translation-editor', 'translation-contributor' ); |
| | 215 | |
| | 216 | if ( ! in_array( $association, $associated_associations ) ) { |
| | 217 | return '-1 Unrecognized association type'; |
| | 218 | } |
| | 219 | |
| | 220 | if ( ! $group_id = BP_Groups_Group::group_exists( $association ) ) { |
| | 221 | return '-1 Association does not exist: ' . $association; |
| | 222 | } |
| | 223 | |
| | 224 | if ( 'add' == $_POST['command'] ) { |
| | 225 | groups_join_group( $group_id, $user->ID ); |
| | 226 | groups_accept_invite( $user->ID, $group_id ); |
| | 227 | } elseif ( 'remove' == $_POST['command'] ) { |
| | 228 | groups_leave_group( $group_id, $user->ID ); |
| | 229 | } else { |
| | 230 | return '-1 Unknown association command'; |
| | 231 | } |
| | 232 | |
| | 233 | return 1; |
| | 234 | } |
| | 235 | |