From 438e156ba68793f95f3e42dcbc0e69735c74660c Mon Sep 17 00:00:00 2001 From: QWp6t Date: Fri, 14 Apr 2017 01:23:42 -0700 Subject: [PATCH] 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 --- CHANGELOG.md | 1 + README.md | 36 +++++++++--------- app/filters.php | 30 ++++++++------- app/lib/Sage/PostCreateProject.php | 2 +- functions.php => resources/functions.php | 24 +++++++----- index.php => resources/index.php | 0 screenshot.png => resources/screenshot.png | Bin style.css => resources/style.css | 0 .../views/partials/content-single.blade.php | 2 +- 9 files changed, 52 insertions(+), 43 deletions(-) rename functions.php => resources/functions.php (84%) rename index.php => resources/index.php (100%) rename screenshot.png => resources/screenshot.png (100%) rename style.css => resources/style.css (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45c5396be0..c410099d1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/README.md b/README.md index 9cce3f20e8..5260fb9be2 100644 --- a/README.md +++ b/README.md @@ -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) ``` diff --git a/app/filters.php b/app/filters.php index 270b531a2c..050bf8facf 100644 --- a/app/filters.php +++ b/app/filters.php @@ -6,19 +6,24 @@ * Add 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); diff --git a/app/lib/Sage/PostCreateProject.php b/app/lib/Sage/PostCreateProject.php index 0dac4fccb9..4499598ac3 100755 --- a/app/lib/Sage/PostCreateProject.php +++ b/app/lib/Sage/PostCreateProject.php @@ -31,7 +31,7 @@ public static function updateHeaders(Event $event) 'author_uri' => $io->ask('Theme Author URI ['.$theme_headers_default['author_uri'].']: ', $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'))); } } diff --git a/functions.php b/resources/functions.php similarity index 84% rename from functions.php rename to resources/functions.php index b2ee7c2c1d..a92aa275af 100644 --- a/functions.php +++ b/resources/functions.php @@ -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 composer install 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 %s 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(); } diff --git a/index.php b/resources/index.php similarity index 100% rename from index.php rename to resources/index.php diff --git a/screenshot.png b/resources/screenshot.png similarity index 100% rename from screenshot.png rename to resources/screenshot.png diff --git a/style.css b/resources/style.css similarity index 100% rename from style.css rename to resources/style.css diff --git a/resources/views/partials/content-single.blade.php b/resources/views/partials/content-single.blade.php index 653804b4e6..ff5aea1e86 100644 --- a/resources/views/partials/content-single.blade.php +++ b/resources/views/partials/content-single.blade.php @@ -9,5 +9,5 @@ - @php(comments_template('/resources/views/partials/comments.blade.php')) + @php(comments_template('/partials/comments.blade.php'))