[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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   *
  15   * @see WP_Themes_List_Table
  16   */
  17  class WP_Theme_Install_List_Table extends WP_Themes_List_Table {
  18  
  19      public $features = array();
  20  
  21      /**
  22       * @return bool
  23       */
  24  	public function ajax_user_can() {
  25          return current_user_can( 'install_themes' );
  26      }
  27  
  28      /**
  29       * @global array  $tabs
  30       * @global string $tab
  31       * @global int    $paged
  32       * @global string $type
  33       * @global array  $theme_field_defaults
  34       */
  35  	public function prepare_items() {
  36          require  ABSPATH . 'wp-admin/includes/theme-install.php';
  37  
  38          global $tabs, $tab, $paged, $type, $theme_field_defaults;
  39  
  40          $tab = ! empty( $_REQUEST['tab'] ) ? sanitize_text_field( $_REQUEST['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, true ) ) ) {
  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           * tab.
 136           *
 137           * Possible hook names include:
 138           *
 139           *  - `install_themes_table_api_args_dashboard`
 140           *  - `install_themes_table_api_args_featured`
 141           *  - `install_themes_table_api_args_new`
 142           *  - `install_themes_table_api_args_search`
 143           *  - `install_themes_table_api_args_updated`
 144           *  - `install_themes_table_api_args_upload`
 145           *
 146           * @since 3.7.0
 147           *
 148           * @param array|false $args Theme install API arguments.
 149           */
 150          $args = apply_filters( "install_themes_table_api_args_{$tab}", $args );
 151  
 152          if ( ! $args ) {
 153              return;
 154          }
 155  
 156          $api = themes_api( 'query_themes', $args );
 157  
 158          if ( is_wp_error( $api ) ) {
 159              wp_die( '<p>' . $api->get_error_message() . '</p> <p><a href="#" onclick="document.location.reload(); return false;">' . __( 'Try Again' ) . '</a></p>' );
 160          }
 161  
 162          $this->items = $api->themes;
 163  
 164          $this->set_pagination_args(
 165              array(
 166                  'total_items'     => $api->info['results'],
 167                  'per_page'        => $args['per_page'],
 168                  'infinite_scroll' => true,
 169              )
 170          );
 171      }
 172  
 173      /**
 174       */
 175  	public function no_items() {
 176          _e( 'No themes match your request.' );
 177      }
 178  
 179      /**
 180       * @global array $tabs
 181       * @global string $tab
 182       * @return array
 183       */
 184  	protected function get_views() {
 185          global $tabs, $tab;
 186  
 187          $display_tabs = array();
 188          foreach ( (array) $tabs as $action => $text ) {
 189              $display_tabs[ 'theme-install-' . $action ] = array(
 190                  'url'     => self_admin_url( 'theme-install.php?tab=' . $action ),
 191                  'label'   => $text,
 192                  'current' => $action === $tab,
 193              );
 194          }
 195  
 196          return $this->get_views_links( $display_tabs );
 197      }
 198  
 199      /**
 200       * Displays the theme install table.
 201       *
 202       * Overrides the parent display() method to provide a different container.
 203       *
 204       * @since 3.1.0
 205       */
 206  	public function display() {
 207          wp_nonce_field( 'fetch-list-' . get_class( $this ), '_ajax_fetch_list_nonce' );
 208          ?>
 209          <div class="tablenav top themes">
 210              <div class="alignleft actions">
 211                  <?php
 212                  /**
 213                   * Fires in the Install Themes list table header.
 214                   *
 215                   * @since 2.8.0
 216                   */
 217                  do_action( 'install_themes_table_header' );
 218                  ?>
 219              </div>
 220              <?php $this->pagination( 'top' ); ?>
 221              <br class="clear" />
 222          </div>
 223  
 224          <div id="availablethemes">
 225              <?php $this->display_rows_or_placeholder(); ?>
 226          </div>
 227  
 228          <?php
 229          $this->tablenav( 'bottom' );
 230      }
 231  
 232      /**
 233       */
 234  	public function display_rows() {
 235          $themes = $this->items;
 236          foreach ( $themes as $theme ) {
 237              ?>
 238                  <div class="available-theme installable-theme">
 239                  <?php
 240                      $this->single_row( $theme );
 241                  ?>
 242                  </div>
 243              <?php
 244          } // End foreach $theme_names.
 245  
 246          $this->theme_installer();
 247      }
 248  
 249      /**
 250       * Prints a theme from the WordPress.org API.
 251       *
 252       * @since 3.1.0
 253       *
 254       * @global array $themes_allowedtags
 255       *
 256       * @param stdClass $theme {
 257       *     An object that contains theme data returned by the WordPress.org API.
 258       *
 259       *     @type string $name           Theme name, e.g. 'Twenty Twenty-One'.
 260       *     @type string $slug           Theme slug, e.g. 'twentytwentyone'.
 261       *     @type string $version        Theme version, e.g. '1.1'.
 262       *     @type string $author         Theme author username, e.g. 'melchoyce'.
 263       *     @type string $preview_url    Preview URL, e.g. 'https://2021.wordpress.net/'.
 264       *     @type string $screenshot_url Screenshot URL, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
 265       *     @type float  $rating         Rating score.
 266       *     @type int    $num_ratings    The number of ratings.
 267       *     @type string $homepage       Theme homepage, e.g. 'https://wordpress.org/themes/twentytwentyone/'.
 268       *     @type string $description    Theme description.
 269       *     @type string $download_link  Theme ZIP download URL.
 270       * }
 271       */
 272  	public function single_row( $theme ) {
 273          global $themes_allowedtags;
 274  
 275          if ( empty( $theme ) ) {
 276              return;
 277          }
 278  
 279          $name   = wp_kses( $theme->name, $themes_allowedtags );
 280          $author = wp_kses( $theme->author, $themes_allowedtags );
 281  
 282          /* translators: %s: Theme name. */
 283          $preview_title = sprintf( __( 'Preview &#8220;%s&#8221;' ), $name );
 284          $preview_url   = add_query_arg(
 285              array(
 286                  'tab'   => 'theme-information',
 287                  'theme' => $theme->slug,
 288              ),
 289              self_admin_url( 'theme-install.php' )
 290          );
 291  
 292          $actions = array();
 293  
 294          $install_url = add_query_arg(
 295              array(
 296                  'action' => 'install-theme',
 297                  'theme'  => $theme->slug,
 298              ),
 299              self_admin_url( 'update.php' )
 300          );
 301  
 302          $update_url = add_query_arg(
 303              array(
 304                  'action' => 'upgrade-theme',
 305                  'theme'  => $theme->slug,
 306              ),
 307              self_admin_url( 'update.php' )
 308          );
 309  
 310          $status = $this->_get_theme_status( $theme );
 311  
 312          switch ( $status ) {
 313              case 'update_available':
 314                  $actions[] = sprintf(
 315                      '<a class="install-now" href="%s" title="%s">%s</a>',
 316                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 317                      /* translators: %s: Theme version. */
 318                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 319                      __( 'Update' )
 320                  );
 321                  break;
 322              case 'newer_installed':
 323              case 'latest_installed':
 324                  $actions[] = sprintf(
 325                      '<span class="install-now" title="%s">%s</span>',
 326                      esc_attr__( 'This theme is already installed and is up to date' ),
 327                      _x( 'Installed', 'theme' )
 328                  );
 329                  break;
 330              case 'install':
 331              default:
 332                  $actions[] = sprintf(
 333                      '<a class="install-now" href="%s" title="%s">%s</a>',
 334                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 335                      /* translators: %s: Theme name. */
 336                      esc_attr( sprintf( _x( 'Install %s', 'theme' ), $name ) ),
 337                      _x( 'Install Now', 'theme' )
 338                  );
 339                  break;
 340          }
 341  
 342          $actions[] = sprintf(
 343              '<a class="install-theme-preview" href="%s" title="%s">%s</a>',
 344              esc_url( $preview_url ),
 345              /* translators: %s: Theme name. */
 346              esc_attr( sprintf( __( 'Preview %s' ), $name ) ),
 347              __( 'Preview' )
 348          );
 349  
 350          /**
 351           * Filters the install action links for a theme in the Install Themes list table.
 352           *
 353           * @since 3.4.0
 354           *
 355           * @param string[] $actions An array of theme action links. Defaults are
 356           *                          links to Install Now, Preview, and Details.
 357           * @param stdClass $theme   An object that contains theme data returned by the
 358           *                          WordPress.org API.
 359           */
 360          $actions = apply_filters( 'theme_install_actions', $actions, $theme );
 361  
 362          ?>
 363          <a class="screenshot install-theme-preview" href="<?php echo esc_url( $preview_url ); ?>" title="<?php echo esc_attr( $preview_title ); ?>">
 364              <img src="<?php echo esc_url( $theme->screenshot_url . '?ver=' . $theme->version ); ?>" width="150" alt="" />
 365          </a>
 366  
 367          <h3><?php echo $name; ?></h3>
 368          <div class="theme-author">
 369          <?php
 370              /* translators: %s: Theme author. */
 371              printf( __( 'By %s' ), $author );
 372          ?>
 373          </div>
 374  
 375          <div class="action-links">
 376              <ul>
 377                  <?php foreach ( $actions as $action ) : ?>
 378                      <li><?php echo $action; ?></li>
 379                  <?php endforeach; ?>
 380                  <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li>
 381              </ul>
 382          </div>
 383  
 384          <?php
 385          $this->install_theme_info( $theme );
 386      }
 387  
 388      /**
 389       * Prints the wrapper for the theme installer.
 390       */
 391  	public function theme_installer() {
 392          ?>
 393          <div id="theme-installer" class="wp-full-overlay expanded">
 394              <div class="wp-full-overlay-sidebar">
 395                  <div class="wp-full-overlay-header">
 396                      <a href="#" class="close-full-overlay button"><?php _e( 'Close' ); ?></a>
 397                      <span class="theme-install"></span>
 398                  </div>
 399                  <div class="wp-full-overlay-sidebar-content">
 400                      <div class="install-theme-info"></div>
 401                  </div>
 402                  <div class="wp-full-overlay-footer">
 403                      <button type="button" class="collapse-sidebar button" aria-expanded="true" aria-label="<?php esc_attr_e( 'Collapse Sidebar' ); ?>">
 404                          <span class="collapse-sidebar-arrow"></span>
 405                          <span class="collapse-sidebar-label"><?php _e( 'Collapse' ); ?></span>
 406                      </button>
 407                  </div>
 408              </div>
 409              <div class="wp-full-overlay-main"></div>
 410          </div>
 411          <?php
 412      }
 413  
 414      /**
 415       * Prints the wrapper for the theme installer with a provided theme's data.
 416       * Used to make the theme installer work for no-js.
 417       *
 418       * @param stdClass $theme A WordPress.org Theme API object.
 419       */
 420  	public function theme_installer_single( $theme ) {
 421          ?>
 422          <div id="theme-installer" class="wp-full-overlay single-theme">
 423              <div class="wp-full-overlay-sidebar">
 424                  <?php $this->install_theme_info( $theme ); ?>
 425              </div>
 426              <div class="wp-full-overlay-main">
 427                  <iframe src="<?php echo esc_url( $theme->preview_url ); ?>"></iframe>
 428              </div>
 429          </div>
 430          <?php
 431      }
 432  
 433      /**
 434       * Prints the info for a theme (to be used in the theme installer modal).
 435       *
 436       * @global array $themes_allowedtags
 437       *
 438       * @param stdClass $theme A WordPress.org Theme API object.
 439       */
 440  	public function install_theme_info( $theme ) {
 441          global $themes_allowedtags;
 442  
 443          if ( empty( $theme ) ) {
 444              return;
 445          }
 446  
 447          $name   = wp_kses( $theme->name, $themes_allowedtags );
 448          $author = wp_kses( $theme->author, $themes_allowedtags );
 449  
 450          $install_url = add_query_arg(
 451              array(
 452                  'action' => 'install-theme',
 453                  'theme'  => $theme->slug,
 454              ),
 455              self_admin_url( 'update.php' )
 456          );
 457  
 458          $update_url = add_query_arg(
 459              array(
 460                  'action' => 'upgrade-theme',
 461                  'theme'  => $theme->slug,
 462              ),
 463              self_admin_url( 'update.php' )
 464          );
 465  
 466          $status = $this->_get_theme_status( $theme );
 467  
 468          ?>
 469          <div class="install-theme-info">
 470          <?php
 471          switch ( $status ) {
 472              case 'update_available':
 473                  printf(
 474                      '<a class="theme-install button button-primary" href="%s" title="%s">%s</a>',
 475                      esc_url( wp_nonce_url( $update_url, 'upgrade-theme_' . $theme->slug ) ),
 476                      /* translators: %s: Theme version. */
 477                      esc_attr( sprintf( __( 'Update to version %s' ), $theme->version ) ),
 478                      __( 'Update' )
 479                  );
 480                  break;
 481              case 'newer_installed':
 482              case 'latest_installed':
 483                  printf(
 484                      '<span class="theme-install" title="%s">%s</span>',
 485                      esc_attr__( 'This theme is already installed and is up to date' ),
 486                      _x( 'Installed', 'theme' )
 487                  );
 488                  break;
 489              case 'install':
 490              default:
 491                  printf(
 492                      '<a class="theme-install button button-primary" href="%s">%s</a>',
 493                      esc_url( wp_nonce_url( $install_url, 'install-theme_' . $theme->slug ) ),
 494                      __( 'Install' )
 495                  );
 496                  break;
 497          }
 498          ?>
 499              <h3 class="theme-name"><?php echo $name; ?></h3>
 500              <span class="theme-by">
 501              <?php
 502                  /* translators: %s: Theme author. */
 503                  printf( __( 'By %s' ), $author );
 504              ?>
 505              </span>
 506              <?php if ( isset( $theme->screenshot_url ) ) : ?>
 507                  <img class="theme-screenshot" src="<?php echo esc_url( $theme->screenshot_url . '?ver=' . $theme->version ); ?>" alt="" />
 508              <?php endif; ?>
 509              <div class="theme-details">
 510                  <?php
 511                  wp_star_rating(
 512                      array(
 513                          'rating' => $theme->rating,
 514                          'type'   => 'percent',
 515                          'number' => $theme->num_ratings,
 516                      )
 517                  );
 518                  ?>
 519                  <div class="theme-version">
 520                      <strong><?php _e( 'Version:' ); ?> </strong>
 521                      <?php echo wp_kses( $theme->version, $themes_allowedtags ); ?>
 522                  </div>
 523                  <div class="theme-description">
 524                      <?php echo wp_kses( $theme->description, $themes_allowedtags ); ?>
 525                  </div>
 526              </div>
 527              <input class="theme-preview-url" type="hidden" value="<?php echo esc_url( $theme->preview_url ); ?>" />
 528          </div>
 529          <?php
 530      }
 531  
 532      /**
 533       * Send required variables to JavaScript land
 534       *
 535       * @since 3.4.0
 536       *
 537       * @global string $tab  Current tab within Themes->Install screen
 538       * @global string $type Type of search.
 539       *
 540       * @param array $extra_args Unused.
 541       */
 542  	public function _js_vars( $extra_args = array() ) {
 543          global $tab, $type;
 544          parent::_js_vars( compact( 'tab', 'type' ) );
 545      }
 546  
 547      /**
 548       * Checks to see if the theme is already installed.
 549       *
 550       * @since 3.4.0
 551       *
 552       * @param stdClass $theme A WordPress.org Theme API object.
 553       * @return string Theme status.
 554       */
 555  	private function _get_theme_status( $theme ) {
 556          $status = 'install';
 557  
 558          $installed_theme = wp_get_theme( $theme->slug );
 559          if ( $installed_theme->exists() ) {
 560              if ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '=' ) ) {
 561                  $status = 'latest_installed';
 562              } elseif ( version_compare( $installed_theme->get( 'Version' ), $theme->version, '>' ) ) {
 563                  $status = 'newer_installed';
 564              } else {
 565                  $status = 'update_available';
 566              }
 567          }
 568  
 569          return $status;
 570      }
 571  }


Generated : Wed May 8 08:20:02 2024 Cross-referenced by PHPXref