| @@ -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,14 +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 = 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 | + } | |
| 76 | 83 | |
| 77 | 84 | if( function_exists( 'fnmatch' ) ) { |
| 78 | 85 | $match = fnmatch( $pattern, $string ); |
| 79 | 86 | } else { |
| @@ -87,8 +94,17 @@ | ||
| 87 | 94 | |
| 88 | 95 | return false; |
| 89 | 96 | } |
| 90 | 97 | |
| 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 | + } | |
| 106 | + | |
| 91 | 107 | /** |
| 92 | 108 | * Check if this rule passes (conditional matches expected value) |
| 93 | 109 | * |
| 94 | 110 | * @param string $condition |
| @@ -109,15 +125,16 @@ | ||
| 109 | 125 | $matched = true; |
| 110 | 126 | break; |
| 111 | 127 | |
| 112 | 128 | case 'is_url': |
| 113 | - $matched = $this->match_patterns( $_SERVER['REQUEST_URI'], $value ); | |
| 129 | + $url = $this->get_request_url(); | |
| 130 | + $matched = $this->match_patterns( $url, $value, $qualifier === 'contains' ); | |
| 114 | 131 | break; |
| 115 | 132 | |
| 116 | 133 | case 'is_referer': |
| 117 | 134 | if( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
| 118 | 135 | $referer = $_SERVER['HTTP_REFERER']; |
| 119 | - $matched = $this->match_patterns( $referer, $value ); | |
| 136 | + $matched = $this->match_patterns( $referer, $value, $qualifier === 'contains' ); | |
| 120 | 137 | } |
| 121 | 138 | break; |
| 122 | 139 | |
| 123 | 140 | case 'is_post_type': |
| @@ -143,8 +160,12 @@ | ||
| 143 | 160 | |
| 144 | 161 | case 'is_post_with_tag': |
| 145 | 162 | $matched = is_singular( 'post' ) && has_tag( $value ); |
| 146 | 163 | break; |
| 164 | + | |
| 165 | + case 'is_user_logged_in': | |
| 166 | + $matched = is_user_logged_in(); | |
| 167 | + break; | |
| 147 | 168 | } |
| 148 | 169 | |
| 149 | 170 | /** |
| 150 | 171 | * Filters whether a given box rule matches the condition and expected value. |
| @@ -237,13 +258,35 @@ | ||
| 237 | 258 | public function load_assets() { |
| 238 | 259 | $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 239 | 260 | wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script' . $pre_suffix . '.js' ), array(), $this->plugin->version(), true ); |
| 240 | 261 | |
| 241 | - $this->pass_box_options(); | |
| 262 | + // create boxzilla_Global_Options object | |
| 263 | + $plugin_options = $this->options; | |
| 264 | + $boxes = $this->get_matched_boxes(); | |
| 242 | 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 | + | |
| 243 | 273 | do_action( 'boxzilla_load_assets', $this ); |
| 244 | 274 | } |
| 245 | 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 | + | |
| 246 | 289 | /** |
| 247 | 290 | * Get an array of Box objects. These are the boxes that will be loaded for the current request. |
| 248 | 291 | * |
| 249 | 292 | * @return Box[] |
| @@ -258,9 +301,9 @@ | ||
| 258 | 301 | return $boxes; |
| 259 | 302 | } |
| 260 | 303 | |
| 261 | 304 | // query Box posts |
| 262 | - $boxes = get_posts( | |
| 305 | + $posts = get_posts( | |
| 263 | 306 | array( |
| 264 | 307 | 'post_type' => 'boxzilla-box', |
| 265 | 308 | 'post_status' => 'publish', |
| 266 | 309 | 'post__in' => $this->box_ids_to_load, |
| @@ -268,31 +311,21 @@ | ||
| 268 | 311 | ) |
| 269 | 312 | ); |
| 270 | 313 | |
| 271 | 314 | // create `Box` instances out of \WP_Post instances |
| 272 | - foreach ( $boxes as $key => $box ) { | |
| 273 | - $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 ); | |
| 274 | 324 | } |
| 275 | 325 | } |
| 276 | 326 | |
| 277 | 327 | return $boxes; |
| 278 | - } | |
| 279 | - | |
| 280 | - /** | |
| 281 | - * Create array of Box options and pass it to JavaScript script. | |
| 282 | - */ | |
| 283 | - public function pass_box_options() { | |
| 284 | - | |
| 285 | - // create boxzilla_Global_Options object | |
| 286 | - $plugin_options = $this->options; | |
| 287 | - $boxes = $this->get_matched_boxes(); | |
| 288 | - | |
| 289 | - $data = array( | |
| 290 | - 'testMode' => (bool) $plugin_options['test_mode'], | |
| 291 | - 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ), | |
| 292 | - ); | |
| 293 | - | |
| 294 | - wp_localize_script( 'boxzilla', 'boxzilla_options', $data ); | |
| 295 | 328 | } |
| 296 | 329 | |
| 297 | 330 | } |
| 298 | 331 | |