home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress2 / wp-admin / includes / class-wp-ms-users-list-table.php < prev    next >
Encoding:
PHP Script  |  2017-10-02  |  12.4 KB  |  454 lines

  1. <?php
  2. /**
  3.  * List Table API: WP_MS_Users_List_Table class
  4.  *
  5.  * @package WordPress
  6.  * @subpackage Administration
  7.  * @since 3.1.0
  8.  */
  9.  
  10. /**
  11.  * Core class used to implement displaying users in a list table for the network admin.
  12.  *
  13.  * @since 3.1.0
  14.  * @access private
  15.  *
  16.  * @see WP_List_Table
  17.  */
  18. class WP_MS_Users_List_Table extends WP_List_Table {
  19.     /**
  20.      *
  21.      * @return bool
  22.      */
  23.     public function ajax_user_can() {
  24.         return current_user_can( 'manage_network_users' );
  25.     }
  26.  
  27.     /**
  28.      *
  29.      * @global string $usersearch
  30.      * @global string $role
  31.      * @global wpdb   $wpdb
  32.      * @global string $mode
  33.      */
  34.     public function prepare_items() {
  35.         global $usersearch, $role, $wpdb, $mode;
  36.  
  37.         $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
  38.  
  39.         $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
  40.  
  41.         $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
  42.  
  43.         $paged = $this->get_pagenum();
  44.  
  45.         $args = array(
  46.             'number' => $users_per_page,
  47.             'offset' => ( $paged-1 ) * $users_per_page,
  48.             'search' => $usersearch,
  49.             'blog_id' => 0,
  50.             'fields' => 'all_with_meta'
  51.         );
  52.  
  53.         if ( wp_is_large_network( 'users' ) ) {
  54.             $args['search'] = ltrim( $args['search'], '*' );
  55.         } else if ( '' !== $args['search'] ) {
  56.             $args['search'] = trim( $args['search'], '*' );
  57.             $args['search'] = '*' . $args['search'] . '*';
  58.         }
  59.  
  60.         if ( $role === 'super' ) {
  61.             $logins = implode( "', '", get_super_admins() );
  62.             $args['include'] = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE user_login IN ('$logins')" );
  63.         }
  64.  
  65.         /*
  66.          * If the network is large and a search is not being performed,
  67.          * show only the latest users with no paging in order to avoid
  68.          * expensive count queries.
  69.          */
  70.         if ( !$usersearch && wp_is_large_network( 'users' ) ) {
  71.             if ( !isset($_REQUEST['orderby']) )
  72.                 $_GET['orderby'] = $_REQUEST['orderby'] = 'id';
  73.             if ( !isset($_REQUEST['order']) )
  74.                 $_GET['order'] = $_REQUEST['order'] = 'DESC';
  75.             $args['count_total'] = false;
  76.         }
  77.  
  78.         if ( isset( $_REQUEST['orderby'] ) )
  79.             $args['orderby'] = $_REQUEST['orderby'];
  80.  
  81.         if ( isset( $_REQUEST['order'] ) )
  82.             $args['order'] = $_REQUEST['order'];
  83.  
  84.         if ( ! empty( $_REQUEST['mode'] ) ) {
  85.             $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
  86.             set_user_setting( 'network_users_list_mode', $mode );
  87.         } else {
  88.             $mode = get_user_setting( 'network_users_list_mode', 'list' );
  89.         }
  90.  
  91.         /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
  92.         $args = apply_filters( 'users_list_table_query_args', $args );
  93.  
  94.         // Query the user IDs for this page
  95.         $wp_user_search = new WP_User_Query( $args );
  96.  
  97.         $this->items = $wp_user_search->get_results();
  98.  
  99.         $this->set_pagination_args( array(
  100.             'total_items' => $wp_user_search->get_total(),
  101.             'per_page' => $users_per_page,
  102.         ) );
  103.     }
  104.  
  105.     /**
  106.      *
  107.      * @return array
  108.      */
  109.     protected function get_bulk_actions() {
  110.         $actions = array();
  111.         if ( current_user_can( 'delete_users' ) )
  112.             $actions['delete'] = __( 'Delete' );
  113.         $actions['spam'] = _x( 'Mark as Spam', 'user' );
  114.         $actions['notspam'] = _x( 'Not Spam', 'user' );
  115.  
  116.         return $actions;
  117.     }
  118.  
  119.     /**
  120.      */
  121.     public function no_items() {
  122.         _e( 'No users found.' );
  123.     }
  124.  
  125.     /**
  126.      *
  127.      * @global string $role
  128.      * @return array
  129.      */
  130.     protected function get_views() {
  131.         global $role;
  132.  
  133.         $total_users = get_user_count();
  134.         $super_admins = get_super_admins();
  135.         $total_admins = count( $super_admins );
  136.  
  137.         $current_link_attributes = $role !== 'super' ? ' class="current" aria-current="page"' : '';
  138.         $role_links = array();
  139.         $role_links['all'] = "<a href='" . network_admin_url( 'users.php' ) . "'$current_link_attributes>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
  140.         $current_link_attributes = $role === 'super' ? ' class="current" aria-current="page"' : '';
  141.         $role_links['super'] = "<a href='" . network_admin_url( 'users.php?role=super' ) . "'$current_link_attributes>" . sprintf( _n( 'Super Admin <span class="count">(%s)</span>', 'Super Admins <span class="count">(%s)</span>', $total_admins ), number_format_i18n( $total_admins ) ) . '</a>';
  142.  
  143.         return $role_links;
  144.     }
  145.  
  146.     /**
  147.      * @global string $mode List table view mode.
  148.      *
  149.      * @param string $which
  150.      */
  151.     protected function pagination( $which ) {
  152.         global $mode;
  153.  
  154.         parent::pagination ( $which );
  155.  
  156.         if ( 'top' === $which ) {
  157.             $this->view_switcher( $mode );
  158.         }
  159.     }
  160.  
  161.     /**
  162.      *
  163.      * @return array
  164.      */
  165.     public function get_columns() {
  166.         $users_columns = array(
  167.             'cb'         => '<input type="checkbox" />',
  168.             'username'   => __( 'Username' ),
  169.             'name'       => __( 'Name' ),
  170.             'email'      => __( 'Email' ),
  171.             'registered' => _x( 'Registered', 'user' ),
  172.             'blogs'      => __( 'Sites' )
  173.         );
  174.         /**
  175.          * Filters the columns displayed in the Network Admin Users list table.
  176.          *
  177.          * @since MU (3.0.0)
  178.          *
  179.          * @param array $users_columns An array of user columns. Default 'cb', 'username',
  180.          *                             'name', 'email', 'registered', 'blogs'.
  181.          */
  182.         return apply_filters( 'wpmu_users_columns', $users_columns );
  183.     }
  184.  
  185.     /**
  186.      *
  187.      * @return array
  188.      */
  189.     protected function get_sortable_columns() {
  190.         return array(
  191.             'username'   => 'login',
  192.             'name'       => 'name',
  193.             'email'      => 'email',
  194.             'registered' => 'id',
  195.         );
  196.     }
  197.  
  198.     /**
  199.      * Handles the checkbox column output.
  200.      *
  201.      * @since 4.3.0
  202.      *
  203.      * @param WP_User $user The current WP_User object.
  204.      */
  205.     public function column_cb( $user ) {
  206.         if ( is_super_admin( $user->ID ) ) {
  207.             return;
  208.         }
  209.         ?>
  210.         <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
  211.         <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
  212.         <?php
  213.     }
  214.  
  215.     /**
  216.      * Handles the ID column output.
  217.      *
  218.      * @since 4.4.0
  219.      *
  220.      * @param WP_User $user The current WP_User object.
  221.      */
  222.     public function column_id( $user ) {
  223.         echo $user->ID;
  224.     }
  225.  
  226.     /**
  227.      * Handles the username column output.
  228.      *
  229.      * @since 4.3.0
  230.      *
  231.      * @param WP_User $user The current WP_User object.
  232.      */
  233.     public function column_username( $user ) {
  234.         $super_admins = get_super_admins();
  235.         $avatar    = get_avatar( $user->user_email, 32 );
  236.         $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
  237.  
  238.         echo $avatar;
  239.  
  240.         ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
  241.         if ( in_array( $user->user_login, $super_admins ) ) {
  242.             echo ' — ' . __( 'Super Admin' );
  243.         }
  244.         ?></strong>
  245.     <?php
  246.     }
  247.  
  248.     /**
  249.      * Handles the name column output.
  250.      *
  251.      * @since 4.3.0
  252.      *
  253.      * @param WP_User $user The current WP_User object.
  254.      */
  255.     public function column_name( $user ) {
  256.         if ( $user->first_name && $user->last_name ) {
  257.             echo "$user->first_name $user->last_name";
  258.         } else {
  259.             echo '<span aria-hidden="true">—</span><span class="screen-reader-text">' . _x( 'Unknown', 'name' ) . '</span>';
  260.         }
  261.     }
  262.  
  263.     /**
  264.      * Handles the email column output.
  265.      *
  266.      * @since 4.3.0
  267.      *
  268.      * @param WP_User $user The current WP_User object.
  269.      */
  270.     public function column_email( $user ) {
  271.         echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
  272.     }
  273.  
  274.     /**
  275.      * Handles the registered date column output.
  276.      *
  277.      * @since 4.3.0
  278.      *
  279.      * @global string $mode List table view mode.
  280.      *
  281.      * @param WP_User $user The current WP_User object.
  282.      */
  283.     public function column_registered( $user ) {
  284.         global $mode;
  285.         if ( 'list' === $mode ) {
  286.             $date = __( 'Y/m/d' );
  287.         } else {
  288.             $date = __( 'Y/m/d g:i:s a' );
  289.         }
  290.         echo mysql2date( $date, $user->user_registered );
  291.     }
  292.  
  293.     /**
  294.      * @since 4.3.0
  295.      *
  296.      * @param WP_User $user
  297.      * @param string  $classes
  298.      * @param string  $data
  299.      * @param string  $primary
  300.      */
  301.     protected function _column_blogs( $user, $classes, $data, $primary ) {
  302.         echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
  303.         echo $this->column_blogs( $user );
  304.         echo $this->handle_row_actions( $user, 'blogs', $primary );
  305.         echo '</td>';
  306.     }
  307.  
  308.     /**
  309.      * Handles the sites column output.
  310.      *
  311.      * @since 4.3.0
  312.      *
  313.      * @param WP_User $user The current WP_User object.
  314.      */
  315.     public function column_blogs( $user ) {
  316.         $blogs = get_blogs_of_user( $user->ID, true );
  317.         if ( ! is_array( $blogs ) ) {
  318.             return;
  319.         }
  320.  
  321.         foreach ( $blogs as $val ) {
  322.             if ( ! can_edit_network( $val->site_id ) ) {
  323.                 continue;
  324.             }
  325.  
  326.             $path    = ( $val->path === '/' ) ? '' : $val->path;
  327.             echo '<span class="site-' . $val->site_id . '" >';
  328.             echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_network()->domain, '', $val->domain . $path ) . '</a>';
  329.             echo ' <small class="row-actions">';
  330.             $actions = array();
  331.             $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
  332.  
  333.             $class = '';
  334.             if ( $val->spam == 1 ) {
  335.                 $class .= 'site-spammed ';
  336.             }
  337.             if ( $val->mature == 1 ) {
  338.                 $class .= 'site-mature ';
  339.             }
  340.             if ( $val->deleted == 1 ) {
  341.                 $class .= 'site-deleted ';
  342.             }
  343.             if ( $val->archived == 1 ) {
  344.                 $class .= 'site-archived ';
  345.             }
  346.  
  347.             $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
  348.  
  349.             /**
  350.              * Filters the action links displayed next the sites a user belongs to
  351.              * in the Network Admin Users list table.
  352.              *
  353.              * @since 3.1.0
  354.              *
  355.              * @param array $actions     An array of action links to be displayed.
  356.              *                           Default 'Edit', 'View'.
  357.              * @param int   $userblog_id The site ID.
  358.              */
  359.             $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
  360.  
  361.             $i=0;
  362.             $action_count = count( $actions );
  363.             foreach ( $actions as $action => $link ) {
  364.                 ++$i;
  365.                 $sep = ( $i == $action_count ) ? '' : ' | ';
  366.                 echo "<span class='$action'>$link$sep</span>";
  367.             }
  368.             echo '</small></span><br/>';
  369.         }
  370.     }
  371.  
  372.     /**
  373.      * Handles the default column output.
  374.      *
  375.      * @since 4.3.0
  376.      *
  377.      * @param WP_User $user       The current WP_User object.
  378.      * @param string $column_name The current column name.
  379.      */
  380.     public function column_default( $user, $column_name ) {
  381.         /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
  382.         echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
  383.     }
  384.  
  385.     public function display_rows() {
  386.         foreach ( $this->items as $user ) {
  387.             $class = '';
  388.  
  389.             $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
  390.  
  391.             foreach ( $status_list as $status => $col ) {
  392.                 if ( $user->$status ) {
  393.                     $class .= " $col";
  394.                 }
  395.             }
  396.  
  397.             ?>
  398.             <tr class="<?php echo trim( $class ); ?>">
  399.                 <?php $this->single_row_columns( $user ); ?>
  400.             </tr>
  401.             <?php
  402.         }
  403.     }
  404.  
  405.     /**
  406.      * Gets the name of the default primary column.
  407.      *
  408.      * @since 4.3.0
  409.      *
  410.      * @return string Name of the default primary column, in this case, 'username'.
  411.      */
  412.     protected function get_default_primary_column_name() {
  413.         return 'username';
  414.     }
  415.  
  416.     /**
  417.      * Generates and displays row action links.
  418.      *
  419.      * @since 4.3.0
  420.      *
  421.      * @param object $user        User being acted upon.
  422.      * @param string $column_name Current column name.
  423.      * @param string $primary     Primary column name.
  424.      * @return string Row actions output for users in Multisite.
  425.      */
  426.     protected function handle_row_actions( $user, $column_name, $primary ) {
  427.         if ( $primary !== $column_name ) {
  428.             return '';
  429.         }
  430.  
  431.         $super_admins = get_super_admins();
  432.         $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
  433.  
  434.         $actions = array();
  435.         $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
  436.  
  437.         if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
  438.             $actions['delete'] = '<a href="' . $delete = esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&action=deleteuser&id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
  439.         }
  440.  
  441.         /**
  442.          * Filters the action links displayed under each user in the Network Admin Users list table.
  443.          *
  444.          * @since 3.2.0
  445.          *
  446.          * @param array   $actions An array of action links to be displayed.
  447.          *                         Default 'Edit', 'Delete'.
  448.          * @param WP_User $user    WP_User object.
  449.          */
  450.         $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
  451.         return $this->row_actions( $actions );
  452.     }
  453. }
  454.