| 1 |
<?php |
| 2 |
|
| 3 |
class WPUPG_Content { |
| 4 |
|
| 5 |
public function __construct() |
| 6 |
{ |
| 7 |
add_filter( 'the_content', array( $this, 'prefilter_grid' ), 1 ); |
| 8 |
add_filter( 'the_content', array( $this, 'content_filter' ), 10 ); |
| 9 |
} |
| 10 |
|
| 11 |
public function prefilter_grid( $content ) |
| 12 |
{ |
| 13 |
$pattern = get_shortcode_regex( array( 'wpupg-grid' ) ); |
| 14 |
|
| 15 |
if ( preg_match_all( '/' . $pattern . '/s', $content, $matches ) && array_key_exists( 2, $matches ) ) { |
| 16 |
foreach ( $matches[2] as $key => $value ) { |
| 17 |
if ( 'wpupg-grid' === $value ) { |
| 18 |
$grid_shortcode = $matches[0][ $key ]; |
| 19 |
$grid_shortcode = str_ireplace( '[wpupg-grid ', '[wpupg-grid wpupg_no_output="1" ', $grid_shortcode ); |
| 20 |
|
| 21 |
// This forces a prefilter of the grid without actual output. |
| 22 |
do_shortcode( $grid_shortcode ); |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return $content; |
| 28 |
} |
| 29 |
|
| 30 |
public function content_filter( $content ) |
| 31 |
{ |
| 32 |
$ignore_query = !is_main_query(); |
| 33 |
if ( apply_filters( 'wpupg_content_loop_check', $ignore_query ) ) { |
| 34 |
return $content; |
| 35 |
} |
| 36 |
|
| 37 |
if ( get_post_type() == WPUPG_POST_TYPE ) { |
| 38 |
remove_filter( 'the_content', array( $this, 'content_filter' ), 10 ); |
| 39 |
|
| 40 |
$grid = WPUPG_Grid_Manager::get( get_post() ); |
| 41 |
|
| 42 |
$content .= '[wpupg-filter id="' . $grid->slug() . '"]'; |
| 43 |
$content .= '[wpupg-grid id="' . $grid->slug() . '"]'; |
| 44 |
|
| 45 |
add_filter( 'the_content', array( $this, 'content_filter' ), 10 ); |
| 46 |
} |
| 47 |
|
| 48 |
return $content; |
| 49 |
} |
| 50 |
} |