From 588ea8afa31080568f5115522d39ded8e8f48953 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Thu, 20 Sep 2012 14:36:25 -0600 Subject: [PATCH 1/5] Refactor sidebar configuration Instead of editing an if statement (which could get ugly quickly), just edit configuration arrays of conditional tags and page template checks. --- CHANGELOG.md | 1 + base.php | 2 +- doc/lib.md | 4 ++++ functions.php | 1 + lib/config.php | 35 +++++++++++++++++++++++++++-------- lib/sidebar.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 lib/sidebar.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f28fdd..5a47d3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ### HEAD +* Change roots_sidebar into a more explicit configuration array * Re-organize configuration/setup files * Update to jQuery 1.8.2 * Refactor/simplify Roots vCard Widget diff --git a/base.php b/base.php index dc4c79b..02e31ed 100644 --- a/base.php +++ b/base.php @@ -17,7 +17,7 @@
- +
diff --git a/doc/lib.md b/doc/lib.md index b173127..153991e 100644 --- a/doc/lib.md +++ b/doc/lib.md @@ -67,6 +67,10 @@ This file is a placeholder for you to put in [custom post types](http://codex.wo This file handles all of the CSS and JavaScript. +### sidebar.php + +Class which provides a simple configuration interface to define what pages you want to show the sidebar on. + #### Stylesheets Stylesheets are enqueued in the following order: diff --git a/functions.php b/functions.php index 9fde0de..c2a1d0e 100644 --- a/functions.php +++ b/functions.php @@ -7,6 +7,7 @@ require_once locate_template('/lib/utils.php'); // Utility functions require_once locate_template('/lib/init.php'); // Initial theme setup and constants +require_once locate_template('/lib/sidebar.php'); // Sidebar class require_once locate_template('/lib/config.php'); // Configuration require_once locate_template('/lib/activation.php'); // Theme activation require_once locate_template('/lib/cleanup.php'); // Cleanup diff --git a/lib/config.php b/lib/config.php index 165001b..48eff61 100644 --- a/lib/config.php +++ b/lib/config.php @@ -9,18 +9,37 @@ add_theme_support('rewrite-urls'); // Enable URL rewrites add_theme_support('h5bp-htaccess'); // Enable HTML5 Boilerplate's .htaccess add_theme_support('bootstrap-top-navbar'); // Enable Bootstrap's fixed navbar -// Define which pages shouldn't have the sidebar -function roots_sidebar() { - if (is_404() || is_page_template('page-custom.php')) { - return false; - } else { - return true; - } + +/** + * Define which pages shouldn't have the sidebar + * + * See lib/sidebar.php for more details + */ +function roots_display_sidebar() { + $exclude = new Roots_Sidebar( + /** + * Conditionals tag checks (http://codex.wordpress.org/Conditional_Tags) + * Any of these conditional tags that return true won't show the sidebar + */ + array( + '404', + 'front_page' + ), + /** + * Page template checks (via is_page_template()) + * Any of these page templates that return true won't show the sidebar + */ + array( + 'page-custom' + ) + ); + + return $exclude->display; } // #main CSS classes function roots_main_class() { - if (roots_sidebar()) { + if (roots_display_sidebar()) { echo 'span8'; } else { echo 'span12'; diff --git a/lib/sidebar.php b/lib/sidebar.php new file mode 100644 index 0000000..5427860 --- /dev/null +++ b/lib/sidebar.php @@ -0,0 +1,47 @@ +conditionals = $conditionals; + $this->templates = $templates; + + foreach($this->conditionals as $conditional_tag) { + if ($this->check_conditional_tag($conditional_tag)) { + $this->display = false; + } + } + + foreach($this->templates as $page_template) { + if ($this->check_page_template($page_template)) { + $this->display = false; + } + } + } + + private function check_conditional_tag($conditional_tag) { + $function = "is_$conditional_tag"; + return $function(); + } + + private function check_page_template($page_template) { + return is_page_template($page_template . self::EXTENSION); + } +} + +?> From a7ab3e03079ba0a20f3607a1aef22c45c677b372 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Thu, 20 Sep 2012 15:18:11 -0600 Subject: [PATCH 2/5] Minor naming tweak --- lib/sidebar.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/sidebar.php b/lib/sidebar.php index 5427860..a8baf32 100644 --- a/lib/sidebar.php +++ b/lib/sidebar.php @@ -1,5 +1,4 @@ From 85596e11d0f9aff85b501967c124b1c8bee20a6a Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Thu, 20 Sep 2012 16:47:23 -0600 Subject: [PATCH 3/5] Clarity over cleverness --- lib/config.php | 6 +++--- lib/sidebar.php | 10 ++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/config.php b/lib/config.php index 48eff61..9ab33df 100644 --- a/lib/config.php +++ b/lib/config.php @@ -22,15 +22,15 @@ function roots_display_sidebar() { * Any of these conditional tags that return true won't show the sidebar */ array( - '404', - 'front_page' + 'is_404', + 'is_front_page' ), /** * Page template checks (via is_page_template()) * Any of these page templates that return true won't show the sidebar */ array( - 'page-custom' + 'page-custom.php' ) ); diff --git a/lib/sidebar.php b/lib/sidebar.php index a8baf32..93d821c 100644 --- a/lib/sidebar.php +++ b/lib/sidebar.php @@ -4,14 +4,13 @@ * * If any of the is_* conditional tags or is_page_template(template_file) checks return true, the sidebar will NOT be displayed. * - * @param array list of conditional tags (http://codex.wordpress.org/Conditional_Tags) without the 'is_' prefix - * @param array list of templates without the '.php' extension. These will be checked via is_page_template() + * @param array list of conditional tags (http://codex.wordpress.org/Conditional_Tags) + * @param array list of page templates. These will be checked via is_page_template() * * @return boolean True will display the sidebar, False will not * */ class Roots_Sidebar { - const EXTENSION = '.php'; private $conditionals; private $templates; public $display = true; @@ -34,12 +33,11 @@ class Roots_Sidebar { } private function check_conditional_tag($conditional_tag) { - $conditional_tag_function = "is_$conditional_tag"; - return $conditional_tag_function(); + return $conditional_tag(); } private function check_page_template($page_template) { - return is_page_template($page_template . self::EXTENSION); + return is_page_template($page_template); } } ?> From 7f5eea8c69a3ec88a3ff66edd372a3a5fba7da88 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Tue, 2 Oct 2012 15:36:51 -0400 Subject: [PATCH 4/5] Refactor Roots_Sidebar --- lib/config.php | 6 +++--- lib/sidebar.php | 14 +++++--------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/lib/config.php b/lib/config.php index 9ab33df..8e8640d 100644 --- a/lib/config.php +++ b/lib/config.php @@ -16,9 +16,9 @@ add_theme_support('bootstrap-top-navbar'); // Enable Bootstrap's fixed navbar * See lib/sidebar.php for more details */ function roots_display_sidebar() { - $exclude = new Roots_Sidebar( + $sidebar_config = new Roots_Sidebar( /** - * Conditionals tag checks (http://codex.wordpress.org/Conditional_Tags) + * Conditional tag checks (http://codex.wordpress.org/Conditional_Tags) * Any of these conditional tags that return true won't show the sidebar */ array( @@ -34,7 +34,7 @@ function roots_display_sidebar() { ) ); - return $exclude->display; + return $sidebar_config->display; } // #main CSS classes diff --git a/lib/sidebar.php b/lib/sidebar.php index 93d821c..e9841df 100644 --- a/lib/sidebar.php +++ b/lib/sidebar.php @@ -13,22 +13,18 @@ class Roots_Sidebar { private $conditionals; private $templates; + public $display = true; function __construct($conditionals = array(), $templates = array()) { $this->conditionals = $conditionals; $this->templates = $templates; - foreach($this->conditionals as $conditional_tag) { - if ($this->check_conditional_tag($conditional_tag)) { - $this->display = false; - } - } + $conditionals = array_map($this->check_conditional_tag, $this->conditionals); + $templates = array_map($this->check_page_template, $this->templates); - foreach($this->templates as $page_template) { - if ($this->check_page_template($page_template)) { - $this->display = false; - } + if (in_array(true, $conditionals) || in_array(true, $templates)) { + $this->display = false; } } From c02f877b59ece6cb572edaba87b711ecfbf1b2e2 Mon Sep 17 00:00:00 2001 From: Scott Walkinshaw Date: Tue, 2 Oct 2012 15:52:31 -0400 Subject: [PATCH 5/5] Fix callback reference --- lib/sidebar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sidebar.php b/lib/sidebar.php index e9841df..26c4e00 100644 --- a/lib/sidebar.php +++ b/lib/sidebar.php @@ -20,8 +20,8 @@ class Roots_Sidebar { $this->conditionals = $conditionals; $this->templates = $templates; - $conditionals = array_map($this->check_conditional_tag, $this->conditionals); - $templates = array_map($this->check_page_template, $this->templates); + $conditionals = array_map(array($this, 'check_conditional_tag'), $this->conditionals); + $templates = array_map(array($this, 'check_page_template'), $this->templates); if (in_array(true, $conditionals) || in_array(true, $templates)) { $this->display = false;