| @@ -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 | |
| @@ -258,13 +259,35 @@ | ||
| 258 | 259 | public function load_assets() { |
| 259 | 260 | $pre_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 260 | 261 | wp_enqueue_script('boxzilla', $this->plugin->url('/assets/js/script' . $pre_suffix . '.js' ), array(), $this->plugin->version(), true ); |
| 261 | 262 | |
| 262 | - $this->pass_box_options(); | |
| 263 | + // create boxzilla_Global_Options object | |
| 264 | + $plugin_options = $this->options; | |
| 265 | + $boxes = $this->get_matched_boxes(); | |
| 263 | 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 | + | |
| 264 | 274 | do_action( 'boxzilla_load_assets', $this ); |
| 265 | 275 | } |
| 266 | 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 | + | |
| 267 | 290 | /** |
| 268 | 291 | * Get an array of Box objects. These are the boxes that will be loaded for the current request. |
| 269 | 292 | * |
| 270 | 293 | * @return Box[] |
| @@ -279,9 +302,9 @@ | ||
| 279 | 302 | return $boxes; |
| 280 | 303 | } |
| 281 | 304 | |
| 282 | 305 | // query Box posts |
| 283 | - $boxes = get_posts( | |
| 306 | + $posts = get_posts( | |
| 284 | 307 | array( |
| 285 | 308 | 'post_type' => 'boxzilla-box', |
| 286 | 309 | 'post_status' => 'publish', |
| 287 | 310 | 'post__in' => $this->box_ids_to_load, |
| @@ -289,31 +312,20 @@ | ||
| 289 | 312 | ) |
| 290 | 313 | ); |
| 291 | 314 | |
| 292 | 315 | // create `Box` instances out of \WP_Post instances |
| 293 | - foreach ( $boxes as $key => $box ) { | |
| 294 | - $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 ); | |
| 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 | |