Diff: STRATO-apps/wordpress_03/app/wp-content/themes/blocksy/inc/classes/hooks-manager.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace Blocksy;
4 +
5 + class WpHooksManager {
6 + private $tokens = [];
7 +
8 + public function redirect_callbacks($args = []) {
9 + // TODO: token is unused for now because we dont need the revert
10 + // operation for callbacks redirection. But at some point we might
11 + // need it and it will be here for us.
12 + $args = wp_parse_args(
13 + $args,
14 + [
15 + 'token' => '',
16 + 'source' => [],
17 + 'destination' => '',
18 +
19 + 'priority_min' => PHP_INT_MIN,
20 + 'priority_max' => PHP_INT_MAX
21 + ]
22 + );
23 +
24 + blocksy_assert_args($args, ['token', 'source', 'destination']);
25 +
26 + if (
27 + (
28 + $args['priority_min'] !== PHP_INT_MIN
29 + ||
30 + $args['priority_max'] !== PHP_INT_MAX
31 + ) && count($args['source']) > 1
32 + ) {
33 + throw new \Error(
34 + "You can't use priority_min or priority_max with multiple sources."
35 + );
36 + }
37 +
38 + global $wp_filter;
39 +
40 + if (! isset($wp_filter[$args['destination']])) {
41 + $wp_filter[$args['destination']] = new \WP_Hook();
42 + }
43 +
44 + /*
45 + foreach ($args['source'] as $source_id) {
46 + if (! isset($wp_filter[$source_id])) {
47 + continue;
48 + }
49 +
50 + $this->tokens[$args['token']][$source_id] = $wp_filter[$source_id];
51 + }
52 +
53 + $this->tokens[$args['token']][$args['destination']] = $wp_filter[$args['destination']]->callbacks;
54 + */
55 +
56 + foreach ($args['source'] as $source_id) {
57 + if (! isset($wp_filter[$source_id])) {
58 + continue;
59 + }
60 +
61 + $source_callbacks = $wp_filter[$source_id]->callbacks;
62 +
63 + foreach ($source_callbacks as $priority => $callbacks) {
64 + if ($priority < $args['priority_min']) {
65 + continue;
66 + }
67 +
68 + if ($priority > $args['priority_max']) {
69 + continue;
70 + }
71 +
72 + foreach ($callbacks as $callback_id => $callback) {
73 + $wp_filter[$source_id]->remove_filter(
74 + $source_id,
75 + $callback['function'],
76 + $priority
77 + );
78 +
79 + $wp_filter[$args['destination']]->add_filter(
80 + $args['destination'],
81 + $callback['function'],
82 + $priority,
83 + $callback['accepted_args']
84 + );
85 + }
86 + }
87 +
88 + // $this->tokens[$args['token']][$source_id] = $wp_filter[$source_id];
89 + }
90 + }
91 +
92 + public function disable_callbacks($args = []) {
93 + $args = wp_parse_args(
94 + $args,
95 + [
96 + 'token' => '',
97 + 'source' => []
98 + ]
99 + );
100 +
101 + blocksy_assert_args($args, ['token', 'source']);
102 +
103 + global $wp_filter;
104 +
105 + foreach ($args['source'] as $source_id) {
106 + if (! isset($wp_filter[$source_id])) {
107 + continue;
108 + }
109 +
110 + $this->tokens[$args['token']][$source_id] = $wp_filter[$source_id];
111 + unset($wp_filter[$source_id]);
112 + }
113 + }
114 +
115 + public function enable_callbacks($args = []) {
116 + $args = wp_parse_args(
117 + $args,
118 + [
119 + 'token' => '',
120 + 'source' => []
121 + ]
122 + );
123 +
124 + blocksy_assert_args($args, ['token', 'source']);
125 +
126 + global $wp_filter;
127 +
128 + foreach ($args['source'] as $source_id) {
129 + if (! isset($this->tokens[$args['token']][$source_id])) {
130 + continue;
131 + }
132 +
133 + $wp_filter[$source_id] = $this->tokens[$args['token']][$source_id];
134 +
135 + unset($this->tokens[$args['token']][$source_id]);
136 + }
137 + }
138 +
139 + // For now callback rolling is not needed, but it may be needed eventually
140 + /*
141 + public function rollback_callbacks($args = []) {
142 + $args = wp_parse_args(
143 + $args,
144 + [
145 + 'token' => '',
146 + 'source' => '',
147 + 'destination' => ''
148 + ]
149 + );
150 +
151 + blocksy_assert_args($args, ['token', 'source', 'destination']);
152 +
153 + if (
154 + ! isset($this->tokens[$args['token']])
155 + ||
156 + ! isset($this->tokens[$args['token']][$args['destination']])
157 + ) {
158 + return;
159 + }
160 +
161 + global $wp_filter;
162 +
163 + foreach ($args['source'] as $source_id) {
164 + if (! isset($this->tokens[$args['token']][$source_id])) {
165 + continue;
166 + }
167 +
168 + $wp_filter[$source_id] = $this->tokens[$args['token']][$source_id];
169 + }
170 +
171 + $wp_filter[$args['destination']]->callbacks = $this->tokens[$args['token']][$args['destination']];
172 + }
173 + */
174 + }
175 +
176 +