Skip to:
Content

BuddyPress.org

Changeset 11111


Ignore:
Timestamp:
09/17/2016 09:38:42 PM (4 years ago)
Author:
dcavins
Message:

WP Users List: Add member type bulk change controls and filters.

Add a dropdown select to change the member type of many members in
bulk. Also add a new column, “Member Type,” showing the member’s
assigned member type as a link that leads to a filtered view of the WP
Users table, much like a tag link on the posts table works.

Props slaFFik, dcavins.

Fixes #6060.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/classes/class-bp-members-admin.php

    r10828 r11111  
    215215                add_filter( 'set-screen-option',    array( $this, 'signup_screen_options' ), 10, 3 );
    216216            }
     217        }
     218
     219        /** Users List - Members Types ***************************************
     220         */
     221
     222        if ( is_admin() && bp_get_member_types() ) {
     223
     224            // Add "Change type" <select> to WP admin users list table and process bulk members type changes.
     225            add_action( 'restrict_manage_users', array( $this, 'users_table_output_type_change_select' ) );
     226            add_action( 'load-users.php',        array( $this, 'users_table_process_bulk_type_change'  ) );
     227
     228            // Add the member type column to the WP admin users list table.
     229            add_filter( 'manage_users_columns',       array( $this, 'users_table_add_type_column'    )        );
     230            add_filter( 'manage_users_custom_column', array( $this, 'users_table_populate_type_cell' ), 10, 3 );
     231
     232            // Filter WP admin users list table to include users of the specified type.
     233            add_filter( 'pre_get_users', array( $this, 'users_table_filter_by_type' ) );
    217234        }
    218235    }
     
    19061923     *
    19071924     * @param string $action Delete, activate, or resend activation link.
     1925     *
    19081926     * @return string
    19091927     */
     
    20332051        <?php
    20342052    }
     2053
     2054    /** Users List Management ****************************************************/
     2055
     2056    /**
     2057     * Display a dropdown to bulk change the member type of selected user(s).
     2058     *
     2059     * @since 2.7.0
     2060     *
     2061     * @param string $which Where this dropdown is displayed - top or bottom.
     2062     */
     2063    public function users_table_output_type_change_select( $which = 'top' ) {
     2064
     2065        // Bail if current user cannot promote users.
     2066        if ( ! current_user_can( 'promote_users' ) ) {
     2067            return;
     2068        }
     2069
     2070        // `$which` is only passed in WordPress 4.6+. Avoid duplicating controls in earlier versions.
     2071        static $displayed = false;
     2072        if ( version_compare( bp_get_major_wp_version(), '4.6', '<' ) && $displayed ) {
     2073            return;
     2074        }
     2075        $displayed = true;
     2076
     2077        $id_name = 'bottom' === $which ? 'bp_change_type2' : 'bp_change_type';
     2078
     2079        $types = bp_get_member_types( array(), 'objects' ); ?>
     2080
     2081        <label class="screen-reader-text" for="<?php echo $id_name; ?>"><?php _e( 'Change member type to&hellip;', 'buddypress' ) ?></label>
     2082        <select name="<?php echo $id_name; ?>" id="<?php echo $id_name; ?>" style="display:inline-block;float:none;">
     2083            <option value=""><?php _e( 'Change member type to&hellip;', 'buddypress' ) ?></option>
     2084
     2085            <?php foreach( $types as $type ) : ?>
     2086
     2087                <option value="<?php echo esc_attr( $type->name ); ?>"><?php echo $type->labels['name']; ?></option>
     2088
     2089            <?php endforeach; ?>
     2090
     2091            <option value="remove_member_type"><?php _e( 'No Member Type', 'buddypress' ) ?></option>
     2092
     2093        </select>
     2094        <?php
     2095        wp_nonce_field( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' );
     2096        submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_member_type', false );
     2097    }
     2098
     2099    /**
     2100     * Process bulk member type change submission from the WP admin users list table.
     2101     *
     2102     * @since 2.7.0
     2103     */
     2104    public function users_table_process_bulk_type_change() {
     2105        // Output the admin notice.
     2106        $this->users_type_change_notice();
     2107
     2108        // Bail if no users specified.
     2109        if ( empty( $_REQUEST['users'] ) ) {
     2110            return;
     2111        }
     2112
     2113        // Bail if this isn't a BuddyPress action.
     2114        if ( ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
     2115            || empty( $_REQUEST['bp_change_member_type'] )
     2116        ) {
     2117            return;
     2118        }
     2119
     2120        // Bail if nonce check fails.
     2121        check_admin_referer( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' );
     2122
     2123        // Bail if current user cannot promote users.
     2124        if ( ! current_user_can( 'promote_users' ) ) {
     2125            return;
     2126        }
     2127
     2128        $new_type = '';
     2129        if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
     2130            $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
     2131        } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
     2132            $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
     2133        }
     2134
     2135        // Check that the selected type actually exists.
     2136        if ( 'remove_member_type' != $new_type && null == bp_get_member_type_object( $new_type ) ) {
     2137            return;
     2138        }
     2139
     2140        // Run through user ids.
     2141        $error = false;
     2142        foreach ( (array) $_REQUEST['users'] as $user_id ) {
     2143            $user_id = (int) $user_id;
     2144
     2145            // Get the old member type to check against.
     2146            $member_type = bp_get_member_type( $user_id );
     2147
     2148            if ( 'remove_member_type' == $new_type ) {
     2149                // Remove the current member type, if there's one to remove.
     2150                if ( $member_type ) {
     2151                    $removed = bp_remove_member_type( $user_id, $member_type );
     2152                    if ( false == $removed || is_wp_error( $removed ) ) {
     2153                        $error = true;
     2154                    }
     2155                }
     2156            } else {
     2157                // Set the new member type.
     2158                if ( $new_type !== $member_type ) {
     2159                    $set = bp_set_member_type( $user_id, $new_type );
     2160                    if ( false == $set || is_wp_error( $set ) ) {
     2161                        $error = true;
     2162                    }
     2163                }
     2164            }
     2165        }
     2166
     2167        // If there were any errors, show the error message.
     2168        if ( $error ) {
     2169            $redirect = add_query_arg( array( 'updated' => 'member-type-change-error' ), wp_get_referer() );
     2170        } else {
     2171            $redirect = add_query_arg( array( 'updated' => 'member-type-change-success' ), wp_get_referer() );
     2172        }
     2173
     2174        wp_redirect( $redirect );
     2175        exit();
     2176    }
     2177
     2178    /**
     2179     * Display an admin notice upon member type bulk update.
     2180     *
     2181     * @since 2.7.0
     2182     */
     2183    public function users_type_change_notice() {
     2184        $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
     2185
     2186        // Display feedback.
     2187        if ( $updated && in_array( $updated, array( 'member-type-change-error', 'member-type-change-success' ) ) ) {
     2188
     2189            if ( 'member-type-change-error' === $updated ) {
     2190                $notice = __( 'There was an error while changing member type. Please try again.', 'buddypress' );
     2191            } else {
     2192                $notice = __( 'Member type was changed successfully.', 'buddypress' );
     2193            }
     2194
     2195            bp_core_add_admin_notice( $notice );
     2196        }
     2197    }
     2198
     2199    /**
     2200     * Add member type column to the WordPress admin users list table.
     2201     *
     2202     * @since 2.7.0
     2203     *
     2204     * @param array $columns Users table columns.
     2205     *
     2206     * @return array $columns
     2207     */
     2208    public function users_table_add_type_column( $columns = array() ) {
     2209        $columns[ bp_get_member_type_tax_name() ] = _x( 'Member Type', 'Label for the WP users table member type column' , 'buddypress' );
     2210
     2211        return $columns;
     2212    }
     2213
     2214    /**
     2215     * Return member's type for display in the WP admin users list table.
     2216     *
     2217     * @since 2.7.0
     2218     *
     2219     * @param string $retval
     2220     * @param string $column_name
     2221     * @param int $user_id
     2222     *
     2223     * @return string Member type as a link to filter all users.
     2224     */
     2225    public function users_table_populate_type_cell( $retval = '', $column_name = '', $user_id = 0 ) {
     2226        // Only looking for member type column.
     2227        if ( bp_get_member_type_tax_name() !== $column_name ) {
     2228            return $retval;
     2229        }
     2230
     2231        // Get the member type.
     2232        $type = bp_get_member_type( $user_id );
     2233
     2234        // Output the
     2235        if ( $type_obj = bp_get_member_type_object( $type ) ) {
     2236            $url = add_query_arg( array( 'bp-member-type' => urlencode( $type ) ) );
     2237            $retval = '<a href="' . esc_url( $url ) . '">' . $type_obj->labels['singular_name'] . '</a>';
     2238        }
     2239
     2240        return $retval;
     2241    }
     2242
     2243    /**
     2244     * Filter WP Admin users list table to include users of the specified type.
     2245     *
     2246     * @param WP_Query $query
     2247     *
     2248     * @since 2.7.0
     2249     */
     2250    public function users_table_filter_by_type( $query ) {
     2251        global $pagenow;
     2252
     2253        if ( is_admin() && 'users.php' === $pagenow && ! empty( $_REQUEST['bp-member-type'] ) ) {
     2254            $type_slug = sanitize_text_field( $_REQUEST['bp-member-type'] );
     2255
     2256            // Check that the type is registered.
     2257            if ( null == bp_get_member_type_object( $type_slug ) ) {
     2258                return;
     2259            }
     2260
     2261            // Get the list of users that are assigned to this member type.
     2262            $type = get_term_by( 'slug', $type_slug, bp_get_member_type_tax_name() );
     2263
     2264            if ( empty( $type->term_id ) ) {
     2265                return;
     2266            }
     2267
     2268            $user_ids = get_objects_in_term( $type->term_id, bp_get_member_type_tax_name() );
     2269
     2270            if ( $user_ids && ! is_wp_error( $user_ids ) ) {
     2271                $query->set( 'include', (array) $user_ids );
     2272            }
     2273        }
     2274    }
    20352275}
    20362276endif; // End class_exists check.
Note: See TracChangeset for help on using the changeset viewer.