WordPress.org

Make WordPress Core

Changeset 35180


Ignore:
Timestamp:
10/15/2015 02:19:45 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Theme Editor: Repurpose the 'wp_theme_editor_filetypes' filter added in [35134] to actually filter file types instead of files.

Default types include 'php' and 'css' and cannot be removed, only new types can be added.

Props DrewAPicture, SergeyBiryukov.
Fixes #22924.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/theme-editor.php

    r35134 r35180  
    6262}
    6363
    64 $allowed_files = $theme->get_files( 'php', 1 );
    65 $has_templates = ! empty( $allowed_files );
    66 $style_files = $theme->get_files( 'css' );
    67 $allowed_files['style.css'] = $style_files['style.css'];
     64$allowed_files = $style_files = array();
     65$has_templates = false;
     66$default_types = array( 'php', 'css' );
     67
    6868/**
    69  * Filter the allowed files.
     69 * Filter the list of file types allowed for editing in the Theme editor.
    7070 *
    7171 * @since 4.4.0
    7272 *
    73  * @param array  $style_files List of style files.
    74  * @param object $theme       The current Theme object.
     73 * @param array    $default_types List of file types. Default types include 'php' and 'css'.
     74 * @param WP_Theme $theme         The current Theme object.
    7575 */
    76 $allowed_files += apply_filters( 'wp_theme_editor_filetypes', $style_files, $theme );
     76$file_types = apply_filters( 'wp_theme_editor_filetypes', $default_types, $theme );
     77
     78// Ensure that default types are still there.
     79$file_types = array_unique( array_merge( $file_types, $default_types ) );
     80
     81foreach ( $file_types as $type ) {
     82    switch ( $type ) {
     83        case 'php':
     84            $allowed_files += $theme->get_files( 'php', 1 );
     85            $has_templates = ! empty( $allowed_files );
     86            break;
     87        case 'css':
     88            $style_files = $theme->get_files( 'css' );
     89            $allowed_files['style.css'] = $style_files['style.css'];
     90            $allowed_files += $style_files;
     91            break;
     92        default:
     93            $allowed_files += $theme->get_files( $type );
     94            break;
     95    }
     96}
    7797
    7898if ( empty( $file ) ) {
     
    175195<?php
    176196if ( $allowed_files ) :
    177     if ( $has_templates || $theme->parent() ) :
    178 ?>
    179     <h2><?php _e( 'Templates' ); ?></h2>
    180     <?php if ( $theme->parent() ) : ?>
    181     <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), '<a href="' . self_admin_url('theme-editor.php?theme=' . urlencode( $theme->get_template() ) ) . '">' . $theme->parent()->display('Name') . '</a>' ); ?></p>
    182     <?php endif; ?>
    183     <ul>
    184 <?php
    185     endif;
     197    $previous_file_type = '';
    186198
    187199    foreach ( $allowed_files as $filename => $absolute_filename ) :
    188         if ( 'style.css' == $filename )
    189             echo "\t</ul>\n\t<h2>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h2>\n\t<ul>\n";
    190 
     200        $file_type = substr( $filename, strrpos( $filename, '.' ) );
     201
     202        if ( $file_type !== $previous_file_type ) {
     203            if ( '' !== $previous_file_type ) {
     204                echo "\t</ul>\n";
     205            }
     206
     207            switch ( $file_type ) {
     208                case '.php':
     209                    if ( $has_templates || $theme->parent() ) :
     210                        echo "\t<h2>" . __( 'Templates' ) . "</h2>\n";
     211                        if ( $theme->parent() ) {
     212                            echo '<p class="howto">' . sprintf( __( 'This child theme inherits templates from a parent theme, %s.' ),
     213                                sprintf( '<a href="%s">%s</a>',
     214                                    self_admin_url( 'theme-editor.php?theme=' . urlencode( $theme->get_template() ) ),
     215                                    $theme->parent()->display( 'Name' )
     216                                )
     217                            ) . "</p>\n";
     218                        }
     219                    endif;
     220                    break;
     221                case '.css':
     222                    echo "\t<h2>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h2>\n";
     223                    break;
     224                default:
     225                    /* translators: %s: file extension */
     226                    echo "\t<h2>" . sprintf( __( '%s files' ), $file_type ) . "</h2>\n";
     227                    break;
     228            }
     229
     230            echo "\t<ul>\n";
     231        }
     232       
    191233        $file_description = get_file_description( $filename );
    192234        if ( $filename !== basename( $absolute_filename ) || $file_description !== $filename ) {
     
    197239            $file_description = '<span class="highlight">' . $file_description . '</span>';
    198240        }
     241
     242        $previous_file_type = $file_type;
    199243?>
    200244        <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
Note: See TracChangeset for help on using the changeset viewer.