[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Wed Oct 9 08:20:01 2024 Cross-referenced by PHPXref