函数文档

wp_make_plugin_file_tree()

💡 云策文档标注

概述

wp_make_plugin_file_tree() 函数用于为插件文件编辑器的文件列表生成树形结构。它接收插件文件路径数组作为输入,并返回一个表示文件层次结构的数组。

关键要点

  • 函数接受一个必需参数 $plugin_editable_files,这是一个插件文件路径的数组。
  • 返回一个数组,表示插件文件的树形结构,便于在编辑器中展示。
  • 函数内部通过遍历文件路径,使用 explode() 和引用操作来构建嵌套数组结构。

代码示例

function wp_make_plugin_file_tree( $plugin_editable_files ) {
    $tree_list = array();

    foreach ( $plugin_editable_files as $plugin_file ) {
        $list     = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
        $last_dir = &$tree_list;

        foreach ( $list as $dir ) {
            $last_dir =& $last_dir[ $dir ];
        }

        $last_dir = $plugin_file;
    }

    return $tree_list;
}

注意事项

  • 此函数自 WordPress 4.9.0 版本引入,使用时需确保版本兼容性。
  • 输入参数 $plugin_editable_files 应为有效的文件路径数组,否则可能导致错误或异常输出。

📄 原文内容

Makes a tree structure for the plugin file editor’s file list.

Parameters

$plugin_editable_filesarrayrequired
List of plugin file paths.

Return

array Tree structure for listing plugin files.

Source

function wp_make_plugin_file_tree( $plugin_editable_files ) {
	$tree_list = array();

	foreach ( $plugin_editable_files as $plugin_file ) {
		$list     = explode( '/', preg_replace( '#^.+?/#', '', $plugin_file ) );
		$last_dir = &$tree_list;

		foreach ( $list as $dir ) {
			$last_dir =& $last_dir[ $dir ];
		}

		$last_dir = $plugin_file;
	}

	return $tree_list;
}

Changelog

Version Description
4.9.0 Introduced.