| @@ -38,8 +38,9 @@ | ||
| 38 | 38 | $this->box_ids_to_load = $this->filter_boxes(); |
| 39 | 39 | |
| 40 | 40 | // Only add other hooks if necessary |
| 41 | 41 | if( count( $this->box_ids_to_load ) > 0 ) { |
| 42 | + add_action( 'wp_footer', array( $this, 'print_boxes_content' ), 1 ); | |
| 42 | 43 | add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 90 ); |
| 43 | 44 | } |
| 44 | 45 | } |
| 45 | 46 | |
| @@ -66,15 +67,20 @@ | ||
| 66 | 67 | * @param array $patterns |
| 67 | 68 | * |
| 68 | 69 | * @return boolean |
| 69 | 70 | */ |
| 70 | - protected function match_patterns( $string, $patterns ) { | |
| 71 | + protected function match_patterns( $string, $patterns, $contains = false ) { | |
| 71 | 72 | $string = strtolower( $string ); |
| 72 | 73 | |
| 73 | 74 | foreach( $patterns as $pattern ) { |
| 75 | + $pattern = rtrim( $pattern, '/' ); | |
| 76 | + $pattern = strtolower( $pattern ); | |
| 74 | 77 | |
| 75 | - $pattern = rtrim( $pattern, '/' ); | |
| 76 | - $pattern = strtolower( $pattern ); | |
| 78 | + // contains means we should do a simple occurence check | |
| 79 | + // does not support wildcards | |
| 80 | + if( $contains ) { | |
| 81 | + return strpos( $string, $pattern ) !== false; | |
| 82 | + } | |
| 77 | 83 | |
| 78 | 84 | if( function_exists( 'fnmatch' ) ) { |
| 79 | 85 | $match = fnmatch( $pattern, $string ); |
| 80 | 86 | } else { |
| @@ -92,18 +98,11 @@ | ||
| 92 | 98 | /** |
| 93 | 99 | * @return string |
| 94 | 100 | */ |
| 95 | 101 | 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; | |
| 102 | + // strip trailing slashes | |
| 103 | + $request_uri = rtrim( $_SERVER['REQUEST_URI'], '/' ); | |
| 104 | + return $request_uri; | |
| 106 | 105 | } |
| 107 | 106 | |
| 108 | 107 | /** |
| 109 | 108 | * Check if this rule passes (conditional matches expected value) |
| @@ -126,15 +125,16 @@ | ||
| 126 | 125 | $matched = true; |
| 127 | 126 | break; |
| 128 | 127 | |
| 129 | 128 | case 'is_url': |
| 130 | - $matched = $this->match_patterns( $this->get_request_url(), $value ); | |
| 129 | + $url = $this->get_request_url(); | |
| 130 | + $matched = $this->match_patterns( $url, $value, $qualifier === 'contains' ); | |
| 131 | 131 | break; |
| 132 | 132 | |
| 133 | 133 | case 'is_referer': |
| 134 | 134 | if( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
| 135 | 135 | $referer = $_SERVER['HTTP_REFERER']; |
| 136 | - $matched = $this->match_patterns( $referer, $value ); | |
| 136 | + $matched = $this->match_patterns( $referer, $value, $qualifier === 'contains' ); | |
| 137 | 137 | } |
| 138 | 138 | break; |
| 139 | 139 | |
| 140 | 140 | case 'is_post_type': |
| @@ -161,11 +161,11 @@ | ||
| 161 | 161 | case 'is_post_with_tag': |
| 162 | 162 | $matched = is_singular( 'post' ) && has_tag( $value ); |
| 163 | 163 | break; |
| 164 | 164 | |
| 165 | - case 'is_user_logged_in': | |
| 166 | - $matched = is_user_logged_in(); | |
| 167 | - break; | |
| 165 | + case 'is_user_logged_in': | |
| 166 | + $matched = is_user_logged_in(); | |
| 167 | + break; | |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
| 171 | 171 | * Filters whether a given box rule matches the condition and expected value. |
| @@ -258,13 +258,35 @@ | ||
| 258 | 258 | public function load_assets() { |
| 259 | 259 | $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 260 | 260 | wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script' . $pre_suffix . '.js' ), array(), $this->plugin->version(), true ); |
| 261 | 261 | |
| 262 | - $this->pass_box_options(); | |
| 262 | + // create boxzilla_Global_Options object | |
| 263 | + $plugin_options = $this->options; | |
| 264 | + $boxes = $this->get_matched_boxes(); | |
| 263 | 265 | |
| 266 | + $data = array( | |
| 267 | + 'testMode' => (bool) $plugin_options['test_mode'], | |
| 268 | + 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ), | |
| 269 | + ); | |
| 270 | + | |
| 271 | + wp_localize_script( 'boxzilla', 'boxzilla_options', $data ); | |
| 272 | + | |
| 264 | 273 | do_action( 'boxzilla_load_assets', $this ); |
| 265 | 274 | } |
| 266 | 275 | |
| 276 | + public function print_boxes_content() { | |
| 277 | + $boxes = $this->get_matched_boxes(); | |
| 278 | + if( empty( $boxes ) ) { | |
| 279 | + return; | |
| 280 | + } | |
| 281 | + | |
| 282 | + echo '<div style="display: none;">'; | |
| 283 | + foreach( $boxes as $box ) { | |
| 284 | + echo sprintf( '<div id="boxzilla-box-%d-content">', $box->ID ) . $box->get_content() . '</div>'; | |
| 285 | + } | |
| 286 | + echo '</div>'; | |
| 287 | + } | |
| 288 | + | |
| 267 | 289 | /** |
| 268 | 290 | * Get an array of Box objects. These are the boxes that will be loaded for the current request. |
| 269 | 291 | * |
| 270 | 292 | * @return Box[] |
| @@ -279,9 +301,9 @@ | ||
| 279 | 301 | return $boxes; |
| 280 | 302 | } |
| 281 | 303 | |
| 282 | 304 | // query Box posts |
| 283 | - $boxes = get_posts( | |
| 305 | + $posts = get_posts( | |
| 284 | 306 | array( |
| 285 | 307 | 'post_type' => 'boxzilla-box', |
| 286 | 308 | 'post_status' => 'publish', |
| 287 | 309 | 'post__in' => $this->box_ids_to_load, |
| @@ -289,31 +311,21 @@ | ||
| 289 | 311 | ) |
| 290 | 312 | ); |
| 291 | 313 | |
| 292 | 314 | // create `Box` instances out of \WP_Post instances |
| 293 | - foreach ( $boxes as $key => $box ) { | |
| 294 | - $boxes[ $key ] = new Box( $box ); | |
| 315 | + $boxes = array(); | |
| 316 | + foreach ( $posts as $key => $post ) { | |
| 317 | + // skip posts with no content | |
| 318 | + $post_content = trim( strip_tags( nl2br( $post->post_content ) ) ); | |
| 319 | + if( empty( $post_content ) ) { | |
| 320 | + continue; | |
| 321 | + } | |
| 322 | + | |
| 323 | + $boxes[ $key ] = new Box( $post ); | |
| 295 | 324 | } |
| 296 | 325 | } |
| 297 | 326 | |
| 298 | 327 | return $boxes; |
| 299 | - } | |
| 300 | - | |
| 301 | - /** | |
| 302 | - * Create array of Box options and pass it to JavaScript script. | |
| 303 | - */ | |
| 304 | - public function pass_box_options() { | |
| 305 | - | |
| 306 | - // create boxzilla_Global_Options object | |
| 307 | - $plugin_options = $this->options; | |
| 308 | - $boxes = $this->get_matched_boxes(); | |
| 309 | - | |
| 310 | - $data = array( | |
| 311 | - 'testMode' => (bool) $plugin_options['test_mode'], | |
| 312 | - 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ), | |
| 313 | - ); | |
| 314 | - | |
| 315 | - wp_localize_script( 'boxzilla', 'boxzilla_options', $data ); | |
| 316 | 328 | } |
| 317 | 329 | |
| 318 | 330 | } |
| 319 | 331 | |