| 1 |
<?php |
| 2 |
/** |
| 3 |
* Pagination output and helpers |
| 4 |
* |
| 5 |
* @package Flex Posts |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Get current page for pagination |
| 14 |
* |
| 15 |
* @return int |
| 16 |
*/ |
| 17 |
function flex_posts_get_current_page() { |
| 18 |
if ( get_query_var( 'paged' ) ) { |
| 19 |
$current_page = get_query_var( 'paged' ); |
| 20 |
} elseif ( get_query_var( 'page' ) ) { |
| 21 |
$current_page = get_query_var( 'page' ); |
| 22 |
} else { |
| 23 |
$current_page = 1; |
| 24 |
} |
| 25 |
return $current_page; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Display pagination |
| 30 |
* |
| 31 |
* @param int $total The total amount of pages. |
| 32 |
*/ |
| 33 |
function flex_posts_pagination( $total ) { |
| 34 |
if ( $total > 1 ) { |
| 35 |
/** |
| 36 |
* Filter: flex_posts_pagination_args |
| 37 |
* |
| 38 |
* Allow modification of the arguments passed to paginate_links() used |
| 39 |
* by the plugin. Expected return is an array of paginate_links args. |
| 40 |
* |
| 41 |
* @param array $args Default paginate_links args. |
| 42 |
* @return array Modified paginate_links args. |
| 43 |
* |
| 44 |
* Example: |
| 45 |
* add_filter( 'flex_posts_pagination_args', function( $args ) { |
| 46 |
* $args['mid_size'] = 2; |
| 47 |
* return $args; |
| 48 |
* } ); |
| 49 |
*/ |
| 50 |
$links = paginate_links( |
| 51 |
apply_filters( |
| 52 |
'flex_posts_pagination_args', |
| 53 |
array( |
| 54 |
'total' => $total, |
| 55 |
'current' => flex_posts_get_current_page(), |
| 56 |
'mid_size' => 1, |
| 57 |
'prev_text' => '<span class="screen-reader-text">' . __( 'Previous', 'flex-posts' ) . '</span> <span aria-hidden="true">«</span>', |
| 58 |
'next_text' => '<span class="screen-reader-text">' . __( 'Next', 'flex-posts' ) . '</span> <span aria-hidden="true">»</span>', |
| 59 |
) |
| 60 |
) |
| 61 |
); |
| 62 |
|
| 63 |
if ( $links ) { |
| 64 |
echo '<div class="fp-pagination">'; |
| 65 |
echo '<span class="screen-reader-text">'; |
| 66 |
echo esc_html__( 'Page', 'flex-posts' ); |
| 67 |
echo ': </span>'; |
| 68 |
echo wp_kses( $links, flex_posts_get_allowed_html() ); |
| 69 |
echo '</div>'; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Add pagination at the end of widget |
| 76 |
* |
| 77 |
* @param array $instance Widget settings. |
| 78 |
* @param array $args Query arguments. |
| 79 |
* @param int $max_num_pages Total number of pages. |
| 80 |
* @param int $found_posts Total number of posts found. |
| 81 |
*/ |
| 82 |
function flex_posts_end( $instance, $args, $max_num_pages, $found_posts ) { |
| 83 |
if ( ! empty( $instance['pagination'] ) ) { |
| 84 |
if ( ! empty( $args['old_offset'] ) ) { |
| 85 |
// Modify max_num_pages value if offset is set. |
| 86 |
$found_posts = $found_posts - $args['old_offset']; |
| 87 |
$max_num_pages = ceil( $found_posts / $args['posts_per_page'] ); |
| 88 |
} |
| 89 |
flex_posts_pagination( $max_num_pages ); |
| 90 |
} |
| 91 |
} |
| 92 |
add_action( 'flex_posts_end', 'flex_posts_end', 10, 4 ); |
| 93 |
|