From 6aeae915652c0d5db82def3240228afa38956421 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BA=8C=E5=BA=86=EF=BC=88Amos=20Lee=EF=BC=89?= Date: Thu, 31 Jan 2019 12:53:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A8=A1=E7=89=88=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data-options.php | 73 ++++++++------------------------------------ src/helpers.php | 43 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/data-options.php b/src/data-options.php index bad04f0..e270ba9 100644 --- a/src/data-options.php +++ b/src/data-options.php @@ -1,9 +1,5 @@ 'Loop Template Name', + ]; + $plugin_template_dir = $default_path . $dir; $theme_template_dir = get_theme_file_path($dir); $templates_in_plugin = []; $templates_in_theme = []; - // 插件中的模板 if (is_dir($plugin_template_dir)) { - - // 查找目录中的文件 - $finder = Finder::findFiles('*.php')->in($plugin_template_dir); - - foreach ($finder as $key => $file) { - - $filename = $file->getFilename(); - $file_name_array = explode('-', $filename); - $name = Arrays::get($file_name_array, 1, ''); - - $headers = [ - 'name' => 'Loop Template Name', - ]; - - $file_info = get_file_data($key, $headers); - - // 获取模板名称 - if ($file_info[ 'name' ]) { - $option_name = $file_info[ 'name' ]; - } else { - $option_name = ucfirst($name); - } - - $templates_in_plugin[ explode('.', $name)[ 0 ] ] = $option_name; - - } + $templates_in_plugin = wprs_get_templates_in_path($plugin_template_dir, $headers); } - // 主题中的模板 if (is_dir($theme_template_dir)) { - - $finder = Finder::findFiles('*.php')->in($theme_template_dir); - - foreach ($finder as $key => $file) { - - $filename = $file->getFilename(); - $file_name_array = explode('-', $filename); - $name = Arrays::get($file_name_array, 1, ''); - - $headers = [ - 'name' => 'Loop Template Name', - ]; - - $file_info = get_file_data($key, $headers); - - // 获取模板名称 - if ($file_info[ 'name' ]) { - $option_name = $file_info[ 'name' ]; - } else { - $option_name = ucfirst($name); - } - - $templates_in_theme[ explode('.', $name)[ 0 ] ] = $option_name; - - } + $templates_in_theme = wprs_get_templates_in_path($theme_template_dir, $headers); } - // 合并插件和主题中的模板,优先使用主题中模板 $templates = wp_parse_args($templates_in_theme, $templates_in_plugin); - ksort($templates); + $result = []; + foreach ($templates as $key => $name) { + $result[ $key ] = $name[ 'name' ]; + } + + ksort($result); - return $templates; + return $result; } } diff --git a/src/helpers.php b/src/helpers.php index 4f5d249..b1858c2 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,5 +1,7 @@ in($path); + + foreach ($finder as $key => $file) { + + $filename = $file->getFilename(); + $file_name_array = explode('-', $filename); + $name = Arrays::get($file_name_array, 1, ''); + + if ( ! $name) { + $name = $filename; + } + + $file_info = get_file_data($key, $headers); + + // 获取模板名称 + if (isset($file_info[ 'name' ]) && $file_info[ 'name' ] != '') { + $templates[ explode('.', $name)[ 0 ] ] = $file_info; + } + + } + } + + return $templates; + } +}