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