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