[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 */ 193 public function display_rows() { 194 $themes = $this->items; 195 196 foreach ( $themes as $theme ) : 197 ?> 198 <div class="available-theme"> 199 <?php 200 201 $template = $theme->get_template(); 202 $stylesheet = $theme->get_stylesheet(); 203 $title = $theme->display( 'Name' ); 204 $version = $theme->display( 'Version' ); 205 $author = $theme->display( 'Author' ); 206 207 $activate_link = wp_nonce_url( 'themes.php?action=activate&template=' . urlencode( $template ) . '&stylesheet=' . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet ); 208 209 $actions = array(); 210 $actions['activate'] = sprintf( 211 '<a href="%s" class="activatelink" title="%s">%s</a>', 212 $activate_link, 213 /* translators: %s: Theme name. */ 214 esc_attr( sprintf( _x( 'Activate “%s”', 'theme' ), $title ) ), 215 __( 'Activate' ) 216 ); 217 218 if ( current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) { 219 $actions['preview'] .= sprintf( 220 '<a href="%s" class="load-customize hide-if-no-customize">%s</a>', 221 wp_customize_url( $stylesheet ), 222 __( 'Live Preview' ) 223 ); 224 } 225 226 if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) { 227 $actions['delete'] = sprintf( 228 '<a class="submitdelete deletion" href="%s" onclick="return confirm( \'%s\' );">%s</a>', 229 wp_nonce_url( 'themes.php?action=delete&stylesheet=' . urlencode( $stylesheet ), 'delete-theme_' . $stylesheet ), 230 /* translators: %s: Theme name. */ 231 esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ), 232 __( 'Delete' ) 233 ); 234 } 235 236 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ 237 $actions = apply_filters( 'theme_action_links', $actions, $theme, 'all' ); 238 239 /** This filter is documented in wp-admin/includes/class-wp-ms-themes-list-table.php */ 240 $actions = apply_filters( "theme_action_links_{$stylesheet}", $actions, $theme, 'all' ); 241 $delete_action = isset( $actions['delete'] ) ? '<div class="delete-theme">' . $actions['delete'] . '</div>' : ''; 242 unset( $actions['delete'] ); 243 244 $screenshot = $theme->get_screenshot(); 245 ?> 246 247 <span class="screenshot hide-if-customize"> 248 <?php if ( $screenshot ) : ?> 249 <img src="<?php echo esc_url( $screenshot . '?ver=' . $theme->version ); ?>" alt="" /> 250 <?php endif; ?> 251 </span> 252 <a href="<?php echo wp_customize_url( $stylesheet ); ?>" class="screenshot load-customize hide-if-no-customize"> 253 <?php if ( $screenshot ) : ?> 254 <img src="<?php echo esc_url( $screenshot . '?ver=' . $theme->version ); ?>" alt="" /> 255 <?php endif; ?> 256 </a> 257 258 <h3><?php echo $title; ?></h3> 259 <div class="theme-author"> 260 <?php 261 /* translators: %s: Theme author. */ 262 printf( __( 'By %s' ), $author ); 263 ?> 264 </div> 265 <div class="action-links"> 266 <ul> 267 <?php foreach ( $actions as $action ) : ?> 268 <li><?php echo $action; ?></li> 269 <?php endforeach; ?> 270 <li class="hide-if-no-js"><a href="#" class="theme-detail"><?php _e( 'Details' ); ?></a></li> 271 </ul> 272 <?php echo $delete_action; ?> 273 274 <?php theme_update_available( $theme ); ?> 275 </div> 276 277 <div class="themedetaildiv hide-if-js"> 278 <p><strong><?php _e( 'Version:' ); ?></strong> <?php echo $version; ?></p> 279 <p><?php echo $theme->display( 'Description' ); ?></p> 280 <?php 281 if ( $theme->parent() ) { 282 printf( 283 /* translators: 1: Link to documentation on child themes, 2: Name of parent theme. */ 284 ' <p class="howto">' . __( 'This <a href="%1$s">child theme</a> requires its parent theme, %2$s.' ) . '</p>', 285 __( 'https://developer.wordpress.org/themes/advanced-topics/child-themes/' ), 286 $theme->parent()->display( 'Name' ) 287 ); 288 } 289 ?> 290 </div> 291 292 </div> 293 <?php 294 endforeach; 295 } 296 297 /** 298 * @param WP_Theme $theme 299 * @return bool 300 */ 301 public function search_theme( $theme ) { 302 // Search the features. 303 foreach ( $this->features as $word ) { 304 if ( ! in_array( $word, $theme->get( 'Tags' ), true ) ) { 305 return false; 306 } 307 } 308 309 // Match all phrases. 310 foreach ( $this->search_terms as $word ) { 311 if ( in_array( $word, $theme->get( 'Tags' ), true ) ) { 312 continue; 313 } 314 315 foreach ( array( 'Name', 'Description', 'Author', 'AuthorURI' ) as $header ) { 316 // Don't mark up; Do translate. 317 if ( false !== stripos( strip_tags( $theme->display( $header, false, true ) ), $word ) ) { 318 continue 2; 319 } 320 } 321 322 if ( false !== stripos( $theme->get_stylesheet(), $word ) ) { 323 continue; 324 } 325 326 if ( false !== stripos( $theme->get_template(), $word ) ) { 327 continue; 328 } 329 330 return false; 331 } 332 333 return true; 334 } 335 336 /** 337 * Send required variables to JavaScript land 338 * 339 * @since 3.4.0 340 * 341 * @param array $extra_args 342 */ 343 public function _js_vars( $extra_args = array() ) { 344 $search_string = isset( $_REQUEST['s'] ) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : ''; 345 346 $args = array( 347 'search' => $search_string, 348 'features' => $this->features, 349 'paged' => $this->get_pagenum(), 350 'total_pages' => ! empty( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 1, 351 ); 352 353 if ( is_array( $extra_args ) ) { 354 $args = array_merge( $args, $extra_args ); 355 } 356 357 printf( "<script type='text/javascript'>var theme_list_args = %s;</script>\n", wp_json_encode( $args ) ); 358 parent::_js_vars(); 359 } 360 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Mon Dec 4 08:20:01 2023 | Cross-referenced by PHPXref |