Files
bedrock/lib/conditional-tag-check.php
Louis des Landes 4c51724b25 Adjust comments on ConditionalTagCheck to match actual functionality.
The check returns false, if any of it's tags are true, not vise versa.
2015-06-02 15:08:03 +10:00

44 lines
1.0 KiB
PHP

<?php
namespace Roots\Sage;
/**
* Utility class which takes an array of conditional tags (or any function which returns a boolean)
* and returns `false` if *any* of them are `true`, and `true` 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) {
if (is_array($conditional)) {
list($tag, $args) = $conditional;
} else {
$tag = $conditional;
$args = false;
}
if (function_exists($tag)) {
return $args ? $tag($args) : $tag();
} else {
return false;
}
}
}