Diff: STRATO-apps/wordpress_03/app/wp-content/themes/blocksy/inc/components/builder/header-logic.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + class Blocksy_Header_Builder {
4 + private $default_value = null;
5 +
6 + private $section_value = null;
7 + private $current_section = null;
8 +
9 + public function get_default_value() {
10 + if ($this->default_value) {
11 + return $this->default_value;
12 + }
13 +
14 + $this->default_value = apply_filters('blocksy:header:default_value', [
15 + 'current_section' => 'type-1',
16 + 'sections' => [
17 + $this->get_structure_for([
18 + 'id' => 'type-1',
19 + 'mode' => 'placements',
20 + 'items' => [
21 + 'desktop' => [
22 + 'middle-row' => [
23 + 'start' => ['logo'],
24 + 'end' => ['menu', 'search']
25 + ]
26 + ],
27 +
28 + 'mobile' => [
29 + 'middle-row' => [
30 + 'start' => ['logo'],
31 + 'end' => ['trigger']
32 + ],
33 +
34 + 'offcanvas' => [
35 + 'start' => [
36 + 'mobile-menu',
37 + ]
38 + ]
39 + ]
40 + ]
41 + ])
42 + ]
43 + ], $this);
44 +
45 + return $this->default_value;
46 + }
47 +
48 + public function get_current_section_id() {
49 + return $this->get_current_section()['id'];
50 + }
51 +
52 + public function get_current_section($initial_section_id = null) {
53 + // TODO: needs heavy refactoring
54 +
55 + if (! $this->current_section || $initial_section_id) {
56 + if (! $initial_section_id) {
57 + $section_id = $this->get_filtered_section_id();
58 + } else {
59 + $section_id = $initial_section_id;
60 + }
61 +
62 + if (! $initial_section_id) {
63 + $this->current_section = $this->get_section_value()['sections'][0];
64 + }
65 +
66 + foreach ($this->get_section_value()['sections'] as $single_section) {
67 + if ($single_section['id'] === $section_id) {
68 + if (! $initial_section_id) {
69 + $this->current_section = $single_section;
70 + } else {
71 + return $single_section;
72 + }
73 +
74 + break;
75 + }
76 + }
77 +
78 + if ($initial_section_id) {
79 + return $this->get_section_value()['sections'][0];
80 + }
81 + }
82 +
83 + return $this->current_section;
84 + }
85 +
86 + public function get_structure_for($args = []) {
87 + $args = wp_parse_args($args, [
88 + 'id' => null,
89 + 'name' => null,
90 + 'mode' => 'placements',
91 + 'items' => [],
92 + 'settings' => []
93 + ]);
94 +
95 + $args['items'] = wp_parse_args($args['items'], [
96 + 'desktop' => [],
97 + 'mobile' => []
98 + ]);
99 +
100 + $args['items']['desktop'] = wp_parse_args($args['items']['desktop'], [
101 + 'top-row' => [],
102 + 'middle-row' => [],
103 + 'bottom-row' => [],
104 + 'offcanvas' => []
105 + ]);
106 +
107 + $args['items']['mobile'] = wp_parse_args($args['items']['mobile'], [
108 + 'top-row' => [],
109 + 'middle-row' => [],
110 + 'bottom-row' => [],
111 + 'offcanvas' => [],
112 + ]);
113 +
114 + $base = [
115 + 'id' => $args['id'],
116 + 'mode' => $args['mode'],
117 + 'items' => [],
118 + 'settings' => $args['settings']
119 + ];
120 +
121 + if ($args['name']) {
122 + $base['name'] = $args['name'];
123 + }
124 +
125 + if ($args['mode'] === 'placements') {
126 + $base['desktop'] = [
127 + $this->get_bar_structure_for([
128 + 'id' => 'top-row',
129 + 'mode' => $args['mode'],
130 + 'items' => $args['items']['desktop']['top-row']
131 + ]),
132 + $this->get_bar_structure_for([
133 + 'id' => 'middle-row',
134 + 'mode' => $args['mode'],
135 + 'items' => $args['items']['desktop']['middle-row']
136 + ]),
137 + $this->get_bar_structure_for([
138 + 'id' => 'bottom-row',
139 + 'mode' => $args['mode'],
140 + 'items' => $args['items']['desktop']['bottom-row']
141 + ]),
142 + $this->get_bar_structure_for([
143 + 'id' => 'offcanvas',
144 + 'mode' => $args['mode'],
145 + 'has_secondary' => false,
146 + 'items' => $args['items']['desktop']['offcanvas']
147 + ]),
148 + ];
149 +
150 + $base['mobile'] = [
151 + $this->get_bar_structure_for([
152 + 'id' => 'top-row',
153 + 'mode' => $args['mode'],
154 + 'items' => $args['items']['mobile']['top-row']
155 + ]),
156 + $this->get_bar_structure_for([
157 + 'id' => 'middle-row',
158 + 'mode' => $args['mode'],
159 + 'items' => $args['items']['mobile']['middle-row']
160 + ]),
161 + $this->get_bar_structure_for([
162 + 'id' => 'bottom-row',
163 + 'mode' => $args['mode'],
164 + 'items' => $args['items']['mobile']['bottom-row']
165 + ]),
166 + $this->get_bar_structure_for([
167 + 'id' => 'offcanvas',
168 + 'mode' => $args['mode'],
169 + 'has_secondary' => false,
170 + 'items' => $args['items']['mobile']['offcanvas']
171 + ]),
172 + ];
173 + }
174 +
175 + if ($args['mode'] === 'rows') {
176 + $base['desktop'] = [
177 + $this->get_bar_structure_for([
178 + 'id' => 'top-row',
179 + 'mode' => $args['mode']
180 + ]),
181 + $this->get_bar_structure_for([
182 + 'id' => 'middle-row',
183 + 'mode' => $args['mode']
184 + ]),
185 + $this->get_bar_structure_for([
186 + 'id' => 'bottom-row',
187 + 'mode' => $args['mode']
188 + ]),
189 + ];
190 + }
191 +
192 + return $base;
193 + }
194 +
195 + private function get_bar_structure_for($args = []) {
196 + $args = wp_parse_args($args, [
197 + 'id' => null,
198 + 'mode' => 'placements',
199 + 'has_secondary' => true,
200 + 'items' => []
201 + ]);
202 +
203 + $args['items'] = wp_parse_args($args['items'], [
204 + 'start' => [],
205 + 'middle' => [],
206 + 'end' => [],
207 + 'start-middle' => [],
208 + 'end-middle' => [],
209 + ]);
210 +
211 + $placements = [
212 + ['id' => 'start', 'items' => $args['items']['start']]
213 + ];
214 +
215 + if ($args['has_secondary']) {
216 + $placements[] = ['id' => 'middle', 'items' => $args['items']['middle']];
217 + $placements[] = ['id' => 'end', 'items' => $args['items']['end']];
218 +
219 + $placements[] = ['id' => 'start-middle', 'items' => $args['items']['start-middle']];
220 + $placements[] = ['id' => 'end-middle', 'items' => $args['items']['end-middle']];
221 + }
222 +
223 + return array_merge([
224 + 'id' => $args['id'],
225 + ], (
226 + $args['mode'] === 'rows' ? [
227 + 'row' => []
228 + ] : ['placements' => $placements]
229 + ));
230 + }
231 +
232 + public function enabled_on_this_page() {
233 + return blocksy_default_akg(
234 + 'disable_header',
235 + blocksy_get_post_options(),
236 + 'no'
237 + ) === 'no';
238 + }
239 +
240 + public function render() {
241 + if (! $this->enabled_on_this_page()) {
242 + return '';
243 + }
244 +
245 + $renderer = new Blocksy_Header_Builder_Render();
246 + return $renderer->render();
247 + }
248 +
249 + public function get_section_value() {
250 + if (! $this->section_value || is_customize_preview()) {
251 + $this->section_value = blocksy_get_theme_mod(
252 + 'header_placements',
253 + $this->get_default_value()
254 + );
255 + }
256 +
257 + return $this->section_value;
258 + }
259 +
260 + public function translation_keys() {
261 + $render = new Blocksy_Header_Builder_Render();
262 + $sections = $this->get_section_value();
263 +
264 + $result = [];
265 +
266 + foreach ($sections['sections'] as $section) {
267 + foreach ($section['items'] as $item) {
268 + $nested_item = $render->get_item_config_for($item['id']);
269 +
270 + if (
271 + ! isset($nested_item['config']['translation_keys'])
272 + ||
273 + empty($nested_item['config']['translation_keys'])
274 + ) {
275 + continue;
276 + }
277 +
278 + foreach ($nested_item['config']['translation_keys'] as $key) {
279 + if (! isset($item['values'][$key['key']])) {
280 + continue;
281 + }
282 +
283 + $key_prefix = 'header:' . $section['id'] . ':' . $item['id'] . ':' . $key['key'];
284 +
285 + if (isset($key['all_layers'])) {
286 + foreach ($item['values'][$key['key']] as $single_layer) {
287 + foreach ($key['all_layers'] as $layer_key) {
288 + if (! isset($single_layer[$layer_key])) {
289 + continue;
290 + }
291 +
292 + $result[] = array_merge($key, [
293 + 'key' => $key_prefix . ':' . $single_layer['id'] . ':' . $layer_key,
294 + 'value' => $single_layer[$layer_key]
295 + ]);
296 + }
297 + }
298 + } else {
299 + $result[] = array_merge($key, [
300 + 'key' => $key_prefix,
301 + 'value' => $item['values'][$key['key']]
302 + ]);
303 + }
304 +
305 + }
306 + }
307 + }
308 +
309 + return $result;
310 + }
311 +
312 + public function typography_keys() {
313 + $render = new Blocksy_Header_Builder_Render();
314 + $section = $render->get_current_section();
315 +
316 + $result = [];
317 +
318 + foreach ($section['items'] as $item) {
319 + $nested_item = $render->get_item_config_for($item['id']);
320 +
321 + if (
322 + ! isset($nested_item['config']['typography_keys'])
323 + ||
324 + empty($nested_item['config']['typography_keys'])
325 + ) {
326 + continue;
327 + }
328 +
329 + $data = $render->get_item_data_for($item['id']);
330 +
331 + foreach ($nested_item['config']['typography_keys'] as $key) {
332 + $result[] = blocksy_akg($key, $data, []);
333 + }
334 + }
335 +
336 + return $result;
337 + }
338 +
339 + public function patch_value_for($processed_terms) {
340 + $current_value = blocksy_get_theme_mod(
341 + 'header_placements',
342 + $this->get_default_value()
343 + );
344 +
345 + foreach ($current_value['sections'] as $index => $header) {
346 + if (! isset($header['items'])) {
347 + continue;
348 + }
349 +
350 + foreach ($header['items'] as $item_index => $item) {
351 + if (! isset($item['values'])) {
352 + continue;
353 + }
354 +
355 + if (! isset($item['values']['menu'])) {
356 + continue;
357 + }
358 +
359 + if (! isset($processed_terms[$item['values']['menu']])) {
360 + continue;
361 + }
362 +
363 + $current_value['sections'][$index][
364 + 'items'
365 + ][$item_index]['values']['menu'] = $processed_terms[$item['values']['menu']];
366 + }
367 + }
368 +
369 + set_theme_mod('header_placements', $current_value);
370 + }
371 +
372 + public function get_filtered_section_id() {
373 + if (
374 + is_customize_preview()
375 + &&
376 + isset($this->get_section_value()['__forced_static_header__'])
377 + ) {
378 + return $this->get_section_value()['__forced_static_header__'];
379 + }
380 +
381 + return apply_filters(
382 + 'blocksy:header:current_section_id',
383 + 'type-1',
384 + $this->get_section_value()
385 + );
386 + }
387 + }
388 +