Convert Sidebar to ConditionalTagCheck
Simplify and generalize class since it had nothing to do directly with the sidebar. Now it can be used for other purposes as well.
This commit is contained in:
@@ -13,7 +13,7 @@ $sage_includes = [
|
||||
'lib/utils.php', // Utility functions
|
||||
'lib/init.php', // Initial theme setup and constants
|
||||
'lib/wrapper.php', // Theme wrapper class
|
||||
'lib/sidebar.php', // Sidebar class
|
||||
'lib/conditional-tag-check.php', // ConditionalTagCheck class
|
||||
'lib/config.php', // Configuration
|
||||
'lib/assets.php', // Scripts and stylesheets
|
||||
'lib/titles.php', // Page titles
|
||||
|
||||
39
lib/conditional-tag-check.php
Normal file
39
lib/conditional-tag-check.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Roots\Sage;
|
||||
|
||||
/**
|
||||
* Utility class which takes an array of conditional tags (or any function which returns a boolean)
|
||||
* and returns `true` if *any* of them are `true`, and `false` otherwise.
|
||||
*
|
||||
* @param array list of conditional tags (http://codex.wordpress.org/Conditional_Tags)
|
||||
* or custom function which returns a boolean
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
class ConditionalTagCheck {
|
||||
private $conditionals;
|
||||
|
||||
public $result = true;
|
||||
|
||||
public function __construct($conditionals = []) {
|
||||
$this->conditionals = $conditionals;
|
||||
|
||||
$conditionals = array_map([$this, 'checkConditionalTag'], $this->conditionals);
|
||||
|
||||
if (in_array(true, $conditionals)) {
|
||||
$this->result = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkConditionalTag($conditional_tag) {
|
||||
$conditional_arg = is_array($conditional_tag) ? $conditional_tag[1] : false;
|
||||
$conditional_tag = $conditional_arg ? $conditional_tag[0] : $conditional_tag;
|
||||
|
||||
if (function_exists($conditional_tag)) {
|
||||
return $conditional_arg ? $conditional_tag($conditional_arg) : $conditional_tag();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Roots\Sage\Config;
|
||||
|
||||
use Roots\Sage\Sidebar;
|
||||
use Roots\Sage;
|
||||
|
||||
/**
|
||||
* Enable theme features
|
||||
@@ -42,30 +42,33 @@ function display_sidebar() {
|
||||
static $display;
|
||||
|
||||
if (!isset($display)) {
|
||||
$sidebar_config = new Sidebar\SageSidebar(
|
||||
$conditionalCheck = new ConditionalTagCheck(
|
||||
/**
|
||||
* Conditional tag checks (http://codex.wordpress.org/Conditional_Tags)
|
||||
* Any of these conditional tags that return true won't show the sidebar
|
||||
* Any of these conditional tags that return true won't show the sidebar.
|
||||
* You can also specify your own custom function as long as it returns a boolean.
|
||||
*
|
||||
* To use a function that accepts arguments, use the following format:
|
||||
*
|
||||
* ['function_name', ['arg1', 'arg2']]
|
||||
*
|
||||
* The second element must be an array even if there's only 1 argument.
|
||||
* Note: The second element must be an array even if there's only 1 argument.
|
||||
*
|
||||
* Examples:
|
||||
*
|
||||
* 'is_single'
|
||||
* ['is_page', ['about-me']]
|
||||
* ['is_tax', ['flavor', 'mild']]
|
||||
* ['is_page_template', ['about.php']]
|
||||
* ['is_post_type_archive', [['foo', 'bar', 'baz']]]
|
||||
*
|
||||
*/
|
||||
[
|
||||
'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
|
||||
*/
|
||||
[
|
||||
'template-custom.php'
|
||||
]
|
||||
);
|
||||
$display = apply_filters('sage/display_sidebar', $sidebar_config->display);
|
||||
|
||||
$display = apply_filters('sage/display_sidebar', $conditionalCheck->result);
|
||||
}
|
||||
|
||||
return $display;
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Roots\Sage\Sidebar;
|
||||
|
||||
use Roots\Sage\Wrapper;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @link http://roots.io/getting-started/theme-sidebar/
|
||||
*
|
||||
* @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 SageSidebar {
|
||||
private $conditionals;
|
||||
private $templates;
|
||||
|
||||
public $display = true;
|
||||
|
||||
public function __construct($conditionals = [], $templates = []) {
|
||||
$this->conditionals = $conditionals;
|
||||
$this->templates = $templates;
|
||||
|
||||
$conditionals = array_map([$this, 'checkConditionalTag'], $this->conditionals);
|
||||
$templates = array_map([$this, 'checkPageTemplate'], $this->templates);
|
||||
|
||||
if (in_array(true, $conditionals) || in_array(true, $templates)) {
|
||||
$this->display = false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkConditionalTag($conditional_tag) {
|
||||
$conditional_arg = is_array($conditional_tag) ? $conditional_tag[1] : false;
|
||||
$conditional_tag = $conditional_arg ? $conditional_tag[0] : $conditional_tag;
|
||||
|
||||
if (function_exists($conditional_tag)) {
|
||||
return $conditional_arg ? $conditional_tag($conditional_arg) : $conditional_tag();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function checkPageTemplate($page_template) {
|
||||
return is_page_template($page_template) || Wrapper\SageWrapping::$base . '.php' === $page_template;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user