index.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio\Blocks; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\Registry; |
| 7 | |
| 8 | class PaginationNavButtonBlock extends ButtonBlock { |
| 9 | |
| 10 | private $action_url_map = array( |
| 11 | 'prev' => 'get_previous_posts_page_link', |
| 12 | 'next' => 'get_next_posts_page_link', |
| 13 | 'single_prev' => array( PaginationNavButtonBlock::class, 'getPrevPostLink' ), |
| 14 | 'single_next' => array( PaginationNavButtonBlock::class, 'getPrevNextLink' ), |
| 15 | ); |
| 16 | |
| 17 | |
| 18 | public static function getPrevPostLink() { |
| 19 | if ( is_attachment() ) { |
| 20 | $post = get_post( get_post()->post_parent ); |
| 21 | } else { |
| 22 | $post = get_adjacent_post( false, '', true, 'category' ); |
| 23 | } |
| 24 | |
| 25 | return get_permalink( $post ); |
| 26 | } |
| 27 | |
| 28 | public function getPrevNextLink() { |
| 29 | |
| 30 | $post = get_adjacent_post( false, '', false, 'category' ); |
| 31 | |
| 32 | return get_permalink( $post ); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | public function mapPropsToElements() { |
| 37 | $current_map = parent::mapPropsToElements(); |
| 38 | |
| 39 | $action_type = $this->getAttribute( 'action', 'prev' ); |
| 40 | |
| 41 | global $wp_query; |
| 42 | |
| 43 | if ( $wp_query->is_single() ) { |
| 44 | $action_type = "single_{$action_type}"; |
| 45 | } |
| 46 | |
| 47 | $current_map[ ButtonBlock::LINK ] = array( |
| 48 | 'href' => call_user_func( $this->action_url_map[ $action_type ] ), |
| 49 | 'typeOpenLink' => 'sameWindow', |
| 50 | 'noFollow' => false, |
| 51 | 'lightboxMedia' => '', |
| 52 | ); |
| 53 | |
| 54 | return $current_map; |
| 55 | } |
| 56 | |
| 57 | public function render( $wp_block ) { |
| 58 | if ( ! $this->shouldRender() && ! $this->isSandboxRender() ) { |
| 59 | return ''; |
| 60 | } |
| 61 | |
| 62 | return parent::render( $wp_block ); |
| 63 | } |
| 64 | |
| 65 | private function shouldRender() { |
| 66 | |
| 67 | if ( empty( $this->block_context ) || Arr::get( $this->block_context, 'useMainQuery', false ) ) { |
| 68 | return $this->shouldRenderMainQueryButton(); |
| 69 | } |
| 70 | |
| 71 | return $this->shouldRenderCustomQueryButton(); |
| 72 | } |
| 73 | |
| 74 | private function shouldRenderMainQueryButton() { |
| 75 | global $wp_query; |
| 76 | |
| 77 | if ( $wp_query->is_single() ) { |
| 78 | if ( $this->getAttribute( 'action', 'prev' ) === 'prev' ) { |
| 79 | $post = get_adjacent_post( false, '', true, 'category' ); |
| 80 | |
| 81 | } else { |
| 82 | $post = get_adjacent_post( false, '', false, 'category' ); |
| 83 | } |
| 84 | |
| 85 | return ( $post instanceof \WP_Post ); |
| 86 | } |
| 87 | |
| 88 | $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; |
| 89 | $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1; |
| 90 | |
| 91 | if ( $this->getAttribute( 'action' ) === 'prev' ) { |
| 92 | return ( $current > 1 ); |
| 93 | } else { |
| 94 | return ( intval( $current ) < intval( $total ) ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private function shouldRenderCustomQueryButton() { |
| 99 | $query_id = Arr::get( $this->block_context, 'queryId', false ); |
| 100 | $page_key = $query_id ? 'query-' . $query_id . '-page' : 'query-page'; |
| 101 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 102 | $page = empty( $_GET[ $page_key ] ) ? 1 : filter_var( $_GET[ $page_key ], FILTER_VALIDATE_INT ); |
| 103 | $pages = Arr::get( $this->block_context, 'query.pages', 1 ); |
| 104 | |
| 105 | if ( $this->getAttribute( 'action' ) === 'prev' ) { |
| 106 | return ( intval( $page ) !== 1 ); |
| 107 | } else { |
| 108 | return ( intval( $page ) < intval( $pages ) ); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | Registry::registerBlock( |
| 114 | __DIR__, |
| 115 | PaginationNavButtonBlock::class, |
| 116 | array( |
| 117 | 'metadata' => '../button/block.json', |
| 118 | 'metadata_mixins' => array( './block.json' ), |
| 119 | 'mixins_exact_replace' => array( |
| 120 | './block.json' => array( |
| 121 | 'supports.kubio.template', |
| 122 | ), |
| 123 | ), |
| 124 | ) |
| 125 | ); |
| 126 |