PluginProbe
Boxzilla – WordPress Popup Builder / 3.0.1
Boxzilla – WordPress Popup Builder v3.0.1
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.0.1, at src/class-loader.php

307 lines 6.9 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 = strtolower( $pattern );
76
77 if( function_exists( 'fnmatch' ) ) {
78 $match = fnmatch( $pattern, $string );
79 } else {
80 $match = ( $pattern === $string );
81 }
82
83 if( $match ) {
84 return true;
85 }
86 }
87
88 return false;
89 }
90
91 /**
92 * Check if this rule passes (conditional matches expected value)
93 *
94 * @param string $condition
95 * @param string $value
96 * @param boolean $qualifier
97 *
98 * @return bool
99 */
100 protected function match_rule( $condition, $value, $qualifier = true ) {
101
102 $matched = false;
103
104 // cast value to array & trim whitespace or excess comma's
105 $value = array_map( 'trim', explode( ',', rtrim( trim( $value ), ',' ) ) );
106
107 switch ( $condition ) {
108 case 'everywhere';
109 $matched = true;
110 break;
111
112 case 'is_url':
113 $matched = $this->match_patterns( $_SERVER['REQUEST_URI'], $value );
114 break;
115
116 case 'is_referer':
117 if( ! empty( $_SERVER['HTTP_REFERER'] ) ) {
118 $referer = $_SERVER['HTTP_REFERER'];
119 $matched = $this->match_patterns( $referer, $value );
120 }
121 break;
122
123 case 'is_post_type':
124 $post_type = (string) get_post_type();
125 $matched = in_array( $post_type, (array) $value );
126 break;
127
128 case 'is_single':
129 case 'is_post':
130 $matched = is_single( $value );
131 break;
132
133 case 'is_post_in_category':
134 $matched = is_singular( 'post' ) && has_category( $value );
135 break;
136
137
138 case 'is_page':
139 $matched = is_page( $value );
140 break;
141
142 case 'is_post_with_tag':
143 $matched = is_singular( 'post' ) && has_tag( $value );
144 break;
145 }
146
147 /**
148 * Filters whether a given box rule matches the condition and expected value.
149 *
150 * The dynamic portion of the hook, `$condition`, refers to the condition being matched.
151 *
152 * @param boolean $matched
153 * @param array $value
154 */
155 $matched = apply_filters( 'boxzilla_box_rule_matches_' . $condition, $matched, $value );
156
157 // if qualifier is set to false, we need to reverse this value here.
158 if( ! $qualifier ) {
159 $matched = ! $matched;
160 }
161
162 return $matched;
163 }
164
165 /**
166 * Checks which boxes should be loaded for this request.
167 *
168 * @return array
169 */
170 private function filter_boxes() {
171
172 $box_ids_to_load = array();
173 $rules = $this->get_filter_rules();
174
175 foreach( $rules as $box_id => $box_rules ) {
176
177 $matched = false;
178 $comparision = isset( $box_rules['comparision'] ) ? $box_rules['comparision'] : 'any';
179
180 // loop through all rules for all boxes
181 foreach ( $box_rules as $rule ) {
182
183 // skip faulty values (and comparision rule)
184 if( empty( $rule['condition'] ) ) {
185 continue;
186 }
187
188 $qualifier = isset( $rule['qualifier'] ) ? $rule['qualifier'] : true;
189 $matched = $this->match_rule( $rule['condition'], $rule['value'], $qualifier );
190
191 // break out of loop if we've already matched
192 if( $comparision === 'any' && $matched ) {
193 break;
194 }
195
196 // no need to continue if this rule didn't match
197 if( $comparision === 'all' && ! $matched ) {
198 break;
199 }
200 }
201
202 // value of $matched at this point determines whether box should be loaded
203 $load_box = $matched;
204
205 /**
206 * Filters whether a box should be loaded into the page HTML.
207 *
208 * The dynamic portion of the hook, `$box_id`, refers to the ID of the box. Return true if you want to output the box.
209 *
210 * @param boolean $load_box
211 */
212 $load_box = apply_filters( 'boxzilla_load_box_' . $box_id, $load_box );
213
214 /**
215 * Filters whether a box should be loaded into the page HTML.
216 *
217 * @param boolean $load_box
218 * @param int $box_id
219 */
220 $load_box = apply_filters( 'boxzilla_load_box', $load_box, $box_id );
221
222 // if matched, box should be loaded on this page
223 if ( $load_box ) {
224 $box_ids_to_load[] = $box_id;
225 }
226
227 }
228
229 return $box_ids_to_load;
230 }
231
232 /**
233 * Load plugin styles
234 */
235 public function load_assets() {
236 $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
237
238 // stylesheets
239 wp_register_style( 'boxzilla', $this->plugin->url( '/assets/css/styles' . $pre_suffix . '.css' ), array(), $this->plugin->version() );
240
241 // scripts
242 wp_register_script( 'boxzilla',$this->plugin->url( '/assets/js/script' . $pre_suffix . '.js' ), array( 'jquery' ), $this->plugin->version(), true );
243
244 // Finally, enqueue style.
245 wp_enqueue_style( 'boxzilla' );
246 wp_enqueue_script( 'boxzilla' );
247
248 $this->pass_box_options();
249
250 do_action( 'boxzilla_load_assets', $this );
251 }
252
253 /**
254 * Get an array of Box objects. These are the boxes that will be loaded for the current request.
255 *
256 * @return Box[]
257 */
258 public function get_matched_boxes() {
259 static $boxes;
260
261 if( is_null( $boxes ) ) {
262
263 if( count( $this->box_ids_to_load ) === 0 ) {
264 $boxes = array();
265 return $boxes;
266 }
267
268 // query Box posts
269 $boxes = get_posts(
270 array(
271 'post_type' => 'boxzilla-box',
272 'post_status' => 'publish',
273 'post__in' => $this->box_ids_to_load,
274 'numberposts' => -1
275 )
276 );
277
278 // create `Box` instances out of \WP_Post instances
279 foreach ( $boxes as $key => $box ) {
280 $boxes[ $key ] = new Box( $box );
281 }
282 }
283
284 return $boxes;
285 }
286
287 /**
288 * Create array of Box options and pass it to JavaScript script.
289 */
290 public function pass_box_options() {
291
292 // create boxzilla_Global_Options object
293 $plugin_options = $this->options;
294 $boxes = $this->get_matched_boxes();
295
296 $data = array(
297 'testMode' => (boolean) $plugin_options['test_mode'],
298 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ),
299 );
300
301 wp_localize_script( 'boxzilla', 'boxzilla_options', $data );
302 }
303
304 }
305
306
307