home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / includes / class-wp-ms-sites-list-table.php < prev    next >
Encoding:
PHP Script  |  2017-08-22  |  15.2 KB  |  560 lines

  1. <?php
  2. /**
  3.  * List Table API: WP_MS_Sites_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 sites 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_Sites_List_Table extends WP_List_Table {
  19.  
  20.     /**
  21.      * Site status list.
  22.      *
  23.      * @since 4.3.0
  24.      * @var array
  25.      */
  26.     public $status_list;
  27.  
  28.     /**
  29.      * Constructor.
  30.      *
  31.      * @since 3.1.0
  32.      *
  33.      * @see WP_List_Table::__construct() for more information on default arguments.
  34.      *
  35.      * @param array $args An associative array of arguments.
  36.      */
  37.     public function __construct( $args = array() ) {
  38.         $this->status_list = array(
  39.             'archived' => array( 'site-archived', __( 'Archived' ) ),
  40.             'spam'     => array( 'site-spammed', _x( 'Spam', 'site' ) ),
  41.             'deleted'  => array( 'site-deleted', __( 'Deleted' ) ),
  42.             'mature'   => array( 'site-mature', __( 'Mature' ) )
  43.         );
  44.  
  45.         parent::__construct( array(
  46.             'plural' => 'sites',
  47.             'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  48.         ) );
  49.     }
  50.  
  51.     /**
  52.      *
  53.      * @return bool
  54.      */
  55.     public function ajax_user_can() {
  56.         return current_user_can( 'manage_sites' );
  57.     }
  58.  
  59.     /**
  60.      * Prepares the list of sites for display.
  61.      *
  62.      * @since 3.1.0
  63.      *
  64.      * @global string $s
  65.      * @global string $mode
  66.      * @global wpdb   $wpdb
  67.      */
  68.     public function prepare_items() {
  69.         global $s, $mode, $wpdb;
  70.  
  71.         if ( ! empty( $_REQUEST['mode'] ) ) {
  72.             $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
  73.             set_user_setting( 'sites_list_mode', $mode );
  74.         } else {
  75.             $mode = get_user_setting( 'sites_list_mode', 'list' );
  76.         }
  77.  
  78.         $per_page = $this->get_items_per_page( 'sites_network_per_page' );
  79.  
  80.         $pagenum = $this->get_pagenum();
  81.  
  82.         $s = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST[ 's' ] ) ) : '';
  83.         $wild = '';
  84.         if ( false !== strpos($s, '*') ) {
  85.             $wild = '*';
  86.             $s = trim($s, '*');
  87.         }
  88.  
  89.         /*
  90.          * If the network is large and a search is not being performed, show only
  91.          * the latest sites with no paging in order to avoid expensive count queries.
  92.          */
  93.         if ( !$s && wp_is_large_network() ) {
  94.             if ( !isset($_REQUEST['orderby']) )
  95.                 $_GET['orderby'] = $_REQUEST['orderby'] = '';
  96.             if ( !isset($_REQUEST['order']) )
  97.                 $_GET['order'] = $_REQUEST['order'] = 'DESC';
  98.         }
  99.  
  100.         $args = array(
  101.             'number'     => intval( $per_page ),
  102.             'offset'     => intval( ( $pagenum - 1 ) * $per_page ),
  103.             'network_id' => get_current_network_id(),
  104.         );
  105.  
  106.         if ( empty($s) ) {
  107.             // Nothing to do.
  108.         } elseif ( preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $s ) ||
  109.                     preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
  110.                     preg_match( '/^[0-9]{1,3}\.[0-9]{1,3}\.?$/', $s ) ||
  111.                     preg_match( '/^[0-9]{1,3}\.$/', $s ) ) {
  112.             // IPv4 address
  113.             $sql = $wpdb->prepare( "SELECT blog_id FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.IP LIKE %s", $wpdb->esc_like( $s ) . ( ! empty( $wild ) ? '%' : '' ) );
  114.             $reg_blog_ids = $wpdb->get_col( $sql );
  115.  
  116.             if ( $reg_blog_ids ) {
  117.                 $args['site__in'] = $reg_blog_ids;
  118.             }
  119.         } elseif ( is_numeric( $s ) && empty( $wild ) ) {
  120.             $args['ID'] = $s;
  121.         } else {
  122.             $args['search'] = $s;
  123.  
  124.             if ( ! is_subdomain_install() ) {
  125.                 $args['search_columns'] = array( 'path' );
  126.             }
  127.         }
  128.  
  129.         $order_by = isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : '';
  130.         if ( 'registered' === $order_by ) {
  131.             // registered is a valid field name.
  132.         } elseif ( 'lastupdated' === $order_by ) {
  133.             $order_by = 'last_updated';
  134.         } elseif ( 'blogname' === $order_by ) {
  135.             if ( is_subdomain_install() ) {
  136.                 $order_by = 'domain';
  137.             } else {
  138.                 $order_by = 'path';
  139.             }
  140.         } elseif ( 'blog_id' === $order_by ) {
  141.             $order_by = 'id';
  142.         } elseif ( ! $order_by ) {
  143.             $order_by = false;
  144.         }
  145.  
  146.         $args['orderby'] = $order_by;
  147.  
  148.         if ( $order_by ) {
  149.             $args['order'] = ( isset( $_REQUEST['order'] ) && 'DESC' === strtoupper( $_REQUEST['order'] ) ) ? "DESC" : "ASC";
  150.         }
  151.  
  152.         if ( wp_is_large_network() ) {
  153.             $args['no_found_rows'] = true;
  154.         } else {
  155.             $args['no_found_rows'] = false;
  156.         }
  157.  
  158.         /**
  159.          * Filters the arguments for the site query in the sites list table.
  160.          *
  161.          * @since 4.6.0
  162.          *
  163.          * @param array $args An array of get_sites() arguments.
  164.          */
  165.         $args = apply_filters( 'ms_sites_list_table_query_args', $args );
  166.  
  167.         $_sites = get_sites( $args );
  168.         if ( is_array( $_sites ) ) {
  169.             update_site_cache( $_sites );
  170.  
  171.             $this->items = array_slice( $_sites, 0, $per_page );
  172.         }
  173.  
  174.         $total_sites = get_sites( array_merge( $args, array(
  175.             'count' => true,
  176.             'offset' => 0,
  177.             'number' => 0,
  178.         ) ) );
  179.  
  180.         $this->set_pagination_args( array(
  181.             'total_items' => $total_sites,
  182.             'per_page' => $per_page,
  183.         ) );
  184.     }
  185.  
  186.     /**
  187.      */
  188.     public function no_items() {
  189.         _e( 'No sites found.' );
  190.     }
  191.  
  192.     /**
  193.      *
  194.      * @return array
  195.      */
  196.     protected function get_bulk_actions() {
  197.         $actions = array();
  198.         if ( current_user_can( 'delete_sites' ) )
  199.             $actions['delete'] = __( 'Delete' );
  200.         $actions['spam'] = _x( 'Mark as Spam', 'site' );
  201.         $actions['notspam'] = _x( 'Not Spam', 'site' );
  202.  
  203.         return $actions;
  204.     }
  205.  
  206.     /**
  207.      * @global string $mode List table view mode.
  208.      *
  209.      * @param string $which
  210.      */
  211.     protected function pagination( $which ) {
  212.         global $mode;
  213.  
  214.         parent::pagination( $which );
  215.  
  216.         if ( 'top' === $which )
  217.             $this->view_switcher( $mode );
  218.     }
  219.  
  220.     /**
  221.      * @return array
  222.      */
  223.     public function get_columns() {
  224.         $sites_columns = array(
  225.             'cb'          => '<input type="checkbox" />',
  226.             'blogname'    => __( 'URL' ),
  227.             'lastupdated' => __( 'Last Updated' ),
  228.             'registered'  => _x( 'Registered', 'site' ),
  229.             'users'       => __( 'Users' ),
  230.         );
  231.  
  232.         if ( has_filter( 'wpmublogsaction' ) ) {
  233.             $sites_columns['plugins'] = __( 'Actions' );
  234.         }
  235.  
  236.         /**
  237.          * Filters the displayed site columns in Sites list table.
  238.          *
  239.          * @since MU (3.0.0)
  240.          *
  241.          * @param array $sites_columns An array of displayed site columns. Default 'cb',
  242.          *                             'blogname', 'lastupdated', 'registered', 'users'.
  243.          */
  244.         return apply_filters( 'wpmu_blogs_columns', $sites_columns );
  245.     }
  246.  
  247.     /**
  248.      * @return array
  249.      */
  250.     protected function get_sortable_columns() {
  251.         return array(
  252.             'blogname'    => 'blogname',
  253.             'lastupdated' => 'lastupdated',
  254.             'registered'  => 'blog_id',
  255.         );
  256.     }
  257.  
  258.     /**
  259.      * Handles the checkbox column output.
  260.      *
  261.      * @since 4.3.0
  262.      *
  263.      * @param array $blog Current site.
  264.      */
  265.     public function column_cb( $blog ) {
  266.         if ( ! is_main_site( $blog['blog_id'] ) ) :
  267.             $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  268.         ?>
  269.             <label class="screen-reader-text" for="blog_<?php echo $blog['blog_id']; ?>"><?php
  270.                 printf( __( 'Select %s' ), $blogname );
  271.             ?></label>
  272.             <input type="checkbox" id="blog_<?php echo $blog['blog_id'] ?>" name="allblogs[]" value="<?php echo esc_attr( $blog['blog_id'] ) ?>" />
  273.         <?php endif;
  274.     }
  275.  
  276.     /**
  277.      * Handles the ID column output.
  278.      *
  279.      * @since 4.4.0
  280.      *
  281.      * @param array $blog Current site.
  282.      */
  283.     public function column_id( $blog ) {
  284.         echo $blog['blog_id'];
  285.     }
  286.  
  287.     /**
  288.      * Handles the site name column output.
  289.      *
  290.      * @since 4.3.0
  291.      *
  292.      * @global string $mode List table view mode.
  293.      *
  294.      * @param array $blog Current site.
  295.      */
  296.     public function column_blogname( $blog ) {
  297.         global $mode;
  298.  
  299.         $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  300.         $blog_states = array();
  301.         reset( $this->status_list );
  302.  
  303.         foreach ( $this->status_list as $status => $col ) {
  304.             if ( $blog[ $status ] == 1 ) {
  305.                 $blog_states[] = $col[1];
  306.             }
  307.         }
  308.         $blog_state = '';
  309.         if ( ! empty( $blog_states ) ) {
  310.             $state_count = count( $blog_states );
  311.             $i = 0;
  312.             $blog_state .= ' — ';
  313.             foreach ( $blog_states as $state ) {
  314.                 ++$i;
  315.                 $sep = ( $i == $state_count ) ? '' : ', ';
  316.                 $blog_state .= "<span class='post-state'>$state$sep</span>";
  317.             }
  318.         }
  319.  
  320.         ?>
  321.         <strong>
  322.             <a href="<?php echo esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ); ?>" class="edit"><?php echo $blogname; ?></a>
  323.             <?php echo $blog_state; ?>
  324.         </strong>
  325.         <?php
  326.         if ( 'list' !== $mode ) {
  327.             switch_to_blog( $blog['blog_id'] );
  328.             echo '<p>';
  329.             printf(
  330.                 /* translators: 1: site name, 2: site tagline. */
  331.                 __( '%1$s – %2$s' ),
  332.                 get_option( 'blogname' ),
  333.                 '<em>' . get_option( 'blogdescription ' ) . '</em>'
  334.             );
  335.             echo '</p>';
  336.             restore_current_blog();
  337.         }
  338.     }
  339.  
  340.     /**
  341.      * Handles the lastupdated column output.
  342.      *
  343.      * @since 4.3.0
  344.      *
  345.      * @global string $mode List table view mode.
  346.      *
  347.      * @param array $blog Current site.
  348.      */
  349.     public function column_lastupdated( $blog ) {
  350.         global $mode;
  351.  
  352.         if ( 'list' === $mode ) {
  353.             $date = __( 'Y/m/d' );
  354.         } else {
  355.             $date = __( 'Y/m/d g:i:s a' );
  356.         }
  357.  
  358.         echo ( $blog['last_updated'] === '0000-00-00 00:00:00' ) ? __( 'Never' ) : mysql2date( $date, $blog['last_updated'] );
  359.     }
  360.  
  361.     /**
  362.      * Handles the registered column output.
  363.      *
  364.      * @since 4.3.0
  365.      *
  366.      * @global string $mode List table view mode.
  367.      *
  368.      * @param array $blog Current site.
  369.      */
  370.     public function column_registered( $blog ) {
  371.         global $mode;
  372.  
  373.         if ( 'list' === $mode ) {
  374.             $date = __( 'Y/m/d' );
  375.         } else {
  376.             $date = __( 'Y/m/d g:i:s a' );
  377.         }
  378.  
  379.         if ( $blog['registered'] === '0000-00-00 00:00:00' ) {
  380.             echo '—';
  381.         } else {
  382.             echo mysql2date( $date, $blog['registered'] );
  383.         }
  384.     }
  385.  
  386.     /**
  387.      * Handles the users column output.
  388.      *
  389.      * @since 4.3.0
  390.      *
  391.      * @param array $blog Current site.
  392.      */
  393.     public function column_users( $blog ) {
  394.         $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' );
  395.         if ( ! $user_count ) {
  396.             $blog_users = get_users( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID' ) );
  397.             $user_count = count( $blog_users );
  398.             unset( $blog_users );
  399.             wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS );
  400.         }
  401.  
  402.         printf(
  403.             '<a href="%s">%s</a>',
  404.             esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ),
  405.             number_format_i18n( $user_count )
  406.         );
  407.     }
  408.  
  409.     /**
  410.      * Handles the plugins column output.
  411.      *
  412.      * @since 4.3.0
  413.      *
  414.      * @param array $blog Current site.
  415.      */
  416.     public function column_plugins( $blog ) {
  417.         if ( has_filter( 'wpmublogsaction' ) ) {
  418.             /**
  419.              * Fires inside the auxiliary 'Actions' column of the Sites list table.
  420.              *
  421.              * By default this column is hidden unless something is hooked to the action.
  422.              *
  423.              * @since MU (3.0.0)
  424.              *
  425.              * @param int $blog_id The site ID.
  426.              */
  427.             do_action( 'wpmublogsaction', $blog['blog_id'] );
  428.         }
  429.     }
  430.  
  431.     /**
  432.      * Handles output for the default column.
  433.      *
  434.      * @since 4.3.0
  435.      *
  436.      * @param array  $blog        Current site.
  437.      * @param string $column_name Current column name.
  438.      */
  439.     public function column_default( $blog, $column_name ) {
  440.         /**
  441.          * Fires for each registered custom column in the Sites list table.
  442.          *
  443.          * @since 3.1.0
  444.          *
  445.          * @param string $column_name The name of the column to display.
  446.          * @param int    $blog_id     The site ID.
  447.          */
  448.         do_action( 'manage_sites_custom_column', $column_name, $blog['blog_id'] );
  449.     }
  450.  
  451.     /**
  452.      *
  453.      * @global string $mode
  454.      */
  455.     public function display_rows() {
  456.         foreach ( $this->items as $blog ) {
  457.             $blog = $blog->to_array();
  458.             $class = '';
  459.             reset( $this->status_list );
  460.  
  461.             foreach ( $this->status_list as $status => $col ) {
  462.                 if ( $blog[ $status ] == 1 ) {
  463.                     $class = " class='{$col[0]}'";
  464.                 }
  465.             }
  466.  
  467.             echo "<tr{$class}>";
  468.  
  469.             $this->single_row_columns( $blog );
  470.  
  471.             echo '</tr>';
  472.         }
  473.     }
  474.  
  475.     /**
  476.      * Gets the name of the default primary column.
  477.      *
  478.      * @since 4.3.0
  479.      *
  480.      * @return string Name of the default primary column, in this case, 'blogname'.
  481.      */
  482.     protected function get_default_primary_column_name() {
  483.         return 'blogname';
  484.     }
  485.  
  486.     /**
  487.      * Generates and displays row action links.
  488.      *
  489.      * @since 4.3.0
  490.      *
  491.      * @param object $blog        Site being acted upon.
  492.      * @param string $column_name Current column name.
  493.      * @param string $primary     Primary column name.
  494.      * @return string Row actions output.
  495.      */
  496.     protected function handle_row_actions( $blog, $column_name, $primary ) {
  497.         if ( $primary !== $column_name ) {
  498.             return;
  499.         }
  500.  
  501.         $blogname = untrailingslashit( $blog['domain'] . $blog['path'] );
  502.  
  503.         // Preordered.
  504.         $actions = array(
  505.             'edit' => '', 'backend' => '',
  506.             'activate' => '', 'deactivate' => '',
  507.             'archive' => '', 'unarchive' => '',
  508.             'spam' => '', 'unspam' => '',
  509.             'delete' => '',
  510.             'visit' => '',
  511.         );
  512.  
  513.         $actions['edit']    = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $blog['blog_id'] ) ) . '">' . __( 'Edit' ) . '</a>';
  514.         $actions['backend']    = "<a href='" . esc_url( get_admin_url( $blog['blog_id'] ) ) . "' class='edit'>" . __( 'Dashboard' ) . '</a>';
  515.         if ( get_network()->site_id != $blog['blog_id'] ) {
  516.             if ( $blog['deleted'] == '1' ) {
  517.                 $actions['activate']   = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=activateblog&id=' . $blog['blog_id'] ), 'activateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Activate' ) . '</a>';
  518.             } else {
  519.                 $actions['deactivate'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=deactivateblog&id=' . $blog['blog_id'] ), 'deactivateblog_' . $blog['blog_id'] ) ) . '">' . __( 'Deactivate' ) . '</a>';
  520.             }
  521.  
  522.             if ( $blog['archived'] == '1' ) {
  523.                 $actions['unarchive'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=unarchiveblog&id=' . $blog['blog_id'] ), 'unarchiveblog_' . $blog['blog_id'] ) ) . '">' . __( 'Unarchive' ) . '</a>';
  524.             } else {
  525.                 $actions['archive']   = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=archiveblog&id=' . $blog['blog_id'] ), 'archiveblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Archive', 'verb; site' ) . '</a>';
  526.             }
  527.  
  528.             if ( $blog['spam'] == '1' ) {
  529.                 $actions['unspam'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=unspamblog&id=' . $blog['blog_id'] ), 'unspamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Not Spam', 'site' ) . '</a>';
  530.             } else {
  531.                 $actions['spam']   = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=spamblog&id=' . $blog['blog_id'] ), 'spamblog_' . $blog['blog_id'] ) ) . '">' . _x( 'Spam', 'site' ) . '</a>';
  532.             }
  533.  
  534.             if ( current_user_can( 'delete_site', $blog['blog_id'] ) ) {
  535.                 $actions['delete'] = '<a href="' . esc_url( wp_nonce_url( network_admin_url( 'sites.php?action=confirm&action2=deleteblog&id=' . $blog['blog_id'] ), 'deleteblog_' . $blog['blog_id'] ) ) . '">' . __( 'Delete' ) . '</a>';
  536.             }
  537.         }
  538.  
  539.         $actions['visit']    = "<a href='" . esc_url( get_home_url( $blog['blog_id'], '/' ) ) . "' rel='bookmark'>" . __( 'Visit' ) . '</a>';
  540.  
  541.         /**
  542.          * Filters the action links displayed for each site in the Sites list table.
  543.          *
  544.          * The 'Edit', 'Dashboard', 'Delete', and 'Visit' links are displayed by
  545.          * default for each site. The site's status determines whether to show the
  546.          * 'Activate' or 'Deactivate' link, 'Unarchive' or 'Archive' links, and
  547.          * 'Not Spam' or 'Spam' link for each site.
  548.          *
  549.          * @since 3.1.0
  550.          *
  551.          * @param array  $actions  An array of action links to be displayed.
  552.          * @param int    $blog_id  The site ID.
  553.          * @param string $blogname Site path, formatted depending on whether it is a sub-domain
  554.          *                         or subdirectory multisite installation.
  555.          */
  556.         $actions = apply_filters( 'manage_sites_action_links', array_filter( $actions ), $blog['blog_id'], $blogname );
  557.         return $this->row_actions( $actions );
  558.     }
  559. }
  560.