From 78eab9c702027f9b2f618a447d8921fb9bdc1cbe Mon Sep 17 00:00:00 2001 From: Foxaii Date: Thu, 4 Jul 2013 17:01:29 +0100 Subject: [PATCH] Rework Roots_Wrapping class Apply the wrapper to any template by creating a new instance. --- lib/wrapper.php | 46 +++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/wrapper.php b/lib/wrapper.php index 1ce5e5f..f2482dd 100644 --- a/lib/wrapper.php +++ b/lib/wrapper.php @@ -9,7 +9,7 @@ function roots_template_path() { } function roots_sidebar_path() { - return Roots_Wrapping::sidebar(); + return Roots_Wrapping('templates/sidebar.php'); } class Roots_Wrapping { @@ -19,34 +19,30 @@ class Roots_Wrapping { // Stores the base name of the template file; e.g. 'page' for 'page.php' etc. static $base; - static function wrap($template) { - self::$main_template = $template; - - self::$base = substr(basename(self::$main_template), 0, -4); + public function __construct($template='base.php') { + $this->slug = basename($template, '.php'); + $this->templates = array($template); + + if (self::$base) { + $str = substr($template, 0, -4); + array_unshift($this->templates, sprintf($str . '-%s.php', self::$base)); + } + } + + public function __toString() { + $this->templates = apply_filters('roots_wrap_' . $this->slug, $this->templates); + return locate_template($this->templates); + } + + static function wrap($main) { + self::$main_template = $main; + self::$base = basename(self::$main_template, '.php'); if (self::$base === 'index') { self::$base = false; } - - $templates = array('base.php'); - - if (self::$base) { - array_unshift($templates, sprintf('base-%s.php', self::$base)); - } - - $templates = apply_filters('roots_wrap_base', $templates); - return locate_template($templates); - } - - static function sidebar() { - $templates = array('templates/sidebar.php'); - - if (self::$base) { - array_unshift($templates, sprintf('templates/sidebar-%s.php', self::$base)); - } - - $templates = apply_filters('roots_wrap_sidebar', $templates); - return locate_template($templates); + + return new Roots_Wrapping(); } } add_filter('template_include', array('Roots_Wrapping', 'wrap'), 99);