Files
sage/lib/conditional-tag-check.php
Scott Walkinshaw fe94039e70 Update ConditionalTagCheck and usage docs
When passing an argument to a function for ConditionalTagCheck, the
argument should *not* always be an array. The function will be called
wit the arguments as is and not modified. So if the function takes a
string argument, keep it as a string. If it takes an array, use an
array.
2015-03-03 15:26:30 -05: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 `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) {
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;
}
}
}