Move required theme files to sage/resources

* Resolves issue with WP only looking one-level deep for templates #1870
* get_template_dir() and related functions now point to sage/resources
* Use collect() when assembling views paths
* Update tree in README
This commit is contained in:
QWp6t
2017-04-14 01:23:42 -07:00
parent bea1b34322
commit 99160be992
10 changed files with 54 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
### HEAD ### HEAD
* Move required theme files to `sage/resources` ([#1877](https://github.com/roots/sage/pull/1877))
* Move `src/` to `app/` ([#1868](https://github.com/roots/sage/pull/1868)) * Move `src/` to `app/` ([#1868](https://github.com/roots/sage/pull/1868))
* Move `templates/` to `resources/views/`, move `assets/` to `resources/assets/`, rename `base.blade.php` to `app.blade.php` ([#1864](https://github.com/roots/sage/pull/1864)) * Move `templates/` to `resources/views/`, move `assets/` to `resources/assets/`, rename `base.blade.php` to `app.blade.php` ([#1864](https://github.com/roots/sage/pull/1864))
* Add option to configure build settings ([#1822](https://github.com/roots/sage/pull/1822)) * Add option to configure build settings ([#1822](https://github.com/roots/sage/pull/1822))

View File

@@ -53,31 +53,31 @@ During theme installation you will have the options to:
```shell ```shell
themes/your-theme-name/ # → Root of your Sage based theme themes/your-theme-name/ # → Root of your Sage based theme
├── app/ # → Theme PHP ├── app/ # → Theme PHP
   ├── lib/Sage/ # → Blade implementation, asset manifest ├── lib/Sage/ # → Blade implementation, asset manifest
   ├── admin.php # → Theme customizer setup ├── admin.php # → Theme customizer setup
   ├── filters.php # → Theme filters ├── filters.php # → Theme filters
   ├── helpers.php # → Helper functions ├── helpers.php # → Helper functions
   └── setup.php # → Theme setup └── setup.php # → Theme setup
├── composer.json # → Autoloading for `app/` files ├── composer.json # → Autoloading for `app/` files
├── composer.lock # → Composer lock file (never edit) ├── composer.lock # → Composer lock file (never edit)
├── dist/ # → Built theme assets (never edit) ├── dist/ # → Built theme assets (never edit)
├── functions.php # → Composer autoloader, theme includes
├── index.php # → Never manually edit
├── node_modules/ # → Node.js packages (never edit) ├── node_modules/ # → Node.js packages (never edit)
├── package.json # → Node.js dependencies and scripts ├── package.json # → Node.js dependencies and scripts
├── screenshot.png # → Theme screenshot for WP admin
├── style.css # → Theme meta information
├── resources/ # → Theme assets and templates ├── resources/ # → Theme assets and templates
│ ├── assets/ # → Front-end assets │ ├── assets/ # → Front-end assets
   │ ├── config.json # → Settings for compiled assets │ ├── config.json # → Settings for compiled assets
   │ ├── build/ # → Webpack and ESLint config │ ├── build/ # → Webpack and ESLint config
   │ ├── fonts/ # → Theme fonts │ ├── fonts/ # → Theme fonts
   │ ├── images/ # → Theme images │ ├── images/ # → Theme images
   │ ├── scripts/ # → Theme JS │ ├── scripts/ # → Theme JS
   │ └── styles/ # → Theme stylesheets │ └── styles/ # → Theme stylesheets
   └── views/ # → Theme templates ├── functions.php # → Composer autoloader, theme includes
   ├── layouts/ # → Base templates │ ├── index.php # → Never manually edit
   ── partials/ # → Partial templates ── screenshot.png # → Theme screenshot for WP admin
│ ├── style.css # → Theme meta information
│ └── views/ # → Theme templates
│ ├── layouts/ # → Base templates
│ └── partials/ # → Partial templates
└── vendor/ # → Composer packages (never edit) └── vendor/ # → Composer packages (never edit)
``` ```

View File

@@ -6,19 +6,24 @@ namespace App;
* Add <body> classes * Add <body> classes
*/ */
add_filter('body_class', function (array $classes) { add_filter('body_class', function (array $classes) {
// Add page slug if it doesn't exist /** Add page slug if it doesn't exist */
if (is_single() || is_page() && !is_front_page()) { if (is_single() || is_page() && !is_front_page()) {
if (!in_array(basename(get_permalink()), $classes)) { if (!in_array(basename(get_permalink()), $classes)) {
$classes[] = basename(get_permalink()); $classes[] = basename(get_permalink());
} }
} }
// Add class if sidebar is active /** Add class if sidebar is active */
if (display_sidebar()) { if (display_sidebar()) {
$classes[] = 'sidebar-primary'; $classes[] = 'sidebar-primary';
} }
return $classes; /** Clean up class names for custom templates */
$classes = array_map(function ($class) {
return preg_replace(['/-blade(-php)?$/', '/^page-template-views/'], '', $class);
}, $classes);
return array_filter($classes);
}); });
/** /**
@@ -31,31 +36,30 @@ add_filter('excerpt_more', function () {
/** /**
* Template Hierarchy should search for .blade.php files * Template Hierarchy should search for .blade.php files
*/ */
array_map(function ($type) { collect([
'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home',
'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment'
])->map(function ($type) {
add_filter("{$type}_template_hierarchy", function ($templates) { add_filter("{$type}_template_hierarchy", function ($templates) {
return call_user_func_array('array_merge', array_map(function ($template) { return collect($templates)->flatMap(function ($template) {
$transforms = [ $transforms = [
'%^/?(resources/views)?/?%' => config('sage.disable_option_hack') ? 'resources/views/' : '', '%^/?(resources[\\/]views)?[\\/]?%' => '',
'%(\.blade)?(\.php)?$%' => '' '%(\.blade)?(\.php)?$%' => ''
]; ];
$normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template); $normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template);
return ["{$normalizedTemplate}.blade.php", "{$normalizedTemplate}.php"]; return ["{$normalizedTemplate}.blade.php", "{$normalizedTemplate}.php"];
}, $templates)); })->toArray();
}); });
}, [ });
'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'home',
'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment'
]);
/** /**
* Render page using Blade * Render page using Blade
*/ */
add_filter('template_include', function ($template) { add_filter('template_include', function ($template) {
$data = array_reduce(get_body_class(), function ($data, $class) use ($template) { $data = collect(get_body_class())->reduce(function ($data, $class) use ($template) {
return apply_filters("sage/template/{$class}/data", $data, $template); return apply_filters("sage/template/{$class}/data", $data, $template);
}, []); }, []);
echo template($template, $data); echo template($template, $data);
// Return a blank file to make WordPress happy // Return a blank file to make WordPress happy
return get_theme_file_path('index.php'); return get_theme_file_path('index.php');
}, PHP_INT_MAX); }, PHP_INT_MAX);

View File

@@ -31,7 +31,7 @@ class PostCreateProject
'author_uri' => $io->ask('<info>Theme Author URI [<comment>'.$theme_headers_default['author_uri'].'</comment>]:</info> ', $theme_headers_default['author_uri']) 'author_uri' => $io->ask('<info>Theme Author URI [<comment>'.$theme_headers_default['author_uri'].'</comment>]:</info> ', $theme_headers_default['author_uri'])
]; ];
file_put_contents('style.css', str_replace($theme_headers_default, $theme_headers, file_get_contents('style.css'))); file_put_contents('resources/style.css', str_replace($theme_headers_default, $theme_headers, file_get_contents('resources/style.css')));
} }
} }

