index.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\Blocks\BlockBase; |
| 7 | use Kubio\Core\Registry; |
| 8 | |
| 9 | class PaginationNumbersBlock extends BlockBase { |
| 10 | const OUTER = 'outer'; |
| 11 | |
| 12 | public function mapPropsToElements() { |
| 13 | return array( |
| 14 | self::OUTER => array( |
| 15 | 'innerHTML' => $this->getPageNumbers(), |
| 16 | ), |
| 17 | ); |
| 18 | } |
| 19 | |
| 20 | private function getPageNumbers() { |
| 21 | |
| 22 | $pages_data = $this->getPagesData(); |
| 23 | |
| 24 | return paginate_links( |
| 25 | array( |
| 26 | 'prev_next' => false, |
| 27 | 'total' => esc_attr( $pages_data['total'] ), |
| 28 | 'current' => esc_attr( $pages_data['current'] ), |
| 29 | 'show_all' => esc_attr( $this->getAttribute( 'show_all', false ) ), |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | private function getPagesData() { |
| 35 | global $wp_query; |
| 36 | $current = 1; |
| 37 | $total = 1; |
| 38 | |
| 39 | if ( Arr::get( $this->block_context, 'useMainQuery', false ) ) { |
| 40 | $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1; |
| 41 | $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; |
| 42 | } else { |
| 43 | |
| 44 | $query_id = Arr::get( $this->block_context, 'queryId', false ); |
| 45 | $page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page'; |
| 46 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 47 | $current = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); |
| 48 | $total = Arr::get( $this->block_context, 'query.pages', 1 ); |
| 49 | } |
| 50 | |
| 51 | return array( |
| 52 | 'total' => $total, |
| 53 | 'current' => $current, |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | public function serverSideRender( $wp_block ) { |
| 58 | return paginate_links( |
| 59 | array( |
| 60 | 'prev_next' => false, |
| 61 | 'total' => 12, |
| 62 | 'current' => 2, |
| 63 | 'show_all' => $this->getAttribute( 'show_all', false ), |
| 64 | ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | public function render( $wp_block ) { |
| 69 | |
| 70 | if ( ! $this->shouldRender() && ! $this->isSandboxRender() ) { |
| 71 | return ''; |
| 72 | } |
| 73 | |
| 74 | return parent::render( $wp_block ); |
| 75 | } |
| 76 | |
| 77 | private function shouldRender() { |
| 78 | |
| 79 | if ( Arr::get( $this->block_context, 'useMainQuery', false ) ) { |
| 80 | return $this->shouldRenderMainQueryButton(); |
| 81 | } |
| 82 | |
| 83 | return $this->shouldRenderCustomQueryButton(); |
| 84 | } |
| 85 | |
| 86 | private function shouldRenderMainQueryButton() { |
| 87 | global $wp_query; |
| 88 | |
| 89 | $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; |
| 90 | |
| 91 | return $total > 1; |
| 92 | } |
| 93 | |
| 94 | private function shouldRenderCustomQueryButton() { |
| 95 | $pages = Arr::get( $this->block_context, 'query.pages', 1 ); |
| 96 | |
| 97 | return ( $pages > 1 ); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | Registry::registerBlock( __DIR__, PaginationNumbersBlock::class ); |
| 102 |