[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> class-wp-ms-sites-list-table.php (source)

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


Generated: Wed Sep 18 01:00:03 2019 Cross-referenced by PHPXref 0.7.1