index.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class QueryPaginationBlock extends RowBlock { |
| 9 | |
| 10 | |
| 11 | /** |
| 12 | * Render block only if the query has more pages |
| 13 | * |
| 14 | * @param mixed $wp_block |
| 15 | * |
| 16 | * @return string |
| 17 | */ |
| 18 | public function render( $wp_block ) { |
| 19 | |
| 20 | if ( $this->queryHasPages() || $this->isSandboxRender() ) { |
| 21 | return parent::render( $wp_block ); |
| 22 | } |
| 23 | |
| 24 | return ''; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Check if query has more than one page |
| 29 | * @return bool |
| 30 | */ |
| 31 | private function queryHasPages() { |
| 32 | |
| 33 | global $wp_query; |
| 34 | $use_main_query = Arr::get( $this->block_context, 'useMainQuery', false ); |
| 35 | |
| 36 | if ( empty( $this->block_context ) || $use_main_query ) { |
| 37 | /** |
| 38 | * Global WordPress query |
| 39 | * @var \WP_Query $wp_query |
| 40 | */ |
| 41 | |
| 42 | if ( $wp_query->is_singular() ) { |
| 43 | $prev_post = get_adjacent_post(); |
| 44 | $next_post = get_adjacent_post( false, '', false ); |
| 45 | |
| 46 | return ( $prev_post instanceof \WP_Post || $next_post instanceof \WP_Post ); |
| 47 | } |
| 48 | |
| 49 | global $wp_query; |
| 50 | $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; |
| 51 | |
| 52 | return ( $total > 1 ); |
| 53 | |
| 54 | } |
| 55 | |
| 56 | return Arr::get( $this->block_context, 'query.pages', 1 ) > 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | |
| 61 | Registry::registerBlock( |
| 62 | __DIR__, |
| 63 | QueryPaginationBlock::class, |
| 64 | array( |
| 65 | 'metadata' => '../row/block.json', |
| 66 | 'metadata_mixins' => array( './block.json' ), |
| 67 | ) |
| 68 | ); |
| 69 |