| 1 |
<?php |
| 2 |
/** |
| 3 |
* Globals service. |
| 4 |
* |
| 5 |
* @copyright (c) 2021, Code Atlantic LLC. |
| 6 |
* @package ContentControl |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Services; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Globals service. |
| 15 |
*/ |
| 16 |
class Globals { |
| 17 |
|
| 18 |
/** |
| 19 |
* Context. |
| 20 |
* |
| 21 |
* @var string|null |
| 22 |
*/ |
| 23 |
public $current_query_context = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Current query. |
| 27 |
* |
| 28 |
* @var \WP_Query|\WP_Term_Query|null |
| 29 |
*/ |
| 30 |
public $current_query = null; |
| 31 |
|
| 32 |
/** |
| 33 |
* Current post query. |
| 34 |
* |
| 35 |
* @var \WP_Query|null |
| 36 |
*/ |
| 37 |
public $current_post_query = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* Current term query. |
| 41 |
* |
| 42 |
* @var \WP_Term_Query|null |
| 43 |
*/ |
| 44 |
public $current_term_query = null; |
| 45 |
|
| 46 |
/** |
| 47 |
* Current term. |
| 48 |
* |
| 49 |
* @var \WP_Term|null |
| 50 |
*/ |
| 51 |
public $term = null; |
| 52 |
|
| 53 |
/** |
| 54 |
* Overloaded posts stack. |
| 55 |
* |
| 56 |
* @var \WP_Post[]|null $overloaded_posts |
| 57 |
*/ |
| 58 |
public $overloaded_posts = null; |
| 59 |
|
| 60 |
/** |
| 61 |
* Overloaded terms stack. |
| 62 |
* |
| 63 |
* @var \WP_Term[]|null $overloaded_terms |
| 64 |
*/ |
| 65 |
public $overloaded_terms = null; |
| 66 |
|
| 67 |
/** |
| 68 |
* Last term. |
| 69 |
* |
| 70 |
* @var \WP_Term[]|null |
| 71 |
*/ |
| 72 |
public $last_term = null; |
| 73 |
|
| 74 |
/** |
| 75 |
* Current rule. |
| 76 |
* |
| 77 |
* @var \ContentControl\Models\RuleEngine\Rule|null |
| 78 |
*/ |
| 79 |
public $current_rule = null; |
| 80 |
|
| 81 |
/** |
| 82 |
* Current intent. |
| 83 |
* |
| 84 |
* @var null|array{ |
| 85 |
* type: string, |
| 86 |
* name: string, |
| 87 |
* id: int, |
| 88 |
* index: bool, |
| 89 |
* search: string|false, |
| 90 |
* } |
| 91 |
*/ |
| 92 |
public $rest_intent = null; |
| 93 |
|
| 94 |
/** |
| 95 |
* Constructor. |
| 96 |
*/ |
| 97 |
public function __construct() { |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get context items by key. |
| 102 |
* |
| 103 |
* @param string $key Context key. |
| 104 |
* @param mixed $default_value Default value. |
| 105 |
* |
| 106 |
* @return mixed |
| 107 |
*/ |
| 108 |
public function get( $key, $default_value = null ) { |
| 109 |
return property_exists( $this, $key ) ? $this->$key : $default_value; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Set context items by key. |
| 114 |
* |
| 115 |
* @param string $key Context key. |
| 116 |
* @param mixed $value Context value. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function set( $key, $value ) { |
| 121 |
if ( property_exists( $this, $key ) ) { |
| 122 |
$this->$key = $value; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Reset context items by key. |
| 128 |
* |
| 129 |
* @param string $key Context key. |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public function reset( $key ) { |
| 134 |
if ( property_exists( $this, $key ) ) { |
| 135 |
$this->$key = null; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Reset all context items. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function reset_all() { |
| 145 |
$this->reset( 'current_query_context' ); |
| 146 |
$this->reset( 'current_query' ); |
| 147 |
$this->reset( 'current_post_query' ); |
| 148 |
$this->reset( 'current_term_query' ); |
| 149 |
$this->reset( 'current_rule' ); |
| 150 |
$this->reset( 'term' ); |
| 151 |
$this->reset( 'overloaded_posts' ); |
| 152 |
$this->reset( 'overloaded_terms' ); |
| 153 |
$this->reset( 'last_term' ); |
| 154 |
$this->reset( 'rest_intent' ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Push to stack. |
| 159 |
* |
| 160 |
* @param string $key Context key. |
| 161 |
* @param mixed $value Context value. |
| 162 |
* |
| 163 |
* @return void |
| 164 |
*/ |
| 165 |
public function push_to_stack( $key, $value ) { |
| 166 |
if ( property_exists( $this, $key ) ) { |
| 167 |
$values = $this->get( $key, [] ); |
| 168 |
|
| 169 |
$values[] = $value; |
| 170 |
|
| 171 |
$this->set( $key, $values ); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Pop from stack. |
| 177 |
* |
| 178 |
* @param string $key Context key. |
| 179 |
* |
| 180 |
* @return mixed |
| 181 |
*/ |
| 182 |
public function pop_from_stack( $key ) { |
| 183 |
if ( property_exists( $this, $key ) ) { |
| 184 |
$values = $this->get( $key, [] ); |
| 185 |
|
| 186 |
if ( empty( $values ) ) { |
| 187 |
return null; |
| 188 |
} |
| 189 |
|
| 190 |
$value = array_pop( $values ); |
| 191 |
|
| 192 |
$this->set( $key, $values ); |
| 193 |
|
| 194 |
return $value; |
| 195 |
} |
| 196 |
|
| 197 |
return null; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Check if stack is empty. |
| 202 |
* |
| 203 |
* @param string $key Context key. |
| 204 |
* |
| 205 |
* @return bool |
| 206 |
*/ |
| 207 |
public function is_empty( $key ) { |
| 208 |
$value = $this->get( $key ); |
| 209 |
|
| 210 |
return ! isset( $value ) || empty( $value ); |
| 211 |
} |
| 212 |
} |
| 213 |
|