<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[BuddyPress][11139] trunk/src/bp-groups: WP Admin Groups List: Add group type bulk change controls and filters.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://buddypress.trac.wordpress.org/changeset/11139">11139</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://buddypress.trac.wordpress.org/changeset/11139","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>dcavins</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-21 16:57:45 +0000 (Wed, 21 Sep 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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, ?\226?\128?\156Group Type,?\226?\128?\157 showing the group?\226?\128?\153s
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 <a href="http://buddypress.trac.wordpress.org/ticket/7175">#7175</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpgroupsbpgroupsadminphp">trunk/src/bp-groups/bp-groups-admin.php</a></li>
<li><a href="#trunksrcbpgroupsclassesclassbpgroupslisttablephp">trunk/src/bp-groups/classes/class-bp-groups-list-table.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpgroupsbpgroupsadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/bp-groups-admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/bp-groups-admin.php   2016-09-21 15:40:45 UTC (rev 11138)
+++ trunk/src/bp-groups/bp-groups-admin.php     2016-09-21 16:57:45 UTC (rev 11139)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1224,3 +1224,101 @@
</span><span class="cx" style="display: block; padding: 0 10px">        wp_die( json_encode( $matches ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_ajax_bp_group_admin_member_autocomplete', 'bp_groups_admin_autocomplete_handler' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Process input from the Group Type bulk change select.
+ *
+ * @since 2.7.0
+ *
+ * @param string $doaction Current $_GET action being performed in admin screen.
+ */
+function bp_groups_admin_process_group_type_bulk_changes( $doaction ) {
+       // Bail if no groups are specified or if this isn't a relevant action.
+       if ( empty( $_REQUEST['gid'] )
+               || ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
+               || empty( $_REQUEST['bp_change_group_type'] )
+       ) {
+               return;
+       }
+
+       // Bail if nonce check fails.
+       check_admin_referer( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
+
+       if ( ! bp_current_user_can( 'bp_moderate' )  ) {
+               return;
+       }
+
+       $new_type = '';
+       if ( ! empty( $_REQUEST['bp_change_type2'] ) ) {
+               $new_type = sanitize_text_field( $_REQUEST['bp_change_type2'] );
+       } elseif ( ! empty( $_REQUEST['bp_change_type'] ) ) {
+               $new_type = sanitize_text_field( $_REQUEST['bp_change_type'] );
+       }
+
+       // Check that the selected type actually exists.
+       if ( 'remove_group_type' !== $new_type && null === bp_groups_get_group_type_object( $new_type ) ) {
+               $error = true;
+       } else {
+               // Run through group ids.
+               $error = false;
+               foreach ( (array) $_REQUEST['gid'] as $group_id ) {
+                       $group_id = (int) $group_id;
+
+                       // Get the old group type to check against.
+                       $group_type = bp_groups_get_group_type( $group_id );
+
+                       if ( 'remove_group_type' === $new_type ) {
+                               // Remove the current group type, if there's one to remove.
+                               if ( $group_type ) {
+                                       $removed = bp_groups_remove_group_type( $group_id, $group_type );
+                                       if ( false === $removed || is_wp_error( $removed ) ) {
+                                               $error = true;
+                                       }
+                               }
+                       } else {
+                               // Set the new group type.
+                               if ( $new_type !== $group_type ) {
+                                       $set = bp_groups_set_group_type( $group_id, $new_type );
+                                       if ( false === $set || is_wp_error( $set ) ) {
+                                               $error = true;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       // If there were any errors, show the error message.
+       if ( $error ) {
+               $redirect = add_query_arg( array( 'updated' => 'group-type-change-error' ), wp_get_referer() );
+       } else {
+               $redirect = add_query_arg( array( 'updated' => 'group-type-change-success' ), wp_get_referer() );
+       }
+
+       wp_redirect( $redirect );
+       exit();
+}
+add_action( 'bp_groups_admin_load', 'bp_groups_admin_process_group_type_bulk_changes' );
+
+/**
+ * Display an admin notice upon group type bulk update.
+ *
+ * @since 2.7.0
+ */
+function bp_groups_admin_groups_type_change_notice() {
+       $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
+
+       // Display feedback.
+       if ( $updated && in_array( $updated, array( 'group-type-change-error', 'group-type-change-success' ), true ) ) {
+
+               if ( 'group-type-change-error' === $updated ) {
+                       $notice = __( 'There was an error while changing group type. Please try again.', 'buddypress' );
+                       $type   = 'error';
+               } else {
+                       $notice = __( 'Group type was changed successfully.', 'buddypress' );
+                       $type   = 'updated';
+               }
+
+               bp_core_add_admin_notice( $notice, $type );
+       }
+}
+add_action( bp_core_admin_hook(), 'bp_groups_admin_groups_type_change_notice' );
</ins></span></pre></div>
<a id="trunksrcbpgroupsclassesclassbpgroupslisttablephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-groups/classes/class-bp-groups-list-table.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-groups/classes/class-bp-groups-list-table.php        2016-09-21 15:40:45 UTC (rev 11138)
+++ trunk/src/bp-groups/classes/class-bp-groups-list-table.php  2016-09-21 16:57:45 UTC (rev 11139)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -39,7 +39,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public $group_counts = 0;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Multidimensional array of group visibility types and their groups.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Multidimensional array of group visibility (status) types and their groups.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @link https://buddypress.trac.wordpress.org/ticket/6277
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,6 +59,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        'plural'   => 'groups',
</span><span class="cx" style="display: block; padding: 0 10px">                        'singular' => 'group',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Add Group Type column and bulk change controls.
+               if ( bp_groups_get_group_types() ) {
+                       // Add Group Type column.
+                       add_filter( 'bp_groups_list_table_get_columns',        array( $this, 'add_type_column' )                  );
+                       add_filter( 'bp_groups_admin_get_group_custom_column', array( $this, 'column_content_group_type' ), 10, 3 );
+                       // Add the bulk change select.
+                       add_action( 'bp_groups_list_table_after_bulk_actions', array( $this, 'add_group_type_bulk_change_select' ) );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -122,7 +131,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->view = $_GET['group_status'];
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // We'll use the ids of group types for the 'include' param.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // We'll use the ids of group status types for the 'include' param.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->group_type_ids = BP_Groups_Group::get_group_type_ids();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Pass a dummy array if there are no groups of this type.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -137,6 +146,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->group_counts[ $group_type ] = count( $group_ids );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                // Group types
+               $group_type = false;
+               if ( isset( $_GET['bp-group-type'] ) && null !== bp_groups_get_group_type_object( $_GET['bp-group-type'] ) ) {
+                       $group_type = $_GET['bp-group-type'];
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // If we're viewing a specific group, flatten all activities into a single array.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $include_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $groups = array( (array) groups_get_group( $include_id ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -149,6 +164,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'order'    => $order
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( $group_type ) {
+                               $groups_args['group_type'] = $group_type;
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $groups = array();
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( bp_has_groups( $groups_args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                while ( bp_groups() ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -244,6 +263,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Extra controls to be displayed between bulk actions and pagination
+        *
+        * @since 2.7.0
+        * @access protected
+        *
+        * @param string $which
+        */
+       protected function extra_tablenav( $which ) {
+               /**
+                * Fires just after the bulk action controls in the WP Admin groups list table.
+                *
+                * @since 2.7.0
+                *
+                * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
+                */
+               do_action( 'bp_groups_list_table_after_bulk_actions', $which );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Generate content for a single row of the table.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 1.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -645,4 +683,95 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                return apply_filters( 'bp_groups_admin_get_group_custom_column', '', $column_name, $item );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       // Group Types
+
+       /**
+        * Add group type column to the WordPress admin groups list table.
+        *
+        * @since 2.7.0
+        *
+        * @param array $columns Groups table columns.
+        *
+        * @return array $columns
+        */
+       public function add_type_column( $columns = array() ) {
+               $columns['bp_group_type'] = _x( 'Group Type', 'Label for the WP groups table group type column', 'buddypress' );
+
+               return $columns;
+       }
+
+       /**
+        * Markup for the Group Type column.
+        *
+        * @since 2.7.0
+        *
+        * @param string $value       Empty string.
+        * @param string $column_name Name of the column being rendered.
+        * @param array  $item        The current group item in the loop.
+        */
+       public function column_content_group_type( $retval, $column_name, $item ) {
+               if ( 'bp_group_type' !== $column_name ) {
+                       return $retval;
+               }
+
+               // Get the group type.
+               $type = bp_groups_get_group_type( $item['id'] );
+
+               // Output the
+               if ( $type_obj = bp_groups_get_group_type_object( $type ) ) {
+                       $url         = add_query_arg( array( 'bp-group-type' => urlencode( $type ) ) );
+                       $type_string = '<a href="' . esc_url( $url ) . '">' . esc_html( $type_obj->labels['singular_name'] ) . '</a>';
+               }
+
+               /**
+                * Filters the markup for the Group Type column.
+                *
+                * @since 2.7.0
+                *
+                * @param string $type_string Markup for the Group Type column.
+                * @parma array  $item        The current group item in the loop.
+                */
+               echo apply_filters_ref_array( 'bp_groups_admin_get_group_type_column', array( $type_string, $item ) );
+       }
+
+       /**
+        * Markup for the Group Type bulk change select.
+        *
+        * @since 2.7.0
+        *
+        * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
+        */
+       public function add_group_type_bulk_change_select( $which ) {
+               // `$which` is only passed in WordPress 4.6+. Avoid duplicating controls in earlier versions.
+               static $displayed = false;
+               if ( version_compare( bp_get_major_wp_version(), '4.6', '<' ) && $displayed ) {
+                       return;
+               }
+               $displayed = true;
+               $id_name = 'bottom' === $which ? 'bp_change_type2' : 'bp_change_type';
+
+               $types = bp_groups_get_group_types( array(), 'objects' );
+               ?>
+               <div class="alignleft actions">
+                       <label class="screen-reader-text" for="<?php echo $id_name; ?>"><?php _e( 'Change group type to&hellip;', 'buddypress' ) ?></label>
+                       <select name="<?php echo $id_name; ?>" id="<?php echo $id_name; ?>" style="display:inline-block;float:none;">
+                               <option value=""><?php _e( 'Change group type to&hellip;', 'buddypress' ) ?></option>
+
+                               <?php foreach( $types as $type ) : ?>
+
+                                       <option value="<?php echo esc_attr( $type->name ); ?>"><?php esc_html_e( $type->labels['singular_name'] ); ?></option>
+
+                               <?php endforeach; ?>
+
+                               <option value="remove_group_type"><?php _e( 'No Group Type', 'buddypress' ) ?></option>
+
+                       </select>
+                       <?php
+                       wp_nonce_field( 'bp-bulk-groups-change-type-' . bp_loggedin_user_id(), 'bp-bulk-groups-change-type-nonce' );
+                       submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_group_type', false );
+               ?>
+               </div>
+               <?php
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>