[ 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       */
 191  	public function display() {
 192          wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
 193          ?>
 194          <div class="tablenav top themes">
 195              <div class="alignleft actions">
 196                  <?php
 197                  /**
 198                   * Fires in the Install Themes list table header.
 199                   *
 200                   * @since 2.8.0
 201                   */
 202                  do_action( 'install_themes_table_header' );
 203                  ?>
 204              </div>
 205              <?php $this->pagination( 'top' ); ?>
 206              <br class="clear" />
 207          </div>
 208  
 209          <div id="availablethemes">
 210              <?php $this->display_rows_or_placeholder(); ?>
 211          </div>
 212  
 213          <?php
 214          $this->tablenav( 'bottom' );
 215      }
 216  
 217      /**
 218       */
 219  	public function display_rows() {
 220          $themes = $this->items;
 221          foreach ( $themes as $theme ) {
 222              ?>
 223                  <div class="available-theme installable-theme">
 224                  <?php
 225                      $this->single_row( $theme );
 226                  ?>
 227                  </div>
 228              <?php
 229          } // end foreach $theme_names
 230  
 231          $this->theme_installer();
 232      }
 233  
 234      /**
 235       * Prints a theme from the WordPress.org API.
 236       *
 237       * @since 3.1.0
 238       *
 239       * @global array $themes_allowedtags
 240       *
 241       * @param object $theme {
 242       *     An object that contains theme data returned by the WordPress.org API.
 243       *
 244       *     @type string $name           Theme name, e.g. 'Twenty Nineteen'.
 245       *     @type string $slug           Theme slug, e.g. 'twentynineteen'.
 246       *     @type string $version        Theme version, e.g. '1.1'.
 247       *     @type string $author         Theme author username, e.g. 'melchoyce'.
 248       *     @type string $preview_url    Preview URL, e.g. 'http://2019.wordpress.net/'.
 249       *     @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentynineteen/'.
 250       *     @type float  $rating         Rating score.
 251       *     @type int    $num_ratings    The number of ratings.
 252       *     @type string $homepage       Theme homepage, e.g. 'https://wordpress.org/themes/twentynineteen/'.
 253       *     @type string $description    Theme description.
 254       *     @type string $download_link  Theme ZIP download URL.
 255       * }
 256       */
 257  	public function single_row( $theme ) {
 258          global $themes_allowedtags;
 259  
 260          if ( empty( $theme ) ) {
 261              return;
 262          }
 263  
 264          $name   = wp_kses( $theme->name, $themes_allowedtags );
 265          $author = wp_kses( $theme->author, $themes_allowedtags );
 266  
 267          /* translators: %s: theme name */
 268          $preview_title = sprintf( __( 'Preview &#8220;%s&#8221;' ), $name );
 269          $preview_url   = add_query_arg(
 270              array(
 271                  'tab'   => 'theme-information',
 272                  'theme' => $theme->slug,
 273              ),
 274              self_admin_url( 'theme-install.php' )
 275          );
 276  
 277          $actions = array();
 278  
 279          $install_url = add_query_arg(
 280              array(
 281                  'action' => 'install-theme',
 282                  'theme'  => $theme->slug,
 283              ),
 284              self_admin_url( 'update.php' )
 285          );
 286  
 287          $update_url = add_query_arg(
 288              array(
 289                  'action' => 'upgrade-theme',
 290                  'theme'  => $theme->slug,
 291              ),
 292              self_admin_url( 'update.php' )
 293          );
 294  
 295          $status = $this->_get_theme_status( $theme );
 296  
 297          switch ( $status ) {
 298              case 'update_available':
 299                  $actions[] = sprintf(
 300                      '<a class="install-now" href="%s" title="%s">%s</a>',
 301                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 302                      /* translators: %s: theme version */
 303                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 304                      __( 'Update' )
 305                  );
 306                  break;
 307              case 'newer_installed':
 308              case 'latest_installed':
 309                  $actions[] = sprintf(
 310                      '<span class="install-now" title="%s">%s</span>',
 311                      esc_attr__( 'This theme is already installed and is up to date' ),
 312                      _x( 'Installed', 'theme' )
 313                  );
 314                  break;
 315              case 'install':
 316              default:
 317                  $actions[] = sprintf(
 318                      '<a class="install-now" href="%s" title="%s">%s</a>',
 319                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 320                      /* translators: %s: theme name */
 321                      esc_attr( sprintf( __( 'Install %s' ), $name ) ),
 322                      __( 'Install Now' )
 323                  );
 324                  break;
 325          }
 326  
 327          $actions[] = sprintf(
 328              '<a class="install-theme-preview" href="%s" title="%s">%s</a>',
 329              esc_url( $preview_url ),
 330              /* translators: %s: theme name */
 331              esc_attr( sprintf( __( 'Preview %s' ), $name ) ),
 332              __( 'Preview' )
 333          );
 334  
 335          /**
 336           * Filters the install action links for a theme in the Install Themes list table.
 337           *
 338           * @since 3.4.0
 339           *
 340           * @param string[] $actions An array of theme action links. Defaults are
 341           *                          links to Install Now, Preview, and Details.
 342           * @param WP_Theme $theme   Theme object.
 343           */
 344          $actions = apply_filters( 'theme_install_actions', $actions, $theme );
 345  
 346          ?>
 347          <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
 348              <img src="<?php echo esc_url( $theme->screenshot_url ); ?>" width="150" alt="" />
 349          </a>
 350  
 351          <h3><?php echo $name; ?></h3>
 352          <div class="theme-author">
 353          <?php
 354              /* translators: %s: theme author */
 355              printf( __( 'By %s' ), $author );
 356          ?>
 357          </div>
 358  
 359          <div class="action-links">
 360              <ul>
 361                  <?php foreach ( $actions as $action ) : ?>
 362                      <li><?php echo $action; ?></li>
 363                  <?php endforeach; ?>
 364                  <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
 365              </ul>
 366          </div>
 367  
 368          <?php
 369          $this->install_theme_info( $theme );
 370      }
 371  
 372      /**
 373       * Prints the wrapper for the theme installer.
 374       */
 375  	public function theme_installer() {
 376          ?>
 377          <div id="theme-installer" class="wp-full-overlay expanded">
 378              <div class="wp-full-overlay-sidebar">
 379                  <div class="wp-full-overlay-header">
 380                      <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
 381                      <span class="theme-install"></span>
 382                  </div>
 383                  <div class="wp-full-overlay-sidebar-content">
 384                      <div class="install-theme-info"></div>
 385                  </div>
 386                  <div class="wp-full-overlay-footer">
 387                      <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
 388                          <span class="collapse-sidebar-arrow"></span>
 389                          <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
 390                      </button>
 391                  </div>
 392              </div>
 393              <div class="wp-full-overlay-main"></div>
 394          </div>
 395          <?php
 396      }
 397  
 398      /**
 399       * Prints the wrapper for the theme installer with a provided theme's data.
 400       * Used to make the theme installer work for no-js.
 401       *
 402       * @param object $theme - A WordPress.org Theme API object.
 403       */
 404  	public function theme_installer_single( $theme ) {
 405          ?>
 406          <div id="theme-installer" class="wp-full-overlay single-theme">
 407              <div class="wp-full-overlay-sidebar">
 408                  <?php $this->install_theme_info( $theme ); ?>
 409              </div>
 410              <div class="wp-full-overlay-main">
 411                  <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
 412              </div>
 413          </div>
 414          <?php
 415      }
 416  
 417      /**
 418       * Prints the info for a theme (to be used in the theme installer modal).
 419       *
 420       * @global array $themes_allowedtags
 421       *
 422       * @param object $theme - A WordPress.org Theme API object.
 423       */
 424  	public function install_theme_info( $theme ) {
 425          global $themes_allowedtags;
 426  
 427          if ( empty( $theme ) ) {
 428              return;
 429          }
 430  
 431          $name   = wp_kses( $theme->name, $themes_allowedtags );
 432          $author = wp_kses( $theme->author, $themes_allowedtags );
 433  
 434          $install_url = add_query_arg(
 435              array(
 436                  'action' => 'install-theme',
 437                  'theme'  => $theme->slug,
 438              ),
 439              self_admin_url( 'update.php' )
 440          );
 441  
 442          $update_url = add_query_arg(
 443              array(
 444                  'action' => 'upgrade-theme',
 445                  'theme'  => $theme->slug,
 446              ),
 447              self_admin_url( 'update.php' )
 448          );
 449  
 450          $status = $this->_get_theme_status( $theme );
 451  
 452          ?>
 453          <div class="install-theme-info">
 454          <?php
 455          switch ( $status ) {
 456              case 'update_available':
 457                  printf(
 458                      '<a class="theme-install button button-primary" href="%s" title="%s">%s</a>',
 459                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 460                      /* translators: %s: theme version */
 461                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 462                      __( 'Update' )
 463                  );
 464                  break;
 465              case 'newer_installed':
 466              case 'latest_installed':
 467                  printf(
 468                      '<span class="theme-install" title="%s">%s</span>',
 469                      esc_attr__( 'This theme is already installed and is up to date' ),
 470                      _x( 'Installed', 'theme' )
 471                  );
 472                  break;
 473              case 'install':
 474              default:
 475                  printf(
 476                      '<a class="theme-install button button-primary" href="%s">%s</a>',
 477                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 478                      __( 'Install' )
 479                  );
 480                  break;
 481          }
 482          ?>
 483              <h3 class="theme-name"><?php echo $name; ?></h3>
 484              <span class="theme-by">
 485              <?php
 486                  /* translators: %s: theme author */
 487                  printf( __( 'By %s' ), $author );
 488              ?>
 489              </span>
 490              <?php if ( isset( $theme->screenshot_url ) ) : ?>
 491                  <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url ); ?>" alt="" />
 492              <?php endif; ?>
 493              <div class="theme-details">
 494                  <?php
 495                  wp_star_rating(
 496                      array(
 497                          'rating' => $theme->rating,
 498                          'type'   => 'percent',
 499                          'number' => $theme->num_ratings,
 500                      )
 501                  );
 502                  ?>
 503                  <div class="theme-version">
 504                      <strong><?php _e( 'Version:' ); ?> </strong>
 505                      <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
 506                  </div>
 507                  <div class="theme-description">
 508                      <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
 509                  </div>
 510              </div>
 511              <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
 512          </div>
 513          <?php
 514      }
 515  
 516      /**
 517       * Send required variables to JavaScript land
 518       *
 519       * @since 3.4.0
 520       *
 521       * @global string $tab  Current tab within Themes->Install screen
 522       * @global string $type Type of search.
 523       *
 524       * @param array $extra_args Unused.
 525       */
 526  	public function _js_vars( $extra_args = array() ) {
 527          global $tab, $type;
 528          parent::_js_vars( compact( 'tab', 'type' ) );
 529      }
 530  
 531      /**
 532       * Check to see if the theme is already installed.
 533       *
 534       * @since 3.4.0
 535       *
 536       * @param object $theme - A WordPress.org Theme API object.
 537       * @return string Theme status.
 538       */
 539  	private function _get_theme_status( $theme ) {
 540          $status = 'install';
 541  
 542          $installed_theme = wp_get_theme( $theme->slug );
 543          if ( $installed_theme->exists() ) {
 544              if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
 545                  $status = 'latest_installed';
 546              } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
 547                  $status = 'newer_installed';
 548              } else {
 549                  $status = 'update_available';
 550              }
 551          }
 552  
 553          return $status;
 554      }
 555  }


Generated: Sat Jul 20 01:00:03 2019 Cross-referenced by PHPXref 0.7.1