BP_Core_Members_Switching::action_init()

Loads localisation files and routes actions depending on the ‘action’ query var.

Description

Source

File: bp-members/classes/class-bp-core-members-switching.php

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
public function action_init() {
 
    if ( ! isset( $_REQUEST['action'] ) ) {
        return;
    }
 
    $current_user = ( is_user_logged_in() ) ? wp_get_current_user() : null;
 
    switch ( $_REQUEST['action'] ) {
 
        // We're attempting to switch to another user:
        case 'switch_to_user':
            if ( isset( $_REQUEST['user_id'] ) ) {
                $user_id = absint( $_REQUEST['user_id'] );
            } else {
                $user_id = 0;
            }
 
            // Check authentication:
            $old_user = bp_current_member_switched();
            if ( ! current_user_can( 'switch_to_user', $user_id ) && ( ! $old_user || ! user_can( $old_user, 'switch_to_user' ) ) ) {
                wp_die( esc_html__( 'Could not switch users.', 'buddyboss' ) );
            }
 
            // Check intent:
            check_admin_referer( "switch_to_user_{$user_id}" );
 
            // Switch user:
            $user = bp_member_switch_to( $user_id, self::remember() );
            if ( $user ) {
                $redirect_to = $_GET['redirect_to'] ?: self::get_redirect( $user, $current_user );
 
                // Redirect to the dashboard or the home URL depending on capabilities:
                $args = array(
                    'user_switched' => 'true',
                );
 
                if ( $redirect_to ) {
                    wp_safe_redirect( add_query_arg( $args, $redirect_to ), 302, self::$application );
                } elseif ( ! current_user_can( 'read' ) ) {
                    wp_safe_redirect( add_query_arg( $args, home_url() ), 302, self::$application );
                } else {
                    wp_safe_redirect( add_query_arg( $args, admin_url() ), 302, self::$application );
                }
                exit;
            } else {
                wp_die( esc_html__( 'Could not switch users.', 'buddyboss' ) );
            }
            break;
 
        // We're attempting to switch back to the originating user:
        case 'switch_to_olduser':
            // Fetch the originating user data:
            $old_user = self::get_old_user();
            if ( ! $old_user ) {
                wp_die( esc_html__( 'Could not switch users.', 'buddyboss' ) );
            }
 
            // Check authentication:
            if ( ! self::authenticate_old_user( $old_user ) ) {
                wp_die( esc_html__( 'Could not switch users.', 'buddyboss' ) );
            }
 
            // Check intent:
            check_admin_referer( "switch_to_olduser_{$old_user->ID}" );
 
            // Switch user:
            if ( bp_member_switch_to( $old_user->ID, self::remember(), false ) ) {
 
                if ( ! empty( $_REQUEST['interim-login'] ) ) {
                    $GLOBALS['interim_login'] = 'success'; // @codingStandardsIgnoreLine
                    login_header( '', '' );
                    exit;
                }
 
                $redirect_to = self::get_redirect( $old_user, $current_user );
                $args        = array(
                    'user_switched' => 'true',
                    'switched_back' => 'true',
                );
 
                if ( $redirect_to ) {
                    wp_safe_redirect( add_query_arg( $args, $redirect_to ), 302, self::$application );
                } else {
                    wp_safe_redirect( add_query_arg( $args, admin_url( 'users.php' ) ), 302, self::$application );
                }
                exit;
            } else {
                wp_die( esc_html__( 'Could not switch users.', 'buddyboss' ) );
            }
            break;
 
    }
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.