@@ -15,324 +15,324 @@ |
||
15 | 15 | class Benchmark |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var string $output |
|
20 | - */ |
|
21 | - private static $output; |
|
22 | - |
|
23 | - /** |
|
24 | - * @var array $start_times array containing the start time for the timers |
|
25 | - */ |
|
26 | - private static $start_times; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array $times array containing all the timer'd times, which can be outputted via show_times() |
|
30 | - */ |
|
31 | - private static $times = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * @var array $memory_usage |
|
35 | - */ |
|
36 | - protected static $memory_usage = array(); |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $output |
|
41 | - * @param bool $formatted |
|
42 | - */ |
|
43 | - public static function addOutput($output, $formatted = true) |
|
44 | - { |
|
45 | - Benchmark::$output .= $formatted |
|
46 | - ? "<br />{$output}" |
|
47 | - : "\n{$output}"; |
|
48 | - } |
|
49 | - |
|
50 | - |
|
51 | - /** |
|
52 | - * @return void |
|
53 | - */ |
|
54 | - public static function resetOutput() |
|
55 | - { |
|
56 | - Benchmark::$output = ''; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * whether to benchmark code or not |
|
61 | - */ |
|
62 | - public static function doNotRun() |
|
63 | - { |
|
64 | - return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * resetTimes |
|
70 | - */ |
|
71 | - public static function resetTimes() |
|
72 | - { |
|
73 | - Benchmark::$times = array(); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - /** |
|
78 | - * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
79 | - * |
|
80 | - * @param null $timer_name |
|
81 | - */ |
|
82 | - public static function startTimer($timer_name = null) |
|
83 | - { |
|
84 | - if (Benchmark::doNotRun()) { |
|
85 | - return; |
|
86 | - } |
|
87 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
88 | - Benchmark::$start_times[$timer_name] = microtime(true); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
94 | - * |
|
95 | - * @param string $timer_name |
|
96 | - */ |
|
97 | - public static function stopTimer($timer_name = '') |
|
98 | - { |
|
99 | - if (Benchmark::doNotRun()) { |
|
100 | - return; |
|
101 | - } |
|
102 | - $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
103 | - if (isset(Benchmark::$start_times[$timer_name])) { |
|
104 | - $start_time = Benchmark::$start_times[$timer_name]; |
|
105 | - unset(Benchmark::$start_times[$timer_name]); |
|
106 | - } else { |
|
107 | - $start_time = array_pop(Benchmark::$start_times); |
|
108 | - } |
|
109 | - Benchmark::$times[$timer_name] = number_format(microtime(true) - $start_time, 8); |
|
110 | - } |
|
111 | - |
|
112 | - |
|
113 | - /** |
|
114 | - * Measure the memory usage by PHP so far. |
|
115 | - * |
|
116 | - * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
117 | - * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
118 | - * @param bool $formatted |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
122 | - { |
|
123 | - if (Benchmark::doNotRun()) { |
|
124 | - return; |
|
125 | - } |
|
126 | - $memory_used = Benchmark::convert(memory_get_usage(true)); |
|
127 | - Benchmark::$memory_usage[$label] = $memory_used; |
|
128 | - if ($output_now) { |
|
129 | - echo $formatted |
|
130 | - ? "<br>{$label} : {$memory_used}" |
|
131 | - : "\n {$label} : {$memory_used}"; |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - |
|
136 | - /** |
|
137 | - * will display the benchmarking results at shutdown |
|
138 | - * |
|
139 | - * @param bool $formatted |
|
140 | - * @return void |
|
141 | - */ |
|
142 | - public static function displayResultsAtShutdown($formatted = true) |
|
143 | - { |
|
144 | - Benchmark::resetOutput(); |
|
145 | - add_action( |
|
146 | - 'shutdown', |
|
147 | - function () use ($formatted) { |
|
148 | - Benchmark::displayResults(true, $formatted); |
|
149 | - }, |
|
150 | - 999999 |
|
151 | - ); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * will display the benchmarking results at shutdown |
|
157 | - * |
|
158 | - * @param string $filepath |
|
159 | - * @param bool $formatted |
|
160 | - * @param bool $append |
|
161 | - * @return void |
|
162 | - */ |
|
163 | - public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
164 | - { |
|
165 | - Benchmark::resetOutput(); |
|
166 | - add_action( |
|
167 | - 'shutdown', |
|
168 | - function () use ($filepath, $formatted, $append) { |
|
169 | - Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
170 | - }, |
|
171 | - 999999 |
|
172 | - ); |
|
173 | - } |
|
174 | - |
|
175 | - |
|
176 | - /** |
|
177 | - * @param bool $formatted |
|
178 | - * @return string |
|
179 | - */ |
|
180 | - private static function generateResults($formatted = true) |
|
181 | - { |
|
182 | - if (Benchmark::doNotRun()) { |
|
183 | - return ''; |
|
184 | - } |
|
185 | - if (! empty(Benchmark::$times)) { |
|
186 | - $total = 0; |
|
187 | - Benchmark::$output .= $formatted |
|
188 | - ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
189 | - : ''; |
|
190 | - foreach (Benchmark::$times as $timer_name => $total_time) { |
|
191 | - Benchmark::$output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
192 | - Benchmark::$output .= $formatted ? '<br />' : "\n"; |
|
193 | - $total += $total_time; |
|
194 | - } |
|
195 | - if ($formatted) { |
|
196 | - Benchmark::$output .= '<br />'; |
|
197 | - Benchmark::$output .= '<h4>TOTAL TIME</h4>'; |
|
198 | - Benchmark::$output .= Benchmark::formatTime('', $total, $formatted); |
|
199 | - Benchmark::$output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
200 | - Benchmark::$output .= '<br />'; |
|
201 | - Benchmark::$output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
202 | - Benchmark::$output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
203 | - Benchmark::$output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
204 | - Benchmark::$output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
205 | - Benchmark::$output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
206 | - Benchmark::$output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
207 | - Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
208 | - } |
|
209 | - } |
|
210 | - if (! empty(Benchmark::$memory_usage)) { |
|
211 | - Benchmark::$output .= $formatted |
|
212 | - ? '<h5>Memory</h5>' |
|
213 | - : "\nMemory"; |
|
214 | - foreach (Benchmark::$memory_usage as $label => $memory_usage) { |
|
215 | - Benchmark::$output .= $formatted |
|
216 | - ? "<br />{$memory_usage} : {$label}" |
|
217 | - : "\n{$memory_usage} : {$label}"; |
|
218 | - } |
|
219 | - } |
|
220 | - if (empty(Benchmark::$output)) { |
|
221 | - return ''; |
|
222 | - } |
|
223 | - Benchmark::$output = $formatted |
|
224 | - ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
225 | - . (is_admin() |
|
226 | - ? ' margin:2em 2em 2em 180px;' |
|
227 | - : ' margin:2em;') |
|
228 | - . ' padding:2em;">' |
|
229 | - . '<h4>BENCHMARKING</h4>' |
|
230 | - . Benchmark::$output |
|
231 | - . '</div>' |
|
232 | - : Benchmark::$output; |
|
233 | - return Benchmark::$output; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * @param bool $echo |
|
239 | - * @param bool $formatted |
|
240 | - * @return string |
|
241 | - */ |
|
242 | - public static function displayResults($echo = true, $formatted = true) |
|
243 | - { |
|
244 | - $results = Benchmark::generateResults($formatted); |
|
245 | - if ($echo) { |
|
246 | - echo $results; |
|
247 | - $results = ''; |
|
248 | - } |
|
249 | - return $results; |
|
250 | - } |
|
251 | - |
|
252 | - |
|
253 | - /** |
|
254 | - * @param string $filepath |
|
255 | - * @param bool $formatted |
|
256 | - * @param bool $append |
|
257 | - * @throws EE_Error |
|
258 | - */ |
|
259 | - public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
260 | - { |
|
261 | - $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
262 | - ? $filepath |
|
263 | - : ''; |
|
264 | - if (empty($filepath)) { |
|
265 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
266 | - } |
|
267 | - EEH_File::ensure_file_exists_and_is_writable($filepath); |
|
268 | - file_put_contents( |
|
269 | - $filepath, |
|
270 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
271 | - $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
272 | - ); |
|
273 | - } |
|
274 | - |
|
275 | - |
|
276 | - /** |
|
277 | - * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
278 | - * |
|
279 | - * @param int $size |
|
280 | - * @return string |
|
281 | - */ |
|
282 | - public static function convert($size) |
|
283 | - { |
|
284 | - $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
285 | - return round( |
|
286 | - $size / pow(1024, $i = floor(log($size, 1024))), |
|
287 | - 2 |
|
288 | - ) . ' ' . $unit[absint($i)]; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * @param string $timer_name |
|
294 | - * @param float $total_time |
|
295 | - * @param bool $formatted |
|
296 | - * @return string |
|
297 | - */ |
|
298 | - public static function formatTime($timer_name, $total_time, $formatted = true) |
|
299 | - { |
|
300 | - $total_time *= 1000; |
|
301 | - switch ($total_time) { |
|
302 | - case $total_time > 12500: |
|
303 | - $color = 'red'; |
|
304 | - $bold = 'bold'; |
|
305 | - break; |
|
306 | - case $total_time > 2500: |
|
307 | - $color = 'darkorange'; |
|
308 | - $bold = 'bold'; |
|
309 | - break; |
|
310 | - case $total_time > 500: |
|
311 | - $color = 'gold'; |
|
312 | - $bold = 'bold'; |
|
313 | - break; |
|
314 | - case $total_time > 100: |
|
315 | - $color = 'limegreen'; |
|
316 | - $bold = 'normal'; |
|
317 | - break; |
|
318 | - case $total_time > 20: |
|
319 | - $color = 'deepskyblue'; |
|
320 | - $bold = 'normal'; |
|
321 | - break; |
|
322 | - default: |
|
323 | - $color = 'mediumpurple'; |
|
324 | - $bold = 'normal'; |
|
325 | - break; |
|
326 | - } |
|
327 | - return $formatted |
|
328 | - ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
329 | - . $color |
|
330 | - . '; font-weight:' |
|
331 | - . $bold |
|
332 | - . '; font-size:1.2em;">' |
|
333 | - . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
334 | - . '</span> ' |
|
335 | - . $timer_name |
|
336 | - : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
337 | - } |
|
18 | + /** |
|
19 | + * @var string $output |
|
20 | + */ |
|
21 | + private static $output; |
|
22 | + |
|
23 | + /** |
|
24 | + * @var array $start_times array containing the start time for the timers |
|
25 | + */ |
|
26 | + private static $start_times; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array $times array containing all the timer'd times, which can be outputted via show_times() |
|
30 | + */ |
|
31 | + private static $times = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * @var array $memory_usage |
|
35 | + */ |
|
36 | + protected static $memory_usage = array(); |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $output |
|
41 | + * @param bool $formatted |
|
42 | + */ |
|
43 | + public static function addOutput($output, $formatted = true) |
|
44 | + { |
|
45 | + Benchmark::$output .= $formatted |
|
46 | + ? "<br />{$output}" |
|
47 | + : "\n{$output}"; |
|
48 | + } |
|
49 | + |
|
50 | + |
|
51 | + /** |
|
52 | + * @return void |
|
53 | + */ |
|
54 | + public static function resetOutput() |
|
55 | + { |
|
56 | + Benchmark::$output = ''; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * whether to benchmark code or not |
|
61 | + */ |
|
62 | + public static function doNotRun() |
|
63 | + { |
|
64 | + return ! WP_DEBUG || (defined('DOING_AJAX') && DOING_AJAX); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * resetTimes |
|
70 | + */ |
|
71 | + public static function resetTimes() |
|
72 | + { |
|
73 | + Benchmark::$times = array(); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + /** |
|
78 | + * Add Benchmark::startTimer() before a block of code you want to measure the performance of |
|
79 | + * |
|
80 | + * @param null $timer_name |
|
81 | + */ |
|
82 | + public static function startTimer($timer_name = null) |
|
83 | + { |
|
84 | + if (Benchmark::doNotRun()) { |
|
85 | + return; |
|
86 | + } |
|
87 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
88 | + Benchmark::$start_times[$timer_name] = microtime(true); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Add Benchmark::stopTimer() after a block of code you want to measure the performance of |
|
94 | + * |
|
95 | + * @param string $timer_name |
|
96 | + */ |
|
97 | + public static function stopTimer($timer_name = '') |
|
98 | + { |
|
99 | + if (Benchmark::doNotRun()) { |
|
100 | + return; |
|
101 | + } |
|
102 | + $timer_name = $timer_name !== '' ? $timer_name : get_called_class(); |
|
103 | + if (isset(Benchmark::$start_times[$timer_name])) { |
|
104 | + $start_time = Benchmark::$start_times[$timer_name]; |
|
105 | + unset(Benchmark::$start_times[$timer_name]); |
|
106 | + } else { |
|
107 | + $start_time = array_pop(Benchmark::$start_times); |
|
108 | + } |
|
109 | + Benchmark::$times[$timer_name] = number_format(microtime(true) - $start_time, 8); |
|
110 | + } |
|
111 | + |
|
112 | + |
|
113 | + /** |
|
114 | + * Measure the memory usage by PHP so far. |
|
115 | + * |
|
116 | + * @param string $label The label to show for this time eg "Start of calling Some_Class::some_function" |
|
117 | + * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called |
|
118 | + * @param bool $formatted |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + public static function measureMemory($label = 'memory usage', $output_now = false, $formatted = true) |
|
122 | + { |
|
123 | + if (Benchmark::doNotRun()) { |
|
124 | + return; |
|
125 | + } |
|
126 | + $memory_used = Benchmark::convert(memory_get_usage(true)); |
|
127 | + Benchmark::$memory_usage[$label] = $memory_used; |
|
128 | + if ($output_now) { |
|
129 | + echo $formatted |
|
130 | + ? "<br>{$label} : {$memory_used}" |
|
131 | + : "\n {$label} : {$memory_used}"; |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + |
|
136 | + /** |
|
137 | + * will display the benchmarking results at shutdown |
|
138 | + * |
|
139 | + * @param bool $formatted |
|
140 | + * @return void |
|
141 | + */ |
|
142 | + public static function displayResultsAtShutdown($formatted = true) |
|
143 | + { |
|
144 | + Benchmark::resetOutput(); |
|
145 | + add_action( |
|
146 | + 'shutdown', |
|
147 | + function () use ($formatted) { |
|
148 | + Benchmark::displayResults(true, $formatted); |
|
149 | + }, |
|
150 | + 999999 |
|
151 | + ); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * will display the benchmarking results at shutdown |
|
157 | + * |
|
158 | + * @param string $filepath |
|
159 | + * @param bool $formatted |
|
160 | + * @param bool $append |
|
161 | + * @return void |
|
162 | + */ |
|
163 | + public static function writeResultsAtShutdown($filepath = '', $formatted = true, $append = true) |
|
164 | + { |
|
165 | + Benchmark::resetOutput(); |
|
166 | + add_action( |
|
167 | + 'shutdown', |
|
168 | + function () use ($filepath, $formatted, $append) { |
|
169 | + Benchmark::writeResultsToFile($filepath, $formatted, $append); |
|
170 | + }, |
|
171 | + 999999 |
|
172 | + ); |
|
173 | + } |
|
174 | + |
|
175 | + |
|
176 | + /** |
|
177 | + * @param bool $formatted |
|
178 | + * @return string |
|
179 | + */ |
|
180 | + private static function generateResults($formatted = true) |
|
181 | + { |
|
182 | + if (Benchmark::doNotRun()) { |
|
183 | + return ''; |
|
184 | + } |
|
185 | + if (! empty(Benchmark::$times)) { |
|
186 | + $total = 0; |
|
187 | + Benchmark::$output .= $formatted |
|
188 | + ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
|
189 | + : ''; |
|
190 | + foreach (Benchmark::$times as $timer_name => $total_time) { |
|
191 | + Benchmark::$output .= Benchmark::formatTime($timer_name, $total_time, $formatted); |
|
192 | + Benchmark::$output .= $formatted ? '<br />' : "\n"; |
|
193 | + $total += $total_time; |
|
194 | + } |
|
195 | + if ($formatted) { |
|
196 | + Benchmark::$output .= '<br />'; |
|
197 | + Benchmark::$output .= '<h4>TOTAL TIME</h4>'; |
|
198 | + Benchmark::$output .= Benchmark::formatTime('', $total, $formatted); |
|
199 | + Benchmark::$output .= '<span style="color:#999999; font-size:.8em;"> milliseconds</span><br />'; |
|
200 | + Benchmark::$output .= '<br />'; |
|
201 | + Benchmark::$output .= '<h5>Performance scale (from best to worse)</h5>'; |
|
202 | + Benchmark::$output .= '<span style="color:mediumpurple">Like wow! How about a Scooby snack?</span><br />'; |
|
203 | + Benchmark::$output .= '<span style="color:deepskyblue">Like...no way man!</span><br />'; |
|
204 | + Benchmark::$output .= '<span style="color:limegreen">Like...groovy!</span><br />'; |
|
205 | + Benchmark::$output .= '<span style="color:gold">Ruh Oh</span><br />'; |
|
206 | + Benchmark::$output .= '<span style="color:darkorange">Zoinks!</span><br />'; |
|
207 | + Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
|
208 | + } |
|
209 | + } |
|
210 | + if (! empty(Benchmark::$memory_usage)) { |
|
211 | + Benchmark::$output .= $formatted |
|
212 | + ? '<h5>Memory</h5>' |
|
213 | + : "\nMemory"; |
|
214 | + foreach (Benchmark::$memory_usage as $label => $memory_usage) { |
|
215 | + Benchmark::$output .= $formatted |
|
216 | + ? "<br />{$memory_usage} : {$label}" |
|
217 | + : "\n{$memory_usage} : {$label}"; |
|
218 | + } |
|
219 | + } |
|
220 | + if (empty(Benchmark::$output)) { |
|
221 | + return ''; |
|
222 | + } |
|
223 | + Benchmark::$output = $formatted |
|
224 | + ? '<div style="border:1px solid #dddddd; background-color:#ffffff;' |
|
225 | + . (is_admin() |
|
226 | + ? ' margin:2em 2em 2em 180px;' |
|
227 | + : ' margin:2em;') |
|
228 | + . ' padding:2em;">' |
|
229 | + . '<h4>BENCHMARKING</h4>' |
|
230 | + . Benchmark::$output |
|
231 | + . '</div>' |
|
232 | + : Benchmark::$output; |
|
233 | + return Benchmark::$output; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * @param bool $echo |
|
239 | + * @param bool $formatted |
|
240 | + * @return string |
|
241 | + */ |
|
242 | + public static function displayResults($echo = true, $formatted = true) |
|
243 | + { |
|
244 | + $results = Benchmark::generateResults($formatted); |
|
245 | + if ($echo) { |
|
246 | + echo $results; |
|
247 | + $results = ''; |
|
248 | + } |
|
249 | + return $results; |
|
250 | + } |
|
251 | + |
|
252 | + |
|
253 | + /** |
|
254 | + * @param string $filepath |
|
255 | + * @param bool $formatted |
|
256 | + * @param bool $append |
|
257 | + * @throws EE_Error |
|
258 | + */ |
|
259 | + public static function writeResultsToFile($filepath = '', $formatted = true, $append = true) |
|
260 | + { |
|
261 | + $filepath = ! empty($filepath) && is_readable(dirname($filepath)) |
|
262 | + ? $filepath |
|
263 | + : ''; |
|
264 | + if (empty($filepath)) { |
|
265 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
266 | + } |
|
267 | + EEH_File::ensure_file_exists_and_is_writable($filepath); |
|
268 | + file_put_contents( |
|
269 | + $filepath, |
|
270 | + "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
271 | + $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
|
272 | + ); |
|
273 | + } |
|
274 | + |
|
275 | + |
|
276 | + /** |
|
277 | + * Converts a measure of memory bytes into the most logical units (eg kb, mb, etc) |
|
278 | + * |
|
279 | + * @param int $size |
|
280 | + * @return string |
|
281 | + */ |
|
282 | + public static function convert($size) |
|
283 | + { |
|
284 | + $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
|
285 | + return round( |
|
286 | + $size / pow(1024, $i = floor(log($size, 1024))), |
|
287 | + 2 |
|
288 | + ) . ' ' . $unit[absint($i)]; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * @param string $timer_name |
|
294 | + * @param float $total_time |
|
295 | + * @param bool $formatted |
|
296 | + * @return string |
|
297 | + */ |
|
298 | + public static function formatTime($timer_name, $total_time, $formatted = true) |
|
299 | + { |
|
300 | + $total_time *= 1000; |
|
301 | + switch ($total_time) { |
|
302 | + case $total_time > 12500: |
|
303 | + $color = 'red'; |
|
304 | + $bold = 'bold'; |
|
305 | + break; |
|
306 | + case $total_time > 2500: |
|
307 | + $color = 'darkorange'; |
|
308 | + $bold = 'bold'; |
|
309 | + break; |
|
310 | + case $total_time > 500: |
|
311 | + $color = 'gold'; |
|
312 | + $bold = 'bold'; |
|
313 | + break; |
|
314 | + case $total_time > 100: |
|
315 | + $color = 'limegreen'; |
|
316 | + $bold = 'normal'; |
|
317 | + break; |
|
318 | + case $total_time > 20: |
|
319 | + $color = 'deepskyblue'; |
|
320 | + $bold = 'normal'; |
|
321 | + break; |
|
322 | + default: |
|
323 | + $color = 'mediumpurple'; |
|
324 | + $bold = 'normal'; |
|
325 | + break; |
|
326 | + } |
|
327 | + return $formatted |
|
328 | + ? '<span style="min-width: 10px; margin:0 1em; color:' |
|
329 | + . $color |
|
330 | + . '; font-weight:' |
|
331 | + . $bold |
|
332 | + . '; font-size:1.2em;">' |
|
333 | + . str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT) |
|
334 | + . '</span> ' |
|
335 | + . $timer_name |
|
336 | + : str_pad(number_format($total_time, 3), 9, '0', STR_PAD_LEFT); |
|
337 | + } |
|
338 | 338 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | Benchmark::resetOutput(); |
145 | 145 | add_action( |
146 | 146 | 'shutdown', |
147 | - function () use ($formatted) { |
|
147 | + function() use ($formatted) { |
|
148 | 148 | Benchmark::displayResults(true, $formatted); |
149 | 149 | }, |
150 | 150 | 999999 |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | Benchmark::resetOutput(); |
166 | 166 | add_action( |
167 | 167 | 'shutdown', |
168 | - function () use ($filepath, $formatted, $append) { |
|
168 | + function() use ($filepath, $formatted, $append) { |
|
169 | 169 | Benchmark::writeResultsToFile($filepath, $formatted, $append); |
170 | 170 | }, |
171 | 171 | 999999 |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | if (Benchmark::doNotRun()) { |
183 | 183 | return ''; |
184 | 184 | } |
185 | - if (! empty(Benchmark::$times)) { |
|
185 | + if ( ! empty(Benchmark::$times)) { |
|
186 | 186 | $total = 0; |
187 | 187 | Benchmark::$output .= $formatted |
188 | 188 | ? '<span style="color:#999999; font-size:.8em;">( time in milliseconds )</span><br />' |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | Benchmark::$output .= '<span style="color:red">Like...HEEELLLP</span><br />'; |
208 | 208 | } |
209 | 209 | } |
210 | - if (! empty(Benchmark::$memory_usage)) { |
|
210 | + if ( ! empty(Benchmark::$memory_usage)) { |
|
211 | 211 | Benchmark::$output .= $formatted |
212 | 212 | ? '<h5>Memory</h5>' |
213 | 213 | : "\nMemory"; |
@@ -262,12 +262,12 @@ discard block |
||
262 | 262 | ? $filepath |
263 | 263 | : ''; |
264 | 264 | if (empty($filepath)) { |
265 | - $filepath = EVENT_ESPRESSO_UPLOAD_DIR . 'logs/benchmarking-' . date('Y-m-d') . '.html'; |
|
265 | + $filepath = EVENT_ESPRESSO_UPLOAD_DIR.'logs/benchmarking-'.date('Y-m-d').'.html'; |
|
266 | 266 | } |
267 | 267 | EEH_File::ensure_file_exists_and_is_writable($filepath); |
268 | 268 | file_put_contents( |
269 | 269 | $filepath, |
270 | - "\n" . date('Y-m-d H:i:s') . Benchmark::generateResults($formatted), |
|
270 | + "\n".date('Y-m-d H:i:s').Benchmark::generateResults($formatted), |
|
271 | 271 | $append ? FILE_APPEND | LOCK_EX : LOCK_EX |
272 | 272 | ); |
273 | 273 | } |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | return round( |
286 | 286 | $size / pow(1024, $i = floor(log($size, 1024))), |
287 | 287 | 2 |
288 | - ) . ' ' . $unit[absint($i)]; |
|
288 | + ).' '.$unit[absint($i)]; |
|
289 | 289 | } |
290 | 290 | |
291 | 291 |
@@ -14,19 +14,19 @@ |
||
14 | 14 | { |
15 | 15 | |
16 | 16 | |
17 | - /** |
|
18 | - * @var LoaderDecoratorInterface $loader |
|
19 | - */ |
|
20 | - protected $loader; |
|
17 | + /** |
|
18 | + * @var LoaderDecoratorInterface $loader |
|
19 | + */ |
|
20 | + protected $loader; |
|
21 | 21 | |
22 | 22 | |
23 | - /** |
|
24 | - * LoaderDecorator constructor. |
|
25 | - * |
|
26 | - * @param LoaderDecoratorInterface $loader |
|
27 | - */ |
|
28 | - public function __construct(LoaderDecoratorInterface $loader) |
|
29 | - { |
|
30 | - $this->loader = $loader; |
|
31 | - } |
|
23 | + /** |
|
24 | + * LoaderDecorator constructor. |
|
25 | + * |
|
26 | + * @param LoaderDecoratorInterface $loader |
|
27 | + */ |
|
28 | + public function __construct(LoaderDecoratorInterface $loader) |
|
29 | + { |
|
30 | + $this->loader = $loader; |
|
31 | + } |
|
32 | 32 | } |
@@ -17,105 +17,105 @@ |
||
17 | 17 | { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * @var LoaderDecoratorInterface $new_loader |
|
22 | - */ |
|
23 | - private $new_loader; |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * @var LoaderDecoratorInterface $shared_loader |
|
28 | - */ |
|
29 | - private $shared_loader; |
|
30 | - |
|
31 | - |
|
32 | - /** |
|
33 | - * Loader constructor. |
|
34 | - * |
|
35 | - * @param LoaderDecoratorInterface $new_loader |
|
36 | - * @param CachingLoaderDecoratorInterface $shared_loader |
|
37 | - * @throws InvalidInterfaceException |
|
38 | - * @throws InvalidArgumentException |
|
39 | - * @throws InvalidDataTypeException |
|
40 | - */ |
|
41 | - public function __construct(LoaderDecoratorInterface $new_loader, CachingLoaderDecoratorInterface $shared_loader) |
|
42 | - { |
|
43 | - $this->new_loader = $new_loader; |
|
44 | - $this->shared_loader = $shared_loader; |
|
45 | - } |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * @return LoaderDecoratorInterface |
|
50 | - */ |
|
51 | - public function getNewLoader() |
|
52 | - { |
|
53 | - return $this->new_loader; |
|
54 | - } |
|
55 | - |
|
56 | - |
|
57 | - /** |
|
58 | - * @return CachingLoaderDecoratorInterface |
|
59 | - */ |
|
60 | - public function getSharedLoader() |
|
61 | - { |
|
62 | - return $this->shared_loader; |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @param string $fqcn |
|
68 | - * @param array $arguments |
|
69 | - * @param bool $shared |
|
70 | - * @return mixed |
|
71 | - */ |
|
72 | - public function load($fqcn, $arguments = array(), $shared = true) |
|
73 | - { |
|
74 | - return $shared |
|
75 | - ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
76 | - : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
77 | - } |
|
78 | - |
|
79 | - |
|
80 | - /** |
|
81 | - * @param string $fqcn |
|
82 | - * @param array $arguments |
|
83 | - * @return mixed |
|
84 | - */ |
|
85 | - public function getNew($fqcn, $arguments = array()) |
|
86 | - { |
|
87 | - return $this->getNewLoader()->load($fqcn, $arguments, false); |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * @param string $fqcn |
|
93 | - * @param array $arguments |
|
94 | - * @return mixed |
|
95 | - */ |
|
96 | - public function getShared($fqcn, $arguments = array()) |
|
97 | - { |
|
98 | - return $this->getSharedLoader()->load($fqcn, $arguments); |
|
99 | - } |
|
100 | - |
|
101 | - |
|
102 | - /** |
|
103 | - * @param string $fqcn |
|
104 | - * @param mixed $object |
|
105 | - * @return bool |
|
106 | - * @throws InvalidArgumentException |
|
107 | - */ |
|
108 | - public function share($fqcn, $object) |
|
109 | - { |
|
110 | - return $this->getSharedLoader()->share($fqcn, $object); |
|
111 | - } |
|
112 | - |
|
113 | - |
|
114 | - /** |
|
115 | - * calls reset() on loaders if that method exists |
|
116 | - */ |
|
117 | - public function reset() |
|
118 | - { |
|
119 | - $this->shared_loader->reset(); |
|
120 | - } |
|
20 | + /** |
|
21 | + * @var LoaderDecoratorInterface $new_loader |
|
22 | + */ |
|
23 | + private $new_loader; |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * @var LoaderDecoratorInterface $shared_loader |
|
28 | + */ |
|
29 | + private $shared_loader; |
|
30 | + |
|
31 | + |
|
32 | + /** |
|
33 | + * Loader constructor. |
|
34 | + * |
|
35 | + * @param LoaderDecoratorInterface $new_loader |
|
36 | + * @param CachingLoaderDecoratorInterface $shared_loader |
|
37 | + * @throws InvalidInterfaceException |
|
38 | + * @throws InvalidArgumentException |
|
39 | + * @throws InvalidDataTypeException |
|
40 | + */ |
|
41 | + public function __construct(LoaderDecoratorInterface $new_loader, CachingLoaderDecoratorInterface $shared_loader) |
|
42 | + { |
|
43 | + $this->new_loader = $new_loader; |
|
44 | + $this->shared_loader = $shared_loader; |
|
45 | + } |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * @return LoaderDecoratorInterface |
|
50 | + */ |
|
51 | + public function getNewLoader() |
|
52 | + { |
|
53 | + return $this->new_loader; |
|
54 | + } |
|
55 | + |
|
56 | + |
|
57 | + /** |
|
58 | + * @return CachingLoaderDecoratorInterface |
|
59 | + */ |
|
60 | + public function getSharedLoader() |
|
61 | + { |
|
62 | + return $this->shared_loader; |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @param string $fqcn |
|
68 | + * @param array $arguments |
|
69 | + * @param bool $shared |
|
70 | + * @return mixed |
|
71 | + */ |
|
72 | + public function load($fqcn, $arguments = array(), $shared = true) |
|
73 | + { |
|
74 | + return $shared |
|
75 | + ? $this->getSharedLoader()->load($fqcn, $arguments, $shared) |
|
76 | + : $this->getNewLoader()->load($fqcn, $arguments, $shared); |
|
77 | + } |
|
78 | + |
|
79 | + |
|
80 | + /** |
|
81 | + * @param string $fqcn |
|
82 | + * @param array $arguments |
|
83 | + * @return mixed |
|
84 | + */ |
|
85 | + public function getNew($fqcn, $arguments = array()) |
|
86 | + { |
|
87 | + return $this->getNewLoader()->load($fqcn, $arguments, false); |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * @param string $fqcn |
|
93 | + * @param array $arguments |
|
94 | + * @return mixed |
|
95 | + */ |
|
96 | + public function getShared($fqcn, $arguments = array()) |
|
97 | + { |
|
98 | + return $this->getSharedLoader()->load($fqcn, $arguments); |
|
99 | + } |
|
100 | + |
|
101 | + |
|
102 | + /** |
|
103 | + * @param string $fqcn |
|
104 | + * @param mixed $object |
|
105 | + * @return bool |
|
106 | + * @throws InvalidArgumentException |
|
107 | + */ |
|
108 | + public function share($fqcn, $object) |
|
109 | + { |
|
110 | + return $this->getSharedLoader()->share($fqcn, $object); |
|
111 | + } |
|
112 | + |
|
113 | + |
|
114 | + /** |
|
115 | + * calls reset() on loaders if that method exists |
|
116 | + */ |
|
117 | + public function reset() |
|
118 | + { |
|
119 | + $this->shared_loader->reset(); |
|
120 | + } |
|
121 | 121 | } |
@@ -17,88 +17,88 @@ |
||
17 | 17 | class FileLocator extends Locator |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var string $file_mask |
|
22 | - */ |
|
23 | - protected $file_mask = '*.php'; |
|
20 | + /** |
|
21 | + * @var string $file_mask |
|
22 | + */ |
|
23 | + protected $file_mask = '*.php'; |
|
24 | 24 | |
25 | - /** |
|
26 | - * @var array $filepaths |
|
27 | - */ |
|
28 | - protected $filepaths = array(); |
|
25 | + /** |
|
26 | + * @var array $filepaths |
|
27 | + */ |
|
28 | + protected $filepaths = array(); |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * @param string $file_mask |
|
33 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
34 | - */ |
|
35 | - public function setFileMask($file_mask) |
|
36 | - { |
|
37 | - if (! is_string($file_mask)) { |
|
38 | - throw new InvalidDataTypeException('$file_mask', $file_mask, 'string'); |
|
39 | - } |
|
40 | - $this->file_mask = $file_mask; |
|
41 | - } |
|
31 | + /** |
|
32 | + * @param string $file_mask |
|
33 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
34 | + */ |
|
35 | + public function setFileMask($file_mask) |
|
36 | + { |
|
37 | + if (! is_string($file_mask)) { |
|
38 | + throw new InvalidDataTypeException('$file_mask', $file_mask, 'string'); |
|
39 | + } |
|
40 | + $this->file_mask = $file_mask; |
|
41 | + } |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @access public |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function getFilePaths() |
|
49 | - { |
|
50 | - return $this->filepaths; |
|
51 | - } |
|
44 | + /** |
|
45 | + * @access public |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function getFilePaths() |
|
49 | + { |
|
50 | + return $this->filepaths; |
|
51 | + } |
|
52 | 52 | |
53 | 53 | |
54 | - /** |
|
55 | - * @access public |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function count() |
|
59 | - { |
|
60 | - return count($this->filepaths); |
|
61 | - } |
|
54 | + /** |
|
55 | + * @access public |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function count() |
|
59 | + { |
|
60 | + return count($this->filepaths); |
|
61 | + } |
|
62 | 62 | |
63 | 63 | |
64 | - /** |
|
65 | - * given a path to a valid directory, or an array of valid paths, |
|
66 | - * will find all files that match the provided mask |
|
67 | - * |
|
68 | - * @access public |
|
69 | - * @param array|string $directory_paths |
|
70 | - * @return \FilesystemIterator |
|
71 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
72 | - */ |
|
73 | - public function locate($directory_paths) |
|
74 | - { |
|
75 | - if (! (is_string($directory_paths) || is_array($directory_paths))) { |
|
76 | - throw new InvalidDataTypeException('$directory_paths', $directory_paths, 'string or array'); |
|
77 | - } |
|
78 | - foreach ((array)$directory_paths as $directory_path) { |
|
79 | - foreach ($this->findFilesByPath($directory_path) as $key => $file) { |
|
80 | - $this->filepaths[$key] = \EEH_File::standardise_directory_separators($file); |
|
81 | - } |
|
82 | - } |
|
83 | - return $this->filepaths; |
|
84 | - } |
|
64 | + /** |
|
65 | + * given a path to a valid directory, or an array of valid paths, |
|
66 | + * will find all files that match the provided mask |
|
67 | + * |
|
68 | + * @access public |
|
69 | + * @param array|string $directory_paths |
|
70 | + * @return \FilesystemIterator |
|
71 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
72 | + */ |
|
73 | + public function locate($directory_paths) |
|
74 | + { |
|
75 | + if (! (is_string($directory_paths) || is_array($directory_paths))) { |
|
76 | + throw new InvalidDataTypeException('$directory_paths', $directory_paths, 'string or array'); |
|
77 | + } |
|
78 | + foreach ((array)$directory_paths as $directory_path) { |
|
79 | + foreach ($this->findFilesByPath($directory_path) as $key => $file) { |
|
80 | + $this->filepaths[$key] = \EEH_File::standardise_directory_separators($file); |
|
81 | + } |
|
82 | + } |
|
83 | + return $this->filepaths; |
|
84 | + } |
|
85 | 85 | |
86 | 86 | |
87 | - /** |
|
88 | - * given a path to a valid directory, will find all files that match the provided mask |
|
89 | - * |
|
90 | - * @access protected |
|
91 | - * @param string $directory_path |
|
92 | - * @return \FilesystemIterator |
|
93 | - */ |
|
94 | - protected function findFilesByPath($directory_path = '') |
|
95 | - { |
|
96 | - $iterator = new GlobIterator( |
|
97 | - \EEH_File::end_with_directory_separator($directory_path) . $this->file_mask |
|
98 | - ); |
|
99 | - foreach ($this->flags as $flag) { |
|
100 | - $iterator->setFlags($flag); |
|
101 | - } |
|
102 | - return $iterator; |
|
103 | - } |
|
87 | + /** |
|
88 | + * given a path to a valid directory, will find all files that match the provided mask |
|
89 | + * |
|
90 | + * @access protected |
|
91 | + * @param string $directory_path |
|
92 | + * @return \FilesystemIterator |
|
93 | + */ |
|
94 | + protected function findFilesByPath($directory_path = '') |
|
95 | + { |
|
96 | + $iterator = new GlobIterator( |
|
97 | + \EEH_File::end_with_directory_separator($directory_path) . $this->file_mask |
|
98 | + ); |
|
99 | + foreach ($this->flags as $flag) { |
|
100 | + $iterator->setFlags($flag); |
|
101 | + } |
|
102 | + return $iterator; |
|
103 | + } |
|
104 | 104 | } |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function setFileMask($file_mask) |
36 | 36 | { |
37 | - if (! is_string($file_mask)) { |
|
37 | + if ( ! is_string($file_mask)) { |
|
38 | 38 | throw new InvalidDataTypeException('$file_mask', $file_mask, 'string'); |
39 | 39 | } |
40 | 40 | $this->file_mask = $file_mask; |
@@ -72,10 +72,10 @@ discard block |
||
72 | 72 | */ |
73 | 73 | public function locate($directory_paths) |
74 | 74 | { |
75 | - if (! (is_string($directory_paths) || is_array($directory_paths))) { |
|
75 | + if ( ! (is_string($directory_paths) || is_array($directory_paths))) { |
|
76 | 76 | throw new InvalidDataTypeException('$directory_paths', $directory_paths, 'string or array'); |
77 | 77 | } |
78 | - foreach ((array)$directory_paths as $directory_path) { |
|
78 | + foreach ((array) $directory_paths as $directory_path) { |
|
79 | 79 | foreach ($this->findFilesByPath($directory_path) as $key => $file) { |
80 | 80 | $this->filepaths[$key] = \EEH_File::standardise_directory_separators($file); |
81 | 81 | } |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | protected function findFilesByPath($directory_path = '') |
95 | 95 | { |
96 | 96 | $iterator = new GlobIterator( |
97 | - \EEH_File::end_with_directory_separator($directory_path) . $this->file_mask |
|
97 | + \EEH_File::end_with_directory_separator($directory_path).$this->file_mask |
|
98 | 98 | ); |
99 | 99 | foreach ($this->flags as $flag) { |
100 | 100 | $iterator->setFlags($flag); |
@@ -17,43 +17,43 @@ |
||
17 | 17 | abstract class Locator implements LocatorInterface, Countable |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var array $flags |
|
22 | - */ |
|
23 | - protected $flags = array(); |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * FileLocator constructor. |
|
28 | - * |
|
29 | - * @access public |
|
30 | - * @param array $flags controls how files are found and/or file data is returned |
|
31 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
32 | - */ |
|
33 | - public function __construct($flags = array()) |
|
34 | - { |
|
35 | - if (empty($flags)) { |
|
36 | - $flags = array( |
|
37 | - FilesystemIterator::SKIP_DOTS, |
|
38 | - FilesystemIterator::UNIX_PATHS, |
|
39 | - FilesystemIterator::CURRENT_AS_PATHNAME, |
|
40 | - ); |
|
41 | - } |
|
42 | - $this->setFlags($flags); |
|
43 | - } |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * @see http://php.net/manual/en/class.filesystemiterator.php#filesystemiterator.constants |
|
48 | - * @access public |
|
49 | - * @param array $flags |
|
50 | - * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
51 | - */ |
|
52 | - public function setFlags($flags) |
|
53 | - { |
|
54 | - if (! is_array($flags)) { |
|
55 | - throw new InvalidDataTypeException('$flags', $flags, 'array'); |
|
56 | - } |
|
57 | - $this->flags = $flags; |
|
58 | - } |
|
20 | + /** |
|
21 | + * @var array $flags |
|
22 | + */ |
|
23 | + protected $flags = array(); |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * FileLocator constructor. |
|
28 | + * |
|
29 | + * @access public |
|
30 | + * @param array $flags controls how files are found and/or file data is returned |
|
31 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
32 | + */ |
|
33 | + public function __construct($flags = array()) |
|
34 | + { |
|
35 | + if (empty($flags)) { |
|
36 | + $flags = array( |
|
37 | + FilesystemIterator::SKIP_DOTS, |
|
38 | + FilesystemIterator::UNIX_PATHS, |
|
39 | + FilesystemIterator::CURRENT_AS_PATHNAME, |
|
40 | + ); |
|
41 | + } |
|
42 | + $this->setFlags($flags); |
|
43 | + } |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * @see http://php.net/manual/en/class.filesystemiterator.php#filesystemiterator.constants |
|
48 | + * @access public |
|
49 | + * @param array $flags |
|
50 | + * @throws \EventEspresso\core\exceptions\InvalidDataTypeException |
|
51 | + */ |
|
52 | + public function setFlags($flags) |
|
53 | + { |
|
54 | + if (! is_array($flags)) { |
|
55 | + throw new InvalidDataTypeException('$flags', $flags, 'array'); |
|
56 | + } |
|
57 | + $this->flags = $flags; |
|
58 | + } |
|
59 | 59 | } |
@@ -51,7 +51,7 @@ |
||
51 | 51 | */ |
52 | 52 | public function setFlags($flags) |
53 | 53 | { |
54 | - if (! is_array($flags)) { |
|
54 | + if ( ! is_array($flags)) { |
|
55 | 55 | throw new InvalidDataTypeException('$flags', $flags, 'array'); |
56 | 56 | } |
57 | 57 | $this->flags = $flags; |
@@ -29,203 +29,203 @@ |
||
29 | 29 | class ShortcodesManager |
30 | 30 | { |
31 | 31 | |
32 | - /** |
|
33 | - * @var LegacyShortcodesManager $legacy_shortcodes_manager |
|
34 | - */ |
|
35 | - private $legacy_shortcodes_manager; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var ShortcodeInterface[] $shortcodes |
|
39 | - */ |
|
40 | - private $shortcodes; |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * ShortcodesManager constructor |
|
45 | - * |
|
46 | - * @param LegacyShortcodesManager $legacy_shortcodes_manager |
|
47 | - */ |
|
48 | - public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager) |
|
49 | - { |
|
50 | - $this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
|
51 | - // assemble a list of installed and active shortcodes |
|
52 | - add_action( |
|
53 | - 'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
|
54 | - array($this, 'registerShortcodes'), |
|
55 | - 999 |
|
56 | - ); |
|
57 | - // call add_shortcode() for all installed shortcodes |
|
58 | - add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'addShortcodes')); |
|
59 | - // check content for shortcodes the old way |
|
60 | - add_action('parse_query', array($this->legacy_shortcodes_manager, 'initializeShortcodes'), 5); |
|
61 | - // check content for shortcodes the NEW more efficient way |
|
62 | - add_action('template_redirect', array($this, 'templateRedirect'), 999); |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @return CollectionInterface|ShortcodeInterface[] |
|
68 | - * @throws InvalidIdentifierException |
|
69 | - * @throws InvalidInterfaceException |
|
70 | - * @throws InvalidFilePathException |
|
71 | - * @throws InvalidEntityException |
|
72 | - * @throws InvalidDataTypeException |
|
73 | - * @throws InvalidClassException |
|
74 | - */ |
|
75 | - public function getShortcodes() |
|
76 | - { |
|
77 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
78 | - $this->shortcodes = $this->loadShortcodesCollection(); |
|
79 | - } |
|
80 | - return $this->shortcodes; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * @return CollectionInterface|ShortcodeInterface[] |
|
86 | - * @throws InvalidIdentifierException |
|
87 | - * @throws InvalidInterfaceException |
|
88 | - * @throws InvalidFilePathException |
|
89 | - * @throws InvalidEntityException |
|
90 | - * @throws InvalidDataTypeException |
|
91 | - * @throws InvalidClassException |
|
92 | - */ |
|
93 | - protected function loadShortcodesCollection() |
|
94 | - { |
|
95 | - $loader = new CollectionLoader( |
|
96 | - new CollectionDetails( |
|
97 | - // collection name |
|
98 | - 'shortcodes', |
|
99 | - // collection interface |
|
100 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
101 | - // FQCNs for classes to add (all classes within that namespace will be loaded) |
|
102 | - array('EventEspresso\core\domain\entities\shortcodes'), |
|
103 | - // filepaths to classes to add |
|
104 | - array(), |
|
105 | - // file mask to use if parsing folder for files to add |
|
106 | - '', |
|
107 | - // what to use as identifier for collection entities |
|
108 | - // using CLASS NAME prevents duplicates (works like a singleton) |
|
109 | - CollectionDetails::ID_CLASS_NAME |
|
110 | - ) |
|
111 | - ); |
|
112 | - return $loader->getCollection(); |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * @return void |
|
118 | - * @throws DomainException |
|
119 | - * @throws InvalidInterfaceException |
|
120 | - * @throws InvalidIdentifierException |
|
121 | - * @throws InvalidFilePathException |
|
122 | - * @throws InvalidEntityException |
|
123 | - * @throws InvalidDataTypeException |
|
124 | - * @throws InvalidClassException |
|
125 | - */ |
|
126 | - public function registerShortcodes() |
|
127 | - { |
|
128 | - try { |
|
129 | - $this->shortcodes = apply_filters( |
|
130 | - 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
131 | - $this->getShortcodes() |
|
132 | - ); |
|
133 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
134 | - throw new InvalidEntityException( |
|
135 | - $this->shortcodes, |
|
136 | - 'CollectionInterface', |
|
137 | - sprintf( |
|
138 | - esc_html__( |
|
139 | - 'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.', |
|
140 | - 'event_espresso' |
|
141 | - ), |
|
142 | - is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes) |
|
143 | - ) |
|
144 | - ); |
|
145 | - } |
|
146 | - $this->legacy_shortcodes_manager->registerShortcodes(); |
|
147 | - } catch (Exception $exception) { |
|
148 | - new ExceptionStackTraceDisplay($exception); |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function addShortcodes() |
|
157 | - { |
|
158 | - try { |
|
159 | - // cycle thru shortcode folders |
|
160 | - foreach ($this->shortcodes as $shortcode) { |
|
161 | - /** @var ShortcodeInterface $shortcode */ |
|
162 | - if ($shortcode instanceof EnqueueAssetsInterface) { |
|
163 | - add_action('wp_enqueue_scripts', array($shortcode, 'registerScriptsAndStylesheets'), 10); |
|
164 | - add_action('wp_enqueue_scripts', array($shortcode, 'enqueueStylesheets'), 11); |
|
165 | - } |
|
166 | - // add_shortcode() if it has not already been added |
|
167 | - if (! shortcode_exists($shortcode->getTag())) { |
|
168 | - add_shortcode($shortcode->getTag(), array($shortcode, 'processShortcodeCallback')); |
|
169 | - } |
|
170 | - } |
|
171 | - $this->legacy_shortcodes_manager->addShortcodes(); |
|
172 | - } catch (Exception $exception) { |
|
173 | - new ExceptionStackTraceDisplay($exception); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * callback for the "template_redirect" hook point |
|
180 | - * checks posts for EE shortcodes, and initializes them, |
|
181 | - * then toggles filter switch that loads core default assets |
|
182 | - * |
|
183 | - * @return void |
|
184 | - */ |
|
185 | - public function templateRedirect() |
|
186 | - { |
|
187 | - global $wp_query; |
|
188 | - if (empty($wp_query->posts)) { |
|
189 | - return; |
|
190 | - } |
|
191 | - $load_assets = false; |
|
192 | - // array of posts displayed in current request |
|
193 | - $posts = is_array($wp_query->posts) ? $wp_query->posts : array($wp_query->posts); |
|
194 | - foreach ($posts as $post) { |
|
195 | - // now check post content and excerpt for EE shortcodes |
|
196 | - $load_assets = $this->parseContentForShortcodes($post->post_content) |
|
197 | - ? true |
|
198 | - : $load_assets; |
|
199 | - } |
|
200 | - if ($load_assets) { |
|
201 | - $this->legacy_shortcodes_manager->registry()->REQ->set_espresso_page(true); |
|
202 | - add_filter('FHEE_load_css', '__return_true'); |
|
203 | - add_filter('FHEE_load_js', '__return_true'); |
|
204 | - } |
|
205 | - } |
|
206 | - |
|
207 | - |
|
208 | - /** |
|
209 | - * checks supplied content against list of shortcodes, |
|
210 | - * then initializes any found shortcodes, and returns true. |
|
211 | - * returns false if no shortcodes found. |
|
212 | - * |
|
213 | - * @param string $content |
|
214 | - * @return bool |
|
215 | - */ |
|
216 | - public function parseContentForShortcodes($content) |
|
217 | - { |
|
218 | - $has_shortcode = false; |
|
219 | - if (empty($this->shortcodes)) { |
|
220 | - return $has_shortcode; |
|
221 | - } |
|
222 | - foreach ($this->shortcodes as $shortcode) { |
|
223 | - /** @var ShortcodeInterface $shortcode */ |
|
224 | - if (has_shortcode($content, $shortcode->getTag())) { |
|
225 | - $shortcode->initializeShortcode(); |
|
226 | - $has_shortcode = true; |
|
227 | - } |
|
228 | - } |
|
229 | - return $has_shortcode; |
|
230 | - } |
|
32 | + /** |
|
33 | + * @var LegacyShortcodesManager $legacy_shortcodes_manager |
|
34 | + */ |
|
35 | + private $legacy_shortcodes_manager; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var ShortcodeInterface[] $shortcodes |
|
39 | + */ |
|
40 | + private $shortcodes; |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * ShortcodesManager constructor |
|
45 | + * |
|
46 | + * @param LegacyShortcodesManager $legacy_shortcodes_manager |
|
47 | + */ |
|
48 | + public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager) |
|
49 | + { |
|
50 | + $this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
|
51 | + // assemble a list of installed and active shortcodes |
|
52 | + add_action( |
|
53 | + 'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
|
54 | + array($this, 'registerShortcodes'), |
|
55 | + 999 |
|
56 | + ); |
|
57 | + // call add_shortcode() for all installed shortcodes |
|
58 | + add_action('AHEE__EE_System__core_loaded_and_ready', array($this, 'addShortcodes')); |
|
59 | + // check content for shortcodes the old way |
|
60 | + add_action('parse_query', array($this->legacy_shortcodes_manager, 'initializeShortcodes'), 5); |
|
61 | + // check content for shortcodes the NEW more efficient way |
|
62 | + add_action('template_redirect', array($this, 'templateRedirect'), 999); |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @return CollectionInterface|ShortcodeInterface[] |
|
68 | + * @throws InvalidIdentifierException |
|
69 | + * @throws InvalidInterfaceException |
|
70 | + * @throws InvalidFilePathException |
|
71 | + * @throws InvalidEntityException |
|
72 | + * @throws InvalidDataTypeException |
|
73 | + * @throws InvalidClassException |
|
74 | + */ |
|
75 | + public function getShortcodes() |
|
76 | + { |
|
77 | + if (! $this->shortcodes instanceof CollectionInterface) { |
|
78 | + $this->shortcodes = $this->loadShortcodesCollection(); |
|
79 | + } |
|
80 | + return $this->shortcodes; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * @return CollectionInterface|ShortcodeInterface[] |
|
86 | + * @throws InvalidIdentifierException |
|
87 | + * @throws InvalidInterfaceException |
|
88 | + * @throws InvalidFilePathException |
|
89 | + * @throws InvalidEntityException |
|
90 | + * @throws InvalidDataTypeException |
|
91 | + * @throws InvalidClassException |
|
92 | + */ |
|
93 | + protected function loadShortcodesCollection() |
|
94 | + { |
|
95 | + $loader = new CollectionLoader( |
|
96 | + new CollectionDetails( |
|
97 | + // collection name |
|
98 | + 'shortcodes', |
|
99 | + // collection interface |
|
100 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
101 | + // FQCNs for classes to add (all classes within that namespace will be loaded) |
|
102 | + array('EventEspresso\core\domain\entities\shortcodes'), |
|
103 | + // filepaths to classes to add |
|
104 | + array(), |
|
105 | + // file mask to use if parsing folder for files to add |
|
106 | + '', |
|
107 | + // what to use as identifier for collection entities |
|
108 | + // using CLASS NAME prevents duplicates (works like a singleton) |
|
109 | + CollectionDetails::ID_CLASS_NAME |
|
110 | + ) |
|
111 | + ); |
|
112 | + return $loader->getCollection(); |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * @return void |
|
118 | + * @throws DomainException |
|
119 | + * @throws InvalidInterfaceException |
|
120 | + * @throws InvalidIdentifierException |
|
121 | + * @throws InvalidFilePathException |
|
122 | + * @throws InvalidEntityException |
|
123 | + * @throws InvalidDataTypeException |
|
124 | + * @throws InvalidClassException |
|
125 | + */ |
|
126 | + public function registerShortcodes() |
|
127 | + { |
|
128 | + try { |
|
129 | + $this->shortcodes = apply_filters( |
|
130 | + 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
131 | + $this->getShortcodes() |
|
132 | + ); |
|
133 | + if (! $this->shortcodes instanceof CollectionInterface) { |
|
134 | + throw new InvalidEntityException( |
|
135 | + $this->shortcodes, |
|
136 | + 'CollectionInterface', |
|
137 | + sprintf( |
|
138 | + esc_html__( |
|
139 | + 'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.', |
|
140 | + 'event_espresso' |
|
141 | + ), |
|
142 | + is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes) |
|
143 | + ) |
|
144 | + ); |
|
145 | + } |
|
146 | + $this->legacy_shortcodes_manager->registerShortcodes(); |
|
147 | + } catch (Exception $exception) { |
|
148 | + new ExceptionStackTraceDisplay($exception); |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function addShortcodes() |
|
157 | + { |
|
158 | + try { |
|
159 | + // cycle thru shortcode folders |
|
160 | + foreach ($this->shortcodes as $shortcode) { |
|
161 | + /** @var ShortcodeInterface $shortcode */ |
|
162 | + if ($shortcode instanceof EnqueueAssetsInterface) { |
|
163 | + add_action('wp_enqueue_scripts', array($shortcode, 'registerScriptsAndStylesheets'), 10); |
|
164 | + add_action('wp_enqueue_scripts', array($shortcode, 'enqueueStylesheets'), 11); |
|
165 | + } |
|
166 | + // add_shortcode() if it has not already been added |
|
167 | + if (! shortcode_exists($shortcode->getTag())) { |
|
168 | + add_shortcode($shortcode->getTag(), array($shortcode, 'processShortcodeCallback')); |
|
169 | + } |
|
170 | + } |
|
171 | + $this->legacy_shortcodes_manager->addShortcodes(); |
|
172 | + } catch (Exception $exception) { |
|
173 | + new ExceptionStackTraceDisplay($exception); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * callback for the "template_redirect" hook point |
|
180 | + * checks posts for EE shortcodes, and initializes them, |
|
181 | + * then toggles filter switch that loads core default assets |
|
182 | + * |
|
183 | + * @return void |
|
184 | + */ |
|
185 | + public function templateRedirect() |
|
186 | + { |
|
187 | + global $wp_query; |
|
188 | + if (empty($wp_query->posts)) { |
|
189 | + return; |
|
190 | + } |
|
191 | + $load_assets = false; |
|
192 | + // array of posts displayed in current request |
|
193 | + $posts = is_array($wp_query->posts) ? $wp_query->posts : array($wp_query->posts); |
|
194 | + foreach ($posts as $post) { |
|
195 | + // now check post content and excerpt for EE shortcodes |
|
196 | + $load_assets = $this->parseContentForShortcodes($post->post_content) |
|
197 | + ? true |
|
198 | + : $load_assets; |
|
199 | + } |
|
200 | + if ($load_assets) { |
|
201 | + $this->legacy_shortcodes_manager->registry()->REQ->set_espresso_page(true); |
|
202 | + add_filter('FHEE_load_css', '__return_true'); |
|
203 | + add_filter('FHEE_load_js', '__return_true'); |
|
204 | + } |
|
205 | + } |
|
206 | + |
|
207 | + |
|
208 | + /** |
|
209 | + * checks supplied content against list of shortcodes, |
|
210 | + * then initializes any found shortcodes, and returns true. |
|
211 | + * returns false if no shortcodes found. |
|
212 | + * |
|
213 | + * @param string $content |
|
214 | + * @return bool |
|
215 | + */ |
|
216 | + public function parseContentForShortcodes($content) |
|
217 | + { |
|
218 | + $has_shortcode = false; |
|
219 | + if (empty($this->shortcodes)) { |
|
220 | + return $has_shortcode; |
|
221 | + } |
|
222 | + foreach ($this->shortcodes as $shortcode) { |
|
223 | + /** @var ShortcodeInterface $shortcode */ |
|
224 | + if (has_shortcode($content, $shortcode->getTag())) { |
|
225 | + $shortcode->initializeShortcode(); |
|
226 | + $has_shortcode = true; |
|
227 | + } |
|
228 | + } |
|
229 | + return $has_shortcode; |
|
230 | + } |
|
231 | 231 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function getShortcodes() |
76 | 76 | { |
77 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
77 | + if ( ! $this->shortcodes instanceof CollectionInterface) { |
|
78 | 78 | $this->shortcodes = $this->loadShortcodesCollection(); |
79 | 79 | } |
80 | 80 | return $this->shortcodes; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
131 | 131 | $this->getShortcodes() |
132 | 132 | ); |
133 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
133 | + if ( ! $this->shortcodes instanceof CollectionInterface) { |
|
134 | 134 | throw new InvalidEntityException( |
135 | 135 | $this->shortcodes, |
136 | 136 | 'CollectionInterface', |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | add_action('wp_enqueue_scripts', array($shortcode, 'enqueueStylesheets'), 11); |
165 | 165 | } |
166 | 166 | // add_shortcode() if it has not already been added |
167 | - if (! shortcode_exists($shortcode->getTag())) { |
|
167 | + if ( ! shortcode_exists($shortcode->getTag())) { |
|
168 | 168 | add_shortcode($shortcode->getTag(), array($shortcode, 'processShortcodeCallback')); |
169 | 169 | } |
170 | 170 | } |
@@ -19,221 +19,221 @@ |
||
19 | 19 | abstract class EspressoShortcode implements ShortcodeInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * transient prefix |
|
24 | - * |
|
25 | - * @type string |
|
26 | - */ |
|
27 | - const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var PostRelatedCacheManager $cache_manager |
|
31 | - */ |
|
32 | - private $cache_manager; |
|
33 | - |
|
34 | - /** |
|
35 | - * true if ShortcodeInterface::initializeShortcode() has been called |
|
36 | - * if false, then that will get called before processing |
|
37 | - * |
|
38 | - * @var boolean $initialized |
|
39 | - */ |
|
40 | - private $initialized = false; |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * EspressoShortcode constructor |
|
45 | - * |
|
46 | - * @param PostRelatedCacheManager $cache_manager |
|
47 | - */ |
|
48 | - public function __construct(PostRelatedCacheManager $cache_manager) |
|
49 | - { |
|
50 | - $this->cache_manager = $cache_manager; |
|
51 | - } |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function shortcodeHasBeenInitialized() |
|
58 | - { |
|
59 | - $this->initialized = true; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * enqueues scripts then processes the shortcode |
|
65 | - * |
|
66 | - * @param array $attributes |
|
67 | - * @return string |
|
68 | - * @throws EE_Error |
|
69 | - */ |
|
70 | - final public function processShortcodeCallback($attributes = array()) |
|
71 | - { |
|
72 | - if ($this instanceof EnqueueAssetsInterface) { |
|
73 | - if (is_admin()) { |
|
74 | - $this->enqueueAdminScripts(); |
|
75 | - } else { |
|
76 | - $this->enqueueScripts(); |
|
77 | - } |
|
78 | - } |
|
79 | - return $this->shortcodeContent( |
|
80 | - $this->sanitizeAttributes((array)$attributes) |
|
81 | - ); |
|
82 | - } |
|
83 | - |
|
84 | - |
|
85 | - /** |
|
86 | - * If shortcode caching is enabled for the shortcode, |
|
87 | - * and cached results exist, then that will be returned |
|
88 | - * else new content will be generated. |
|
89 | - * If caching is enabled, then the new content will be cached for later. |
|
90 | - * |
|
91 | - * @param array $attributes |
|
92 | - * @return mixed|string |
|
93 | - * @throws EE_Error |
|
94 | - */ |
|
95 | - private function shortcodeContent(array $attributes) |
|
96 | - { |
|
97 | - $shortcode = $this; |
|
98 | - $post_ID = $this->currentPostID(); |
|
99 | - // something like "SC_EVENTS-123" |
|
100 | - $cache_ID = $this->shortcodeCacheID($post_ID); |
|
101 | - $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
102 | - return $this->cache_manager->get( |
|
103 | - $cache_ID, |
|
104 | - // serialized attributes |
|
105 | - wp_json_encode($attributes), |
|
106 | - // Closure for generating content if cache is expired |
|
107 | - function () use ($shortcode, $attributes) { |
|
108 | - if ($shortcode->initialized() === false) { |
|
109 | - $shortcode->initializeShortcode(); |
|
110 | - } |
|
111 | - return $shortcode->processShortcode($attributes); |
|
112 | - }, |
|
113 | - // filterable cache expiration set by each shortcode |
|
114 | - apply_filters( |
|
115 | - 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
116 | - $this->cacheExpiration(), |
|
117 | - $this->getTag(), |
|
118 | - $this |
|
119 | - ) |
|
120 | - ); |
|
121 | - } |
|
122 | - |
|
123 | - |
|
124 | - /** |
|
125 | - * @return int |
|
126 | - * @throws EE_Error |
|
127 | - */ |
|
128 | - private function currentPostID() |
|
129 | - { |
|
130 | - // try to get EE_Event any way we can |
|
131 | - $event = EEH_Event_View::get_event(); |
|
132 | - // then get some kind of ID |
|
133 | - if ($event instanceof EE_Event) { |
|
134 | - return $event->ID(); |
|
135 | - } |
|
136 | - global $post; |
|
137 | - if ($post instanceof WP_Post) { |
|
138 | - return $post->ID; |
|
139 | - } |
|
140 | - return 0; |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @param int $post_ID |
|
146 | - * @return string |
|
147 | - * @throws EE_Error |
|
148 | - */ |
|
149 | - private function shortcodeCacheID($post_ID) |
|
150 | - { |
|
151 | - $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
152 | - return "SC_{$tag}-{$post_ID}"; |
|
153 | - } |
|
154 | - |
|
155 | - |
|
156 | - /** |
|
157 | - * array for defining custom attribute sanitization callbacks, |
|
158 | - * where keys match keys in your attributes array, |
|
159 | - * and values represent the sanitization function you wish to be applied to that attribute. |
|
160 | - * So for example, if you had an integer attribute named "event_id" |
|
161 | - * that you wanted to be sanitized using absint(), |
|
162 | - * then you would return the following: |
|
163 | - * array('event_id' => 'absint') |
|
164 | - * Entering 'skip_sanitization' for the callback value |
|
165 | - * means that no sanitization will be applied |
|
166 | - * on the assumption that the attribute |
|
167 | - * will be sanitized at some point... right? |
|
168 | - * You wouldn't pass around unsanitized attributes would you? |
|
169 | - * That would be very Tom Foolery of you!!! |
|
170 | - * |
|
171 | - * @return array |
|
172 | - */ |
|
173 | - protected function customAttributeSanitizationMap() |
|
174 | - { |
|
175 | - return array(); |
|
176 | - } |
|
177 | - |
|
178 | - |
|
179 | - /** |
|
180 | - * Performs basic sanitization on shortcode attributes |
|
181 | - * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
182 | - * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
183 | - * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
184 | - * all other attributes would be sanitized using the defaults in the switch statement below |
|
185 | - * |
|
186 | - * @param array $attributes |
|
187 | - * @return array |
|
188 | - */ |
|
189 | - private function sanitizeAttributes(array $attributes) |
|
190 | - { |
|
191 | - $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
192 | - foreach ($attributes as $key => $value) { |
|
193 | - // is a custom sanitization callback specified ? |
|
194 | - if (isset($custom_sanitization[$key])) { |
|
195 | - $callback = $custom_sanitization[$key]; |
|
196 | - if ($callback === 'skip_sanitization') { |
|
197 | - $attributes[$key] = $value; |
|
198 | - continue; |
|
199 | - } |
|
200 | - if (function_exists($callback)) { |
|
201 | - $attributes[$key] = $callback($value); |
|
202 | - continue; |
|
203 | - } |
|
204 | - } |
|
205 | - switch (true) { |
|
206 | - case $value === null: |
|
207 | - case is_int($value): |
|
208 | - case is_float($value): |
|
209 | - // typical booleans |
|
210 | - case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true): |
|
211 | - $attributes[$key] = $value; |
|
212 | - break; |
|
213 | - case is_string($value): |
|
214 | - $attributes[$key] = sanitize_text_field($value); |
|
215 | - break; |
|
216 | - case is_array($value): |
|
217 | - $attributes[$key] = $this->sanitizeAttributes($value); |
|
218 | - break; |
|
219 | - default: |
|
220 | - // only remaining data types are Object and Resource |
|
221 | - // which are not allowed as shortcode attributes |
|
222 | - $attributes[$key] = null; |
|
223 | - break; |
|
224 | - } |
|
225 | - } |
|
226 | - return $attributes; |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * Returns whether or not this shortcode has been initialized |
|
232 | - * |
|
233 | - * @return boolean |
|
234 | - */ |
|
235 | - public function initialized() |
|
236 | - { |
|
237 | - return $this->initialized; |
|
238 | - } |
|
22 | + /** |
|
23 | + * transient prefix |
|
24 | + * |
|
25 | + * @type string |
|
26 | + */ |
|
27 | + const CACHE_TRANSIENT_PREFIX = 'ee_sc_'; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var PostRelatedCacheManager $cache_manager |
|
31 | + */ |
|
32 | + private $cache_manager; |
|
33 | + |
|
34 | + /** |
|
35 | + * true if ShortcodeInterface::initializeShortcode() has been called |
|
36 | + * if false, then that will get called before processing |
|
37 | + * |
|
38 | + * @var boolean $initialized |
|
39 | + */ |
|
40 | + private $initialized = false; |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * EspressoShortcode constructor |
|
45 | + * |
|
46 | + * @param PostRelatedCacheManager $cache_manager |
|
47 | + */ |
|
48 | + public function __construct(PostRelatedCacheManager $cache_manager) |
|
49 | + { |
|
50 | + $this->cache_manager = $cache_manager; |
|
51 | + } |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * @return void |
|
56 | + */ |
|
57 | + public function shortcodeHasBeenInitialized() |
|
58 | + { |
|
59 | + $this->initialized = true; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * enqueues scripts then processes the shortcode |
|
65 | + * |
|
66 | + * @param array $attributes |
|
67 | + * @return string |
|
68 | + * @throws EE_Error |
|
69 | + */ |
|
70 | + final public function processShortcodeCallback($attributes = array()) |
|
71 | + { |
|
72 | + if ($this instanceof EnqueueAssetsInterface) { |
|
73 | + if (is_admin()) { |
|
74 | + $this->enqueueAdminScripts(); |
|
75 | + } else { |
|
76 | + $this->enqueueScripts(); |
|
77 | + } |
|
78 | + } |
|
79 | + return $this->shortcodeContent( |
|
80 | + $this->sanitizeAttributes((array)$attributes) |
|
81 | + ); |
|
82 | + } |
|
83 | + |
|
84 | + |
|
85 | + /** |
|
86 | + * If shortcode caching is enabled for the shortcode, |
|
87 | + * and cached results exist, then that will be returned |
|
88 | + * else new content will be generated. |
|
89 | + * If caching is enabled, then the new content will be cached for later. |
|
90 | + * |
|
91 | + * @param array $attributes |
|
92 | + * @return mixed|string |
|
93 | + * @throws EE_Error |
|
94 | + */ |
|
95 | + private function shortcodeContent(array $attributes) |
|
96 | + { |
|
97 | + $shortcode = $this; |
|
98 | + $post_ID = $this->currentPostID(); |
|
99 | + // something like "SC_EVENTS-123" |
|
100 | + $cache_ID = $this->shortcodeCacheID($post_ID); |
|
101 | + $this->cache_manager->clearPostRelatedCacheOnUpdate($post_ID, $cache_ID); |
|
102 | + return $this->cache_manager->get( |
|
103 | + $cache_ID, |
|
104 | + // serialized attributes |
|
105 | + wp_json_encode($attributes), |
|
106 | + // Closure for generating content if cache is expired |
|
107 | + function () use ($shortcode, $attributes) { |
|
108 | + if ($shortcode->initialized() === false) { |
|
109 | + $shortcode->initializeShortcode(); |
|
110 | + } |
|
111 | + return $shortcode->processShortcode($attributes); |
|
112 | + }, |
|
113 | + // filterable cache expiration set by each shortcode |
|
114 | + apply_filters( |
|
115 | + 'FHEE__EventEspresso_core_services_shortcodes_EspressoShortcode__shortcodeContent__cache_expiration', |
|
116 | + $this->cacheExpiration(), |
|
117 | + $this->getTag(), |
|
118 | + $this |
|
119 | + ) |
|
120 | + ); |
|
121 | + } |
|
122 | + |
|
123 | + |
|
124 | + /** |
|
125 | + * @return int |
|
126 | + * @throws EE_Error |
|
127 | + */ |
|
128 | + private function currentPostID() |
|
129 | + { |
|
130 | + // try to get EE_Event any way we can |
|
131 | + $event = EEH_Event_View::get_event(); |
|
132 | + // then get some kind of ID |
|
133 | + if ($event instanceof EE_Event) { |
|
134 | + return $event->ID(); |
|
135 | + } |
|
136 | + global $post; |
|
137 | + if ($post instanceof WP_Post) { |
|
138 | + return $post->ID; |
|
139 | + } |
|
140 | + return 0; |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @param int $post_ID |
|
146 | + * @return string |
|
147 | + * @throws EE_Error |
|
148 | + */ |
|
149 | + private function shortcodeCacheID($post_ID) |
|
150 | + { |
|
151 | + $tag = str_replace('ESPRESSO_', '', $this->getTag()); |
|
152 | + return "SC_{$tag}-{$post_ID}"; |
|
153 | + } |
|
154 | + |
|
155 | + |
|
156 | + /** |
|
157 | + * array for defining custom attribute sanitization callbacks, |
|
158 | + * where keys match keys in your attributes array, |
|
159 | + * and values represent the sanitization function you wish to be applied to that attribute. |
|
160 | + * So for example, if you had an integer attribute named "event_id" |
|
161 | + * that you wanted to be sanitized using absint(), |
|
162 | + * then you would return the following: |
|
163 | + * array('event_id' => 'absint') |
|
164 | + * Entering 'skip_sanitization' for the callback value |
|
165 | + * means that no sanitization will be applied |
|
166 | + * on the assumption that the attribute |
|
167 | + * will be sanitized at some point... right? |
|
168 | + * You wouldn't pass around unsanitized attributes would you? |
|
169 | + * That would be very Tom Foolery of you!!! |
|
170 | + * |
|
171 | + * @return array |
|
172 | + */ |
|
173 | + protected function customAttributeSanitizationMap() |
|
174 | + { |
|
175 | + return array(); |
|
176 | + } |
|
177 | + |
|
178 | + |
|
179 | + /** |
|
180 | + * Performs basic sanitization on shortcode attributes |
|
181 | + * Since incoming attributes from the shortcode usage in the WP editor will all be strings, |
|
182 | + * most attributes will by default be sanitized using the sanitize_text_field() function. |
|
183 | + * This can be overridden using the customAttributeSanitizationMap() method (see above), |
|
184 | + * all other attributes would be sanitized using the defaults in the switch statement below |
|
185 | + * |
|
186 | + * @param array $attributes |
|
187 | + * @return array |
|
188 | + */ |
|
189 | + private function sanitizeAttributes(array $attributes) |
|
190 | + { |
|
191 | + $custom_sanitization = $this->customAttributeSanitizationMap(); |
|
192 | + foreach ($attributes as $key => $value) { |
|
193 | + // is a custom sanitization callback specified ? |
|
194 | + if (isset($custom_sanitization[$key])) { |
|
195 | + $callback = $custom_sanitization[$key]; |
|
196 | + if ($callback === 'skip_sanitization') { |
|
197 | + $attributes[$key] = $value; |
|
198 | + continue; |
|
199 | + } |
|
200 | + if (function_exists($callback)) { |
|
201 | + $attributes[$key] = $callback($value); |
|
202 | + continue; |
|
203 | + } |
|
204 | + } |
|
205 | + switch (true) { |
|
206 | + case $value === null: |
|
207 | + case is_int($value): |
|
208 | + case is_float($value): |
|
209 | + // typical booleans |
|
210 | + case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true): |
|
211 | + $attributes[$key] = $value; |
|
212 | + break; |
|
213 | + case is_string($value): |
|
214 | + $attributes[$key] = sanitize_text_field($value); |
|
215 | + break; |
|
216 | + case is_array($value): |
|
217 | + $attributes[$key] = $this->sanitizeAttributes($value); |
|
218 | + break; |
|
219 | + default: |
|
220 | + // only remaining data types are Object and Resource |
|
221 | + // which are not allowed as shortcode attributes |
|
222 | + $attributes[$key] = null; |
|
223 | + break; |
|
224 | + } |
|
225 | + } |
|
226 | + return $attributes; |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * Returns whether or not this shortcode has been initialized |
|
232 | + * |
|
233 | + * @return boolean |
|
234 | + */ |
|
235 | + public function initialized() |
|
236 | + { |
|
237 | + return $this->initialized; |
|
238 | + } |
|
239 | 239 | } |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | } |
78 | 78 | } |
79 | 79 | return $this->shortcodeContent( |
80 | - $this->sanitizeAttributes((array)$attributes) |
|
80 | + $this->sanitizeAttributes((array) $attributes) |
|
81 | 81 | ); |
82 | 82 | } |
83 | 83 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | // serialized attributes |
105 | 105 | wp_json_encode($attributes), |
106 | 106 | // Closure for generating content if cache is expired |
107 | - function () use ($shortcode, $attributes) { |
|
107 | + function() use ($shortcode, $attributes) { |
|
108 | 108 | if ($shortcode->initialized() === false) { |
109 | 109 | $shortcode->initializeShortcode(); |
110 | 110 | } |
@@ -21,409 +21,409 @@ |
||
21 | 21 | class LegacyShortcodesManager |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @var EE_Registry $registry |
|
26 | - */ |
|
27 | - private $registry; |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * LegacyShortcodesManager constructor. |
|
32 | - * |
|
33 | - * @param \EE_Registry $registry |
|
34 | - */ |
|
35 | - public function __construct(EE_Registry $registry) |
|
36 | - { |
|
37 | - $this->registry = $registry; |
|
38 | - } |
|
39 | - |
|
40 | - |
|
41 | - /** |
|
42 | - * @return EE_Registry |
|
43 | - */ |
|
44 | - public function registry() |
|
45 | - { |
|
46 | - return $this->registry; |
|
47 | - } |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * registerShortcodes |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public function registerShortcodes() |
|
56 | - { |
|
57 | - $this->registry->shortcodes = $this->getShortcodes(); |
|
58 | - } |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * getShortcodes |
|
63 | - * |
|
64 | - * @return array |
|
65 | - */ |
|
66 | - public function getShortcodes() |
|
67 | - { |
|
68 | - // previously this method would glob the shortcodes directory |
|
69 | - // then filter that list of shortcodes to register, |
|
70 | - // but now we are going to just supply an empty array. |
|
71 | - // this allows any shortcodes that have not yet been converted to the new system |
|
72 | - // to still get loaded and processed, albeit using the same legacy logic as before |
|
73 | - $shortcodes_to_register = apply_filters( |
|
74 | - 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
75 | - array() |
|
76 | - ); |
|
77 | - if (! empty($shortcodes_to_register)) { |
|
78 | - // cycle thru shortcode folders |
|
79 | - foreach ($shortcodes_to_register as $shortcode_path) { |
|
80 | - // add to list of installed shortcode modules |
|
81 | - $this->registerShortcode($shortcode_path); |
|
82 | - } |
|
83 | - } |
|
84 | - // filter list of installed modules |
|
85 | - return apply_filters( |
|
86 | - 'FHEE__EE_Config___register_shortcodes__installed_shortcodes', |
|
87 | - ! empty($this->registry->shortcodes) |
|
88 | - ? $this->registry->shortcodes |
|
89 | - : array() |
|
90 | - ); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * register_shortcode - makes core aware of this shortcode |
|
96 | - * |
|
97 | - * @access public |
|
98 | - * @param string $shortcode_path - full path up to and including shortcode folder |
|
99 | - * @return bool |
|
100 | - */ |
|
101 | - public function registerShortcode($shortcode_path = null) |
|
102 | - { |
|
103 | - do_action('AHEE__EE_Config__register_shortcode__begin', $shortcode_path); |
|
104 | - $shortcode_ext = '.shortcode.php'; |
|
105 | - // make all separators match |
|
106 | - $shortcode_path = str_replace(array('\\', '/'), DS, $shortcode_path); |
|
107 | - // does the file path INCLUDE the actual file name as part of the path ? |
|
108 | - if (strpos($shortcode_path, $shortcode_ext) !== false) { |
|
109 | - // grab shortcode file name from directory name and break apart at dots |
|
110 | - $shortcode_file = explode('.', basename($shortcode_path)); |
|
111 | - // take first segment from file name pieces and remove class prefix if it exists |
|
112 | - $shortcode = strpos($shortcode_file[0], 'EES_') === 0 |
|
113 | - ? substr($shortcode_file[0], 4) |
|
114 | - : $shortcode_file[0]; |
|
115 | - // sanitize shortcode directory name |
|
116 | - $shortcode = sanitize_key($shortcode); |
|
117 | - // now we need to rebuild the shortcode path |
|
118 | - $shortcode_path = explode(DS, $shortcode_path); |
|
119 | - // remove last segment |
|
120 | - array_pop($shortcode_path); |
|
121 | - // glue it back together |
|
122 | - $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
123 | - } else { |
|
124 | - // we need to generate the filename based off of the folder name |
|
125 | - // grab and sanitize shortcode directory name |
|
126 | - $shortcode = sanitize_key(basename($shortcode_path)); |
|
127 | - $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
128 | - } |
|
129 | - // create classname from shortcode directory or file name |
|
130 | - $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
|
131 | - // add class prefix |
|
132 | - $shortcode_class = 'EES_' . $shortcode; |
|
133 | - // does the shortcode exist ? |
|
134 | - if (! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
135 | - $msg = sprintf( |
|
136 | - esc_html__( |
|
137 | - 'The requested %1$s shortcode file could not be found or is not readable due to file permissions. It should be in %2$s', |
|
138 | - 'event_espresso' |
|
139 | - ), |
|
140 | - $shortcode_class, |
|
141 | - $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
142 | - ); |
|
143 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
144 | - return false; |
|
145 | - } |
|
146 | - // load the shortcode class file |
|
147 | - require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
148 | - // verify that class exists |
|
149 | - if (! class_exists($shortcode_class)) { |
|
150 | - $msg = sprintf( |
|
151 | - esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
|
152 | - $shortcode_class |
|
153 | - ); |
|
154 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
155 | - return false; |
|
156 | - } |
|
157 | - $shortcode = strtoupper($shortcode); |
|
158 | - // add to array of registered shortcodes |
|
159 | - $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
160 | - return true; |
|
161 | - } |
|
162 | - |
|
163 | - |
|
164 | - /** |
|
165 | - * _initialize_shortcodes |
|
166 | - * allow shortcodes to set hooks for the rest of the system |
|
167 | - * |
|
168 | - * @access private |
|
169 | - * @return void |
|
170 | - */ |
|
171 | - public function addShortcodes() |
|
172 | - { |
|
173 | - // cycle thru shortcode folders |
|
174 | - foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
|
175 | - // add class prefix |
|
176 | - $shortcode_class = 'EES_' . $shortcode; |
|
177 | - // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
|
178 | - // which set hooks ? |
|
179 | - if (is_admin()) { |
|
180 | - // fire immediately |
|
181 | - call_user_func(array($shortcode_class, 'set_hooks_admin')); |
|
182 | - } else { |
|
183 | - // delay until other systems are online |
|
184 | - add_action( |
|
185 | - 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', |
|
186 | - array($shortcode_class, 'set_hooks') |
|
187 | - ); |
|
188 | - // convert classname to UPPERCASE and create WP shortcode. |
|
189 | - $shortcode_tag = strtoupper($shortcode); |
|
190 | - // but first check if the shortcode has already |
|
191 | - // been added before assigning 'fallback_shortcode_processor' |
|
192 | - if (! shortcode_exists($shortcode_tag)) { |
|
193 | - // NOTE: this shortcode declaration will get overridden if the shortcode |
|
194 | - // is successfully detected in the post content in initializeShortcode() |
|
195 | - add_shortcode($shortcode_tag, array($shortcode_class, 'fallback_shortcode_processor')); |
|
196 | - } |
|
197 | - } |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * callback for the WP "get_header" hook point |
|
204 | - * checks posts for EE shortcodes, and initializes them, |
|
205 | - * then toggles filter switch that loads core default assets |
|
206 | - * |
|
207 | - * @param \WP_Query $wp_query |
|
208 | - * @return void |
|
209 | - */ |
|
210 | - public function initializeShortcodes(WP_Query $wp_query) |
|
211 | - { |
|
212 | - if (empty($this->registry->shortcodes) || ! $wp_query->is_main_query() || is_admin()) { |
|
213 | - return; |
|
214 | - } |
|
215 | - global $wp; |
|
216 | - /** @var EE_Front_controller $Front_Controller */ |
|
217 | - $Front_Controller = $this->registry->load_core('Front_Controller', array(), false); |
|
218 | - do_action('AHEE__EE_Front_Controller__initialize_shortcodes__begin', $wp, $Front_Controller); |
|
219 | - $Front_Controller->Request_Handler()->set_request_vars(); |
|
220 | - // grab post_name from request |
|
221 | - $current_post = apply_filters( |
|
222 | - 'FHEE__EE_Front_Controller__initialize_shortcodes__current_post_name', |
|
223 | - $Front_Controller->Request_Handler()->get('post_name') |
|
224 | - ); |
|
225 | - $show_on_front = get_option('show_on_front'); |
|
226 | - // if it's not set, then check if frontpage is blog |
|
227 | - if (empty($current_post)) { |
|
228 | - // yup.. this is the posts page, prepare to load all shortcode modules |
|
229 | - $current_post = 'posts'; |
|
230 | - // unless.. |
|
231 | - if ($show_on_front === 'page') { |
|
232 | - // some other page is set as the homepage |
|
233 | - $page_on_front = get_option('page_on_front'); |
|
234 | - if ($page_on_front) { |
|
235 | - // k now we need to find the post_name for this page |
|
236 | - global $wpdb; |
|
237 | - $page_on_front = $wpdb->get_var( |
|
238 | - $wpdb->prepare( |
|
239 | - "SELECT post_name from {$wpdb->posts} WHERE post_type='page' AND post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d", |
|
240 | - $page_on_front |
|
241 | - ) |
|
242 | - ); |
|
243 | - // set the current post slug to what it actually is |
|
244 | - $current_post = $page_on_front ? $page_on_front : $current_post; |
|
245 | - } |
|
246 | - } |
|
247 | - } |
|
248 | - // in case $current_post is hierarchical like: /parent-page/current-page |
|
249 | - $current_post = basename($current_post); |
|
250 | - if (// is current page/post the "blog" page ? |
|
251 | - $current_post === EE_Config::get_page_for_posts() |
|
252 | - // or are we on a category page? |
|
253 | - || ( |
|
254 | - is_array(term_exists($current_post, 'category')) |
|
255 | - || array_key_exists('category_name', $wp->query_vars) |
|
256 | - ) |
|
257 | - ) { |
|
258 | - // initialize all legacy shortcodes |
|
259 | - $load_assets = $this->parseContentForShortcodes('', true); |
|
260 | - } else { |
|
261 | - global $wpdb; |
|
262 | - $post_content = $wpdb->get_var( |
|
263 | - $wpdb->prepare( |
|
264 | - "SELECT post_content from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s", |
|
265 | - $current_post |
|
266 | - ) |
|
267 | - ); |
|
268 | - $load_assets = $this->parseContentForShortcodes($post_content); |
|
269 | - } |
|
270 | - if ($load_assets) { |
|
271 | - $this->registry->REQ->set_espresso_page(true); |
|
272 | - add_filter('FHEE_load_css', '__return_true'); |
|
273 | - add_filter('FHEE_load_js', '__return_true'); |
|
274 | - } |
|
275 | - do_action('AHEE__EE_Front_Controller__initialize_shortcodes__end', $Front_Controller); |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * checks supplied content against list of legacy shortcodes, |
|
281 | - * then initializes any found shortcodes, and returns true. |
|
282 | - * returns false if no shortcodes found. |
|
283 | - * |
|
284 | - * @param string $content |
|
285 | - * @param bool $load_all if true, then ALL active legacy shortcodes will be initialized |
|
286 | - * @return bool |
|
287 | - */ |
|
288 | - public function parseContentForShortcodes($content = '', $load_all = false) |
|
289 | - { |
|
290 | - $has_shortcode = false; |
|
291 | - foreach ($this->registry->shortcodes as $shortcode_class => $shortcode) { |
|
292 | - if ($load_all || has_shortcode($content, $shortcode_class)) { |
|
293 | - // load up the shortcode |
|
294 | - $this->initializeShortcode($shortcode_class); |
|
295 | - $has_shortcode = true; |
|
296 | - } |
|
297 | - } |
|
298 | - return $has_shortcode; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * given a shortcode name, will instantiate the shortcode and call it's run() method |
|
304 | - * |
|
305 | - * @param string $shortcode_class |
|
306 | - * @param WP $wp |
|
307 | - */ |
|
308 | - public function initializeShortcode($shortcode_class = '', WP $wp = null) |
|
309 | - { |
|
310 | - // don't do anything if shortcode is already initialized |
|
311 | - if (empty($this->registry->shortcodes->{$shortcode_class}) |
|
312 | - || ! is_string($this->registry->shortcodes->{$shortcode_class}) |
|
313 | - ) { |
|
314 | - return; |
|
315 | - } |
|
316 | - // let's pause to reflect on this... |
|
317 | - $sc_reflector = new ReflectionClass(LegacyShortcodesManager::addShortcodeClassPrefix($shortcode_class)); |
|
318 | - // ensure that class is actually a shortcode |
|
319 | - if (defined('WP_DEBUG') |
|
320 | - && WP_DEBUG === true |
|
321 | - && ! $sc_reflector->isSubclassOf('EES_Shortcode') |
|
322 | - ) { |
|
323 | - EE_Error::add_error( |
|
324 | - sprintf( |
|
325 | - esc_html__( |
|
326 | - 'The requested %s shortcode is not of the class "EES_Shortcode". Please check your files.', |
|
327 | - 'event_espresso' |
|
328 | - ), |
|
329 | - $shortcode_class |
|
330 | - ), |
|
331 | - __FILE__, |
|
332 | - __FUNCTION__, |
|
333 | - __LINE__ |
|
334 | - ); |
|
335 | - add_filter('FHEE_run_EE_the_content', '__return_true'); |
|
336 | - return; |
|
337 | - } |
|
338 | - global $wp; |
|
339 | - // and pass the request object to the run method |
|
340 | - $this->registry->shortcodes->{$shortcode_class} = $sc_reflector->newInstance(); |
|
341 | - // fire the shortcode class's run method, so that it can activate resources |
|
342 | - $this->registry->shortcodes->{$shortcode_class}->run($wp); |
|
343 | - } |
|
344 | - |
|
345 | - |
|
346 | - /** |
|
347 | - * get classname, remove EES_prefix, and convert to UPPERCASE |
|
348 | - * |
|
349 | - * @param string $class_name |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public static function generateShortcodeTagFromClassName($class_name) |
|
353 | - { |
|
354 | - return strtoupper(str_replace('EES_', '', $class_name)); |
|
355 | - } |
|
356 | - |
|
357 | - |
|
358 | - /** |
|
359 | - * add EES_prefix and Capitalize words |
|
360 | - * |
|
361 | - * @param string $tag |
|
362 | - * @return string |
|
363 | - */ |
|
364 | - public static function generateShortcodeClassNameFromTag($tag) |
|
365 | - { |
|
366 | - // order of operation runs from inside to out |
|
367 | - // 5) maybe add prefix |
|
368 | - return LegacyShortcodesManager::addShortcodeClassPrefix( |
|
369 | - // 4) find spaces, replace with underscores |
|
370 | - str_replace( |
|
371 | - ' ', |
|
372 | - '_', |
|
373 | - // 3) capitalize first letter of each word |
|
374 | - ucwords( |
|
375 | - // 2) also change to lowercase so ucwords() will work |
|
376 | - strtolower( |
|
377 | - // 1) find underscores, replace with spaces so ucwords() will work |
|
378 | - str_replace( |
|
379 | - '_', |
|
380 | - ' ', |
|
381 | - $tag |
|
382 | - ) |
|
383 | - ) |
|
384 | - ) |
|
385 | - ) |
|
386 | - ); |
|
387 | - } |
|
388 | - |
|
389 | - |
|
390 | - /** |
|
391 | - * maybe add EES_prefix |
|
392 | - * |
|
393 | - * @param string $class_name |
|
394 | - * @return string |
|
395 | - */ |
|
396 | - public static function addShortcodeClassPrefix($class_name) |
|
397 | - { |
|
398 | - return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
399 | - } |
|
400 | - |
|
401 | - |
|
402 | - /** |
|
403 | - * @return array |
|
404 | - */ |
|
405 | - public function getEspressoShortcodeTags() |
|
406 | - { |
|
407 | - static $shortcode_tags = array(); |
|
408 | - if (empty($shortcode_tags)) { |
|
409 | - $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
410 | - } |
|
411 | - return $shortcode_tags; |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - /** |
|
416 | - * @param string $content |
|
417 | - * @return string |
|
418 | - */ |
|
419 | - public function doShortcode($content) |
|
420 | - { |
|
421 | - foreach ($this->getEspressoShortcodeTags() as $shortcode_tag) { |
|
422 | - if (strpos($content, $shortcode_tag) !== false) { |
|
423 | - $shortcode_class = LegacyShortcodesManager::generateShortcodeClassNameFromTag($shortcode_tag); |
|
424 | - $this->initializeShortcode($shortcode_class); |
|
425 | - } |
|
426 | - } |
|
427 | - return do_shortcode($content); |
|
428 | - } |
|
24 | + /** |
|
25 | + * @var EE_Registry $registry |
|
26 | + */ |
|
27 | + private $registry; |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * LegacyShortcodesManager constructor. |
|
32 | + * |
|
33 | + * @param \EE_Registry $registry |
|
34 | + */ |
|
35 | + public function __construct(EE_Registry $registry) |
|
36 | + { |
|
37 | + $this->registry = $registry; |
|
38 | + } |
|
39 | + |
|
40 | + |
|
41 | + /** |
|
42 | + * @return EE_Registry |
|
43 | + */ |
|
44 | + public function registry() |
|
45 | + { |
|
46 | + return $this->registry; |
|
47 | + } |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * registerShortcodes |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public function registerShortcodes() |
|
56 | + { |
|
57 | + $this->registry->shortcodes = $this->getShortcodes(); |
|
58 | + } |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * getShortcodes |
|
63 | + * |
|
64 | + * @return array |
|
65 | + */ |
|
66 | + public function getShortcodes() |
|
67 | + { |
|
68 | + // previously this method would glob the shortcodes directory |
|
69 | + // then filter that list of shortcodes to register, |
|
70 | + // but now we are going to just supply an empty array. |
|
71 | + // this allows any shortcodes that have not yet been converted to the new system |
|
72 | + // to still get loaded and processed, albeit using the same legacy logic as before |
|
73 | + $shortcodes_to_register = apply_filters( |
|
74 | + 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
|
75 | + array() |
|
76 | + ); |
|
77 | + if (! empty($shortcodes_to_register)) { |
|
78 | + // cycle thru shortcode folders |
|
79 | + foreach ($shortcodes_to_register as $shortcode_path) { |
|
80 | + // add to list of installed shortcode modules |
|
81 | + $this->registerShortcode($shortcode_path); |
|
82 | + } |
|
83 | + } |
|
84 | + // filter list of installed modules |
|
85 | + return apply_filters( |
|
86 | + 'FHEE__EE_Config___register_shortcodes__installed_shortcodes', |
|
87 | + ! empty($this->registry->shortcodes) |
|
88 | + ? $this->registry->shortcodes |
|
89 | + : array() |
|
90 | + ); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * register_shortcode - makes core aware of this shortcode |
|
96 | + * |
|
97 | + * @access public |
|
98 | + * @param string $shortcode_path - full path up to and including shortcode folder |
|
99 | + * @return bool |
|
100 | + */ |
|
101 | + public function registerShortcode($shortcode_path = null) |
|
102 | + { |
|
103 | + do_action('AHEE__EE_Config__register_shortcode__begin', $shortcode_path); |
|
104 | + $shortcode_ext = '.shortcode.php'; |
|
105 | + // make all separators match |
|
106 | + $shortcode_path = str_replace(array('\\', '/'), DS, $shortcode_path); |
|
107 | + // does the file path INCLUDE the actual file name as part of the path ? |
|
108 | + if (strpos($shortcode_path, $shortcode_ext) !== false) { |
|
109 | + // grab shortcode file name from directory name and break apart at dots |
|
110 | + $shortcode_file = explode('.', basename($shortcode_path)); |
|
111 | + // take first segment from file name pieces and remove class prefix if it exists |
|
112 | + $shortcode = strpos($shortcode_file[0], 'EES_') === 0 |
|
113 | + ? substr($shortcode_file[0], 4) |
|
114 | + : $shortcode_file[0]; |
|
115 | + // sanitize shortcode directory name |
|
116 | + $shortcode = sanitize_key($shortcode); |
|
117 | + // now we need to rebuild the shortcode path |
|
118 | + $shortcode_path = explode(DS, $shortcode_path); |
|
119 | + // remove last segment |
|
120 | + array_pop($shortcode_path); |
|
121 | + // glue it back together |
|
122 | + $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
123 | + } else { |
|
124 | + // we need to generate the filename based off of the folder name |
|
125 | + // grab and sanitize shortcode directory name |
|
126 | + $shortcode = sanitize_key(basename($shortcode_path)); |
|
127 | + $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
128 | + } |
|
129 | + // create classname from shortcode directory or file name |
|
130 | + $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
|
131 | + // add class prefix |
|
132 | + $shortcode_class = 'EES_' . $shortcode; |
|
133 | + // does the shortcode exist ? |
|
134 | + if (! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
135 | + $msg = sprintf( |
|
136 | + esc_html__( |
|
137 | + 'The requested %1$s shortcode file could not be found or is not readable due to file permissions. It should be in %2$s', |
|
138 | + 'event_espresso' |
|
139 | + ), |
|
140 | + $shortcode_class, |
|
141 | + $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
142 | + ); |
|
143 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
144 | + return false; |
|
145 | + } |
|
146 | + // load the shortcode class file |
|
147 | + require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
148 | + // verify that class exists |
|
149 | + if (! class_exists($shortcode_class)) { |
|
150 | + $msg = sprintf( |
|
151 | + esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
|
152 | + $shortcode_class |
|
153 | + ); |
|
154 | + EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
155 | + return false; |
|
156 | + } |
|
157 | + $shortcode = strtoupper($shortcode); |
|
158 | + // add to array of registered shortcodes |
|
159 | + $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
160 | + return true; |
|
161 | + } |
|
162 | + |
|
163 | + |
|
164 | + /** |
|
165 | + * _initialize_shortcodes |
|
166 | + * allow shortcodes to set hooks for the rest of the system |
|
167 | + * |
|
168 | + * @access private |
|
169 | + * @return void |
|
170 | + */ |
|
171 | + public function addShortcodes() |
|
172 | + { |
|
173 | + // cycle thru shortcode folders |
|
174 | + foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
|
175 | + // add class prefix |
|
176 | + $shortcode_class = 'EES_' . $shortcode; |
|
177 | + // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
|
178 | + // which set hooks ? |
|
179 | + if (is_admin()) { |
|
180 | + // fire immediately |
|
181 | + call_user_func(array($shortcode_class, 'set_hooks_admin')); |
|
182 | + } else { |
|
183 | + // delay until other systems are online |
|
184 | + add_action( |
|
185 | + 'AHEE__EE_System__set_hooks_for_shortcodes_modules_and_addons', |
|
186 | + array($shortcode_class, 'set_hooks') |
|
187 | + ); |
|
188 | + // convert classname to UPPERCASE and create WP shortcode. |
|
189 | + $shortcode_tag = strtoupper($shortcode); |
|
190 | + // but first check if the shortcode has already |
|
191 | + // been added before assigning 'fallback_shortcode_processor' |
|
192 | + if (! shortcode_exists($shortcode_tag)) { |
|
193 | + // NOTE: this shortcode declaration will get overridden if the shortcode |
|
194 | + // is successfully detected in the post content in initializeShortcode() |
|
195 | + add_shortcode($shortcode_tag, array($shortcode_class, 'fallback_shortcode_processor')); |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * callback for the WP "get_header" hook point |
|
204 | + * checks posts for EE shortcodes, and initializes them, |
|
205 | + * then toggles filter switch that loads core default assets |
|
206 | + * |
|
207 | + * @param \WP_Query $wp_query |
|
208 | + * @return void |
|
209 | + */ |
|
210 | + public function initializeShortcodes(WP_Query $wp_query) |
|
211 | + { |
|
212 | + if (empty($this->registry->shortcodes) || ! $wp_query->is_main_query() || is_admin()) { |
|
213 | + return; |
|
214 | + } |
|
215 | + global $wp; |
|
216 | + /** @var EE_Front_controller $Front_Controller */ |
|
217 | + $Front_Controller = $this->registry->load_core('Front_Controller', array(), false); |
|
218 | + do_action('AHEE__EE_Front_Controller__initialize_shortcodes__begin', $wp, $Front_Controller); |
|
219 | + $Front_Controller->Request_Handler()->set_request_vars(); |
|
220 | + // grab post_name from request |
|
221 | + $current_post = apply_filters( |
|
222 | + 'FHEE__EE_Front_Controller__initialize_shortcodes__current_post_name', |
|
223 | + $Front_Controller->Request_Handler()->get('post_name') |
|
224 | + ); |
|
225 | + $show_on_front = get_option('show_on_front'); |
|
226 | + // if it's not set, then check if frontpage is blog |
|
227 | + if (empty($current_post)) { |
|
228 | + // yup.. this is the posts page, prepare to load all shortcode modules |
|
229 | + $current_post = 'posts'; |
|
230 | + // unless.. |
|
231 | + if ($show_on_front === 'page') { |
|
232 | + // some other page is set as the homepage |
|
233 | + $page_on_front = get_option('page_on_front'); |
|
234 | + if ($page_on_front) { |
|
235 | + // k now we need to find the post_name for this page |
|
236 | + global $wpdb; |
|
237 | + $page_on_front = $wpdb->get_var( |
|
238 | + $wpdb->prepare( |
|
239 | + "SELECT post_name from {$wpdb->posts} WHERE post_type='page' AND post_status NOT IN ('auto-draft', 'inherit', 'trash') AND ID=%d", |
|
240 | + $page_on_front |
|
241 | + ) |
|
242 | + ); |
|
243 | + // set the current post slug to what it actually is |
|
244 | + $current_post = $page_on_front ? $page_on_front : $current_post; |
|
245 | + } |
|
246 | + } |
|
247 | + } |
|
248 | + // in case $current_post is hierarchical like: /parent-page/current-page |
|
249 | + $current_post = basename($current_post); |
|
250 | + if (// is current page/post the "blog" page ? |
|
251 | + $current_post === EE_Config::get_page_for_posts() |
|
252 | + // or are we on a category page? |
|
253 | + || ( |
|
254 | + is_array(term_exists($current_post, 'category')) |
|
255 | + || array_key_exists('category_name', $wp->query_vars) |
|
256 | + ) |
|
257 | + ) { |
|
258 | + // initialize all legacy shortcodes |
|
259 | + $load_assets = $this->parseContentForShortcodes('', true); |
|
260 | + } else { |
|
261 | + global $wpdb; |
|
262 | + $post_content = $wpdb->get_var( |
|
263 | + $wpdb->prepare( |
|
264 | + "SELECT post_content from {$wpdb->posts} WHERE post_status NOT IN ('auto-draft', 'inherit', 'trash') AND post_name=%s", |
|
265 | + $current_post |
|
266 | + ) |
|
267 | + ); |
|
268 | + $load_assets = $this->parseContentForShortcodes($post_content); |
|
269 | + } |
|
270 | + if ($load_assets) { |
|
271 | + $this->registry->REQ->set_espresso_page(true); |
|
272 | + add_filter('FHEE_load_css', '__return_true'); |
|
273 | + add_filter('FHEE_load_js', '__return_true'); |
|
274 | + } |
|
275 | + do_action('AHEE__EE_Front_Controller__initialize_shortcodes__end', $Front_Controller); |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * checks supplied content against list of legacy shortcodes, |
|
281 | + * then initializes any found shortcodes, and returns true. |
|
282 | + * returns false if no shortcodes found. |
|
283 | + * |
|
284 | + * @param string $content |
|
285 | + * @param bool $load_all if true, then ALL active legacy shortcodes will be initialized |
|
286 | + * @return bool |
|
287 | + */ |
|
288 | + public function parseContentForShortcodes($content = '', $load_all = false) |
|
289 | + { |
|
290 | + $has_shortcode = false; |
|
291 | + foreach ($this->registry->shortcodes as $shortcode_class => $shortcode) { |
|
292 | + if ($load_all || has_shortcode($content, $shortcode_class)) { |
|
293 | + // load up the shortcode |
|
294 | + $this->initializeShortcode($shortcode_class); |
|
295 | + $has_shortcode = true; |
|
296 | + } |
|
297 | + } |
|
298 | + return $has_shortcode; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * given a shortcode name, will instantiate the shortcode and call it's run() method |
|
304 | + * |
|
305 | + * @param string $shortcode_class |
|
306 | + * @param WP $wp |
|
307 | + */ |
|
308 | + public function initializeShortcode($shortcode_class = '', WP $wp = null) |
|
309 | + { |
|
310 | + // don't do anything if shortcode is already initialized |
|
311 | + if (empty($this->registry->shortcodes->{$shortcode_class}) |
|
312 | + || ! is_string($this->registry->shortcodes->{$shortcode_class}) |
|
313 | + ) { |
|
314 | + return; |
|
315 | + } |
|
316 | + // let's pause to reflect on this... |
|
317 | + $sc_reflector = new ReflectionClass(LegacyShortcodesManager::addShortcodeClassPrefix($shortcode_class)); |
|
318 | + // ensure that class is actually a shortcode |
|
319 | + if (defined('WP_DEBUG') |
|
320 | + && WP_DEBUG === true |
|
321 | + && ! $sc_reflector->isSubclassOf('EES_Shortcode') |
|
322 | + ) { |
|
323 | + EE_Error::add_error( |
|
324 | + sprintf( |
|
325 | + esc_html__( |
|
326 | + 'The requested %s shortcode is not of the class "EES_Shortcode". Please check your files.', |
|
327 | + 'event_espresso' |
|
328 | + ), |
|
329 | + $shortcode_class |
|
330 | + ), |
|
331 | + __FILE__, |
|
332 | + __FUNCTION__, |
|
333 | + __LINE__ |
|
334 | + ); |
|
335 | + add_filter('FHEE_run_EE_the_content', '__return_true'); |
|
336 | + return; |
|
337 | + } |
|
338 | + global $wp; |
|
339 | + // and pass the request object to the run method |
|
340 | + $this->registry->shortcodes->{$shortcode_class} = $sc_reflector->newInstance(); |
|
341 | + // fire the shortcode class's run method, so that it can activate resources |
|
342 | + $this->registry->shortcodes->{$shortcode_class}->run($wp); |
|
343 | + } |
|
344 | + |
|
345 | + |
|
346 | + /** |
|
347 | + * get classname, remove EES_prefix, and convert to UPPERCASE |
|
348 | + * |
|
349 | + * @param string $class_name |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public static function generateShortcodeTagFromClassName($class_name) |
|
353 | + { |
|
354 | + return strtoupper(str_replace('EES_', '', $class_name)); |
|
355 | + } |
|
356 | + |
|
357 | + |
|
358 | + /** |
|
359 | + * add EES_prefix and Capitalize words |
|
360 | + * |
|
361 | + * @param string $tag |
|
362 | + * @return string |
|
363 | + */ |
|
364 | + public static function generateShortcodeClassNameFromTag($tag) |
|
365 | + { |
|
366 | + // order of operation runs from inside to out |
|
367 | + // 5) maybe add prefix |
|
368 | + return LegacyShortcodesManager::addShortcodeClassPrefix( |
|
369 | + // 4) find spaces, replace with underscores |
|
370 | + str_replace( |
|
371 | + ' ', |
|
372 | + '_', |
|
373 | + // 3) capitalize first letter of each word |
|
374 | + ucwords( |
|
375 | + // 2) also change to lowercase so ucwords() will work |
|
376 | + strtolower( |
|
377 | + // 1) find underscores, replace with spaces so ucwords() will work |
|
378 | + str_replace( |
|
379 | + '_', |
|
380 | + ' ', |
|
381 | + $tag |
|
382 | + ) |
|
383 | + ) |
|
384 | + ) |
|
385 | + ) |
|
386 | + ); |
|
387 | + } |
|
388 | + |
|
389 | + |
|
390 | + /** |
|
391 | + * maybe add EES_prefix |
|
392 | + * |
|
393 | + * @param string $class_name |
|
394 | + * @return string |
|
395 | + */ |
|
396 | + public static function addShortcodeClassPrefix($class_name) |
|
397 | + { |
|
398 | + return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
399 | + } |
|
400 | + |
|
401 | + |
|
402 | + /** |
|
403 | + * @return array |
|
404 | + */ |
|
405 | + public function getEspressoShortcodeTags() |
|
406 | + { |
|
407 | + static $shortcode_tags = array(); |
|
408 | + if (empty($shortcode_tags)) { |
|
409 | + $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
410 | + } |
|
411 | + return $shortcode_tags; |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + /** |
|
416 | + * @param string $content |
|
417 | + * @return string |
|
418 | + */ |
|
419 | + public function doShortcode($content) |
|
420 | + { |
|
421 | + foreach ($this->getEspressoShortcodeTags() as $shortcode_tag) { |
|
422 | + if (strpos($content, $shortcode_tag) !== false) { |
|
423 | + $shortcode_class = LegacyShortcodesManager::generateShortcodeClassNameFromTag($shortcode_tag); |
|
424 | + $this->initializeShortcode($shortcode_class); |
|
425 | + } |
|
426 | + } |
|
427 | + return do_shortcode($content); |
|
428 | + } |
|
429 | 429 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | 'FHEE__EE_Config__register_shortcodes__shortcodes_to_register', |
75 | 75 | array() |
76 | 76 | ); |
77 | - if (! empty($shortcodes_to_register)) { |
|
77 | + if ( ! empty($shortcodes_to_register)) { |
|
78 | 78 | // cycle thru shortcode folders |
79 | 79 | foreach ($shortcodes_to_register as $shortcode_path) { |
80 | 80 | // add to list of installed shortcode modules |
@@ -119,44 +119,44 @@ discard block |
||
119 | 119 | // remove last segment |
120 | 120 | array_pop($shortcode_path); |
121 | 121 | // glue it back together |
122 | - $shortcode_path = implode(DS, $shortcode_path) . DS; |
|
122 | + $shortcode_path = implode(DS, $shortcode_path).DS; |
|
123 | 123 | } else { |
124 | 124 | // we need to generate the filename based off of the folder name |
125 | 125 | // grab and sanitize shortcode directory name |
126 | 126 | $shortcode = sanitize_key(basename($shortcode_path)); |
127 | - $shortcode_path = rtrim($shortcode_path, DS) . DS; |
|
127 | + $shortcode_path = rtrim($shortcode_path, DS).DS; |
|
128 | 128 | } |
129 | 129 | // create classname from shortcode directory or file name |
130 | 130 | $shortcode = str_replace(' ', '_', ucwords(str_replace('_', ' ', $shortcode))); |
131 | 131 | // add class prefix |
132 | - $shortcode_class = 'EES_' . $shortcode; |
|
132 | + $shortcode_class = 'EES_'.$shortcode; |
|
133 | 133 | // does the shortcode exist ? |
134 | - if (! is_readable($shortcode_path . DS . $shortcode_class . $shortcode_ext)) { |
|
134 | + if ( ! is_readable($shortcode_path.DS.$shortcode_class.$shortcode_ext)) { |
|
135 | 135 | $msg = sprintf( |
136 | 136 | esc_html__( |
137 | 137 | 'The requested %1$s shortcode file could not be found or is not readable due to file permissions. It should be in %2$s', |
138 | 138 | 'event_espresso' |
139 | 139 | ), |
140 | 140 | $shortcode_class, |
141 | - $shortcode_path . DS . $shortcode_class . $shortcode_ext |
|
141 | + $shortcode_path.DS.$shortcode_class.$shortcode_ext |
|
142 | 142 | ); |
143 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
143 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
144 | 144 | return false; |
145 | 145 | } |
146 | 146 | // load the shortcode class file |
147 | - require_once($shortcode_path . $shortcode_class . $shortcode_ext); |
|
147 | + require_once($shortcode_path.$shortcode_class.$shortcode_ext); |
|
148 | 148 | // verify that class exists |
149 | - if (! class_exists($shortcode_class)) { |
|
149 | + if ( ! class_exists($shortcode_class)) { |
|
150 | 150 | $msg = sprintf( |
151 | 151 | esc_html__('The requested %s shortcode class does not exist.', 'event_espresso'), |
152 | 152 | $shortcode_class |
153 | 153 | ); |
154 | - EE_Error::add_error($msg . '||' . $msg, __FILE__, __FUNCTION__, __LINE__); |
|
154 | + EE_Error::add_error($msg.'||'.$msg, __FILE__, __FUNCTION__, __LINE__); |
|
155 | 155 | return false; |
156 | 156 | } |
157 | 157 | $shortcode = strtoupper($shortcode); |
158 | 158 | // add to array of registered shortcodes |
159 | - $this->registry->shortcodes->{$shortcode} = $shortcode_path . $shortcode_class . $shortcode_ext; |
|
159 | + $this->registry->shortcodes->{$shortcode} = $shortcode_path.$shortcode_class.$shortcode_ext; |
|
160 | 160 | return true; |
161 | 161 | } |
162 | 162 | |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | // cycle thru shortcode folders |
174 | 174 | foreach ($this->registry->shortcodes as $shortcode => $shortcode_path) { |
175 | 175 | // add class prefix |
176 | - $shortcode_class = 'EES_' . $shortcode; |
|
176 | + $shortcode_class = 'EES_'.$shortcode; |
|
177 | 177 | // fire the shortcode class's set_hooks methods in case it needs to hook into other parts of the system |
178 | 178 | // which set hooks ? |
179 | 179 | if (is_admin()) { |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | $shortcode_tag = strtoupper($shortcode); |
190 | 190 | // but first check if the shortcode has already |
191 | 191 | // been added before assigning 'fallback_shortcode_processor' |
192 | - if (! shortcode_exists($shortcode_tag)) { |
|
192 | + if ( ! shortcode_exists($shortcode_tag)) { |
|
193 | 193 | // NOTE: this shortcode declaration will get overridden if the shortcode |
194 | 194 | // is successfully detected in the post content in initializeShortcode() |
195 | 195 | add_shortcode($shortcode_tag, array($shortcode_class, 'fallback_shortcode_processor')); |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | */ |
396 | 396 | public static function addShortcodeClassPrefix($class_name) |
397 | 397 | { |
398 | - return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_' . $class_name; |
|
398 | + return strpos($class_name, 'EES_') === 0 ? $class_name : 'EES_'.$class_name; |
|
399 | 399 | } |
400 | 400 | |
401 | 401 | |
@@ -406,7 +406,7 @@ discard block |
||
406 | 406 | { |
407 | 407 | static $shortcode_tags = array(); |
408 | 408 | if (empty($shortcode_tags)) { |
409 | - $shortcode_tags = array_keys((array)$this->registry->shortcodes); |
|
409 | + $shortcode_tags = array_keys((array) $this->registry->shortcodes); |
|
410 | 410 | } |
411 | 411 | return $shortcode_tags; |
412 | 412 | } |
@@ -15,94 +15,94 @@ |
||
15 | 15 | */ |
16 | 16 | class LicenseService |
17 | 17 | { |
18 | - /** |
|
19 | - * @var Config |
|
20 | - */ |
|
21 | - private $config; |
|
22 | - |
|
23 | - |
|
24 | - /** |
|
25 | - * @var Stats |
|
26 | - */ |
|
27 | - private $stats_collection; |
|
28 | - |
|
29 | - public function __construct(Stats $stats_collection, Config $config) |
|
30 | - { |
|
31 | - $this->config = $config; |
|
32 | - $this->stats_collection = $stats_collection; |
|
33 | - $this->loadPueClient(); |
|
34 | - } |
|
35 | - |
|
36 | - private function loadPueClient() |
|
37 | - { |
|
38 | - // PUE Auto Upgrades stuff |
|
39 | - if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { // include the file |
|
40 | - require_once(EE_THIRD_PARTY . 'pue/pue-client.php'); |
|
41 | - |
|
42 | - // $options needs to be an array with the included keys as listed. |
|
43 | - $options = array( |
|
44 | - // 'optionName' => '', //(optional) - used as the reference for saving update information in the |
|
45 | - // clients options table. Will be automatically set if left blank. |
|
46 | - 'apikey' => $this->config->siteLicenseKey(), |
|
47 | - // (required), you will need to obtain the apikey that the client gets from your site and |
|
48 | - // then saves in their sites options table (see 'getting an api-key' below) |
|
49 | - 'lang_domain' => $this->config->i18nDomain(), |
|
50 | - // (optional) - put here whatever reference you are using for the localization of your plugin (if it's |
|
51 | - // localized). That way strings in this file will be included in the translation for your plugin. |
|
52 | - 'checkPeriod' => $this->config->checkPeriod(), |
|
53 | - // (optional) - use this parameter to indicate how often you want the client's install to ping your |
|
54 | - // server for update checks. The integer indicates hours. If you don't include this parameter it will |
|
55 | - // default to 12 hours. |
|
56 | - 'option_key' => $this->config->optionKey(), |
|
57 | - // this is what is used to reference the api_key in your plugin options. PUE uses this to trigger |
|
58 | - // updating your information message whenever this option_key is modified. |
|
59 | - 'options_page_slug' => $this->config->optionsPageSlug(), |
|
60 | - 'plugin_basename' => EE_PLUGIN_BASENAME, |
|
61 | - 'use_wp_update' => true, |
|
62 | - // if TRUE then you want FREE versions of the plugin to be updated from WP |
|
63 | - 'extra_stats' => $this->stats_collection->statsCallback(), |
|
64 | - 'turn_on_notices_saved' => true, |
|
65 | - ); |
|
66 | - // initiate the class and start the plugin update engine! |
|
67 | - new PluginUpdateEngineChecker( |
|
68 | - $this->config->hostServerUrl(), |
|
69 | - $this->config->pluginSlug(), |
|
70 | - $options |
|
71 | - ); |
|
72 | - } |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * This is a handy helper method for retrieving whether there is an update available for the given plugin. |
|
78 | - * |
|
79 | - * @param string $basename Use the equivalent result from plugin_basename() for this param as WP uses that to |
|
80 | - * identify plugins. Defaults to core update |
|
81 | - * @return boolean True if update available, false if not. |
|
82 | - */ |
|
83 | - public static function isUpdateAvailable($basename = '') |
|
84 | - { |
|
85 | - $basename = ! empty($basename) ? $basename : EE_PLUGIN_BASENAME; |
|
86 | - |
|
87 | - $update = false; |
|
88 | - |
|
89 | - // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core" |
|
90 | - $folder = DS . dirname($basename); |
|
91 | - |
|
92 | - $plugins = get_plugins($folder); |
|
93 | - $current = get_site_transient('update_plugins'); |
|
94 | - |
|
95 | - foreach ((array)$plugins as $plugin_file => $plugin_data) { |
|
96 | - if (isset($current->response['plugin_file'])) { |
|
97 | - $update = true; |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - // it's possible that there is an update but an invalid site-license-key is in use |
|
102 | - if (get_site_option('pue_json_error_' . $basename)) { |
|
103 | - $update = true; |
|
104 | - } |
|
105 | - |
|
106 | - return $update; |
|
107 | - } |
|
18 | + /** |
|
19 | + * @var Config |
|
20 | + */ |
|
21 | + private $config; |
|
22 | + |
|
23 | + |
|
24 | + /** |
|
25 | + * @var Stats |
|
26 | + */ |
|
27 | + private $stats_collection; |
|
28 | + |
|
29 | + public function __construct(Stats $stats_collection, Config $config) |
|
30 | + { |
|
31 | + $this->config = $config; |
|
32 | + $this->stats_collection = $stats_collection; |
|
33 | + $this->loadPueClient(); |
|
34 | + } |
|
35 | + |
|
36 | + private function loadPueClient() |
|
37 | + { |
|
38 | + // PUE Auto Upgrades stuff |
|
39 | + if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { // include the file |
|
40 | + require_once(EE_THIRD_PARTY . 'pue/pue-client.php'); |
|
41 | + |
|
42 | + // $options needs to be an array with the included keys as listed. |
|
43 | + $options = array( |
|
44 | + // 'optionName' => '', //(optional) - used as the reference for saving update information in the |
|
45 | + // clients options table. Will be automatically set if left blank. |
|
46 | + 'apikey' => $this->config->siteLicenseKey(), |
|
47 | + // (required), you will need to obtain the apikey that the client gets from your site and |
|
48 | + // then saves in their sites options table (see 'getting an api-key' below) |
|
49 | + 'lang_domain' => $this->config->i18nDomain(), |
|
50 | + // (optional) - put here whatever reference you are using for the localization of your plugin (if it's |
|
51 | + // localized). That way strings in this file will be included in the translation for your plugin. |
|
52 | + 'checkPeriod' => $this->config->checkPeriod(), |
|
53 | + // (optional) - use this parameter to indicate how often you want the client's install to ping your |
|
54 | + // server for update checks. The integer indicates hours. If you don't include this parameter it will |
|
55 | + // default to 12 hours. |
|
56 | + 'option_key' => $this->config->optionKey(), |
|
57 | + // this is what is used to reference the api_key in your plugin options. PUE uses this to trigger |
|
58 | + // updating your information message whenever this option_key is modified. |
|
59 | + 'options_page_slug' => $this->config->optionsPageSlug(), |
|
60 | + 'plugin_basename' => EE_PLUGIN_BASENAME, |
|
61 | + 'use_wp_update' => true, |
|
62 | + // if TRUE then you want FREE versions of the plugin to be updated from WP |
|
63 | + 'extra_stats' => $this->stats_collection->statsCallback(), |
|
64 | + 'turn_on_notices_saved' => true, |
|
65 | + ); |
|
66 | + // initiate the class and start the plugin update engine! |
|
67 | + new PluginUpdateEngineChecker( |
|
68 | + $this->config->hostServerUrl(), |
|
69 | + $this->config->pluginSlug(), |
|
70 | + $options |
|
71 | + ); |
|
72 | + } |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * This is a handy helper method for retrieving whether there is an update available for the given plugin. |
|
78 | + * |
|
79 | + * @param string $basename Use the equivalent result from plugin_basename() for this param as WP uses that to |
|
80 | + * identify plugins. Defaults to core update |
|
81 | + * @return boolean True if update available, false if not. |
|
82 | + */ |
|
83 | + public static function isUpdateAvailable($basename = '') |
|
84 | + { |
|
85 | + $basename = ! empty($basename) ? $basename : EE_PLUGIN_BASENAME; |
|
86 | + |
|
87 | + $update = false; |
|
88 | + |
|
89 | + // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core" |
|
90 | + $folder = DS . dirname($basename); |
|
91 | + |
|
92 | + $plugins = get_plugins($folder); |
|
93 | + $current = get_site_transient('update_plugins'); |
|
94 | + |
|
95 | + foreach ((array)$plugins as $plugin_file => $plugin_data) { |
|
96 | + if (isset($current->response['plugin_file'])) { |
|
97 | + $update = true; |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + // it's possible that there is an update but an invalid site-license-key is in use |
|
102 | + if (get_site_option('pue_json_error_' . $basename)) { |
|
103 | + $update = true; |
|
104 | + } |
|
105 | + |
|
106 | + return $update; |
|
107 | + } |
|
108 | 108 | } |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | private function loadPueClient() |
37 | 37 | { |
38 | 38 | // PUE Auto Upgrades stuff |
39 | - if (is_readable(EE_THIRD_PARTY . 'pue/pue-client.php')) { // include the file |
|
40 | - require_once(EE_THIRD_PARTY . 'pue/pue-client.php'); |
|
39 | + if (is_readable(EE_THIRD_PARTY.'pue/pue-client.php')) { // include the file |
|
40 | + require_once(EE_THIRD_PARTY.'pue/pue-client.php'); |
|
41 | 41 | |
42 | 42 | // $options needs to be an array with the included keys as listed. |
43 | 43 | $options = array( |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | $update = false; |
88 | 88 | |
89 | 89 | // should take "event-espresso-core/espresso.php" and change to "/event-espresso-core" |
90 | - $folder = DS . dirname($basename); |
|
90 | + $folder = DS.dirname($basename); |
|
91 | 91 | |
92 | 92 | $plugins = get_plugins($folder); |
93 | 93 | $current = get_site_transient('update_plugins'); |
94 | 94 | |
95 | - foreach ((array)$plugins as $plugin_file => $plugin_data) { |
|
95 | + foreach ((array) $plugins as $plugin_file => $plugin_data) { |
|
96 | 96 | if (isset($current->response['plugin_file'])) { |
97 | 97 | $update = true; |
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
101 | 101 | // it's possible that there is an update but an invalid site-license-key is in use |
102 | - if (get_site_option('pue_json_error_' . $basename)) { |
|
102 | + if (get_site_option('pue_json_error_'.$basename)) { |
|
103 | 103 | $update = true; |
104 | 104 | } |
105 | 105 |