PluginProbe
Boxzilla – WordPress Popup Builder / 3.1.11
Boxzilla – WordPress Popup Builder v3.1.11
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
boxzilla / src / class-loader.php

class-loader.php in Boxzilla – WordPress Popup Builder 3.1.11, at src/class-loader.php

317 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Boxzilla;
4
5 class BoxLoader {
6
7 /**
8 * @var Plugin
9 */
10 private $plugin;
11
12 /**
13 * @var array
14 */
15 private $box_ids_to_load = array();
16
17 /**
18 * @var array
19 */
20 protected $options;
21
22 /**
23 * Constructor
24 *
25 * @param Plugin $plugin
26 * @param array $options
27 */
28 public function __construct( Plugin $plugin, array $options ) {
29 $this->plugin = $plugin;
30 $this->options = $options;
31 }
32
33 /**
34 * Initializes the plugin, runs on `wp` hook.
35 */
36 public function init() {
37
38 $this->box_ids_to_load = $this->filter_boxes();
39
40 // Only add other hooks if necessary
41 if( count( $this->box_ids_to_load ) > 0 ) {
42 add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 90 );
43 }
44 }
45
46 /**
47 * Get global rules for all boxes
48 *
49 * @return array
50 */
51 protected function get_filter_rules() {
52 $rules = get_option( 'boxzilla_rules', array() );
53
54 if( ! is_array( $rules ) ) {
55 return array();
56 }
57
58 return $rules;
59 }
60
61
62 /**
63 * Match a string against an array of patterns, glob-style.
64 *
65 * @param string $string
66 * @param array $patterns
67 *
68 * @return boolean
69 */
70 protected function match_patterns( $string, $patterns ) {
71 $string = strtolower( $string );
72
73 foreach( $patterns as $pattern ) {
74
75 $pattern = rtrim( $pattern, '/' );
76 $pattern = strtolower( $pattern );
77
78 if( function_exists( 'fnmatch' ) ) {
79 $match = fnmatch( $pattern, $string );
80 } else {
81 $match = ( $pattern === $string );
82 }
83
84 if( $match ) {
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 /**
93 * @return string
94 */
95 protected function get_request_url() {
96 // strip trailing slashes
97 $request_uri = rtrim( $_SERVER['REQUEST_URI'], '/' );
98
99 // strip ? and query string
100 $qpos = strpos( $request_uri, '?' );
101 if( $qpos ) {
102 $request_uri = substr( $request_uri, 0, $qpos );
103 }
104
105 return $request_uri;
106 }
107
108 /**
109 * Check if this rule passes (conditional matches expected value)
110 *
111 * @param string $condition
112 * @param string $value
113 * @param boolean $qualifier
114 *
115 * @return bool
116 */
117 protected function match_rule( $condition, $value, $qualifier = true ) {
118
119 $matched = false;
120
121 // cast value to array & trim whitespace or excess comma's
122 $value = array_map( 'trim', explode( ',', rtrim( trim( $value ), ',' ) ) );
123
124 switch ( $condition ) {
125 case 'everywhere';
126 $matched = true;
127 break;
128
129 case 'is_url':
130 $matched = $this->match_patterns( $this->get_request_url(), $value );
131 break;
132
133 case 'is_referer':
134 if( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
135 $referer = $_SERVER['HTTP_REFERER'];
136 $matched = $this->match_patterns( $referer, $value );
137 }
138 break;
139
140 case 'is_post_type':
141 $post_type = (string) get_post_type();
142 $matched = in_array( $post_type, (array) $value );
143 break;
144
145 case 'is_single':
146 case 'is_post':
147 // convert to empty string if array with just empty string in it
148 $value = ( $value === array( '' ) ) ? '' : $value;
149 $matched = is_single( $value );
150 break;
151
152 case 'is_post_in_category':
153 $matched = is_singular( 'post' ) && has_category( $value );
154 break;
155
156
157 case 'is_page':
158 $matched = is_page( $value );
159 break;
160
161 case 'is_post_with_tag':
162 $matched = is_singular( 'post' ) && has_tag( $value );
163 break;
164 }
165
166 /**
167 * Filters whether a given box rule matches the condition and expected value.
168 *
169 * The dynamic portion of the hook, `$condition`, refers to the condition being matched.
170 *
171 * @param boolean $matched
172 * @param array $value
173 */
174 $matched = apply_filters( 'boxzilla_box_rule_matches_' . $condition, $matched, $value );
175
176 // if qualifier is set to false, we need to reverse this value here.
177 if( ! $qualifier ) {
178 $matched = ! $matched;
179 }
180
181 return $matched;
182 }
183
184 /**
185 * Checks which boxes should be loaded for this request.
186 *
187 * @return array
188 */
189 private function filter_boxes() {
190
191 $box_ids_to_load = array();
192 $rules = $this->get_filter_rules();
193
194 foreach( $rules as $box_id => $box_rules ) {
195
196 $matched = false;
197 $comparision = isset( $box_rules['comparision'] ) ? $box_rules['comparision'] : 'any';
198
199 // loop through all rules for all boxes
200 foreach ( $box_rules as $rule ) {
201
202 // skip faulty values (and comparision rule)
203 if( empty( $rule['condition'] ) ) {
204 continue;
205 }
206
207 $qualifier = isset( $rule['qualifier'] ) ? $rule['qualifier'] : true;
208 $matched = $this->match_rule( $rule['condition'], $rule['value'], $qualifier );
209
210 // break out of loop if we've already matched
211 if( $comparision === 'any' && $matched ) {
212 break;
213 }
214
215 // no need to continue if this rule didn't match
216 if( $comparision === 'all' && ! $matched ) {
217 break;
218 }
219 }
220
221 // value of $matched at this point determines whether box should be loaded
222 $load_box = $matched;
223
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 );
232
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 );
240
241 // if matched, box should be loaded on this page
242 if ( $load_box ) {
243 $box_ids_to_load[] = $box_id;
244 }
245
246 }
247
248 return $box_ids_to_load;
249 }
250
251 /**
252 * Load plugin styles
253 */
254 public function load_assets() {
255 $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
256 wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script' . $pre_suffix . '.js' ), array(), $this->plugin->version(), true );
257
258 $this->pass_box_options();
259
260 do_action( 'boxzilla_load_assets', $this );
261 }
262
263 /**
264 * Get an array of Box objects. These are the boxes that will be loaded for the current request.
265 *
266 * @return Box[]
267 */
268 public function get_matched_boxes() {
269 static $boxes;
270
271 if( is_null( $boxes ) ) {
272
273 if( count( $this->box_ids_to_load ) === 0 ) {
274 $boxes = array();
275 return $boxes;
276 }
277
278 // query Box posts
279 $boxes = get_posts(
280 array(
281 'post_type' => 'boxzilla-box',
282 'post_status' => 'publish',
283 'post__in' => $this->box_ids_to_load,
284 'numberposts' => -1
285 )
286 );
287
288 // create `Box` instances out of \WP_Post instances
289 foreach ( $boxes as $key => $box ) {
290 $boxes[ $key ] = new Box( $box );
291 }
292 }
293
294 return $boxes;
295 }
296
297 /**
298 * Create array of Box options and pass it to JavaScript script.
299 */
300 public function pass_box_options() {
301
302 // create boxzilla_Global_Options object
303 $plugin_options = $this->options;
304 $boxes = $this->get_matched_boxes();
305
306 $data = array(
307 'testMode' => (bool) $plugin_options['test_mode'],
308 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ),
309 );
310
311 wp_localize_script( 'boxzilla', 'boxzilla_options', $data );
312 }
313
314 }
315
316
317