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.
44 lines
1.0 KiB
PHP
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;
|
|
}
|
|
}
|
|
}
|