0 ) { $user_group_table = _groups_get_tablename( 'user_group' ); $group_ids = implode( ',', esc_sql( $group_ids ) ); $conjunctive = !empty( groups_sanitize_request( 'filter_groups_conjunctive' ) ); if ( !$conjunctive ) { $user_query->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT user_id FROM $user_group_table WHERE group_id IN ( $group_ids ) ) "; } else { $user_query->query_where .= " AND $wpdb->users.ID IN ( " . "SELECT user_id FROM ( " . "SELECT user_id, COUNT( group_id ) AS n FROM $user_group_table WHERE group_id IN ( $group_ids ) GROUP BY user_id " . ") group_counts WHERE n = " . intval( $n ) . ") "; } } } } return $user_query; } /** * Enqueue scripts the group-actions. */ public static function admin_enqueue_scripts() { global $pagenow; if ( ( $pagenow == 'users.php' ) && empty( groups_sanitize_get( 'page' ) ) ) { Groups_UIE::enqueue( 'select' ); wp_enqueue_style( 'groups_admin_user' ); } } /** * Adds the group add/remove buttons after the last action box. */ public static function admin_head() { global $pagenow; if ( ( $pagenow == 'users.php' ) && empty( groups_sanitize_get( 'page' ) ) ) { // @since 2.18.0 moved to groups_admin_user.css } } /** * Renders group actions in the users table's extra_tablenav(). */ public static function restrict_manage_users() { global $pagenow, $wpdb, $groups_select_user_groups_index; // We don't handle multiple instances so don't render another. if ( !isset( $groups_select_user_groups_index ) ) { $groups_select_user_groups_index = 0; } else { return ''; } $output = ''; if ( ( $pagenow == 'users.php' ) && empty( groups_sanitize_get( 'page' ) ) ) { // groups select $groups_table = _groups_get_tablename( 'group' ); $groups = apply_filters( 'groups_admin_users_restrict_manage_users_groups', $wpdb->get_results( "SELECT * FROM $groups_table ORDER BY name" ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $groups_select = ''; if ( $groups ) { $groups_select = sprintf( ''; } // group bulk actions added through extra_tablenav() $box = '
'; $box .= '
'; $box .= $groups_select; $box .= '
'; $box .= ''; $box .= sprintf( '', esc_attr__( 'Apply', 'groups' ) ); $box .= '
'; $box = str_replace( '"', "'", $box ); $nonce = wp_nonce_field( 'user-group', 'bulk-user-group-nonce', true, false ); $nonce = str_replace( '"', "'", $nonce ); $box .= $nonce; $box .= ''; $output .= $box; $output .= Groups_UIE::render_select( '#user-groups' ); } echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Hooked on filter in class-wp-list-table.php to filter by group. * * @param array $views * * @return array views */ public static function views_users( $views ) { global $pagenow, $wpdb; if ( ( $pagenow == 'users.php' ) && empty( groups_sanitize_get( 'page' ) ) ) { $output = '
'; $output .= '
'; $output .= '
'; $output .= sprintf( ''; $output .= '
'; // .groups-select-container $output .= '
'; // .groups-filter-container $conjunctive = !empty( groups_sanitize_request( 'filter_groups_conjunctive' ) ); $output .= sprintf( ''; $output .= ''; $output .= '
'; $output .= Groups_UIE::render_select( '#filter-groups' ); $views['groups'] = $output; } return $views; } /** * Adds or removes users to/from groups. */ public static function load_users() { if ( Groups_User::current_user_can( GROUPS_ADMINISTER_GROUPS ) ) { $users = groups_sanitize_request( 'users' ); $action = null; if ( !empty( $_REQUEST['groups'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( groups_sanitize_get( 'groups-action' ) === 'add-group' ) { $action = 'add'; } else if ( groups_sanitize_get( 'groups-action' ) === 'remove-group' ) { $action = 'remove'; } } if ( $users !== null && $action !== null && is_array( $users ) ) { $users = array_map( 'intval', $users ); if ( groups_verify_request_nonce( 'bulk-user-group-nonce', 'user-group' ) ) { foreach ( $users as $user_id ) { switch ( $action ) { case 'add': $group_ids = groups_sanitize_get( 'group_ids' ); if ( $group_ids !== null && is_array( $group_ids ) ) { foreach ( $group_ids as $group_id ) { // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: if ( !Groups_User_Group::read( $user_id, $group_id ) ) { Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) ); } } } break; case 'remove': $group_ids = groups_sanitize_get( 'group_ids' ); if ( $group_ids !== null && is_array( $group_ids ) ) { foreach ( $group_ids as $group_id ) { // Do NOT use Groups_User::user_is_member( ... ) here, as this must not be filtered: if ( Groups_User_Group::read( $user_id, $group_id ) ) { Groups_User_Group::delete( $user_id, $group_id ); } } } break; } } $referer = wp_get_referer(); if ( $referer ) { $redirect_to = remove_query_arg( array( 'action', 'action2', 'add-to-group', 'bulk-user-group-nonce', 'group_id', 'new_role', 'remove-from-group', 'users', 'update', 'id' ), $referer ); wp_safe_redirect( $redirect_to ); exit; } } } } } /** * Adds a new column to the users table to show the groups that users belong to. * * @param array $column_headers * * @return array column headers */ public static function manage_users_columns( $column_headers ) { $column_headers[self::GROUPS] = _x( 'Groups', 'Column header (Users)', 'groups' ); return $column_headers; } /** * Renders custom column content. * * @param string $output * @param string $column_name * @param int $user_id * * @return string custom column content */ public static function manage_users_custom_column( $output, $column_name, $user_id ) { switch ( $column_name ) { case self::GROUPS : $groups_user = new Groups_User( $user_id ); $groups = $groups_user->get_groups(); if ( $groups !== null && count( $groups ) > 0 ) { usort( $groups, array( __CLASS__, 'by_group_name' ) ); $output = ''; } else { $output .= esc_html__( '--', 'groups' ); } break; } return $output; } /** * usort helper * * @param Groups_Group $o1 * @param Groups_Group $o2 * * @return int strcmp result for group names */ public static function by_group_name( $o1, $o2 ) { return strcmp( $o1->get_name(), $o2->get_name() ); } } Groups_Admin_Users::init();