diff --git a/functions.php b/functions.php index 11c9a5e2a4..327b6d0bee 100644 --- a/functions.php +++ b/functions.php @@ -16,10 +16,10 @@ * themes/sage/index.php also contains some self-correcting code, just in case the template option gets reset */ add_filter('template', function ($template) { - return dirname($template); + return dirname($template); }); add_action('after_switch_theme', function () { - update_option('template', get_option('template') . '/templates'); + update_option('template', get_option('template') . '/templates'); }); /** diff --git a/phpcs.xml b/phpcs.xml index d5fce9b533..7dd833312a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -2,8 +2,9 @@ Roots Coding Standards - - + + functions.php + src @@ -11,7 +12,9 @@ - - functions.php - src + + + + + diff --git a/src/admin.php b/src/admin.php index 6489f8c1f2..863a6ced6d 100644 --- a/src/admin.php +++ b/src/admin.php @@ -4,13 +4,13 @@ * Theme customizer */ add_action('customize_register', function (\WP_Customize_Manager $wp_customize) { - // Add postMessage support - $wp_customize->get_setting('blogname')->transport = 'postMessage'; + // Add postMessage support + $wp_customize->get_setting('blogname')->transport = 'postMessage'; }); /** * Customizer JS */ add_action('customize_preview_init', function () { - wp_enqueue_script('sage/customizer.js', asset_path('scripts/customizer.js'), ['customize-preview'], null, true); + wp_enqueue_script('sage/customizer.js', asset_path('scripts/customizer.js'), ['customize-preview'], null, true); }); diff --git a/src/filters.php b/src/filters.php index 959b782e8e..90a95ec27c 100644 --- a/src/filters.php +++ b/src/filters.php @@ -9,45 +9,45 @@ */ add_filter('sage/display_sidebar', function ($display) { // The sidebar will NOT be displayed if ANY of the following return true - return $display ? !in_array(true, [ - is_404(), - is_front_page(), - is_page_template('template-custom.php'), - ]) : $display; + return $display ? !in_array(true, [ + is_404(), + is_front_page(), + is_page_template('template-custom.php'), + ]) : $display; }); /** * Add classes */ add_filter('body_class', function (array $classes) { - // 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 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 - if (display_sidebar()) { - $classes[] = 'sidebar-primary'; - } + // Add class if sidebar is active + if (display_sidebar()) { + $classes[] = 'sidebar-primary'; + } - return $classes; + return $classes; }); /** * Add "… Continued" to the excerpt */ add_filter('excerpt_more', function () { - return ' … ' . __('Continued', 'sage') . ''; + return ' … ' . __('Continued', 'sage') . ''; }); /** * Use theme wrapper */ add_filter('template_include', function ($main) { - if (!is_string($main) || !(string) $main) { - return $main; - } - return template_wrap(new Wrapper(basename($main))); + if (!is_string($main) || !(string) $main) { + return $main; + } + return template_wrap(new Wrapper(basename($main))); }, 109); diff --git a/src/helpers.php b/src/helpers.php index 0ddea2286c..80103780b3 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -6,12 +6,12 @@ use Roots\Sage\Template\WrapperInterface; function template_wrap(WrapperInterface $wrapper, $slug = 'base') { - WrapperCollection::add($wrapper, $slug); - return $wrapper->getWrapper(); + WrapperCollection::add($wrapper, $slug); + return $wrapper->getWrapper(); } function template_unwrap($slug = 'base') { - return WrapperCollection::get($slug)->getTemplate(); + return WrapperCollection::get($slug)->getTemplate(); } /** @@ -19,9 +19,9 @@ function template_unwrap($slug = 'base') { * @return string */ function asset_path($filename) { - static $manifest; - isset($manifest) || $manifest = new JsonManifest(get_template_directory() . '/' . Asset::$dist . '/assets.json'); - return (string) new Asset($filename, $manifest); + static $manifest; + isset($manifest) || $manifest = new JsonManifest(get_template_directory() . '/' . Asset::$dist . '/assets.json'); + return (string) new Asset($filename, $manifest); } /** @@ -29,9 +29,9 @@ function asset_path($filename) { * @return bool */ function display_sidebar() { - static $display; - isset($display) || $display = apply_filters('sage/display_sidebar', true); - return $display; + static $display; + isset($display) || $display = apply_filters('sage/display_sidebar', true); + return $display; } /** @@ -39,20 +39,20 @@ function display_sidebar() { * @return string */ function title() { - if (is_home()) { - if ($home = get_option('page_for_posts', true)) { - return get_the_title($home); + if (is_home()) { + if ($home = get_option('page_for_posts', true)) { + return get_the_title($home); + } + return __('Latest Posts', 'sage'); } - return __('Latest Posts', 'sage'); - } - if (is_archive()) { - return get_the_archive_title(); - } - if (is_search()) { - return sprintf(__('Search Results for %s', 'sage'), get_search_query()); - } - if (is_404()) { - return __('Not Found', 'sage'); - } - return get_the_title(); + if (is_archive()) { + return get_the_archive_title(); + } + if (is_search()) { + return sprintf(__('Search Results for %s', 'sage'), get_search_query()); + } + if (is_404()) { + return __('Not Found', 'sage'); + } + return get_the_title(); } diff --git a/src/lib/Sage/Asset.php b/src/lib/Sage/Asset.php index eb52ce6d7a..8fe70d718c 100644 --- a/src/lib/Sage/Asset.php +++ b/src/lib/Sage/Asset.php @@ -7,28 +7,29 @@ * @package Roots\Sage * @author QWp6t */ -class Asset { - public static $dist = '/dist'; +class Asset +{ + public static $dist = '/dist'; - /** @var ManifestInterface Currently used manifest */ - protected $manifest; + /** @var ManifestInterface Currently used manifest */ + protected $manifest; - protected $asset; + protected $asset; - protected $dir; + protected $dir; - public function __construct($file, ManifestInterface $manifest = null) { - $this->manifest = $manifest; - $this->asset = basename($file); - $this->dir = dirname($file) != '.' ? dirname($file) : ''; - } + public function __construct($file, ManifestInterface $manifest = null) { + $this->manifest = $manifest; + $this->asset = basename($file); + $this->dir = dirname($file) != '.' ? dirname($file) : ''; + } - public function __toString() { - return $this->getUri(); - } + public function __toString() { + return $this->getUri(); + } - public function getUri() { - $file = self::$dist . '/' . $this->dir . '/' . ($this->manifest ? $this->manifest->get($this->asset) : $this->asset); - return get_template_directory_uri() . $file; - } + public function getUri() { + $file = ($this->manifest ? $this->manifest->get($this->asset) : $this->asset); + return get_template_directory_uri() . self::$dist . '/' . $this->dir . '/' . $file; + } } diff --git a/src/lib/Sage/Assets/JsonManifest.php b/src/lib/Sage/Assets/JsonManifest.php index ff9936430e..1ce3377c91 100644 --- a/src/lib/Sage/Assets/JsonManifest.php +++ b/src/lib/Sage/Assets/JsonManifest.php @@ -5,25 +5,29 @@ * @package Roots\Sage * @author QWp6t */ -class JsonManifest implements ManifestInterface { - /** @var array */ - protected $manifest = []; +class JsonManifest implements ManifestInterface +{ + /** @var array */ + protected $manifest = []; - /** - * JsonManifest constructor - * @param string $manifestPath Local filesystem path to JSON-encoded manifest - */ - public function __construct($manifestPath) { - $this->manifest = file_exists($manifestPath) ? json_decode(file_get_contents($manifestPath), true) : []; - } + /** + * JsonManifest constructor + * @param string $manifestPath Local filesystem path to JSON-encoded manifest + */ + public function __construct($manifestPath) + { + $this->manifest = file_exists($manifestPath) ? json_decode(file_get_contents($manifestPath), true) : []; + } - /** @inheritdoc */ - public function get($file) { - return isset($this->manifest[$file]) ? $this->manifest[$file] : $file; - } + /** @inheritdoc */ + public function get($file) + { + return isset($this->manifest[$file]) ? $this->manifest[$file] : $file; + } - /** @inheritdoc */ - public function getAll() { - return $this->manifest; - } + /** @inheritdoc */ + public function getAll() + { + return $this->manifest; + } } diff --git a/src/lib/Sage/Assets/ManifestInterface.php b/src/lib/Sage/Assets/ManifestInterface.php index 714498341b..5af92e7746 100644 --- a/src/lib/Sage/Assets/ManifestInterface.php +++ b/src/lib/Sage/Assets/ManifestInterface.php @@ -5,21 +5,22 @@ * @package Roots\Sage * @author QWp6t */ -interface ManifestInterface { - /** - * Get the cache-busted filename - * - * If the manifest does not have an entry for $file, then return $file - * - * @param string $file The original name of the file before cache-busting - * @return string - */ - public function get($file); +interface ManifestInterface +{ + /** + * Get the cache-busted filename + * + * If the manifest does not have an entry for $file, then return $file + * + * @param string $file The original name of the file before cache-busting + * @return string + */ + public function get($file); - /** - * Get the asset manifest - * - * @return array - */ - public function getAll(); + /** + * Get the asset manifest + * + * @return array + */ + public function getAll(); } diff --git a/src/lib/Sage/Template/Wrapper.php b/src/lib/Sage/Template/Wrapper.php index c62bb58af8..4c291ccb53 100644 --- a/src/lib/Sage/Template/Wrapper.php +++ b/src/lib/Sage/Template/Wrapper.php @@ -5,55 +5,61 @@ * @package Roots\Sage * @author QWp6t */ -class Wrapper implements WrapperInterface { - /** @var string Wrapper slug */ - protected $slug; - - /** @var string Template file that is being wrapped */ - protected $template = ''; - - /** @var string[] Array of template wrappers; e.g., `base-singular.php`, `base-page.php`, `base.php` */ - protected $wrapper = []; - - /** @var string[] Cache template locations */ - protected static $locations = []; - - /** - * Wrapper constructor - * - * @param string $template Template file, as from Template Heirarchy; e.g., `page.php`, `single.php`, `singular.php` - * @param string $base Wrapper's base template, this is what will wrap around $template - */ - public function __construct($template, $base = 'layouts/base.php') { - $this->slug = sanitize_title(basename($base, '.php')); - $this->wrapper = [$base]; - $this->template = $template; - $str = substr($base, 0, -4); - array_unshift($this->wrapper, sprintf($str . '-%s.php', basename($template, '.php'))); - } - - /** - * @return string - * @see getTemplate - */ - public function __toString() { - return $this->getTemplate(); - } - - /** {@inheritdoc} */ - public function getWrapper() { - $wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrapper) ?: $this->wrapper; - return locate_template($wrappers); - } - - /** {@inheritdoc} */ - public function getSlug() { - return $this->slug; - } - - /** {@inheritdoc} */ - public function getTemplate() { - $template = apply_filters('sage/unwrap_' . $this->slug, $this->template) ?: $this->template; - return locate_template($template); - } +class Wrapper implements WrapperInterface +{ + /** @var string Wrapper slug */ + protected $slug; + + /** @var string Template file that is being wrapped */ + protected $template = ''; + + /** @var string[] Array of template wrappers; e.g., `base-singular.php`, `base-page.php`, `base.php` */ + protected $wrapper = []; + + /** @var string[] Cache template locations */ + protected static $locations = []; + + /** + * Wrapper constructor + * + * @param string $template Template file, as from Template Heirarchy; e.g., `page.php`, `single.php`, `singular.php` + * @param string $base Wrapper's base template, this is what will wrap around $template + */ + public function __construct($template, $base = 'layouts/base.php') + { + $this->slug = sanitize_title(basename($base, '.php')); + $this->wrapper = [$base]; + $this->template = $template; + $str = substr($base, 0, -4); + array_unshift($this->wrapper, sprintf($str . '-%s.php', basename($template, '.php'))); + } + + /** + * @return string + * @see getTemplate + */ + public function __toString() + { + return $this->getTemplate(); + } + + /** {@inheritdoc} */ + public function getWrapper() + { + $wrappers = apply_filters('sage/wrap_' . $this->slug, $this->wrapper) ?: $this->wrapper; + return locate_template($wrappers); + } + + /** {@inheritdoc} */ + public function getSlug() + { + return $this->slug; + } + + /** {@inheritdoc} */ + public function getTemplate() + { + $template = apply_filters('sage/unwrap_' . $this->slug, $this->template) ?: $this->template; + return locate_template($template); + } } diff --git a/src/lib/Sage/Template/WrapperCollection.php b/src/lib/Sage/Template/WrapperCollection.php index b50fd75f85..c1a46e28d6 100644 --- a/src/lib/Sage/Template/WrapperCollection.php +++ b/src/lib/Sage/Template/WrapperCollection.php @@ -5,69 +5,78 @@ * @package Roots\Sage * @author QWp6t */ -class WrapperCollection { - /** @var $this */ - protected static $instance; - /** @var WrapperInterface[] $wrappers */ - protected $wrappers = []; +class WrapperCollection +{ + /** @var $this */ + protected static $instance; + /** @var WrapperInterface[] $wrappers */ + protected $wrappers = []; - /** Singleton */ - private function __construct() {} - private function __clone() {} + /** Singleton */ + // @codingStandardsIgnoreStart + private function __construct() {} + private function __clone() {} + // @codingStandardsIgnoreEnd - /** - * @return static - */ - public static function instance() { - isset(self::$instance) || self::$instance = new static; - return self::$instance; - } + /** + * @return static + */ + public static function instance() + { + isset(self::$instance) || self::$instance = new static; + return self::$instance; + } - /** - * @param WrapperInterface $wrapper - * @param string $slug - * @param bool $overwriteIfExists - * @return $this - * @throws \Exception - */ - public static function add(WrapperInterface $wrapper, $slug = '', $overwriteIfExists = false) { - $slug = $slug ?: $wrapper->getSlug(); - if (self::instance()->exists($slug) && !$overwriteIfExists) { - throw new \Exception("Wrapper $slug already exists."); + /** + * @param WrapperInterface $wrapper + * @param string $slug + * @param bool $overwriteIfExists + * @return $this + * @throws \Exception + */ + public static function add(WrapperInterface $wrapper, $slug = '', $overwriteIfExists = false) + { + $slug = $slug ?: $wrapper->getSlug(); + if (self::instance()->exists($slug) && !$overwriteIfExists) { + throw new \Exception("Wrapper $slug already exists."); + } + self::instance()->wrappers[$slug] = $wrapper; + return self::instance(); } - self::instance()->wrappers[$slug] = $wrapper; - return self::instance(); - } - /** - * @param string $slug - * @return $this - */ - public static function remove($slug) { - unset(self::instance()->wrappers[$slug]); - return self::instance(); - } + /** + * @param string $slug + * @return $this + */ + public static function remove($slug) + { + unset(self::instance()->wrappers[$slug]); + return self::instance(); + } - /** - * @param string $slug - * @return null|WrapperInterface - */ - public static function get($slug) { - return isset(self::instance()->wrappers[$slug]) ? self::instance()->wrappers[$slug] : null; - } + /** + * @param string $slug + * @return null|WrapperInterface + */ + public static function get($slug) + { + return isset(self::instance()->wrappers[$slug]) ? self::instance()->wrappers[$slug] : null; + } - /** - * @return string[] Slugs of wrappers in collection - */ - public static function wrappers() { - return array_keys(self::instance()->wrappers); - } + /** + * @return string[] Slugs of wrappers in collection + */ + public static function wrappers() + { + return array_keys(self::instance()->wrappers); + } - /** - * @param $slug - * @return bool - */ - public static function exists($slug) { - return isset(self::instance()->wrappers[$slug]); - } + /** + * @param $slug + * @return bool + */ + public static function exists($slug) + { + return isset(self::instance()->wrappers[$slug]); + } } diff --git a/src/lib/Sage/Template/WrapperInterface.php b/src/lib/Sage/Template/WrapperInterface.php index b94cf08bbf..9f8d39bac7 100644 --- a/src/lib/Sage/Template/WrapperInterface.php +++ b/src/lib/Sage/Template/WrapperInterface.php @@ -5,22 +5,23 @@ * @package Roots\Sage * @author QWp6t */ -interface WrapperInterface { +interface WrapperInterface +{ - /** - * Get wrapper template file - * - * @return string Wrapper template (FQPN of, e.g., `base-page.php`, `base.php`) - */ - public function getWrapper(); + /** + * Get wrapper template file + * + * @return string Wrapper template (FQPN of, e.g., `base-page.php`, `base.php`) + */ + public function getWrapper(); - /** - * @return string Wrapped template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`) - */ - public function getTemplate(); + /** + * @return string Wrapped template (FQPN of, e.g., `page.php`, `single.php`, `singular.php`) + */ + public function getTemplate(); - /** - * @return string Slug of the WrapperInterface; e.g., `base` - */ - public function getSlug(); + /** + * @return string Slug of the WrapperInterface; e.g., `base` + */ + public function getSlug(); } diff --git a/src/setup.php b/src/setup.php index 0c11e6bc69..0d8c8f6e20 100644 --- a/src/setup.php +++ b/src/setup.php @@ -6,81 +6,81 @@ * Theme assets */ add_action('wp_enqueue_scripts', function () { - wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null); - wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true); + wp_enqueue_style('sage/main.css', asset_path('styles/main.css'), false, null); + wp_enqueue_script('sage/main.js', asset_path('scripts/main.js'), ['jquery'], null, true); }, 100); /** * Theme setup */ add_action('after_setup_theme', function () { - /** - * Enable features from Soil when plugin is activated - * @link https://roots.io/plugins/soil/ - */ - add_theme_support('soil-clean-up'); - add_theme_support('soil-jquery-cdn'); - add_theme_support('soil-nav-walker'); - add_theme_support('soil-nice-search'); - add_theme_support('soil-relative-urls'); + /** + * Enable features from Soil when plugin is activated + * @link https://roots.io/plugins/soil/ + */ + add_theme_support('soil-clean-up'); + add_theme_support('soil-jquery-cdn'); + add_theme_support('soil-nav-walker'); + add_theme_support('soil-nice-search'); + add_theme_support('soil-relative-urls'); - /** - * Enable plugins to manage the document title - * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Title_Tag - */ - add_theme_support('title-tag'); + /** + * Enable plugins to manage the document title + * @link http://codex.wordpress.org/Function_Reference/add_theme_support#Title_Tag + */ + add_theme_support('title-tag'); - /** - * Register navigation menus - * @link http://codex.wordpress.org/Function_Reference/register_nav_menus - */ - register_nav_menus([ - 'primary_navigation' => __('Primary Navigation', 'sage') - ]); + /** + * Register navigation menus + * @link http://codex.wordpress.org/Function_Reference/register_nav_menus + */ + register_nav_menus([ + 'primary_navigation' => __('Primary Navigation', 'sage') + ]); - /** - * Enable post thumbnails - * @link http://codex.wordpress.org/Post_Thumbnails - * @link http://codex.wordpress.org/Function_Reference/set_post_thumbnail_size - * @link http://codex.wordpress.org/Function_Reference/add_image_size - */ - add_theme_support('post-thumbnails'); + /** + * Enable post thumbnails + * @link http://codex.wordpress.org/Post_Thumbnails + * @link http://codex.wordpress.org/Function_Reference/set_post_thumbnail_size + * @link http://codex.wordpress.org/Function_Reference/add_image_size + */ + add_theme_support('post-thumbnails'); - /** - * Enable post formats - * @link http://codex.wordpress.org/Post_Formats - */ - add_theme_support('post-formats', ['aside', 'gallery', 'link', 'image', 'quote', 'video', 'audio']); + /** + * Enable post formats + * @link http://codex.wordpress.org/Post_Formats + */ + add_theme_support('post-formats', ['aside', 'gallery', 'link', 'image', 'quote', 'video', 'audio']); - /** - * Enable HTML5 markup support - * @link http://codex.wordpress.org/Function_Reference/add_theme_support#HTML5 - */ - add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']); + /** + * Enable HTML5 markup support + * @link http://codex.wordpress.org/Function_Reference/add_theme_support#HTML5 + */ + add_theme_support('html5', ['caption', 'comment-form', 'comment-list', 'gallery', 'search-form']); - /** - * Use main stylesheet for visual editor - * @see assets/styles/layouts/_tinymce.scss - */ - add_editor_style(asset_path('styles/main.css')); + /** + * Use main stylesheet for visual editor + * @see assets/styles/layouts/_tinymce.scss + */ + add_editor_style(asset_path('styles/main.css')); }); /** * Register sidebars */ add_action('widgets_init', function () { - $config = [ - 'before_widget' => '
', - 'after_widget' => '
', - 'before_title' => '

', - 'after_title' => '

' - ]; - register_sidebar([ - 'name' => __('Primary', 'sage'), - 'id' => 'sidebar-primary' - ] + $config); - register_sidebar([ - 'name' => __('Footer', 'sage'), - 'id' => 'sidebar-footer' - ] + $config); + $config = [ + 'before_widget' => '
', + 'after_widget' => '
', + 'before_title' => '

', + 'after_title' => '

' + ]; + register_sidebar([ + 'name' => __('Primary', 'sage'), + 'id' => 'sidebar-primary' + ] + $config); + register_sidebar([ + 'name' => __('Footer', 'sage'), + 'id' => 'sidebar-footer' + ] + $config); });