Skip to:
Content

BuddyPress.org

Changeset 11139


Ignore:
Timestamp:
09/21/2016 04:57:45 PM (10 years ago)
Author:
dcavins
Message:

WP Admin Groups List: Add group type bulk change controls and filters.

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

Props dcavins, djpaul.

Fixes #7175.

Location:
trunk/src/bp-groups
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-admin.php

    r11091 r11139  
    12251225}
    12261226add_action( 'wp_ajax_bp_group_admin_member_autocomplete', 'bp_groups_admin_autocomplete_handler' );
     1227
     1228/**
     1229 * Process input from the Group Type bulk change select.
     1230 *
     1231 * @since 2.7.0
     1232 *
     1233 * @param string $doaction Current $_GET action being performed in admin screen.
     1234 */
     1235function bp_groups_admin_process_group_type_bulk_changes( $doaction ) {
     1236        // Bail if no groups are specified or if this isn't a relevant action.
     1237        if ( empty( $_REQUEST['gid'] )
     1238                || ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
     1239                || empty( $_REQUEST['bp_change_group_type'] )
     1240        ) {
     1241                return;
     1242        }
     1243
     1244        // Bail if nonce check fails.
     1245        check_admin_referer( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
     1246
     1247        if ( ! bp_current_user_can( 'bp_moderate' )  ) {
     1248                return;
     1249        }
     1250
     1251        $new_type = '';
     1252        if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
     1253                $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
     1254        } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
     1255                $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
     1256        }
     1257
     1258        // Check that the selected type actually exists.
     1259        if ( 'remove_group_type' !== $new_type && null === bp_groups_get_group_type_object( $new_type ) ) {
     1260                $error = true;
     1261        } else {
     1262                // Run through group ids.
     1263                $error = false;
     1264                foreach ( (array) $_REQUEST['gid'] as $group_id ) {
     1265                        $group_id = (int) $group_id;
     1266
     1267                        // Get the old group type to check against.
     1268                        $group_type = bp_groups_get_group_type( $group_id );
     1269
     1270                        if ( 'remove_group_type' === $new_type ) {
     1271                                // Remove the current group type, if there's one to remove.
     1272                                if ( $group_type ) {
     1273                                        $removed = bp_groups_remove_group_type( $group_id, $group_type );
     1274                                        if ( false === $removed || is_wp_error( $removed ) ) {
     1275                                                $error = true;
     1276                                        }
     1277                                }
     1278                        } else {
     1279                                // Set the new group type.
     1280                                if ( $new_type !== $group_type ) {
     1281                                        $set = bp_groups_set_group_type( $group_id, $new_type );
     1282                                        if ( false === $set || is_wp_error( $set ) ) {
     1283                                                $error = true;
     1284                                        }
     1285                                }
     1286                        }
     1287                }
     1288        }
     1289
     1290        // If there were any errors, show the error message.
     1291        if ( $error ) {
     1292                $redirect = add_query_arg( array( 'updated' => 'group-type-change-error' ), wp_get_referer() );
     1293        } else {
     1294                $redirect = add_query_arg( array( 'updated' => 'group-type-change-success' ), wp_get_referer() );
     1295        }
     1296
     1297        wp_redirect( $redirect );
     1298        exit();
     1299}
     1300add_action( 'bp_groups_admin_load', 'bp_groups_admin_process_group_type_bulk_changes' );
     1301
     1302/**
     1303 * Display an admin notice upon group type bulk update.
     1304 *
     1305 * @since 2.7.0
     1306 */
     1307function bp_groups_admin_groups_type_change_notice() {
     1308        $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
     1309
     1310        // Display feedback.
     1311        if ( $updated && in_array( $updated, array( 'group-type-change-error', 'group-type-change-success' ), true ) ) {
     1312
     1313                if ( 'group-type-change-error' === $updated ) {
     1314                        $notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
     1315                        $type   = 'error';
     1316                } else {
     1317                        $notice = __( 'Group type was changed successfully.', 'buddypress' );
     1318                        $type   = 'updated';
     1319                }
     1320
     1321                bp_core_add_admin_notice( $notice, $type );
     1322        }
     1323}
     1324add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );
  • trunk/src/bp-groups/classes/class-bp-groups-list-table.php

    r11091 r11139  
    4040
    4141        /**
    42          * Multidimensional array of group visibility types and their groups.
     42         * Multidimensional array of group visibility (status) types and their groups.
    4343         *
    4444         * @link https://buddypress.trac.wordpress.org/ticket/6277
    … …  
    6060                        'singular' => 'group',
    6161                ) );
     62
     63                // Add Group Type column and bulk change controls.
     64                if ( bp_groups_get_group_types() ) {
     65                        // Add Group Type column.
     66                        add_filter( 'bp_groups_list_table_get_columns',        array( $this, 'add_type_column' )                  );
     67                        add_filter( 'bp_groups_admin_get_group_custom_column', array( $this, 'column_content_group_type' ), 10, 3 );
     68                        // Add the bulk change select.
     69                        add_action( 'bp_groups_list_table_after_bulk_actions', array( $this, 'add_group_type_bulk_change_select' ) );
     70                }
    6271        }
    6372
    … …  
    123132                }
    124133
    125                 // We'll use the ids of group types for the 'include' param.
     134                // We'll use the ids of group status types for the 'include' param.
    126135                $this->group_type_ids = BP_Groups_Group::get_group_type_ids();
    127136
    … …  
    136145                foreach ( $this->group_type_ids as $group_type => $group_ids ) {
    137146                        $this->group_counts[ $group_type ] = count( $group_ids );
     147                }
     148
     149                // Group types
     150                $group_type = false;
     151                if ( isset( $_GET['bp-group-type'] ) && null !== bp_groups_get_group_type_object( $_GET['bp-group-type'] ) ) {
     152                        $group_type = $_GET['bp-group-type'];
    138153                }
    139154
    … …  
    150165                        );
    151166
     167                        if ( $group_type ) {
     168                                $groups_args['group_type'] = $group_type;
     169                        }
     170
    152171                        $groups = array();
    153172                        if ( bp_has_groups( $groups_args ) ) {
    … …  
    242261
    243262                $this->display_tablenav( 'bottom' );
     263        }
     264
     265        /**
     266         * Extra controls to be displayed between bulk actions and pagination
     267         *
     268         * @since 2.7.0
     269         * @access protected
     270         *
     271         * @param string $which
     272         */
     273        protected function extra_tablenav( $which ) {
     274                /**
     275                 * Fires just after the bulk action controls in the WP Admin groups list table.
     276                 *
     277                 * @since 2.7.0
     278                 *
     279                 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
     280                 */
     281                do_action( 'bp_groups_list_table_after_bulk_actions', $which );
    244282        }
    245283
    … …  
    646684                return apply_filters( 'bp_groups_admin_get_group_custom_column', '', $column_name, $item );
    647685        }
     686
     687        // Group Types
     688
     689        /**
     690         * Add group type column to the WordPress admin groups list table.
     691         *
     692         * @since 2.7.0
     693         *
     694         * @param array $columns Groups table columns.
     695         *
     696         * @return array $columns
     697         */
     698        public function add_type_column( $columns = array() ) {
     699                $columns['bp_group_type'] = _x( 'Group Type', 'Label for the WP groups table group type column', 'buddypress' );
     700
     701                return $columns;
     702        }
     703
     704        /**
     705         * Markup for the Group Type column.
     706         *
     707         * @since 2.7.0
     708         *
     709         * @param string $value       Empty string.
     710         * @param string $column_name Name of the column being rendered.
     711         * @param array  $item        The current group item in the loop.
     712         */
     713        public function column_content_group_type( $retval, $column_name, $item ) {
     714                if ( 'bp_group_type' !== $column_name ) {
     715                        return $retval;
     716                }
     717
     718                // Get the group type.
     719                $type = bp_groups_get_group_type( $item['id'] );
     720
     721                // Output the
     722                if ( $type_obj = bp_groups_get_group_type_object( $type ) ) {
     723                        $url         = add_query_arg( array( 'bp-group-type' => urlencode( $type ) ) );
     724                        $type_string = '<a href="' . esc_url( $url ) . '">' . esc_html( $type_obj->labels['singular_name'] ) . '</a>';
     725                }
     726
     727                /**
     728                 * Filters the markup for the Group Type column.
     729                 *
     730                 * @since 2.7.0
     731                 *
     732                 * @param string $type_string Markup for the Group Type column.
     733                 * @parma array  $item        The current group item in the loop.
     734                 */
     735                echo apply_filters_ref_array( 'bp_groups_admin_get_group_type_column', array( $type_string, $item ) );
     736        }
     737
     738        /**
     739         * Markup for the Group Type bulk change select.
     740         *
     741         * @since 2.7.0
     742         *
     743         * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
     744         */
     745        public function add_group_type_bulk_change_select( $which ) {
     746                // `$which` is only passed in WordPress 4.6+. Avoid duplicating controls in earlier versions.
     747                static $displayed = false;
     748                if ( version_compare( bp_get_major_wp_version(), '4.6', '<' ) && $displayed ) {
     749                        return;
     750                }
     751                $displayed = true;
     752                $id_name = 'bottom' === $which ? 'bp_change_type2' : 'bp_change_type';
     753
     754                $types = bp_groups_get_group_types( array(), 'objects' );
     755                ?>
     756                <div class="alignleft actions">
     757                        <label class="screen-reader-text" for="<?php echo $id_name; ?>"><?php _e( 'Change group type to&hellip;', 'buddypress' ) ?></label>
     758                        <select name="<?php echo $id_name; ?>" id="<?php echo $id_name; ?>" style="display:inline-block;float:none;">
     759                                <option value=""><?php _e( 'Change group type to&hellip;', 'buddypress' ) ?></option>
     760
     761                                <?php foreach( $types as $type ) : ?>
     762
     763                                        <option value="<?php echo esc_attr( $type->name ); ?>"><?php esc_html_e( $type->labels['singular_name'] ); ?></option>
     764
     765                                <?php endforeach; ?>
     766
     767                                <option value="remove_group_type"><?php _e( 'No Group Type', 'buddypress' ) ?></option>
     768
     769                        </select>
     770                        <?php
     771                        wp_nonce_field( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
     772                        submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_group_type', false );
     773                ?>
     774                </div>
     775                <?php
     776        }
    648777}
Note: See TracChangeset for help on using the changeset viewer.