Skip to content

Commit

Permalink
优化模版获取函数
Browse files Browse the repository at this point in the history
  • Loading branch information
iwillhappy1314 committed Jan 31, 2019
1 parent 5866b62 commit 6aeae91
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 60 deletions.
73 changes: 13 additions & 60 deletions src/data-options.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<?php

use Nette\Utils\Arrays;
use Nette\Utils\Finder;


/**
* @param string $dir
* @param string $default_path
Expand All @@ -30,80 +26,37 @@ function wprs_get_template_option($dir = "templates", $default_path = '')
function wprs_data_templates($dir = "templates", $default_path = '')
{

$headers = [
'name' => '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;
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Nette\Utils\Arrays;
use Nette\Utils\Finder;

if ( ! function_exists('wprs_get_vue_component_template')) {
/**
Expand Down Expand Up @@ -840,3 +842,44 @@ function wprs_class($class = '')
echo 'class="' . join(' ', (array)$class) . '"';
}
}


/**
* 获取路径中的指定文件
*
* @param $path
* @param $headers
*
* @return array
*/
if ( ! function_exists('wprs_get_templates_in_path')) {
function wprs_get_templates_in_path($path, $headers = [])
{
$templates = [];

if (is_dir($path)) {
$finder = Finder::findFiles('*.php')->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;
}
}

0 comments on commit 6aeae91

Please sign in to comment.