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