1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Carbon Pagination - single post pagination class. |
4
|
|
|
* Provides the pagination for singular post. |
5
|
|
|
* |
6
|
|
|
* @uses Carbon_Pagination_HTML |
7
|
|
|
*/ |
8
|
|
|
class Carbon_Pagination_Post extends Carbon_Pagination_HTML { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Constructor. |
12
|
|
|
* Creates and configures a new pagination with the provided settings. |
13
|
|
|
* |
14
|
|
|
* @param array $args Configuration options to modify the pagination settings. |
15
|
|
|
*/ |
16
|
6 |
|
public function __construct( $args = array() ) { |
17
|
6 |
|
global $post; |
18
|
|
|
|
19
|
|
|
// get all sibling posts/pages |
20
|
6 |
|
$posts = $this->get_pagination_posts(); |
21
|
|
|
|
22
|
|
|
// specify the default args for the Post pagination |
23
|
6 |
|
$this->default_args = array( |
24
|
|
|
// specify the sibling posts/pages for pagination pages |
25
|
6 |
|
'pages' => $posts, |
26
|
|
|
// the current post/page is the current page |
27
|
6 |
|
'current_page' => array_search( get_the_ID(), $posts ) + 1, |
28
|
|
|
// modify the text of the previous page link |
29
|
6 |
|
'prev_html' => '<a href="{URL}" class="paging-prev">' . esc_html__( '« Previous Entry', 'crb' ) . '</a>', |
30
|
|
|
// modify the text of the next page link |
31
|
6 |
|
'next_html' => '<a href="{URL}" class="paging-next">' . esc_html__( 'Next Entry »', 'crb' ) . '</a>', |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
// register additional tokens |
35
|
6 |
|
add_filter( 'carbon_pagination_after_setup_item', array( $this, 'add_tokens' ) ); |
36
|
|
|
|
37
|
6 |
|
parent::__construct( $args ); |
38
|
6 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve the posts that we'll paginate through. |
42
|
|
|
* |
43
|
|
|
* @return array $posts The posts to paginate through. |
44
|
|
|
*/ |
45
|
3 |
|
public function get_pagination_posts() { |
46
|
3 |
|
global $post; |
47
|
|
|
|
48
|
|
|
// specify default query args to get all sibling posts/pages |
49
|
|
|
$query = array( |
50
|
3 |
|
'post_type' => get_post_type( get_the_ID() ), |
51
|
3 |
|
'posts_per_page' => -1, |
|
|
|
|
52
|
3 |
|
'fields' => 'ids', |
53
|
3 |
|
); |
54
|
|
|
|
55
|
|
|
// allow the default query args to be filtered |
56
|
3 |
|
$query = apply_filters( 'carbon_pagination_post_pagination_query', $query, $this ); |
57
|
|
|
|
58
|
|
|
// get all sibling posts/pages |
59
|
3 |
|
$posts = get_posts( $query ); |
60
|
|
|
|
61
|
3 |
|
return $posts; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the URL to a certain page. |
66
|
|
|
* |
67
|
|
|
* @param int $page_number The page number. |
68
|
|
|
* @param string $old_url Optional. The URL to add the page number to. |
69
|
|
|
* @return string $url The URL to the page number. |
70
|
|
|
*/ |
71
|
4 |
|
public function get_page_url( $page_number, $old_url = '' ) { |
72
|
4 |
|
$posts = $this->get_pages(); |
73
|
4 |
|
$page = 0; |
74
|
|
|
|
75
|
4 |
|
if ( isset( $posts[ $page_number ] ) ) { |
76
|
3 |
|
$page = $posts[ $page_number ]; |
77
|
3 |
|
} |
78
|
|
|
|
79
|
4 |
|
$url = get_permalink( $page ); |
80
|
|
|
|
81
|
4 |
|
return $url; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Add tokens to all post items. |
86
|
|
|
* Registers the {TITLE} token for this pagination type |
87
|
|
|
* |
88
|
|
|
* @param Carbon_Pagination_Item $item The item to add tokens to. |
89
|
|
|
*/ |
90
|
1 |
|
public function add_tokens( Carbon_Pagination_Item $item ) { |
91
|
1 |
|
if ( ! ( $item instanceof Carbon_Pagination_Item_Page ) ) { |
92
|
1 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$tokens = $item->get_tokens(); |
96
|
1 |
|
$tokens['TITLE'] = $this->get_post_title( $item ); |
97
|
1 |
|
$item->set_tokens( $tokens ); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieve the title of a certain post. |
102
|
|
|
* |
103
|
|
|
* @param Carbon_Pagination_Item $item The item to add tokens to. |
104
|
|
|
* @return string $title The title of the item's corresponding post. |
105
|
|
|
*/ |
106
|
1 |
|
public function get_post_title( Carbon_Pagination_Item $item ) { |
107
|
1 |
|
if ( ! ( $item instanceof Carbon_Pagination_Item_Page ) ) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
$page_number = $item->get_page_number(); |
112
|
|
|
|
113
|
1 |
|
$pages = $this->get_pages(); |
114
|
|
|
|
115
|
1 |
|
$title = get_post_field( 'post_title', $pages[ $page_number ] ); |
116
|
1 |
|
$title = apply_filters( 'the_title', $title ); |
117
|
|
|
|
118
|
1 |
|
return $title; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |