Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/21/2016 04:57:45 PM (5 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.