* Created on: 31/08/2018 * * @package Neve\Views\Pluggable */ namespace Neve\Views\Pluggable; use Neve\Views\Base_View; /** * Class Pagination * * @package Neve\Views\Pluggable */ class Pagination extends Base_View { /** * Function that is run after instantiation. * * @return void */ public function init() { add_action( 'rest_api_init', array( $this, 'register_endpoints' ) ); add_filter( 'neve_filter_main_script_localization', array( $this, 'filter_localization' ) ); add_action( 'neve_do_pagination', array( $this, 'render_pagination' ) ); add_action( 'neve_post_navigation', array( $this, 'render_post_navigation' ) ); } /** * Register Rest API endpoint for posts retrieval. */ public function register_endpoints() { register_rest_route( 'nv/v1/posts', '/page/(?P\d+)/', array( 'methods' => \WP_REST_Server::READABLE, 'callback' => array( $this, 'get_posts' ), ) ); } /** * Get paginated posts. * * @param \WP_REST_Request $request the rest request. * * @return \WP_REST_Response */ public function get_posts( \WP_REST_Request $request ) { if ( empty( $request['page_number'] ) ) { return new \WP_REST_Response( '' ); } $output = ''; $args = array( 'posts_per_page' => get_option( 'posts_per_page' ), 'post_type' => 'post', 'paged' => $request['page_number'], 'ignore_sticky_posts' => 1, 'post_status' => 'publish', ); $query = new \WP_Query( $args ); if ( $query->have_posts() ) { ob_start(); while ( $query->have_posts() ) { $query->the_post(); get_template_part( 'template-parts/content' ); } wp_reset_postdata(); $output = ob_get_contents(); ob_end_clean(); } return new \WP_REST_Response( $output ); } /** * Filter localization to add infinite scroll to the main script. * * @param array $data localization array. * * @return array */ public function filter_localization( $data ) { if ( ! $this->has_infinite_scroll() ) { return $data; } global $wp_query; $max_pages = $wp_query->max_num_pages; $data['infiniteScroll'] = 'enabled'; $data['infiniteScrollMaxPages'] = $max_pages; $data['infiniteScrollEndpoint'] = rest_url( 'nv/v1/posts/page/' ); return $data; } /** * Render the pagination. * * @param string $context not yet used might come in handy later. */ public function render_pagination( $context ) { if ( $context === 'single' ) { $this->render_single_pagination(); return; } if ( ! $this->has_infinite_scroll() ) { echo wp_kses_post( paginate_links( array( 'type' => 'list', ) ) ); return; } echo wp_kses_post( '' ); } /** * Render single post / page pagination. */ private function render_single_pagination() { wp_link_pages( array( 'before' => '
', 'after' => '
', 'link_before' => '', 'link_after' => '', ) ); } /** * Render single post navigation links */ public function render_post_navigation() { $prev_format = ''; $next_format = ''; $prev_link = sprintf( '%1$s%2$s', esc_html__( 'previous', 'neve' ), '%title' ); $next_link = sprintf( '%1$s%2$s', esc_html__( 'next', 'neve' ), '%title' ); echo '
'; previous_post_link( $prev_format, $prev_link ); next_post_link( $next_format, $next_link ); echo '
'; } /** * Has infinite scroll. * * @return string */ private function has_infinite_scroll() { if ( is_search() ) { return false; } $pagination_type = get_theme_mod( 'neve_pagination_type', 'number' ); if ( $pagination_type === 'infinite' ) { return true; } return false; } }