PluginProbe
Boxzilla – WordPress Popup Builder / 3.4.0
Boxzilla – WordPress Popup Builder v3.4.0
3.4.11 3.4.10 3.4.9 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 trunk 3.0 3.0.1 3.0.2 3.0.3 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 All 72 releases
← All changes | src/class-loader.php +285 -294 3.4.113.4.0 View file →
@@ -1,351 +1,342 @@
1 1 <?php
2 2
3 3 namespace Boxzilla;
4 4
5 -if (! defined('ABSPATH')) {
6 - exit;
7 -}
5 +class BoxLoader {
8 6
9 -class BoxLoader
10 -{
11 - /**
12 - * @var Plugin
13 - */
14 - private $plugin;
15 7
16 - /**
17 - * @var array
18 - */
19 - private $box_ids_to_load = [];
8 + /**
9 + * @var Plugin
10 + */
11 + private $plugin;
20 12
21 - /**
22 - * @var array
23 - */
24 - protected $options;
13 + /**
14 + * @var array
15 + */
16 + private $box_ids_to_load = array();
25 17
26 - /**
27 - * Constructor
28 - *
29 - * @param Plugin $plugin
30 - * @param array $options
31 - */
32 - public function __construct(Plugin $plugin, array $options)
33 - {
34 - $this->plugin = $plugin;
35 - $this->options = $options;
36 - }
18 + /**
19 + * @var array
20 + */
21 + protected $options;
37 22
38 - /**
39 - * Initializes the plugin, runs on `wp` hook.
40 - */
41 - public function init()
42 - {
43 - $this->box_ids_to_load = $this->filter_boxes();
23 + /**
24 + * Constructor
25 + *
26 + * @param Plugin $plugin
27 + * @param array $options
28 + */
29 + public function __construct( Plugin $plugin, array $options ) {
30 + $this->plugin = $plugin;
31 + $this->options = $options;
32 + }
44 33
45 - // Only add other hooks if necessary
46 - add_action('wp_head', [$this, 'print_preload_js'], 10, 0);
47 - if (count($this->box_ids_to_load) > 0) {
48 - add_action('wp_footer', [ $this, 'print_boxes_content' ], 1);
49 - add_action('wp_enqueue_scripts', [ $this, 'load_assets' ], 90);
50 - }
51 - }
34 + /**
35 + * Initializes the plugin, runs on `wp` hook.
36 + */
37 + public function init() {
38 + $this->box_ids_to_load = $this->filter_boxes();
52 39
53 - /**
54 - * Prints the preload API so that the Boxzilla JS API can be used before Boxzilla itself is loaded
55 - * This allows us to defer the Boxzilla script itself.
56 - */
57 - public function print_preload_js(): void
58 - {
59 - echo '<script>';
60 - include BOXZILLA_DIR . '/assets/js/preload.js';
61 - echo '</script>';
62 - }
40 + // Only add other hooks if necessary
41 + add_action('wp_head', array($this, 'print_preload_js'));
42 + if ( count( $this->box_ids_to_load ) > 0 ) {
43 + add_action( 'wp_footer', array( $this, 'print_boxes_content' ), 1 );
44 + add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 90 );
45 + }
46 + }
63 47
64 - /**
65 - * Get global rules for all boxes
66 - *
67 - * @return array
68 - */
69 - protected function get_filter_rules(): array
70 - {
71 - $rules = get_option('boxzilla_rules', []);
72 - return is_array($rules) ? $rules : [];
73 - }
48 + /**
49 + * Prints the preload API so that the Boxzilla JS API can be used before Boxzilla itself is loaded
50 + * This allows us to defer the Boxzilla script itself.
51 + */
52 + public function print_preload_js() {
53 + echo '<script>';
54 + echo file_get_contents(BOXZILLA_DIR . '/assets/js/preload.js');
55 + echo '</script>';
56 + }
74 57
58 + /**
59 + * Get global rules for all boxes
60 + *
61 + * @return array
62 + */
63 + protected function get_filter_rules() {
64 + $rules = get_option( 'boxzilla_rules', array() );
65 + return is_array($rules) ? $rules : array();
66 + }
75 67
76 - /**
77 - * Match a string against an array of patterns, glob-style.
78 - *
79 - * @param string $string
80 - * @param array $patterns
81 - * @param bool $contains
82 - * @return bool
83 - */
84 - protected function match_patterns($string, array $patterns, $contains = false)
85 - {
86 - $string = strtolower($string);
87 68
88 - foreach ($patterns as $pattern) {
89 - $pattern = rtrim($pattern, '/');
90 - $pattern = strtolower($pattern);
69 + /**
70 + * Match a string against an array of patterns, glob-style.
71 + *
72 + * @param string $string
73 + * @param array $patterns
74 + * @param boolean $contains
75 + * @return boolean
76 + */
77 + protected function match_patterns( $string, array $patterns, $contains = false ) {
78 + $string = strtolower( $string );
91 79
92 - if ($contains) {
93 - // contains means we should do a simple occurrence check
94 - // does not support wildcards
95 - $match = strpos($string, $pattern) !== false;
96 - } elseif (function_exists('fnmatch')) {
97 - $match = fnmatch($pattern, $string);
98 - } else {
99 - $match = ( $pattern === $string );
100 - }
80 + foreach ( $patterns as $pattern ) {
81 + $pattern = rtrim( $pattern, '/' );
82 + $pattern = strtolower( $pattern );
101 83
102 - if ($match) {
103 - return true;
104 - }
105 - }
84 + if ( $contains ) {
85 + // contains means we should do a simple occurrence check
86 + // does not support wildcards
87 + $match = strpos( $string, $pattern ) !== false;
88 + } elseif ( function_exists( 'fnmatch' ) ) {
89 + $match = fnmatch( $pattern, $string );
90 + } else {
91 + $match = ( $pattern === $string );
92 + }
106 93
107 - return false;
108 - }
94 + if ( $match ) {
95 + return true;
96 + }
97 + }
109 98
110 - /**
111 - * @return string
112 - */
113 - protected function get_request_url()
114 - {
115 - return rtrim(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), '/');
116 - }
99 + return false;
100 + }
117 101
118 - /**
119 - * Check if this rule passes (conditional matches expected value)
120 - *
121 - * @param string $condition
122 - * @param string $value
123 - * @param boolean $qualifier
124 - *
125 - * @return bool
126 - */
127 - protected function match_rule($condition, $value, $qualifier = true)
128 - {
129 - $matched = false;
102 + /**
103 + * @return string
104 + */
105 + protected function get_request_url() {
106 + return rtrim( $_SERVER['REQUEST_URI'], '/' );
107 + }
130 108
131 - // cast value to array & trim whitespace or excess comma's
132 - $values = array_map('trim', explode(',', rtrim(trim($value), ',')));
109 + /**
110 + * Check if this rule passes (conditional matches expected value)
111 + *
112 + * @param string $condition
113 + * @param string $value
114 + * @param boolean $qualifier
115 + *
116 + * @return bool
117 + */
118 + protected function match_rule( $condition, $value, $qualifier = true ) {
119 + $matched = false;
133 120
134 - switch ($condition) {
135 - case 'everywhere':
136 - $matched = true;
137 - break;
121 + // cast value to array & trim whitespace or excess comma's
122 + $values = array_map( 'trim', explode( ',', rtrim( trim( $value ), ',' ) ) );
138 123
139 - case 'is_url':
140 - $url = $this->get_request_url();
141 - $matched = $this->match_patterns($url, $values, $qualifier === 'contains' || $qualifier === 'not_contains');
142 - break;
124 + switch ( $condition ) {
125 + case 'everywhere':
126 + $matched = true;
127 + break;
143 128
144 - case 'is_referer':
145 - if (! empty($_SERVER['HTTP_REFERER'])) {
146 - $referer = wp_unslash($_SERVER['HTTP_REFERER']);
147 - $matched = $this->match_patterns($referer, $values, $qualifier === 'contains' || $qualifier === 'not_contains');
148 - }
149 - break;
129 + case 'is_url':
130 + $url = $this->get_request_url();
131 + $matched = $this->match_patterns( $url, $values, $qualifier === 'contains' || $qualifier === 'not_contains' );
132 + break;
150 133
151 - case 'is_post_type':
152 - $post_type = (string) get_post_type();
153 - $matched = in_array($post_type, $values, true);
154 - break;
134 + case 'is_referer':
135 + if ( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
136 + $referer = $_SERVER['HTTP_REFERER'];
137 + $matched = $this->match_patterns( $referer, $values, $qualifier === 'contains' || $qualifier === 'not_contains' );
138 + }
139 + break;
155 140
156 - case 'is_single':
157 - case 'is_post':
158 - // convert to empty string if array with just empty string in it
159 - $value = ( $values === [ '' ] ) ? '' : $values;
160 - $matched = is_single($value);
161 - break;
141 + case 'is_post_type':
142 + $post_type = (string) get_post_type();
143 + $matched = in_array( $post_type, $values, true );
144 + break;
162 145
163 - case 'is_post_in_category':
164 - $matched = is_singular() && has_category($values);
165 - break;
146 + case 'is_single':
147 + case 'is_post':
148 + // convert to empty string if array with just empty string in it
149 + $value = ( $values === array( '' ) ) ? '' : $values;
150 + $matched = is_single( $value );
151 + break;
166 152
167 - case 'is_page':
168 - $matched = is_page($values);
169 - break;
153 + case 'is_post_in_category':
154 + $matched = is_singular() && has_category( $values );
155 + break;
170 156
171 - case 'is_post_with_tag':
172 - $matched = is_singular() && has_tag($values);
173 - break;
157 + case 'is_page':
158 + $matched = is_page( $values );
159 + break;
174 160
175 - case 'is_user_logged_in':
176 - $matched = is_user_logged_in();
177 - break;
178 - }
161 + case 'is_post_with_tag':
162 + $matched = is_singular() && has_tag( $values );
163 + break;
179 164
180 - /**
181 - * Filters whether a given box rule matches the condition and expected value.
182 - *
183 - * The dynamic portion of the hook, `$condition`, refers to the condition being matched.
184 - *
185 - * @param boolean $matched
186 - * @param array $values
187 - */
188 - $matched = apply_filters('boxzilla_box_rule_matches_' . $condition, $matched, $values);
165 + case 'is_user_logged_in':
166 + $matched = is_user_logged_in();
167 + break;
168 + }
189 169
190 - // if qualifier is set to false, we need to reverse this value here.
191 - if (! $qualifier || $qualifier === 'not_contains') {
192 - $matched = ! $matched;
193 - }
170 + /**
171 + * Filters whether a given box rule matches the condition and expected value.
172 + *
173 + * The dynamic portion of the hook, `$condition`, refers to the condition being matched.
174 + *
175 + * @param boolean $matched
176 + * @param array $values
177 + */
178 + $matched = apply_filters( 'boxzilla_box_rule_matches_' . $condition, $matched, $values );
194 179
195 - return $matched;
196 - }
180 + // if qualifier is set to false, we need to reverse this value here.
181 + if ( ! $qualifier || $qualifier === 'not_contains' ) {
182 + $matched = ! $matched;
183 + }
197 184
198 - /**
199 - * Checks which boxes should be loaded for this request.
200 - *
201 - * @return array
202 - */
203 - private function filter_boxes()
204 - {
205 - $box_ids_to_load = [];
206 - $rules = $this->get_filter_rules();
185 + return $matched;
186 + }
207 187
208 - foreach ($rules as $box_id => $box_rules) {
209 - $matched = false;
210 - $comparision = isset($box_rules['comparision']) ? $box_rules['comparision'] : 'any';
188 + /**
189 + * Checks which boxes should be loaded for this request.
190 + *
191 + * @return array
192 + */
193 + private function filter_boxes() {
194 + $box_ids_to_load = array();
195 + $rules = $this->get_filter_rules();
211 196
212 - // loop through all rules for all boxes
213 - foreach ($box_rules as $rule) {
214 - // skip faulty values (and comparision rule)
215 - if (! is_array($rule) || empty($rule['condition'])) {
216 - continue;
217 - }
197 + foreach ( $rules as $box_id => $box_rules ) {
198 + $matched = false;
199 + $comparision = isset( $box_rules['comparision'] ) ? $box_rules['comparision'] : 'any';
218 200
219 - $qualifier = isset($rule['qualifier']) ? $rule['qualifier'] : true;
220 - $matched = $this->match_rule($rule['condition'], $rule['value'], $qualifier);
201 + // loop through all rules for all boxes
202 + foreach ( $box_rules as $rule ) {
221 203
222 - // break out of loop if we've already matched
223 - if ($comparision === 'any' && $matched) {
224 - break;
225 - }
204 + // skip faulty values (and comparision rule)
205 + if ( empty( $rule['condition'] ) ) {
206 + continue;
207 + }
226 208
227 - // no need to continue if this rule didn't match
228 - if ($comparision === 'all' && ! $matched) {
229 - break;
230 - }
231 - }
209 + $qualifier = isset( $rule['qualifier'] ) ? $rule['qualifier'] : true;
210 + $matched = $this->match_rule( $rule['condition'], $rule['value'], $qualifier );
232 211
233 - // value of $matched at this point determines whether box should be loaded
234 - $load_box = $matched;
212 + // break out of loop if we've already matched
213 + if ( $comparision === 'any' && $matched ) {
214 + break;
215 + }
235 216
236 - /**
237 - * Filters whether a box should be loaded into the page HTML.
238 - *
239 - * The dynamic portion of the hook, `$box_id`, refers to the ID of the box. Return true if you want to output the box.
240 - *
241 - * @param boolean $load_box
242 - */
243 - $load_box = apply_filters('boxzilla_load_box_' . $box_id, $load_box);
217 + // no need to continue if this rule didn't match
218 + if ( $comparision === 'all' && ! $matched ) {
219 + break;
220 + }
221 + }
244 222
245 - /**
246 - * Filters whether a box should be loaded into the page HTML.
247 - *
248 - * @param boolean $load_box
249 - * @param int $box_id
250 - */
251 - $load_box = apply_filters('boxzilla_load_box', $load_box, $box_id);
223 + // value of $matched at this point determines whether box should be loaded
224 + $load_box = $matched;
252 225
253 - // if matched, box should be loaded on this page
254 - if ($load_box) {
255 - $box_ids_to_load[] = $box_id;
256 - }
257 - }
226 + /**
227 + * Filters whether a box should be loaded into the page HTML.
228 + *
229 + * The dynamic portion of the hook, `$box_id`, refers to the ID of the box. Return true if you want to output the box.
230 + *
231 + * @param boolean $load_box
232 + */
233 + $load_box = apply_filters( 'boxzilla_load_box_' . $box_id, $load_box );
258 234
259 - /**
260 - * Filters which boxes should be loaded on this page, expects an array of post ID's.
261 - *
262 - * @param array $box_ids_to_load
263 - */
264 - return apply_filters('boxzilla_load_boxes', $box_ids_to_load);
265 - }
235 + /**
236 + * Filters whether a box should be loaded into the page HTML.
237 + *
238 + * @param boolean $load_box
239 + * @param int $box_id
240 + */
241 + $load_box = apply_filters( 'boxzilla_load_box', $load_box, $box_id );
266 242
267 - /**
268 - * Load plugin styles
269 - */
270 - public function load_assets()
271 - {
272 - wp_enqueue_style('boxzilla', $this->plugin->url('/assets/css/styles.css'), [], $this->plugin->version());
273 - wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script.js'), [], $this->plugin->version(), [
274 - 'strategy' => 'defer',
275 - ]);
243 + // if matched, box should be loaded on this page
244 + if ( $load_box ) {
245 + $box_ids_to_load[] = $box_id;
246 + }
247 + }
276 248
277 - // create boxzilla_Global_Options object
278 - $plugin_options = $this->options;
279 - $boxes = $this->get_matched_boxes();
249 + /**
250 + * Filters which boxes should be loaded on this page, expects an array of post ID's.
251 + *
252 + * @param array $box_ids_to_load
253 + */
254 + return apply_filters( 'boxzilla_load_boxes', $box_ids_to_load );
255 + }
280 256
281 - $data = [
282 - 'testMode' => (bool) $plugin_options['test_mode'],
283 - 'boxes' => (array) array_map(
284 - function (Box $box) {
285 - return $box->get_client_options();
286 - },
287 - $boxes
288 - ),
289 - ];
290 - wp_add_inline_script('boxzilla', 'var boxzilla_options = ' . wp_json_encode($data) . ';', 'before');
291 - do_action('boxzilla_load_assets', $this);
292 - }
257 + /**
258 + * Load plugin styles
259 + */
260 + public function load_assets() {
261 + wp_enqueue_style( 'boxzilla', $this->plugin->url( '/assets/css/styles.css' ), array(), $this->plugin->version() );
262 + wp_enqueue_script( 'boxzilla', $this->plugin->url( '/assets/js/script.js' ), array(), $this->plugin->version(), array(
263 + 'strategy' => 'defer',
264 + 'in_footer' => true,
265 + ));
293 266
294 - public function print_boxes_content()
295 - {
296 - $boxes = $this->get_matched_boxes();
297 - if (empty($boxes)) {
298 - return;
299 - }
267 + // create boxzilla_Global_Options object
268 + $plugin_options = $this->options;
269 + $boxes = $this->get_matched_boxes();
300 270
301 - echo '<div style="display: none;">';
302 - foreach ($boxes as $box) {
303 - printf('<div id="boxzilla-box-%d-content">%s</div>', absint($box->ID), $box->get_content()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
304 - }
305 - echo '</div>';
306 - }
271 + $data = array(
272 + 'testMode' => (bool) $plugin_options['test_mode'],
273 + 'boxes' => (array) array_map(
274 + function ( Box $box ) {
275 + return $box->get_client_options();
276 + },
277 + $boxes
278 + ),
279 + );
307 280
308 - /**
309 - * Get an array of Box objects. These are the boxes that will be loaded for the current request.
310 - *
311 - * @return Box[]
312 - */
313 - public function get_matched_boxes()
314 - {
315 - static $boxes;
281 + wp_localize_script( 'boxzilla', 'boxzilla_options', $data );
316 282
317 - if ($boxes === null) {
318 - if (count($this->box_ids_to_load) === 0) {
319 - return [];
320 - }
283 + do_action( 'boxzilla_load_assets', $this );
284 + }
321 285
322 - // query Box posts
323 - $q = new \WP_Query();
324 - $posts = $q->query(
325 - [
326 - 'post_type' => 'boxzilla-box',
327 - 'post_status' => 'publish',
328 - 'post__in' => $this->box_ids_to_load,
329 - 'posts_per_page' => count($this->box_ids_to_load),
330 - 'ignore_sticky_posts' => true,
331 - 'no_found_rows' => true,
332 - ]
333 - );
286 + public function print_boxes_content() {
287 + $boxes = $this->get_matched_boxes();
288 + if ( empty( $boxes ) ) {
289 + return;
290 + }
334 291
335 - // create `Box` instances out of \WP_Post instances
336 - $boxes = [];
337 - foreach ($posts as $key => $post) {
338 - $box = new Box($post);
292 + echo '<div style="display: none;">';
293 + foreach ( $boxes as $box ) {
294 + echo "<div id=\"boxzilla-box-{$box->ID}-content\">", $box->get_content(), "</div>";
295 + }
296 + echo '</div>';
297 + }
339 298
340 - // skip boxes without any content
341 - if ($box->get_content() === '') {
342 - continue;
343 - }
299 + /**
300 + * Get an array of Box objects. These are the boxes that will be loaded for the current request.
301 + *
302 + * @return Box[]
303 + */
304 + public function get_matched_boxes() {
305 + static $boxes;
344 306
345 - $boxes[ $key ] = $box;
346 - }
347 - }
307 + if ( is_null( $boxes ) ) {
308 + if ( count( $this->box_ids_to_load ) === 0 ) {
309 + $boxes = array();
310 + return $boxes;
311 + }
348 312
349 - return $boxes;
350 - }
313 + // query Box posts
314 + $q = new \WP_Query();
315 + $posts = $q->query(
316 + array(
317 + 'post_type' => 'boxzilla-box',
318 + 'post_status' => 'publish',
319 + 'post__in' => $this->box_ids_to_load,
320 + 'posts_per_page' => count( $this->box_ids_to_load ),
321 + 'ignore_sticky_posts' => true,
322 + 'no_found_rows' => true,
323 + )
324 + );
325 +
326 + // create `Box` instances out of \WP_Post instances
327 + $boxes = array();
328 + foreach ( $posts as $key => $post ) {
329 + $box = new Box( $post );
330 +
331 + // skip boxes without any content
332 + if ( $box->get_content() === '' ) {
333 + continue;
334 + }
335 +
336 + $boxes[ $key ] = $box;
337 + }
338 + }
339 +
340 + return $boxes;
341 + }
351 342 }