Make WordPress Core


Ignore:
Timestamp:
10/13/2017 02:38:19 AM (9 years ago)
Author:
westonruter
Message:

File Editors: Display list of theme/plugin files in scrollable directory tree.

Props WraithKenny, afercia, melchoyce, westonruter.
Amends [41721].
Fixes #24048.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r41741 r41851  
    268268        }
    269269        update_option( 'recently_edited', $oldfiles );
     270}
     271
     272/**
     273 * Makes a tree structure for the Theme Editor's file list.
     274 *
     275 * @since 4.9.0
     276 * @access private
     277 *
     278 * @param array $allowed_files List of theme file paths.
     279 * @return array Tree structure for listing theme files.
     280 */
     281function wp_make_theme_file_tree( $allowed_files ) {
     282        $tree_list = array();
     283        foreach ( $allowed_files as $file_name => $absolute_filename ) {
     284                $list = explode( '/', $file_name );
     285                $last_dir = &$tree_list;
     286                foreach ( $list as $dir ) {
     287                        $last_dir =& $last_dir[ $dir ];
     288                }
     289                $last_dir = $file_name;
     290        }
     291        return $tree_list;
     292}
     293
     294/**
     295 * Outputs the formatted file list for the Theme Editor.
     296 *
     297 * @since 4.9.0
     298 * @access private
     299 *
     300 * @param array|string $tree  List of file/folder paths, or filename.
     301 * @param int          $level The aria-level for the current iteration.
     302 * @param int          $size  The aria-setsize for the current iteration.
     303 * @param int          $index The aria-posinset for the current iteration.
     304 */
     305function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
     306        global $relative_file, $stylesheet;
     307
     308        if ( is_array( $tree ) ) {
     309                $index = 0;
     310                $size = count( $tree );
     311                foreach ( $tree as $label => $theme_file ) :
     312                        $index++;
     313                        if ( ! is_array( $theme_file ) ) {
     314                                wp_print_theme_file_tree( $theme_file, $level, $index, $size );
     315                                continue;
     316                        }
     317                        ?>
     318                        <li role="treeitem" aria-expanded="true" tabindex="-1"
     319                                aria-level="<?php echo esc_attr( $level ); ?>"
     320                                aria-setsize="<?php echo esc_attr( $size ); ?>"
     321                                aria-posinset="<?php echo esc_attr( $index ); ?>">
     322                                <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
     323                                <ul role="group" class="tree-folder"><?php wp_print_theme_file_tree( $theme_file, $level + 1, $index, $size ); ?></ul>
     324                        </li>
     325                        <?php
     326                endforeach;
     327        } else {
     328                $filename = $tree;
     329                $url = add_query_arg(
     330                        array(
     331                                'file' => rawurlencode( $tree ),
     332                                'theme' => rawurlencode( $stylesheet ),
     333                        ),
     334                        admin_url( 'theme-editor.php' )
     335                );
     336                ?>
     337                <li role="none" class="<?php echo esc_attr( $relative_file === $filename ? 'current-file' : '' ); ?>">
     338                        <a role="treeitem" tabindex="<?php echo esc_attr( $relative_file === $filename ? '0' : '-1' ); ?>"
     339                                href="<?php echo esc_url( $url ); ?>"
     340                                aria-level="<?php echo esc_attr( $level ); ?>"
     341                                aria-setsize="<?php echo esc_attr( $size ); ?>"
     342                                aria-posinset="<?php echo esc_attr( $index ); ?>">
     343                                <?php
     344                                $file_description = esc_html( get_file_description( $filename ) );
     345                                if ( $file_description !== $filename && basename( $filename ) !== $file_description ) {
     346                                        $file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
     347                                }
     348
     349                                if ( $relative_file === $filename ) {
     350                                        echo '<span class="notice notice-info">' . $file_description . '</span>';
     351                                } else {
     352                                        echo $file_description;
     353                                }
     354                                ?>
     355                        </a>
     356                </li>
     357                <?php
     358        }
     359}
     360
     361/**
     362 * Makes a tree structure for the Plugin Editor's file list.
     363 *
     364 * @since 4.9.0
     365 * @access private
     366 *
     367 * @param string $plugin_editable_files List of plugin file paths.
     368 * @return array Tree structure for listing plugin files.
     369 */
     370function wp_make_plugin_file_tree( $plugin_editable_files ) {
     371        $tree_list = array();
     372        foreach ( $plugin_editable_files as $plugin_file ) {
     373                $list = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
     374                $last_dir = &$tree_list;
     375                foreach ( $list as $dir ) {
     376                        $last_dir =& $last_dir[ $dir ];
     377                }
     378                $last_dir = $plugin_file;
     379        }
     380        return $tree_list;
     381}
     382
     383/**
     384 * Outputs the formatted file list for the Plugin Editor.
     385 *
     386 * @since 4.9.0
     387 * @access private
     388 *
     389 * @param array|string $tree  List of file/folder paths, or filename.
     390 * @param string       $label Name of file or folder to print.
     391 * @param int          $level The aria-level for the current iteration.
     392 * @param int          $size  The aria-setsize for the current iteration.
     393 * @param int          $index The aria-posinset for the current iteration.
     394 */
     395function wp_print_plugin_file_tree( $tree, $label = '', $level = 2, $size = 1, $index = 1 ) {
     396        global $file, $plugin;
     397        if ( is_array( $tree ) ) {
     398                $index = 0;
     399                $size = count( $tree );
     400                foreach ( $tree as $label => $plugin_file ) :
     401                        $index++;
     402                        if ( ! is_array( $plugin_file ) ) {
     403                                wp_print_plugin_file_tree( $plugin_file, $label, $level, $index, $size );
     404                                continue;
     405                        }
     406                        ?>
     407                        <li role="treeitem" aria-expanded="true" tabindex="-1"
     408                                aria-level="<?php echo esc_attr( $level ); ?>"
     409                                aria-setsize="<?php echo esc_attr( $size ); ?>"
     410                                aria-posinset="<?php echo esc_attr( $index ); ?>">
     411                                <span class="folder-label"><?php echo esc_html( $label ); ?> <span class="screen-reader-text"><?php _e( 'folder' ); ?></span><span aria-hidden="true" class="icon"></span></span>
     412                                <ul role="group" class="tree-folder"><?php wp_print_plugin_file_tree( $plugin_file, '', $level + 1, $index, $size ); ?></ul>
     413                        </li>
     414                        <?php
     415                endforeach;
     416        } else {
     417                $url = add_query_arg(
     418                        array(
     419                                'file' => rawurlencode( $tree ),
     420                                'plugin' => rawurlencode( $plugin ),
     421                        ),
     422                        admin_url( 'plugin-editor.php' )
     423                );
     424                ?>
     425                <li role="none" class="<?php echo esc_attr( $file === $tree ? 'current-file' : '' ); ?>">
     426                        <a role="treeitem" tabindex="<?php echo esc_attr( $file === $tree ? '0' : '-1' ); ?>"
     427                                href="<?php echo esc_url( $url ); ?>"
     428                                aria-level="<?php echo esc_attr( $level ); ?>"
     429                                aria-setsize="<?php echo esc_attr( $size ); ?>"
     430                                aria-posinset="<?php echo esc_attr( $index ); ?>">
     431                                <?php
     432                                if ( $file === $tree ) {
     433                                        echo '<span class="notice notice-info">' . esc_html( $label ) . '</span>';
     434                                } else {
     435                                        echo esc_html( $label );
     436                                }
     437                                ?>
     438                        </a>
     439                </li>
     440                <?php
     441        }
    270442}
    271443
Note: See TracChangeset for help on using the changeset viewer.