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 weeks 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-page-numbers.php
164 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Handles the No Results block. |
| 4 | * |
| 5 | * @package GenerateBlocks |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * The No Results block. |
| 14 | */ |
| 15 | class GenerateBlocks_Block_Query_Page_Numbers extends GenerateBlocks_Block { |
| 16 | /** |
| 17 | * Keep track of all blocks of this type on the page. |
| 18 | * |
| 19 | * @var array $block_ids The current block id. |
| 20 | */ |
| 21 | protected static $block_ids = []; |
| 22 | |
| 23 | /** |
| 24 | * Store our block name. |
| 25 | * |
| 26 | * @var string $block_name The block name. |
| 27 | */ |
| 28 | public static $block_name = 'generateblocks/query-page-numbers'; |
| 29 | |
| 30 | /** |
| 31 | * Render the Shape block. |
| 32 | * |
| 33 | * @param array $attributes The block attributes. |
| 34 | * @param string $block_content The block content. |
| 35 | * @param object $block The block. |
| 36 | */ |
| 37 | public static function render_block( $attributes, $block_content, $block ) { |
| 38 | $query_id = $block->context['generateblocks/queryData']['id'] ?? null; |
| 39 | $page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page'; |
| 40 | $page = empty( $_GET[ $page_key ] ) ? 1 : (int) $_GET[ $page_key ]; // phpcs:ignore -- No data processing happening. |
| 41 | $args = $block->context['generateblocks/queryData']['args'] ?? []; |
| 42 | $per_page = $args['posts_per_page'] ?? apply_filters( 'generateblocks_query_per_page_default', 10, $args ); |
| 43 | $content = ''; |
| 44 | $mid_size = isset( $block->attributes['midSize'] ) ? (int) $block->attributes['midSize'] : null; |
| 45 | $inherit_query = $block->context['generateblocks/queryData']['inherit'] ?? false; |
| 46 | |
| 47 | if ( $inherit_query ) { |
| 48 | $paginate_args = [ 'prev_next' => false ]; |
| 49 | |
| 50 | if ( null !== $mid_size ) { |
| 51 | $paginate_args['mid_size'] = $mid_size; |
| 52 | } |
| 53 | |
| 54 | $content = paginate_links( $paginate_args ); |
| 55 | } else { |
| 56 | $query_data = $block->context['generateblocks/queryData']['data'] ?? null; |
| 57 | $max_pages = $block->context['generateblocks/queryData']['maxPages'] ?? 0; |
| 58 | |
| 59 | if ( ! $query_data ) { |
| 60 | return ''; |
| 61 | } |
| 62 | |
| 63 | $paginate_args = array( |
| 64 | 'base' => '%_%', |
| 65 | 'format' => "?$page_key=%#%", |
| 66 | 'current' => max( 1, $page ), |
| 67 | 'total' => $max_pages, |
| 68 | 'prev_next' => false, |
| 69 | ); |
| 70 | |
| 71 | if ( null !== $mid_size ) { |
| 72 | $paginate_args['mid_size'] = $mid_size; |
| 73 | } |
| 74 | |
| 75 | if ( 1 !== $page ) { |
| 76 | /** |
| 77 | * `paginate_links` doesn't use the provided `format` when the page is `1`. |
| 78 | * This is great for the main query as it removes the extra query params |
| 79 | * making the URL shorter, but in the case of multiple custom queries is |
| 80 | * problematic. It results in returning an empty link which ends up with |
| 81 | * a link to the current page. |
| 82 | * |
| 83 | * A way to address this is to add a `fake` query arg with no value that |
| 84 | * is the same for all custom queries. This way the link is not empty and |
| 85 | * preserves all the other existent query args. |
| 86 | * |
| 87 | * @see https://developer.wordpress.org/reference/functions/paginate_links/ |
| 88 | * |
| 89 | * The proper fix of this should be in core. Track Ticket: |
| 90 | * @see https://core.trac.wordpress.org/ticket/53868 |
| 91 | * |
| 92 | * TODO: After two WP versions (starting from the WP version the core patch landed), |
| 93 | * we should remove this and call `paginate_links` with the proper new arg. |
| 94 | */ |
| 95 | $paginate_args['add_args'] = array( 'cst' => '' ); |
| 96 | } |
| 97 | |
| 98 | // We still need to preserve `paged` query param if exists, as is used |
| 99 | // for Queries that inherit from global context. |
| 100 | $paged = empty( $_GET['paged'] ) ? null : (int) $_GET['paged']; // phpcs:ignore -- No data processing happening. |
| 101 | |
| 102 | if ( $paged ) { |
| 103 | $paginate_args['add_args'] = array( 'paged' => $paged ); |
| 104 | } |
| 105 | |
| 106 | $content = paginate_links( $paginate_args ); |
| 107 | } |
| 108 | |
| 109 | if ( empty( $content ) ) { |
| 110 | return ''; |
| 111 | } |
| 112 | |
| 113 | $pagination_type = $block->context['generateblocks/queryData']['paginationType'] ?? ''; |
| 114 | $instant_pagination = GenerateBlocks_Block_Query::TYPE_INSTANT_PAGINATION === $pagination_type; |
| 115 | |
| 116 | if ( $instant_pagination && class_exists( 'WP_HTML_Tag_Processor' ) ) { |
| 117 | $p = new WP_HTML_Tag_Processor( $content ); |
| 118 | |
| 119 | while ( $p->next_tag( |
| 120 | array( 'class_name' => 'page-numbers' ) |
| 121 | ) ) { |
| 122 | if ( 'A' === $p->get_tag() ) { |
| 123 | $p->set_attribute( 'data-gb-router-target', 'query-' . $query_id ); |
| 124 | $p->set_attribute( 'data-gb-prefetch', true ); |
| 125 | } |
| 126 | } |
| 127 | $content = $p->get_updated_html(); |
| 128 | } |
| 129 | |
| 130 | $html_attributes = generateblocks_get_processed_html_attributes( $block_content ); |
| 131 | |
| 132 | // If our processing returned nothing, let's try to build our attributes from the block attributes. |
| 133 | if ( empty( $html_attributes ) ) { |
| 134 | $html_attributes = generateblocks_get_backup_html_attributes( 'gb-query-page-numbers', $attributes ); |
| 135 | } |
| 136 | |
| 137 | $tag_name = $attributes['tagName'] ?? 'div'; |
| 138 | |
| 139 | // Add styles to this block if needed. |
| 140 | $output = generateblocks_maybe_add_block_css( |
| 141 | '', |
| 142 | [ |
| 143 | 'class_name' => __CLASS__, |
| 144 | 'attributes' => $attributes, |
| 145 | 'block_ids' => self::$block_ids, |
| 146 | ] |
| 147 | ); |
| 148 | |
| 149 | $output .= sprintf( |
| 150 | '<%1$s %2$s>%3$s</%1$s>', |
| 151 | $tag_name, |
| 152 | generateblocks_attr( |
| 153 | 'query-page-numbers', |
| 154 | $html_attributes, |
| 155 | $attributes, |
| 156 | $block |
| 157 | ), |
| 158 | $content |
| 159 | ); |
| 160 | |
| 161 | return $output; |
| 162 | } |
| 163 | } |
| 164 |