<!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][11111] trunk/src/bp-members/classes/class-bp-members-admin.php: WP Users List: Add member 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/11111">11111</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/11111","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-17 21:38:42 +0000 (Sat, 17 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 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, ?\226?\128?\156Member Type,?\226?\128?\157 showing the member?\226?\128?\153s
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 <a href="http://buddypress.trac.wordpress.org/ticket/6060">#6060</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcbpmembersclassesclassbpmembersadminphp">trunk/src/bp-members/classes/class-bp-members-admin.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcbpmembersclassesclassbpmembersadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/bp-members/classes/class-bp-members-admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/bp-members/classes/class-bp-members-admin.php 2016-09-17 16:54:21 UTC (rev 11110)
+++ trunk/src/bp-members/classes/class-bp-members-admin.php 2016-09-17 21:38:42 UTC (rev 11111)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -215,6 +215,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'set-screen-option', array( $this, 'signup_screen_options' ), 10, 3 );
</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">+
+ /** Users List - Members Types ***************************************
+ */
+
+ if ( is_admin() && bp_get_member_types() ) {
+
+ // Add "Change type" <select> to WP admin users list table and process bulk members type changes.
+ add_action( 'restrict_manage_users', array( $this, 'users_table_output_type_change_select' ) );
+ add_action( 'load-users.php', array( $this, 'users_table_process_bulk_type_change' ) );
+
+ // Add the member type column to the WP admin users list table.
+ add_filter( 'manage_users_columns', array( $this, 'users_table_add_type_column' ) );
+ add_filter( 'manage_users_custom_column', array( $this, 'users_table_populate_type_cell' ), 10, 3 );
+
+ // Filter WP admin users list table to include users of the specified type.
+ add_filter( 'pre_get_users', array( $this, 'users_table_filter_by_type' ) );
+ }
</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">@@ -1905,6 +1922,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 2.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $action Delete, activate, or resend activation link.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return string
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function signups_admin_manage( $action = '' ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2032,5 +2050,227 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /** Users List Management ****************************************************/
+
+ /**
+ * Display a dropdown to bulk change the member type of selected user(s).
+ *
+ * @since 2.7.0
+ *
+ * @param string $which Where this dropdown is displayed - top or bottom.
+ */
+ public function users_table_output_type_change_select( $which = 'top' ) {
+
+ // Bail if current user cannot promote users.
+ if ( ! current_user_can( 'promote_users' ) ) {
+ return;
+ }
+
+ // `$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_get_member_types( array(), 'objects' ); ?>
+
+ <label class="screen-reader-text" for="<?php echo $id_name; ?>"><?php _e( 'Change member type to…', 'buddypress' ) ?></label>
+ <select name="<?php echo $id_name; ?>" id="<?php echo $id_name; ?>" style="display:inline-block;float:none;">
+ <option value=""><?php _e( 'Change member type to…', 'buddypress' ) ?></option>
+
+ <?php foreach( $types as $type ) : ?>
+
+ <option value="<?php echo esc_attr( $type->name ); ?>"><?php echo $type->labels['name']; ?></option>
+
+ <?php endforeach; ?>
+
+ <option value="remove_member_type"><?php _e( 'No Member Type', 'buddypress' ) ?></option>
+
+ </select>
+ <?php
+ wp_nonce_field( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' );
+ submit_button( __( 'Change', 'buddypress' ), 'button', 'bp_change_member_type', false );
+ }
+
+ /**
+ * Process bulk member type change submission from the WP admin users list table.
+ *
+ * @since 2.7.0
+ */
+ public function users_table_process_bulk_type_change() {
+ // Output the admin notice.
+ $this->users_type_change_notice();
+
+ // Bail if no users specified.
+ if ( empty( $_REQUEST['users'] ) ) {
+ return;
+ }
+
+ // Bail if this isn't a BuddyPress action.
+ if ( ( empty( $_REQUEST['bp_change_type'] ) && empty( $_REQUEST['bp_change_type2'] ) )
+ || empty( $_REQUEST['bp_change_member_type'] )
+ ) {
+ return;
+ }
+
+ // Bail if nonce check fails.
+ check_admin_referer( 'bp-bulk-users-change-type-' . bp_loggedin_user_id(), 'bp-bulk-users-change-type-nonce' );
+
+ // Bail if current user cannot promote users.
+ if ( ! current_user_can( 'promote_users' ) ) {
+ 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_member_type' != $new_type && null == bp_get_member_type_object( $new_type ) ) {
+ return;
+ }
+
+ // Run through user ids.
+ $error = false;
+ foreach ( (array) $_REQUEST['users'] as $user_id ) {
+ $user_id = (int) $user_id;
+
+ // Get the old member type to check against.
+ $member_type = bp_get_member_type( $user_id );
+
+ if ( 'remove_member_type' == $new_type ) {
+ // Remove the current member type, if there's one to remove.
+ if ( $member_type ) {
+ $removed = bp_remove_member_type( $user_id, $member_type );
+ if ( false == $removed || is_wp_error( $removed ) ) {
+ $error = true;
+ }
+ }
+ } else {
+ // Set the new member type.
+ if ( $new_type !== $member_type ) {
+ $set = bp_set_member_type( $user_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' => 'member-type-change-error' ), wp_get_referer() );
+ } else {
+ $redirect = add_query_arg( array( 'updated' => 'member-type-change-success' ), wp_get_referer() );
+ }
+
+ wp_redirect( $redirect );
+ exit();
+ }
+
+ /**
+ * Display an admin notice upon member type bulk update.
+ *
+ * @since 2.7.0
+ */
+ public function users_type_change_notice() {
+ $updated = isset( $_REQUEST['updated'] ) ? $_REQUEST['updated'] : false;
+
+ // Display feedback.
+ if ( $updated && in_array( $updated, array( 'member-type-change-error', 'member-type-change-success' ) ) ) {
+
+ if ( 'member-type-change-error' === $updated ) {
+ $notice = __( 'There was an error while changing member type. Please try again.', 'buddypress' );
+ } else {
+ $notice = __( 'Member type was changed successfully.', 'buddypress' );
+ }
+
+ bp_core_add_admin_notice( $notice );
+ }
+ }
+
+ /**
+ * Add member type column to the WordPress admin users list table.
+ *
+ * @since 2.7.0
+ *
+ * @param array $columns Users table columns.
+ *
+ * @return array $columns
+ */
+ public function users_table_add_type_column( $columns = array() ) {
+ $columns[ bp_get_member_type_tax_name() ] = _x( 'Member Type', 'Label for the WP users table member type column' , 'buddypress' );
+
+ return $columns;
+ }
+
+ /**
+ * Return member's type for display in the WP admin users list table.
+ *
+ * @since 2.7.0
+ *
+ * @param string $retval
+ * @param string $column_name
+ * @param int $user_id
+ *
+ * @return string Member type as a link to filter all users.
+ */
+ public function users_table_populate_type_cell( $retval = '', $column_name = '', $user_id = 0 ) {
+ // Only looking for member type column.
+ if ( bp_get_member_type_tax_name() !== $column_name ) {
+ return $retval;
+ }
+
+ // Get the member type.
+ $type = bp_get_member_type( $user_id );
+
+ // Output the
+ if ( $type_obj = bp_get_member_type_object( $type ) ) {
+ $url = add_query_arg( array( 'bp-member-type' => urlencode( $type ) ) );
+ $retval = '<a href="' . esc_url( $url ) . '">' . $type_obj->labels['singular_name'] . '</a>';
+ }
+
+ return $retval;
+ }
+
+ /**
+ * Filter WP Admin users list table to include users of the specified type.
+ *
+ * @param WP_Query $query
+ *
+ * @since 2.7.0
+ */
+ public function users_table_filter_by_type( $query ) {
+ global $pagenow;
+
+ if ( is_admin() && 'users.php' === $pagenow && ! empty( $_REQUEST['bp-member-type'] ) ) {
+ $type_slug = sanitize_text_field( $_REQUEST['bp-member-type'] );
+
+ // Check that the type is registered.
+ if ( null == bp_get_member_type_object( $type_slug ) ) {
+ return;
+ }
+
+ // Get the list of users that are assigned to this member type.
+ $type = get_term_by( 'slug', $type_slug, bp_get_member_type_tax_name() );
+
+ if ( empty( $type->term_id ) ) {
+ return;
+ }
+
+ $user_ids = get_objects_in_term( $type->term_id, bp_get_member_type_tax_name() );
+
+ if ( $user_ids && ! is_wp_error( $user_ids ) ) {
+ $query->set( 'include', (array) $user_ids );
+ }
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> endif; // End class_exists check.
</span></span></pre>
</div>
</div>
</body>
</html>