| @@ -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' ) ); | |
| 42 | 43 | add_action( 'wp_enqueue_scripts', array( $this, 'load_assets' ), 90 ); |
| 43 | 44 | } |
| 44 | 45 | } |
| 45 | 46 | |
| @@ -71,8 +72,9 @@ | ||
| 71 | 72 | $string = strtolower( $string ); |
| 72 | 73 | |
| 73 | 74 | foreach( $patterns as $pattern ) { |
| 74 | 75 | |
| 76 | + $pattern = rtrim( $pattern, '/' ); | |
| 75 | 77 | $pattern = strtolower( $pattern ); |
| 76 | 78 | |
| 77 | 79 | if( function_exists( 'fnmatch' ) ) { |
| 78 | 80 | $match = fnmatch( $pattern, $string ); |
| @@ -87,8 +89,24 @@ | ||
| 87 | 89 | |
| 88 | 90 | return false; |
| 89 | 91 | } |
| 90 | 92 | |
| 93 | + /** | |
| 94 | + * @return string | |
| 95 | + */ | |
| 96 | + protected function get_request_url() { | |
| 97 | + // strip trailing slashes | |
| 98 | + $request_uri = rtrim( $_SERVER['REQUEST_URI'], '/' ); | |
| 99 | + | |
| 100 | + // strip ? and query string | |
| 101 | + $qpos = strpos( $request_uri, '?' ); | |
| 102 | + if( $qpos ) { | |
| 103 | + $request_uri = substr( $request_uri, 0, $qpos ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + return $request_uri; | |
| 107 | + } | |
| 108 | + | |
| 91 | 109 | /** |
| 92 | 110 | * Check if this rule passes (conditional matches expected value) |
| 93 | 111 | * |
| 94 | 112 | * @param string $condition |
| @@ -109,9 +127,9 @@ | ||
| 109 | 127 | $matched = true; |
| 110 | 128 | break; |
| 111 | 129 | |
| 112 | 130 | case 'is_url': |
| 113 | - $matched = $this->match_patterns( $_SERVER['REQUEST_URI'], $value ); | |
| 131 | + $matched = $this->match_patterns( $this->get_request_url(), $value ); | |
| 114 | 132 | break; |
| 115 | 133 | |
| 116 | 134 | case 'is_referer': |
| 117 | 135 | if( ! empty( $_SERVER['HTTP_REFERER'] ) ) { |
| @@ -143,8 +161,12 @@ | ||
| 143 | 161 | |
| 144 | 162 | case 'is_post_with_tag': |
| 145 | 163 | $matched = is_singular( 'post' ) && has_tag( $value ); |
| 146 | 164 | break; |
| 165 | + | |
| 166 | + case 'is_user_logged_in': | |
| 167 | + $matched = is_user_logged_in(); | |
| 168 | + break; | |
| 147 | 169 | } |
| 148 | 170 | |
| 149 | 171 | /** |
| 150 | 172 | * Filters whether a given box rule matches the condition and expected value. |
| @@ -237,13 +259,35 @@ | ||
| 237 | 259 | public function load_assets() { |
| 238 | 260 | $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 239 | 261 | wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script' . $pre_suffix . '.js' ), array(), $this->plugin->version(), true ); |
| 240 | 262 | |
| 241 | - $this->pass_box_options(); | |
| 263 | + // create boxzilla_Global_Options object | |
| 264 | + $plugin_options = $this->options; | |
| 265 | + $boxes = $this->get_matched_boxes(); | |
| 242 | 266 | |
| 267 | + $data = array( | |
| 268 | + 'testMode' => (bool) $plugin_options['test_mode'], | |
| 269 | + 'boxes' => array_map( function(Box $box) { return $box->get_client_options(); }, $boxes ), | |
| 270 | + ); | |
| 271 | + | |
| 272 | + wp_localize_script( 'boxzilla', 'boxzilla_options', $data ); | |
| 273 | + | |
| 243 | 274 | do_action( 'boxzilla_load_assets', $this ); |
| 244 | 275 | } |
| 245 | 276 | |
| 277 | + public function print_boxes_content() { | |
| 278 | + $boxes = $this->get_matched_boxes(); | |
| 279 | + if( empty( $boxes ) ) { | |
| 280 | + return; | |
| 281 | + } | |
| 282 | + | |
| 283 | + echo '<div style="display: none;">'; | |
| 284 | + foreach( $boxes as $box ) { | |
| 285 | + echo sprintf( '<div id="boxzilla-box-%d-content">', $box->ID ) . $box->get_content() . '</div>'; | |
| 286 | + } | |
| 287 | + echo '</div>'; | |
| 288 | + } | |
| 289 | + | |
| 246 | 290 | /** |
| 247 | 291 | * Get an array of Box objects. These are the boxes that will be loaded for the current request. |
| 248 | 292 | * |
| 249 | 293 | * @return Box[] |
| @@ -258,9 +302,9 @@ | ||
| 258 | 302 | return $boxes; |
| 259 | 303 | } |
| 260 | 304 | |
| 261 | 305 | // query Box posts |
| 262 | - $boxes = get_posts( | |
| 306 | + $posts = get_posts( | |
| 263 | 307 | array( |
| 264 | 308 | 'post_type' => 'boxzilla-box', |
| 265 | 309 | 'post_status' => 'publish', |
| 266 | 310 | 'post__in' => $this->box_ids_to_load, |
| @@ -268,31 +312,20 @@ | ||
| 268 | 312 | ) |
| 269 | 313 | ); |
| 270 | 314 | |
| 271 | 315 | // create `Box` instances out of \WP_Post instances |
| 272 | - foreach ( $boxes as $key => $box ) { | |
| 273 | - $boxes[ $key ] = new Box( $box ); | |
| 316 | + $boxes = array(); | |
| 317 | + foreach ( $posts as $key => $post ) { | |
| 318 | + // skip posts with no content | |
| 319 | + if( empty( trim( $post->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 | |