PluginProbe
Filter Everything — WordPress & WooCommerce Filters / 1.9.7
Filter Everything — WordPress & WooCommerce Filters v1.9.7
1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 All 52 releases
filter-everything / src / FilterContext.php

FilterContext.php in Filter Everything — WordPress & WooCommerce Filters 1.9.7, at src/FilterContext.php

186 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FilterEverything\Filter;
4
5 if ( ! defined('ABSPATH') ) {
6 exit;
7 }
8
9 /**
10 * Per-request filter state.
11 *
12 * One instance per HTTP request (see Container::getFilterContext()). It owns the
13 * state that the request pipeline steps (WpManager → EntityManager → widgets)
14 * pass to each other: the parsed filter request, the sets relevant for the
15 * page, the JSON payload for the frontend script and a few one-shot flags.
16 *
17 * Until 1.9.7 this state lived in WpManager::$filterQueryVars and in the PHP
18 * globals $flrt_sets, $flrt_json_data, $wpc_not_fired and $chips_count.
19 *
20 * @since 1.9.7
21 */
22 class FilterContext
23 {
24 /**
25 * Write-once request variables (queried_values, wpc_page_related_set_ids, …).
26 * @var array
27 */
28 private $vars = [];
29
30 /** @var bool */
31 private $filterRequest = false;
32
33 /**
34 * Queue of the filter sets relevant for the current page.
35 * flrt_the_set() consumes it while widgets render.
36 * @var array
37 */
38 private $sets = [];
39
40 /**
41 * Payload for the frontend script (instant recount, permalinks map …).
42 * @var array
43 */
44 private $jsonData = [];
45
46 /** @var bool */
47 private $mainQueryHandled = false;
48
49 /** @var int */
50 private $chipsCount = 0;
51
52 /* ---------------------------------------------------------------------
53 * Request variables (write-once, same contract as WpManager::setQueryVar)
54 * ------------------------------------------------------------------- */
55
56 /**
57 * Stores a variable only if it is not set yet.
58 *
59 * @return bool true when stored, false when the key already existed.
60 */
61 public function set( $key, $value )
62 {
63 if ( ! isset( $this->vars[ $key ] ) ) {
64 $this->vars[ $key ] = $value;
65 return true;
66 }
67 return false;
68 }
69
70 /**
71 * Deliberate overwrite of an existing variable. Used once, by
72 * WpManager::populateQueriedValuesWithAdditionalParams() when the logic
73 * separators become known after the relevant set is resolved.
74 */
75 public function replace( $key, $value )
76 {
77 $this->vars[ $key ] = $value;
78 }
79
80 public function get( $key, $default = false )
81 {
82 if ( isset( $this->vars[ $key ] ) ) {
83 return $this->vars[ $key ];
84 }
85 return $default;
86 }
87
88 public function has( $key )
89 {
90 return isset( $this->vars[ $key ] );
91 }
92
93 /* ---------------------------------------------------------------------
94 * Filter request flag
95 * ------------------------------------------------------------------- */
96
97 public function isFilterRequest()
98 {
99 return $this->filterRequest;
100 }
101
102 public function markFilterRequest()
103 {
104 $this->filterRequest = true;
105 }
106
107 /* ---------------------------------------------------------------------
108 * Relevant sets queue
109 * ------------------------------------------------------------------- */
110
111 public function setSets( array $sets )
112 {
113 $this->sets = $sets;
114 }
115
116 public function getSets()
117 {
118 return $this->sets;
119 }
120
121 /**
122 * Removes and returns one set from the queue: the one with the given ID,
123 * or the first one when $set_id is 0. Returns null when nothing is left.
124 */
125 public function takeSet( $set_id = 0 )
126 {
127 if ( $set_id ) {
128 foreach ( $this->sets as $k => $set ) {
129 if ( $set['ID'] === $set_id ) {
130 unset( $this->sets[ $k ] );
131 return $set;
132 }
133 }
134 }
135
136 return array_shift( $this->sets );
137 }
138
139 /* ---------------------------------------------------------------------
140 * Frontend JSON payload
141 * ------------------------------------------------------------------- */
142
143 /**
144 * Returned by reference so producers can write into it in place:
145 * $json = &$ctx->jsonData();
146 * $json[ $setId ]['allEntities'] = …;
147 *
148 * @return array
149 */
150 public function &jsonData()
151 {
152 return $this->jsonData;
153 }
154
155 public function hasJsonData()
156 {
157 return ! empty( $this->jsonData );
158 }
159
160 /* ---------------------------------------------------------------------
161 * One-shot flags
162 * ------------------------------------------------------------------- */
163
164 /**
165 * True until markMainQueryHandled() is called. Guards the section of
166 * WpManager::addFilterQueryToWpQuery() that must run once per request.
167 */
168 public function isMainQueryPending()
169 {
170 return ! $this->mainQueryHandled;
171 }
172
173 public function markMainQueryHandled()
174 {
175 $this->mainQueryHandled = true;
176 }
177
178 /**
179 * Next 1-based index for a rendered chips list (unique CSS class per list).
180 */
181 public function nextChipsIndex()
182 {
183 return ++$this->chipsCount;
184 }
185 }
186