class-block.php
1 year ago
class-button-container.php
2 years ago
class-button.php
2 years ago
class-container.php
2 years ago
class-element.php
1 year ago
class-grid.php
2 years ago
class-headline.php
2 years ago
class-image.php
2 years ago
class-loop-item.php
1 year ago
class-looper.php
1 year ago
class-media.php
1 year ago
class-query-loop.php
3 years ago
class-query-no-results.php
1 year ago
class-query-page-numbers.php
1 year ago
class-query.php
1 year ago
class-shape.php
1 year ago
class-text.php
1 year ago
class-query-loop.php
79 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles the Query Loop block. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Add Query Loop related functions. |
| 14 | */ |
| 15 | class GenerateBlocks_Block_Query_Loop { |
| 16 | /** |
| 17 | * Wrapper function for our dynamic buttons. |
| 18 | * |
| 19 | * @since 1.6.0 |
| 20 | * @param array $attributes The block attributes. |
| 21 | * @param string $content The dynamic text to display. |
| 22 | * @param WP_Block $block Block instance. |
| 23 | */ |
| 24 | public static function render_block( $attributes, $content, $block ) { |
| 25 | $page_key = isset( $block->context['generateblocks/queryId'] ) ? 'query-' . $block->context['generateblocks/queryId'] . '-page' : 'query-page'; |
| 26 | $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening. |
| 27 | $query_args = GenerateBlocks_Query_Loop::get_query_args( $block, $page ); |
| 28 | |
| 29 | // Override the custom query with the global query if needed. |
| 30 | $use_global_query = ( isset( $block->context['generateblocks/inheritQuery'] ) && $block->context['generateblocks/inheritQuery'] ); |
| 31 | |
| 32 | if ( $use_global_query ) { |
| 33 | global $wp_query; |
| 34 | |
| 35 | if ( $wp_query && isset( $wp_query->query_vars ) && is_array( $wp_query->query_vars ) ) { |
| 36 | // Unset `offset` because if is set, $wp_query overrides/ignores the paged parameter and breaks pagination. |
| 37 | unset( $query_args['offset'] ); |
| 38 | $query_args = wp_parse_args( $wp_query->query_vars, $query_args ); |
| 39 | |
| 40 | if ( empty( $query_args['post_type'] ) && is_singular() ) { |
| 41 | $query_args['post_type'] = get_post_type( get_the_ID() ); |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | $query_args = apply_filters( |
| 47 | 'generateblocks_query_loop_args', |
| 48 | $query_args, |
| 49 | $attributes, |
| 50 | $block |
| 51 | ); |
| 52 | |
| 53 | $the_query = new WP_Query( $query_args ); |
| 54 | |
| 55 | $content = ''; |
| 56 | if ( $the_query->have_posts() ) { |
| 57 | while ( $the_query->have_posts() ) { |
| 58 | $the_query->the_post(); |
| 59 | |
| 60 | $block_content = ( |
| 61 | new WP_Block( |
| 62 | $block->parsed_block, |
| 63 | array( |
| 64 | 'postType' => get_post_type(), |
| 65 | 'postId' => get_the_ID(), |
| 66 | ) |
| 67 | ) |
| 68 | )->render( array( 'dynamic' => false ) ); |
| 69 | |
| 70 | $content .= $block_content; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | wp_reset_postdata(); |
| 75 | |
| 76 | return $content; |
| 77 | } |
| 78 | } |
| 79 |