View File

@@ -3,8 +3,8 @@
<description>Roots Coding Standards</description> <description>Roots Coding Standards</description>
<!-- Scan these files --> <!-- Scan these files -->
<file>functions.php</file> <file>resources/functions.php</file>
<file>index.php</file> <file>resources/index.php</file>
<file>app</file> <file>app</file>
<file>resources/views</file> <file>resources/views</file>

View File

@@ -35,7 +35,7 @@ if (version_compare('4.7.0', get_bloginfo('version'), '>=')) {
* Ensure dependencies are loaded * Ensure dependencies are loaded
*/ */
if (!class_exists('Roots\\Sage\\Container')) { if (!class_exists('Roots\\Sage\\Container')) {
if (!file_exists($composer = __DIR__.'/vendor/autoload.php')) { if (!file_exists($composer = __DIR__.'/../vendor/autoload.php')) {
$sage_error( $sage_error(
__('You must run <code>composer install</code> from the Sage directory.', 'sage'), __('You must run <code>composer install</code> from the Sage directory.', 'sage'),
__('Autoloader not found.', 'sage') __('Autoloader not found.', 'sage')
@@ -51,7 +51,7 @@ if (!class_exists('Roots\\Sage\\Container')) {
* Add or remove files to the array as needed. Supports child theme overrides. * Add or remove files to the array as needed. Supports child theme overrides.
*/ */
array_map(function ($file) use ($sage_error) { array_map(function ($file) use ($sage_error) {
$file = "app/{$file}.php"; $file = "../app/{$file}.php";
if (!locate_template($file, true, true)) { if (!locate_template($file, true, true)) {
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found'); $sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
} }
@@ -68,20 +68,24 @@ array_map(function ($file) use ($sage_error) {
* *
* This is not compatible with the WordPress Customizer theme preview prior to theme activation * This is not compatible with the WordPress Customizer theme preview prior to theme activation
* *
* get_template_directory() -> /srv/www/example.com/current/web/app/themes/sage * get_template_directory() -> /srv/www/example.com/current/web/app/themes/sage/resources
* get_stylesheet_directory() -> /srv/www/example.com/current/web/app/themes/sage * get_stylesheet_directory() -> /srv/www/example.com/current/web/app/themes/sage/resources
* locate_template() * locate_template()
* ├── STYLESHEETPATH -> /srv/www/example.com/current/web/app/themes/sage * ├── STYLESHEETPATH -> /srv/www/example.com/current/web/app/themes/sage/resources/views
* └── TEMPLATEPATH -> /srv/www/example.com/current/web/app/themes/sage/resources/views * └── TEMPLATEPATH -> /srv/www/example.com/current/web/app/themes/sage/resources
*/ */
if (is_customize_preview() && isset($_GET['theme'])) { if (is_customize_preview() && isset($_GET['theme'])) {
$sage_error(__('Theme must be activated prior to using the customizer.', 'sage')); $sage_error(__('Theme must be activated prior to using the customizer.', 'sage'));
} }
add_filter('template', function ($stylesheet) { $sage_views = basename(dirname(__DIR__)).'/'.basename(__DIR__).'/views';
return dirname(dirname($stylesheet)); add_filter('stylesheet', function () use ($sage_views) {
return dirname($sage_views);
}); });
if (($sage_views = basename(__DIR__).'/resources/views') !== get_option('template')) { add_filter('stylesheet_directory_uri', function ($uri) {
update_option('template', $sage_views); return dirname($uri);
});
if ($sage_views !== get_option('stylesheet')) {
update_option('stylesheet', $sage_views);
wp_redirect($_SERVER['REQUEST_URI']); wp_redirect($_SERVER['REQUEST_URI']);
exit(); exit();
} }

View File

Before

Width:  |  Height:  |  Size: 225 B

After

Width:  |  Height:  |  Size: 225 B

View File

@@ -9,5 +9,5 @@
<footer> <footer>
{!! wp_link_pages(['echo' => 0, 'before' => '<nav class="page-nav"><p>' . __('Pages:', 'sage'), 'after' => '</p></nav>']) !!} {!! wp_link_pages(['echo' => 0, 'before' => '<nav class="page-nav"><p>' . __('Pages:', 'sage'), 'after' => '</p></nav>']) !!}
</footer> </footer>
@php(comments_template('/resources/views/partials/comments.blade.php')) @php(comments_template('/partials/comments.blade.php'))
</article> </article>