Skip to content

Commit

Permalink
Refactor sidebar configuration
Browse files Browse the repository at this point in the history
Instead of editing an if statement (which could get ugly quickly), just
edit configuration arrays of conditional tags and page template checks.
  • Loading branch information
swalkinshaw committed Oct 2, 2012
1 parent 33d78b7 commit 588ea8a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion base.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<div id="main" class="<?php roots_main_class(); ?>" role="main">
<?php include roots_template_path(); ?>
</div>
<?php if (roots_sidebar()) : ?>
<?php if (roots_display_sidebar()) : ?>
<aside id="sidebar" class="<?php roots_sidebar_class(); ?>" role="complementary">
<?php get_template_part('templates/sidebar'); ?>
</aside>
Expand Down
4 changes: 4 additions & 0 deletions doc/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 27 additions & 8 deletions lib/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,37 @@
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';
Expand Down
47 changes: 47 additions & 0 deletions lib/sidebar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* Determines whether or not to display the sidebar based on an array of conditional tags or page templates.
*
* 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()
*
* @return boolean True will display the sidebar, False will not
*
*/
class Roots_Sidebar {
const EXTENSION = '.php';
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;
}
}

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);
}
}

?>

0 comments on commit 588ea8a

Please sign in to comment.