Ticket #1202: 1202-better-and-combined.diff

File 1202-better-and-combined.diff, 9.3 KB (added by Gautam Gupta, 2 years ago)

Better Patch

  • bb-admin/delete-user.php

     
     1<?php 
     2require('admin-action.php'); 
     3 
     4$user_id = (int) $_GET['id']; 
     5 
     6bb_check_admin_referer( 'delete-user_' . $user_id ); 
     7 
     8if ( !bb_current_user_can( 'edit_user', $user_id ) ) 
     9        bb_die( __( 'You can\'t do that!' ) ); 
     10 
     11$bb_current_id = bb_get_current_user_info( 'id' ); 
     12$bb_user = bb_get_user( $user_id ); 
     13 
     14if ( !$bb_user ) 
     15        bb_die( __( 'There is a problem with that user.' ) ); 
     16 
     17if ( $bb_current_id == $user_id ) 
     18        bb_die( __( 'You can\'t delete yourself!' ) ); 
     19 
     20if( bb_delete_user( $user_id ) ) 
     21        $message = 'deleted'; 
     22else 
     23        bb_die( __( 'There was an error deleting that user.' ) ); 
     24 
     25if ( $sendto = wp_get_referer() ) { 
     26        $sendto = remove_query_arg( 'message', $sendto ); 
     27        $sendto = add_query_arg( 'message', $message, $sendto ); 
     28} else { 
     29        $sendto = bb_get_uri(); 
     30} 
     31 
     32bb_safe_redirect( $sendto ); 
     33exit; 
  • bb-admin/includes/functions.bb-admin.php

     
    411411        return $ids; 
    412412} 
    413413 
    414 function bb_user_row( $user, $role = '', $email = false ) { 
     414function bb_user_row( $user, $role = '', $ed = false ) { 
    415415        $actions = "<a href='" . esc_attr( get_user_profile_link( $user->ID ) ) . "'>" . __('View') . "</a>"; 
    416416        $title = ''; 
    417         if ( bb_current_user_can( 'edit_user', $user_id ) ) { 
    418                 $actions .= " | <a href='" . esc_attr( get_profile_tab_link( $user->ID, 'edit' ) ) . "'>" . __('Edit') . "</a>"; 
     417        $r  = "\t<tr id='user-$user->ID'" . get_alt_class("user-$role") . ">\n"; 
     418        if ( bb_current_user_can( 'edit_user', $user_id ) && $ed ) { 
     419                $actions .= " | <a href='" . esc_attr( get_profile_tab_link( $user->ID, 'edit' ) ) . "'>" . __( 'Edit' ) . "</a>"; 
     420                $actions .= bb_get_user_delete_link( array( 'id' => $user->ID, 'before' => ' | ', 'after' => '', 'delete_text' => __( 'Delete' ) ) ); 
    419421                $title = " title='" . esc_attr( sprintf( __( 'User ID: %d' ), $user->ID ) ) . "'"; 
     422                $r .= "\t\t<td class=\"check-column\"><input type=\"checkbox\" name=\"user[]\" value=\"" . $user->ID . "\" /></td> \n"; 
    420423        } 
    421         $r  = "\t<tr id='user-$user->ID'" . get_alt_class("user-$role") . ">\n"; 
    422424        $r .= "\t\t<td class=\"user\">" . bb_get_avatar( $user->ID, 32 ) . "<span class=\"row-title\"><a href='" . get_user_profile_link( $user->ID ) . "'" . $title . ">" . get_user_name( $user->ID ) . "</a></span><div><span class=\"row-actions\">$actions</span>&nbsp;</div></td>\n"; 
    423425        $r .= "\t\t<td><a href='" . get_user_profile_link( $user->ID ) . "'>" . get_user_display_name( $user->ID ) . "</a></td>\n"; 
    424         if ( $email ) { 
     426        if ( $ed ) { 
    425427                $email = bb_get_user_email( $user->ID ); 
    426428                $r .= "\t\t<td><a href='mailto:$email'>$email</a></td>\n"; 
    427429        } 
     
    446448                $_roles = $wp_roles->get_names(); 
    447449                $role = array(); 
    448450                foreach ( $user->capabilities as $cap => $cap_set ) { 
    449                         if (!$cap_set) { 
     451                        if ( !$cap_set || !$_roles[$cap] ) { 
    450452                                continue; 
    451453                        } 
    452454                        $role[] = $_roles[$cap]; 
     
    563565                return false; 
    564566        } 
    565567 
    566         function display( $show_search = true, $show_email = false ) { 
     568        /** 
     569         * Displays the controls for users 
     570         * 
     571         * @param bool $show_search Show Search or not. Default true 
     572         * @param bool $show_ed Show Email of the user and delete options or not. Default false. Probable input could be `bb_current_user_can('edit_users') 
     573         */ 
     574        function display( $show_search = true, $show_ed = false ) { 
    567575                global $wp_roles; 
    568576 
    569577                $r = ''; 
     
    627635                        $r .= "</fieldset>\n"; 
    628636                        $r .= "</form>\n\n"; 
    629637                } 
    630  
     638                 
     639                if( $show_ed ) { 
     640                        $bulk_actions = array( 
     641                                'delete' => __( 'Delete' ), 
     642                        ); 
     643                }else{ 
     644                        $bulk_actions = array(); //for plugins 
     645                } 
     646                 
     647                do_action_ref_array( 'bulk_user_actions', array( &$bulk_actions, &$bb_user_search ) ); 
     648                 
     649                $show_bulk = ( is_array( $bulk_actions ) && count( $bulk_actions ) > 0 ) ? true : false; 
     650                 
     651                if( $show_bulk ){ 
     652                        $r .= "<div class='clear'></div>\n\n"; 
     653                        $r .= "<form class='table-form bulk-form' method='post' action=''>\n"; 
     654                        $r .= "\t<fieldset>\n"; 
     655                        $r .= "\t\t<select name='action'>\n"; 
     656                        $r .= "\t\t\t<option>" . __( 'Bulk Actions' ) . "</option>\n"; 
     657                         
     658                        foreach ( $bulk_actions as $value => $label ) { 
     659                                $r .= "\t\t\t<option value='" . esc_attr( $value ) . "'>" . esc_html( $label ) . "</option>\n"; 
     660                        } 
     661                         
     662                        $r .= "\t\t</select>\n"; 
     663                        $r .= "\t\t<input type='submit' value='" . esc_attr__( 'Apply' ) . "' class='button submit-input' />\n"; 
     664                        $r .= "\t\t" . bb_nonce_field( 'user-bulk', '_wpnonce', true, false ) . "\n"; 
     665                        $r .= "\t</fieldset>\n"; 
     666                } 
     667                 
    631668                if ( $this->get_results() ) { 
    632669                        if ( $this->results_are_paged() ) 
    633670                                $r .= "<div class='tablenav'>\n" . $this->paging_text . "</div><div class=\"clear\"></div>\n\n"; 
     
    641678                                $r .= "<table class='widefat'>\n"; 
    642679                                $r .= "<thead>\n"; 
    643680                                $r .= "\t<tr>\n"; 
    644                                 if ( $show_email ) { 
     681                                if ( $show_bulk ) { 
     682                                        $r .= "\t\t<th scope='col' class='check-column'><input type='checkbox' /></th>\n"; 
     683                                } 
     684                                if ( $show_ed ) { 
    645685                                        $r .= "\t\t<th style='width:30%;'>" . __('Username') . "</th>\n"; 
    646686                                        $r .= "\t\t<th style='width:20%;'>" . __('Name') . "</th>\n"; 
    647687                                        $r .= "\t\t<th style='width:20%;'>" . __('E-mail') . "</th>\n"; 
     
    655695                                $r .= "</thead>\n\n"; 
    656696                                $r .= "<tfoot>\n"; 
    657697                                $r .= "\t<tr>\n"; 
    658                                 if ( $show_email ) { 
     698                                if ( $show_bulk ) { 
     699                                        $r .= "\t\t<th scope='col' class='check-column'><input type='checkbox' /></th>\n"; 
     700                                } 
     701                                if ( $show_ed ) { 
    659702                                        $r .= "\t\t<th style='width:30%;'>" . __('Username') . "</th>\n"; 
    660703                                        $r .= "\t\t<th style='width:20%;'>" . __('Name') . "</th>\n"; 
    661704                                        $r .= "\t\t<th style='width:20%;'>" . __('E-mail') . "</th>\n"; 
     
    670713 
    671714                                $r .= "<tbody id='role-$role'>\n"; 
    672715                                foreach ( (array) $this->get_results() as $user_object ) 
    673                                         $r .= bb_user_row($user_object, $role, $show_email); 
     716                                        $r .= bb_user_row( $user_object, $role, $show_ed ); 
    674717                                $r .= "</tbody>\n"; 
    675718                                $r .= "</table>\n\n"; 
     719                                $r .= "</form>\n\n"; 
    676720                        //} 
    677721 
    678722                        if ( $this->results_are_paged() ) 
  • bb-admin/users.php

     
    11<?php 
    22require_once('admin.php'); 
    33 
     4if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) ) { 
     5 
     6        bb_check_admin_referer( 'user-bulk' ); 
     7        $user_ids = array_map( 'absint', $_POST['user'] ); 
     8        $count = 0; 
     9        $bb_current_id = bb_get_current_user_info( 'id' ); 
     10         
     11        $action = trim( $_POST['action'] ); 
     12         
     13        switch ( $action ) { 
     14        case 'delete' : 
     15                foreach ( $user_ids as $user_id ) { 
     16                    if ( $bb_current_id != $user_id ) 
     17                        $count += (int) (bool) bb_delete_user( $user_id ); 
     18                } 
     19                $query_vars = array( 'message' => 'deleted', 'count' => $count ); 
     20                break; 
     21        default : 
     22                if ( $action ) 
     23                        $query_vars = apply_filters( "bulk_user__$action", array(), $user_ids, $action ); 
     24                break; 
     25        } 
     26 
     27        bb_safe_redirect( add_query_arg( $query_vars ) ); 
     28        exit; 
     29} 
     30 
     31if ( !empty( $_GET['message'] ) ) { 
     32        $message_count = isset( $_GET['count'] ) ? (int) $_GET['count'] : 1; 
     33 
     34        switch ( (string) $_GET['message'] ) { 
     35                case 'deleted': 
     36                        bb_admin_notice( sprintf( _n( '<strong>User deleted.</strong>', '<strong>%s users deleted.</strong>', $message_count ), bb_number_format_i18n( $message_count ) ) ); 
     37                        break; 
     38        } 
     39} 
     40 
    441// Query the users 
    542$bb_user_search = new BB_User_Search(@$_GET['usersearch'], @$_GET['page'], @$_GET['userrole']); 
    643 
  • bb-includes/functions.bb-template.php

     
    22472247        return apply_filters( 'get_user_delete_button', $r); 
    22482248} 
    22492249 
     2250function bb_user_delete_link( $args = '' ) { 
     2251        echo bb_get_user_delete_link( $args ); 
     2252} 
     2253 
     2254function bb_get_user_delete_link( $args = '' ) { 
     2255        $defaults = array( 'id' => 0, 'before' => '[', 'after' => ']', 'delete_text' => false, 'redirect' => true ); 
     2256        extract(wp_parse_args( $args, $defaults ), EXTR_SKIP); 
     2257        $id = (int) $id; 
     2258 
     2259        $user = bb_get_user( bb_get_user_id( $id ) ); 
     2260 
     2261        if ( !$user || !bb_current_user_can( 'edit_user', $user->ID ) ) 
     2262                return; 
     2263         
     2264        $bb_current_id = bb_get_current_user_info( 'id' ); 
     2265        if ( $bb_current_id == $user->ID ) 
     2266                return; 
     2267 
     2268        if ( true === $redirect ) 
     2269                $redirect = add_query_arg( bb_is_admin() ? array() : array( 'view' => 'all'  ) ); 
     2270 
     2271        $query   = array( 'id' => $user->ID, '_wp_http_referer' => $redirect ? rawurlencode( $redirect ) : false ); 
     2272        $confirm = __( 'Are you sure you wanna delete that user?' ); 
     2273        $display = esc_html( $delete_text ? $delete_text : __( 'Delete user' ) ); 
     2274        $uri = bb_get_uri('bb-admin/delete-user.php', $query, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN); 
     2275        $uri = esc_url( bb_nonce_url( $uri, 'delete-user_' . $user->ID ) ); 
     2276         
     2277        return $before . '<a href="' . $uri . '" onclick="return confirm(\'' . esc_js( $confirm ) . '\');">' . $display . '</a>' . $after; 
     2278} 
     2279 
    22502280function profile_tab_link( $id = 0, $tab, $page = 1 ) { 
    22512281        echo apply_filters( 'profile_tab_link', get_profile_tab_link( $id, $tab ) ); 
    22522282}