Permalink
Browse files

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
  • Loading branch information...
QWp6t committed Apr 14, 2017
1 parent bea1b34 commit 99160be992458667c8dbf7db4901b47f56faa4bf
@@ -1,4 +1,5 @@
### 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 `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))
@@ -53,31 +53,31 @@ During theme installation you will have the options to:
```shell
themes/your-theme-name/ # → Root of your Sage based theme
├── app/ # → Theme PHP
   ├── lib/Sage/ # → Blade implementation, asset manifest
   ├── admin.php # → Theme customizer setup
   ├── filters.php # → Theme filters
   ├── helpers.php # → Helper functions
   └── setup.php # → Theme setup
├── lib/Sage/ # → Blade implementation, asset manifest
├── admin.php # → Theme customizer setup
├── filters.php # → Theme filters
├── helpers.php # → Helper functions
└── setup.php # → Theme setup
├── composer.json # → Autoloading for `app/` files
├── composer.lock # → Composer lock file (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)
├── package.json # → Node.js dependencies and scripts
├── screenshot.png # → Theme screenshot for WP admin
├── style.css # → Theme meta information
├── resources/ # → Theme assets and templates
│ ├── assets/ # → Front-end assets
│   │ ├── config.json # → Settings for compiled assets
│   │ ├── build/ # → Webpack and ESLint config
│   │ ├── fonts/ # → Theme fonts
│   │ ├── images/ # → Theme images
│   │ ├── scripts/ # → Theme JS
│   │ └── styles/ # → Theme stylesheets
│   └── views/ # → Theme templates
│   ├── layouts/ # → Base templates
│   └── partials/ # → Partial templates
│ │ ├── config.json # → Settings for compiled assets
│ │ ├── build/ # → Webpack and ESLint config
│ │ ├── fonts/ # → Theme fonts
│ │ ├── images/ # → Theme images
│ │ ├── scripts/ # → Theme JS
│ │ └── styles/ # → Theme stylesheets
│ ├── functions.php # → Composer autoloader, theme includes
│ ├── index.php # → Never manually edit
│ ├── 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)
```
@@ -6,19 +6,24 @@
* Add <body> 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 (!in_array(basename(get_permalink()), $classes)) {
$classes[] = basename(get_permalink());
}
}
// Add class if sidebar is active
/** Add class if sidebar is active */
if (display_sidebar()) {
$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 @@
/**
* 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) {
return call_user_func_array('array_merge', array_map(function ($template) {
return collect($templates)->flatMap(function ($template) {
$transforms = [
'%^/?(resources/views)?/?%' => config('sage.disable_option_hack') ? 'resources/views/' : '',
'%^/?(resources[\\/]views)?[\\/]?%' => '',
'%(\.blade)?(\.php)?$%' => ''
];
$normalizedTemplate = preg_replace(array_keys($transforms), array_values($transforms), $template);
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
*/
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);
}, []);
echo template($template, $data);
// Return a blank file to make WordPress happy
return get_theme_file_path('index.php');
}, PHP_INT_MAX);
@@ -31,7 +31,7 @@ public static function updateHeaders(Event $event)
'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')));
}
}
@@ -3,8 +3,8 @@
<description>Roots Coding Standards</description>
<!-- Scan these files -->
<file>functions.php</file>
<file>index.php</file>
<file>resources/functions.php</file>
<file>resources/index.php</file>
<file>app</file>
<file>resources/views</file>
@@ -35,7 +35,7 @@
* Ensure dependencies are loaded
*/
if (!class_exists('Roots\\Sage\\Container')) {
if (!file_exists($composer = __DIR__.'/vendor/autoload.php')) {
if (!file_exists($composer = __DIR__.'/../vendor/autoload.php')) {
$sage_error(
__('You must run <code>composer install</code> from the Sage directory.', 'sage'),
__('Autoloader not found.', 'sage')
@@ -51,7 +51,7 @@
* Add or remove files to the array as needed. Supports child theme overrides.
*/
array_map(function ($file) use ($sage_error) {
$file = "app/{$file}.php";
$file = "../app/{$file}.php";
if (!locate_template($file, true, true)) {
$sage_error(sprintf(__('Error locating <code>%s</code> for inclusion.', 'sage'), $file), 'File not found');
}
@@ -68,20 +68,24 @@
*
* 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_stylesheet_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/resources
* locate_template()
* ├── STYLESHEETPATH -> /srv/www/example.com/current/web/app/themes/sage
* └── TEMPLATEPATH -> /srv/www/example.com/current/web/app/themes/sage/resources/views
* ├── STYLESHEETPATH -> /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'])) {
$sage_error(__('Theme must be activated prior to using the customizer.', 'sage'));
}
add_filter('template', function ($stylesheet) {
return dirname(dirname($stylesheet));
$sage_views = basename(dirname(__DIR__)).'/'.basename(__DIR__).'/views';
add_filter('stylesheet', function () use ($sage_views) {
return dirname($sage_views);
});
if (($sage_views = basename(__DIR__).'/resources/views') !== get_option('template')) {
update_option('template', $sage_views);
add_filter('stylesheet_directory_uri', function ($uri) {
return dirname($uri);
});
if ($sage_views !== get_option('stylesheet')) {
update_option('stylesheet', $sage_views);
wp_redirect($_SERVER['REQUEST_URI']);
exit();
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -9,5 +9,5 @@
<footer>
{!! wp_link_pages(['echo' => 0, 'before' => '<nav class="page-nav"><p>' . __('Pages:', 'sage'), 'after' => '</p></nav>']) !!}
</footer>
@php(comments_template('/resources/views/partials/comments.blade.php'))
@php(comments_template('/partials/comments.blade.php'))
</article>

3 comments on commit 99160be

@zorca

This comment has been minimized.

zorca replied Apr 17, 2017

Now how to activate the template? :-) Without styles.css at root.

@Log1x

This comment has been minimized.

Contributor

Log1x replied Apr 17, 2017

Like every other theme.

@zorca

This comment has been minimized.

zorca replied Apr 18, 2017

Does wordpress works with the theme in a subfolder? Didn't know it.

Please sign in to comment.