Skip to:
Content

BuddyPress.org

Ticket #7175: 7175.1.patch

File 7175.1.patch, 9.6 KB (added by dcavins, 9 years ago)

Add type display and bulk change handling to the WP Admin groups list table.

  • src/bp-groups/bp-groups-admin.php

    diff --git src/bp-groups/bp-groups-admin.php src/bp-groups/bp-groups-admin.php
    index 7fb81d3..46fac3d 100644
    function bp_groups_admin_autocomplete_handler() { 
    12241224        wp_die( json_encode( $matches ) );
    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 ( ! 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                return;
     1261        }
     1262
     1263        // Run through group ids.
     1264        $error = false;
     1265        foreach ( (array) $_REQUEST['gid'] as $group_id ) {
     1266                $group_id = (int) $group_id;
     1267
     1268                // Get the old group type to check against.
     1269                $group_type = bp_groups_get_group_type( $group_id );
     1270
     1271                if ( 'remove_group_type' == $new_type ) {
     1272                        // Remove the current group type, if there's one to remove.
     1273                        if ( $group_type ) {
     1274                                $removed = bp_groups_remove_group_type( $group_id, $group_type );
     1275                                if ( false == $removed || is_wp_error( $removed ) ) {
     1276                                        $error = true;
     1277                                }
     1278                        }
     1279                } else {
     1280                        // Set the new group type.
     1281                        if ( $new_type !== $group_type ) {
     1282                                $set = bp_groups_set_group_type( $group_id, $new_type );
     1283                                if ( false == $set || is_wp_error( $set ) ) {
     1284                                        $error = true;
     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' ) ) ) {
     1312
     1313                if ( 'group-type-change-error' === $updated ) {
     1314                        $notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
     1315                } else {
     1316                        $notice = __( 'Group type was changed successfully.', 'buddypress' );
     1317                }
     1318
     1319                bp_core_add_admin_notice( $notice );
     1320        }
     1321}
     1322add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );
  • src/bp-groups/classes/class-bp-groups-list-table.php

    diff --git src/bp-groups/classes/class-bp-groups-list-table.php src/bp-groups/classes/class-bp-groups-list-table.php
    index 7e05c85..41d6144 100644
    class BP_Groups_List_Table extends WP_List_Table { 
    3939        public $group_counts = 0;
    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
    4545         * @var array
    class BP_Groups_List_Table extends WP_List_Table { 
    5959                        'plural'   => 'groups',
    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
    6473        /**
    class BP_Groups_List_Table extends WP_List_Table { 
    122131                        $this->view = $_GET['group_status'];
    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
    128137                // Pass a dummy array if there are no groups of this type.
    class BP_Groups_List_Table extends WP_List_Table { 
    137146                        $this->group_counts[ $group_type ] = count( $group_ids );
    138147                }
    139148
     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'];
     153                }
     154
    140155                // If we're viewing a specific group, flatten all activities into a single array.
    141156                if ( $include_id ) {
    142157                        $groups = array( (array) groups_get_group( $include_id ) );
    class BP_Groups_List_Table extends WP_List_Table { 
    149164                                'order'    => $order
    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 ) ) {
    154173                                while ( bp_groups() ) {
    class BP_Groups_List_Table extends WP_List_Table { 
    244263        }
    245264
    246265        /**
     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 );
     282        }
     283
     284        /**
    247285         * Generate content for a single row of the table.
    248286         *
    249287         * @since 1.7.0
    class BP_Groups_List_Table extends WP_List_Table { 
    645683                 */
    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 ) . '">' . $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 echo $type->labels['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}