[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * List Table API: WP_Theme_Install_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 themes to install in a list table.
  12   *
  13   * @since 3.1.0
  14   * @access private
  15   *
  16   * @see WP_Themes_List_Table
  17   */
  18  class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
  19  
  20      public $features = array();
  21  
  22      /**
  23       * @return bool
  24       */
  25  	public function ajax_user_can() {
  26          return current_user_can( 'install_themes' );
  27      }
  28  
  29      /**
  30       * @global array  $tabs
  31       * @global string $tab
  32       * @global int    $paged
  33       * @global string $type
  34       * @global array  $theme_field_defaults
  35       */
  36  	public function prepare_items() {
  37          include( ABSPATH . 'wp-admin/includes/theme-install.php' );
  38  
  39          global $tabs, $tab, $paged, $type, $theme_field_defaults;
  40          wp_reset_vars( array( 'tab' ) );
  41  
  42          $search_terms  = array();
  43          $search_string = '';
  44          if ( ! empty( $_REQUEST['s'] ) ) {
  45              $search_string = strtolower( wp_unslash( $_REQUEST['s'] ) );
  46              $search_terms  = array_unique( array_filter( array_map( 'trim', explode( ',', $search_string ) ) ) );
  47          }
  48  
  49          if ( ! empty( $_REQUEST['features'] ) ) {
  50              $this->features = $_REQUEST['features'];
  51          }
  52  
  53          $paged = $this->get_pagenum();
  54  
  55          $per_page = 36;
  56  
  57          // These are the tabs which are shown on the page,
  58          $tabs              = array();
  59          $tabs['dashboard'] = __( 'Search' );
  60          if ( 'search' === $tab ) {
  61              $tabs['search'] = __( 'Search Results' );
  62          }
  63          $tabs['upload']   = __( 'Upload' );
  64          $tabs['featured'] = _x( 'Featured', 'themes' );
  65          //$tabs['popular']  = _x( 'Popular', 'themes' );
  66          $tabs['new']     = _x( 'Latest', 'themes' );
  67          $tabs['updated'] = _x( 'Recently Updated', 'themes' );
  68  
  69          $nonmenu_tabs = array( 'theme-information' ); // Valid actions to perform which do not have a Menu item.
  70  
  71          /** This filter is documented in wp-admin/theme-install.php */
  72          $tabs = apply_filters( 'install_themes_tabs', $tabs );
  73  
  74          /**
  75           * Filters tabs not associated with a menu item on the Install Themes screen.
  76           *
  77           * @since 2.8.0
  78           *
  79           * @param string[] $nonmenu_tabs The tabs that don't have a menu item on
  80           *                               the Install Themes screen.
  81           */
  82          $nonmenu_tabs = apply_filters( 'install_themes_nonmenu_tabs', $nonmenu_tabs );
  83  
  84          // If a non-valid menu tab has been selected, And it's not a non-menu action.
  85          if ( empty( $tab ) || ( ! isset( $tabs[ $tab ] ) && ! in_array( $tab, (array) $nonmenu_tabs ) ) ) {
  86              $tab = key( $tabs );
  87          }
  88  
  89          $args = array(
  90              'page'     => $paged,
  91              'per_page' => $per_page,
  92              'fields'   => $theme_field_defaults,
  93          );
  94  
  95          switch ( $tab ) {
  96              case 'search':
  97                  $type = isset( $_REQUEST['type'] ) ? wp_unslash( $_REQUEST['type'] ) : 'term';
  98                  switch ( $type ) {
  99                      case 'tag':
 100                          $args['tag'] = array_map( 'sanitize_key', $search_terms );
 101                          break;
 102                      case 'term':
 103                          $args['search'] = $search_string;
 104                          break;
 105                      case 'author':
 106                          $args['author'] = $search_string;
 107                          break;
 108                  }
 109  
 110                  if ( ! empty( $this->features ) ) {
 111                      $args['tag']      = $this->features;
 112                      $_REQUEST['s']    = implode( ',', $this->features );
 113                      $_REQUEST['type'] = 'tag';
 114                  }
 115  
 116                  add_action( 'install_themes_table_header', 'install_theme_search_form', 10, 0 );
 117                  break;
 118  
 119              case 'featured':
 120                  // case 'popular':
 121              case 'new':
 122              case 'updated':
 123                  $args['browse'] = $tab;
 124                  break;
 125  
 126              default:
 127                  $args = false;
 128                  break;
 129          }
 130  
 131          /**
 132           * Filters API request arguments for each Install Themes screen tab.
 133           *
 134           * The dynamic portion of the hook name, `$tab`, refers to the theme install
 135           * tabs. Default tabs are 'dashboard', 'search', 'upload', 'featured',
 136           * 'new', and 'updated'.
 137           *
 138           * @since 3.7.0
 139           *
 140           * @param array $args An array of themes API arguments.
 141           */
 142          $args = apply_filters( "install_themes_table_api_args_{$tab}", $args );
 143  
 144          if ( ! $args ) {
 145              return;
 146          }
 147  
 148          $api = themes_api( 'query_themes', $args );
 149  
 150          if ( is_wp_error( $api ) ) {
 151              wp_die( $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try Again' ) . '</a>' );
 152          }
 153  
 154          $this->items = $api->themes;
 155  
 156          $this->set_pagination_args(
 157              array(
 158                  'total_items'     => $api->info['results'],
 159                  'per_page'        => $args['per_page'],
 160                  'infinite_scroll' => true,
 161              )
 162          );
 163      }
 164  
 165      /**
 166       */
 167  	public function no_items() {
 168          _e( 'No themes match your request.' );
 169      }
 170  
 171      /**
 172       * @global array $tabs
 173       * @global string $tab
 174       * @return array
 175       */
 176  	protected function get_views() {
 177          global $tabs, $tab;
 178  
 179          $display_tabs = array();
 180          foreach ( (array) $tabs as $action => $text ) {
 181              $current_link_attributes                    = ( $action === $tab ) ? ' class="current" aria-current="page"' : '';
 182              $href                                       = self_admin_url( 'theme-install.php?tab=' . $action );
 183              $display_tabs[ 'theme-install-' . $action ] = "<a href='$href'$current_link_attributes>$text</a>";
 184          }
 185  
 186          return $display_tabs;
 187      }
 188  
 189      /**
 190       * Displays the theme install table.
 191       *
 192       * Overrides the parent display() method to provide a different container.
 193       *
 194       * @since 3.1.0
 195       */
 196  	public function display() {
 197          wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
 198          ?>
 199          <div class="tablenav top themes">
 200              <div class="alignleft actions">
 201                  <?php
 202                  /**
 203                   * Fires in the Install Themes list table header.
 204                   *
 205                   * @since 2.8.0
 206                   */
 207                  do_action( 'install_themes_table_header' );
 208                  ?>
 209              </div>
 210              <?php $this->pagination( 'top' ); ?>
 211              <br class="clear" />
 212          </div>
 213  
 214          <div id="availablethemes">
 215              <?php $this->display_rows_or_placeholder(); ?>
 216          </div>
 217  
 218          <?php
 219          $this->tablenav( 'bottom' );
 220      }
 221  
 222      /**
 223       */
 224  	public function display_rows() {
 225          $themes = $this->items;
 226          foreach ( $themes as $theme ) {
 227              ?>
 228                  <div class="available-theme installable-theme">
 229                  <?php
 230                      $this->single_row( $theme );
 231                  ?>
 232                  </div>
 233              <?php
 234          } // end foreach $theme_names
 235  
 236          $this->theme_installer();
 237      }
 238  
 239      /**
 240       * Prints a theme from the WordPress.org API.
 241       *
 242       * @since 3.1.0
 243       *
 244       * @global array $themes_allowedtags
 245       *
 246       * @param object $theme {
 247       *     An object that contains theme data returned by the WordPress.org API.
 248       *
 249       *     @type string $name           Theme name, e.g. 'Twenty Twenty'.
 250       *     @type string $slug           Theme slug, e.g. 'twentytwenty'.
 251       *     @type string $version        Theme version, e.g. '1.1'.
 252       *     @type string $author         Theme author username, e.g. 'melchoyce'.
 253       *     @type string $preview_url    Preview URL, e.g. 'http://2020.wordpress.net/'.
 254       *     @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentytwenty/'.
 255       *     @type float  $rating         Rating score.
 256       *     @type int    $num_ratings    The number of ratings.
 257       *     @type string $homepage       Theme homepage, e.g. 'https://wordpress.org/themes/twentytwenty/'.
 258       *     @type string $description    Theme description.
 259       *     @type string $download_link  Theme ZIP download URL.
 260       * }
 261       */
 262  	public function single_row( $theme ) {
 263          global $themes_allowedtags;
 264  
 265          if ( empty( $theme ) ) {
 266              return;
 267          }
 268  
 269          $name   = wp_kses( $theme->name, $themes_allowedtags );
 270          $author = wp_kses( $theme->author, $themes_allowedtags );
 271  
 272          /* translators: %s: Theme name. */
 273          $preview_title = sprintf( __( 'Preview &#8220;%s&#8221;' ), $name );
 274          $preview_url   = add_query_arg(
 275              array(
 276                  'tab'   => 'theme-information',
 277                  'theme' => $theme->slug,
 278              ),
 279              self_admin_url( 'theme-install.php' )
 280          );
 281  
 282          $actions = array();
 283  
 284          $install_url = add_query_arg(
 285              array(
 286                  'action' => 'install-theme',
 287                  'theme'  => $theme->slug,
 288              ),
 289              self_admin_url( 'update.php' )
 290          );
 291  
 292          $update_url = add_query_arg(
 293              array(
 294                  'action' => 'upgrade-theme',
 295                  'theme'  => $theme->slug,
 296              ),
 297              self_admin_url( 'update.php' )
 298          );
 299  
 300          $status = $this->_get_theme_status( $theme );
 301  
 302          switch ( $status ) {
 303              case 'update_available':
 304                  $actions[] = sprintf(
 305                      '<a class="install-now" href="%s" title="%s">%s</a>',
 306                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 307                      /* translators: %s: Theme version. */
 308                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 309                      __( 'Update' )
 310                  );
 311                  break;
 312              case 'newer_installed':
 313              case 'latest_installed':
 314                  $actions[] = sprintf(
 315                      '<span class="install-now" title="%s">%s</span>',
 316                      esc_attr__( 'This theme is already installed and is up to date' ),
 317                      _x( 'Installed', 'theme' )
 318                  );
 319                  break;
 320              case 'install':
 321              default:
 322                  $actions[] = sprintf(
 323                      '<a class="install-now" href="%s" title="%s">%s</a>',
 324                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 325                      /* translators: %s: Theme name. */
 326                      esc_attr( sprintf( __( 'Install %s' ), $name ) ),
 327                      __( 'Install Now' )
 328                  );
 329                  break;
 330          }
 331  
 332          $actions[] = sprintf(
 333              '<a class="install-theme-preview" href="%s" title="%s">%s</a>',
 334              esc_url( $preview_url ),
 335              /* translators: %s: Theme name. */
 336              esc_attr( sprintf( __( 'Preview %s' ), $name ) ),
 337              __( 'Preview' )
 338          );
 339  
 340          /**
 341           * Filters the install action links for a theme in the Install Themes list table.
 342           *
 343           * @since 3.4.0
 344           *
 345           * @param string[] $actions An array of theme action links. Defaults are
 346           *                          links to Install Now, Preview, and Details.
 347           * @param WP_Theme $theme   Theme object.
 348           */
 349          $actions = apply_filters( 'theme_install_actions', $actions, $theme );
 350  
 351          ?>
 352          <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
 353              <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" alt="" />
 354          </a>
 355  
 356          <h3><?php echo $name; ?></h3>
 357          <div class="theme-author">
 358          <?php
 359              /* translators: %s: Theme author. */
 360              printf( __( 'By %s' ), $author );
 361          ?>
 362          </div>
 363  
 364          <div class="action-links">
 365              <ul>
 366                  <?php foreach ( $actions as $action ) : ?>
 367                      <li><?php echo $action; ?></li>
 368                  <?php endforeach; ?>
 369                  <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
 370              </ul>
 371          </div>
 372  
 373          <?php
 374          $this->install_theme_info( $theme );
 375      }
 376  
 377      /**
 378       * Prints the wrapper for the theme installer.
 379       */
 380  	public function theme_installer() {
 381          ?>
 382          <div id="theme-installer" class="wp-full-overlay expanded">
 383              <div class="wp-full-overlay-sidebar">
 384                  <div class="wp-full-overlay-header">
 385                      <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
 386                      <span class="theme-install"></span>
 387                  </div>
 388                  <div class="wp-full-overlay-sidebar-content">
 389                      <div class="install-theme-info"></div>
 390                  </div>
 391                  <div class="wp-full-overlay-footer">
 392                      <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
 393                          <span class="collapse-sidebar-arrow"></span>
 394                          <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
 395                      </button>
 396                  </div>
 397              </div>
 398              <div class="wp-full-overlay-main"></div>
 399          </div>
 400          <?php
 401      }
 402  
 403      /**
 404       * Prints the wrapper for the theme installer with a provided theme's data.
 405       * Used to make the theme installer work for no-js.
 406       *
 407       * @param object $theme - A WordPress.org Theme API object.
 408       */
 409  	public function theme_installer_single( $theme ) {
 410          ?>
 411          <div id="theme-installer" class="wp-full-overlay single-theme">
 412              <div class="wp-full-overlay-sidebar">
 413                  <?php $this->install_theme_info( $theme ); ?>
 414              </div>
 415              <div class="wp-full-overlay-main">
 416                  <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
 417              </div>
 418          </div>
 419          <?php
 420      }
 421  
 422      /**
 423       * Prints the info for a theme (to be used in the theme installer modal).
 424       *
 425       * @global array $themes_allowedtags
 426       *
 427       * @param object $theme - A WordPress.org Theme API object.
 428       */
 429  	public function install_theme_info( $theme ) {
 430          global $themes_allowedtags;
 431  
 432          if ( empty( $theme ) ) {
 433              return;
 434          }
 435  
 436          $name   = wp_kses( $theme->name, $themes_allowedtags );
 437          $author = wp_kses( $theme->author, $themes_allowedtags );
 438  
 439          $install_url = add_query_arg(
 440              array(
 441                  'action' => 'install-theme',
 442                  'theme'  => $theme->slug,
 443              ),
 444              self_admin_url( 'update.php' )
 445          );
 446  
 447          $update_url = add_query_arg(
 448              array(
 449                  'action' => 'upgrade-theme',
 450                  'theme'  => $theme->slug,
 451              ),
 452              self_admin_url( 'update.php' )
 453          );
 454  
 455          $status = $this->_get_theme_status( $theme );
 456  
 457          ?>
 458          <div class="install-theme-info">
 459          <?php
 460          switch ( $status ) {
 461              case 'update_available':
 462                  printf(
 463                      '<a class="theme-install button button-primary" href="%s" title="%s">%s</a>',
 464                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 465                      /* translators: %s: Theme version. */
 466                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 467                      __( 'Update' )
 468                  );
 469                  break;
 470              case 'newer_installed':
 471              case 'latest_installed':
 472                  printf(
 473                      '<span class="theme-install" title="%s">%s</span>',
 474                      esc_attr__( 'This theme is already installed and is up to date' ),
 475                      _x( 'Installed', 'theme' )
 476                  );
 477                  break;
 478              case 'install':
 479              default:
 480                  printf(
 481                      '<a class="theme-install button button-primary" href="%s">%s</a>',
 482                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 483                      __( 'Install' )
 484                  );
 485                  break;
 486          }
 487          ?>
 488              <h3 class="theme-name"><?php echo $name; ?></h3>
 489              <span class="theme-by">
 490              <?php
 491                  /* translators: %s: Theme author. */
 492                  printf( __( 'By %s' ), $author );
 493              ?>
 494              </span>
 495              <?php if ( isset( $theme->screenshot_url ) ) : ?>
 496                  <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" alt="" />
 497              <?php endif; ?>
 498              <div class="theme-details">
 499                  <?php
 500                  wp_star_rating(
 501                      array(
 502                          'rating' => $theme->rating,
 503                          'type'   => 'percent',
 504                          'number' => $theme->num_ratings,
 505                      )
 506                  );
 507                  ?>
 508                  <div class="theme-version">
 509                      <strong><?php _e( 'Version:' ); ?> </strong>
 510                      <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
 511                  </div>
 512                  <div class="theme-description">
 513                      <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
 514                  </div>
 515              </div>
 516              <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
 517          </div>
 518          <?php
 519      }
 520  
 521      /**
 522       * Send required variables to JavaScript land
 523       *
 524       * @since 3.4.0
 525       *
 526       * @global string $tab  Current tab within Themes->Install screen
 527       * @global string $type Type of search.
 528       *
 529       * @param array $extra_args Unused.
 530       */
 531  	public function _js_vars( $extra_args = array() ) {
 532          global $tab, $type;
 533          parent::_js_vars( compact( 'tab', 'type' ) );
 534      }
 535  
 536      /**
 537       * Check to see if the theme is already installed.
 538       *
 539       * @since 3.4.0
 540       *
 541       * @param object $theme - A WordPress.org Theme API object.
 542       * @return string Theme status.
 543       */
 544  	private function _get_theme_status( $theme ) {
 545          $status = 'install';
 546  
 547          $installed_theme = wp_get_theme( $theme->slug );
 548          if ( $installed_theme->exists() ) {
 549              if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
 550                  $status = 'latest_installed';
 551              } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
 552                  $status = 'newer_installed';
 553              } else {
 554                  $status = 'update_available';
 555              }
 556          }
 557  
 558          return $status;
 559      }
 560  }


Generated: Fri Nov 22 01:00:04 2019 Cross-referenced by PHPXref 0.7.1