[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * List Table API: WP_Themes_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 installed themes in a list table.
  12   *
  13   * @since 3.1.0
  14   * @access private
  15   *
  16   * @see WP_List_Table
  17   */
  18  class WP_Themes_List_Table extends WP_List_Table {
  19  
  20      protected $search_terms = array();
  21      public $features        = array();
  22  
  23      /**
  24       * Constructor.
  25       *
  26       * @since 3.1.0
  27       *
  28       * @see WP_List_Table::__construct() for more information on default arguments.
  29       *
  30       * @param array $args An associative array of arguments.
  31       */
  32  	public function __construct( $args = array() ) {
  33          parent::__construct(
  34              array(
  35                  'ajax'   => true,
  36                  'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
  37              )
  38          );
  39      }
  40  
  41      /**
  42       * @return bool
  43       */
  44  	public function ajax_user_can() {
  45          // Do not check edit_theme_options here. Ajax calls for available themes require switch_themes.
  46          return current_user_can( 'switch_themes' );
  47      }
  48  
  49      /**
  50       */
  51  	public function prepare_items() {
  52          $themes = wp_get_themes( array( 'allowed' => true ) );
  53  
  54          if ( ! empty( $_REQUEST['s'] ) ) {
  55              $this->search_terms = array_unique( array_filter( array_map( 'trim', explode( ',', strtolower( wp_unslash( $_REQUEST['s'] ) ) ) ) ) );
  56          }
  57  
  58          if ( ! empty( $_REQUEST['features'] ) ) {
  59              $this->features = $_REQUEST['features'];
  60          }
  61  
  62          if ( $this->search_terms || $this->features ) {
  63              foreach ( $themes as $key => $theme ) {
  64                  if ( ! $this->search_theme( $theme ) ) {
  65                      unset( $themes[ $key ] );
  66                  }
  67              }
  68          }
  69  
  70          unset( $themes[ get_option( 'stylesheet' ) ] );
  71          WP_Theme::sort_by_name( $themes );
  72  
  73          $per_page = 36;
  74          $page     = $this->get_pagenum();
  75  
  76          $start = ( $page - 1 ) * $per_page;
  77  
  78          $this->items = array_slice( $themes, $start, $per_page, true );
  79  
  80          $this->set_pagination_args(
  81              array(
  82                  'total_items'     => count( $themes ),
  83                  'per_page'        => $per_page,
  84                  'infinite_scroll' => true,
  85              )
  86          );
  87      }
  88  
  89      /**
  90       */
  91  	public function no_items() {
  92          if ( $this->search_terms || $this->features ) {
  93              _e( 'No items found.' );
  94              return;
  95          }
  96  
  97          $blog_id = get_current_blog_id();
  98          if ( is_multisite() ) {
  99              if ( current_user_can( 'install_themes' ) && current_user_can( 'manage_network_themes' ) ) {
 100                  printf(
 101                      /* translators: 1: URL to Themes tab on Edit Site screen, 2: URL to Add Themes screen. */
 102                      __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%1$s">enable</a> or <a href="%2$s">install</a> more themes.' ),
 103                      network_admin_url( 'site-themes.php?id=' . $blog_id ),
 104                      network_admin_url( 'theme-install.php' )
 105                  );
 106  
 107                  return;
 108              } elseif ( current_user_can( 'manage_network_themes' ) ) {
 109                  printf(
 110                      /* translators: %s: URL to Themes tab on Edit Site screen. */
 111                      __( 'You only have one theme enabled for this site right now. Visit the Network Admin to <a href="%s">enable</a> more themes.' ),
 112                      network_admin_url( 'site-themes.php?id=' . $blog_id )
 113                  );
 114  
 115                  return;
 116              }
 117              // Else, fallthrough. install_themes doesn't help if you can't enable it.
 118          } else {
 119              if ( current_user_can( 'install_themes' ) ) {
 120                  printf(
 121                      /* translators: %s: URL to Add Themes screen. */
 122                      __( 'You only have one theme installed right now. Live a little! You can choose from over 1,000 free themes in the WordPress Theme Directory at any time: just click on the <a href="%s">Install Themes</a> tab above.' ),
 123                      admin_url( 'theme-install.php' )
 124                  );
 125  
 126                  return;
 127              }
 128          }
 129          // Fallthrough.
 130          printf(
 131              /* translators: %s: Network title. */
 132              __( 'Only the current theme is available to you. Contact the %s administrator for information about accessing additional themes.' ),
 133              get_site_option( 'site_name' )
 134          );
 135      }
 136  
 137      /**
 138       * @param string $which
 139       */
 140  	public function tablenav( $which = 'top' ) {
 141          if ( $this->get_pagination_arg( 'total_pages' ) <= 1 ) {
 142              return;
 143          }
 144          ?>
 145          <div class="tablenav themes <?php echo $which; ?>">
 146              <?php $this->pagination( $which ); ?>
 147              <span class="spinner"></span>
 148              <br class="clear" />
 149          </div>
 150          <?php
 151      }
 152  
 153      /**
 154       */
 155  	public function display() {
 156          wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
 157          ?>
 158          <?php $this->tablenav( 'top' ); ?>
 159  
 160          <div id="availablethemes">
 161              <?php $this->display_rows_or_placeholder(); ?>
 162          </div>
 163  
 164          <?php $this->tablenav( 'bottom' ); ?>
 165          <?php
 166      }
 167  
 168      /**
 169       * @return array
 170       */
 171  	public function get_columns() {
 172          return array();
 173      }
 174  
 175      /**
 176       */
 177  	public function display_rows_or_placeholder() {
 178          if ( $this->has_items() ) {
 179              $this->display_rows();
 180          } else {
 181              echo '<div class="no-items">';
 182              $this->no_items();
 183              echo '</div>';
 184          }
 185      }
 186  
 187      /**
 188       */
 189  	public function display_rows() {
 190          $themes = $this->items;
 191  
 192          foreach ( $themes as $theme ) :
 193              ?>
 194              <div class="available-theme">
 195              <?php
 196  
 197              $template   = $theme->get_template();
 198              $stylesheet = $theme->get_stylesheet();
 199              $title      = $theme->display( 'Name' );
 200              $version    = $theme->display( 'Version' );
 201              $author     = $theme->display( 'Author' );
 202  
 203              $activate_link = wp_nonce_url( 'themes.php?action=activate&amp;template=' . urlencode( $template ) . '&amp;stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
 204  
 205              $actions             = array();
 206              $actions['activate'] = sprintf(
 207                  '<a href="%s" class="activatelink" title="%s">%s</a>',
 208                  $activate_link,
 209                  /* translators: %s: Theme name. */
 210                  esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ),
 211                  __( 'Activate' )
 212              );
 213  
 214              if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
 215                  $actions['preview'] .= sprintf(
 216                      '<a href="%s" class="load-customize hide-if-no-customize">%s</a>',
 217                      wp_customize_url( $stylesheet ),
 218                      __( 'Live Preview' )
 219                  );
 220              }
 221  
 222              if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) {
 223                  $actions['delete'] = sprintf(
 224                      '<a class="submitdelete deletion" href="%s" onclick="return confirm( \'%s\' );">%s</a>',
 225                      wp_nonce_url( 'themes.php?action=delete&amp;stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ),
 226                      /* translators: %s: Theme name. */
 227                      esc_js( sprintf( __( "You are about to delete this theme '%s'\n  'Cancel' to stop, 'OK' to delete." ), $title ) ),
 228                      __( 'Delete' )
 229                  );
 230              }
 231  
 232              /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
 233              $actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' );
 234  
 235              /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */
 236              $actions       = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' );
 237              $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : '';
 238              unset( $actions['delete'] );
 239  
 240              $screenshot = $theme->get_screenshot();
 241              ?>
 242  
 243              <span class="screenshot hide-if-customize">
 244                  <?php if ( $screenshot ) : ?>
 245                      <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
 246                  <?php endif; ?>
 247              </span>
 248              <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize">
 249                  <?php if ( $screenshot ) : ?>
 250                      <img src="<?php echo esc_url( $screenshot ); ?>" alt="" />
 251                  <?php endif; ?>
 252              </a>
 253  
 254              <h3><?php echo $title; ?></h3>
 255              <div class="theme-author">
 256                  <?php
 257                      /* translators: %s: Theme author. */
 258                      printf( __( 'By %s' ), $author );
 259                  ?>
 260              </div>
 261              <div class="action-links">
 262                  <ul>
 263                      <?php foreach ( $actions as $action ) : ?>
 264                          <li><?php echo $action; ?></li>
 265                      <?php endforeach; ?>
 266                      <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
 267                  </ul>
 268                  <?php echo $delete_action; ?>
 269  
 270                  <?php theme_update_available( $theme ); ?>
 271              </div>
 272  
 273              <div class="themedetaildiv hide-if-js">
 274                  <p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p>
 275                  <p><?php echo $theme->display( 'Description' ); ?></p>
 276                  <?php
 277                  if ( $theme->parent() ) {
 278                      printf(
 279                          /* translators: %s: Link to documentation on child themes. */
 280                          ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>',
 281                          __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ),
 282                          $theme->parent()->display( 'Name' )
 283                      );
 284                  }
 285                  ?>
 286              </div>
 287  
 288              </div>
 289              <?php
 290          endforeach;
 291      }
 292  
 293      /**
 294       * @param WP_Theme $theme
 295       * @return bool
 296       */
 297  	public function search_theme( $theme ) {
 298          // Search the features
 299          foreach ( $this->features as $word ) {
 300              if ( ! in_array( $word, $theme->get( 'Tags' ) ) ) {
 301                  return false;
 302              }
 303          }
 304  
 305          // Match all phrases
 306          foreach ( $this->search_terms as $word ) {
 307              if ( in_array( $word, $theme->get( 'Tags' ) ) ) {
 308                  continue;
 309              }
 310  
 311              foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) {
 312                  // Don't mark up; Do translate.
 313                  if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) {
 314                      continue 2;
 315                  }
 316              }
 317  
 318              if ( false !== stripos( $theme->get_stylesheet(), $word ) ) {
 319                  continue;
 320              }
 321  
 322              if ( false !== stripos( $theme->get_template(), $word ) ) {
 323                  continue;
 324              }
 325  
 326              return false;
 327          }
 328  
 329          return true;
 330      }
 331  
 332      /**
 333       * Send required variables to JavaScript land
 334       *
 335       * @since 3.4.0
 336       *
 337       * @param array $extra_args
 338       */
 339  	public function _js_vars( $extra_args = array() ) {
 340          $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
 341  
 342          $args = array(
 343              'search'      => $search_string,
 344              'features'    => $this->features,
 345              'paged'       => $this->get_pagenum(),
 346              'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1,
 347          );
 348  
 349          if ( is_array( $extra_args ) ) {
 350              $args = array_merge( $args, $extra_args );
 351          }
 352  
 353          printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) );
 354          parent::_js_vars();
 355      }
 356  }


Generated: Sun Sep 15 01:00:03 2019 Cross-referenced by PHPXref 0.7.1