1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Carbon Pagination previous page item class. |
4
|
|
|
* |
5
|
|
|
* @uses Carbon_Pagination_Item |
6
|
|
|
*/ |
7
|
|
|
class Carbon_Pagination_Item_Page extends Carbon_Pagination_Item { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var int |
11
|
|
|
* |
12
|
|
|
* The page number index. |
13
|
|
|
*/ |
14
|
|
|
protected $page_number = 0; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
* |
19
|
|
|
* The HTML of the item. |
20
|
|
|
*/ |
21
|
|
|
protected $html = ''; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Setup the item before rendering. |
25
|
|
|
* Setup item tokens. |
26
|
|
|
*/ |
27
|
2 |
|
public function setup() { |
28
|
2 |
|
$pagination = $this->get_collection()->get_pagination(); |
29
|
|
|
|
30
|
|
|
// get various pagination stuff |
31
|
2 |
|
$page_number = $this->get_page_number(); |
32
|
|
|
|
33
|
|
|
// build the page link URL |
34
|
2 |
|
$url = $pagination->get_page_url( $page_number, Carbon_Pagination_Utilities::get_current_url() ); |
35
|
|
|
|
36
|
|
|
// parse tokens |
37
|
|
|
$tokens = array( |
38
|
2 |
|
'URL' => $url, |
39
|
2 |
|
'PAGE_NUMBER' => $page_number + 1, |
40
|
2 |
|
); |
41
|
|
|
|
42
|
2 |
|
$this->set_tokens( $tokens ); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Render the item. |
47
|
|
|
* |
48
|
|
|
* @return string $link The HTML of the item. |
49
|
|
|
*/ |
50
|
5 |
|
public function render() { |
51
|
5 |
|
$pagination = $this->get_collection()->get_pagination(); |
52
|
5 |
|
$current_page_idx = $pagination->get_current_page() - 1; |
53
|
|
|
|
54
|
|
|
// if there is no text/HTML for the link, use the default one |
55
|
5 |
|
$html = $this->get_html(); |
56
|
5 |
|
if ( ! $html ) { |
57
|
3 |
|
if ( $this->get_page_number() == $current_page_idx ) { |
58
|
1 |
|
$html = $pagination->get_current_number_html(); |
59
|
1 |
|
} else { |
60
|
2 |
|
$html = $pagination->get_number_html(); |
61
|
|
|
} |
62
|
3 |
|
} |
63
|
|
|
|
64
|
|
|
// allow the page link HTML to be filtered |
65
|
5 |
|
$html = apply_filters( 'carbon_pagination_page_link', $html, $this ); |
66
|
|
|
|
67
|
5 |
|
return $html; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Retrieve the page index number. |
72
|
|
|
* |
73
|
|
|
* @return int $page_number The page index number. |
74
|
|
|
*/ |
75
|
1 |
|
public function get_page_number() { |
76
|
1 |
|
return $this->page_number; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Modify the page index number. |
81
|
|
|
* |
82
|
|
|
* @param int $page_number The new page index number. |
83
|
|
|
*/ |
84
|
1 |
|
public function set_page_number( $page_number ) { |
85
|
1 |
|
$this->page_number = $page_number; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve the page item HTML. |
90
|
|
|
* |
91
|
|
|
* @return string $html The page item HTML. |
92
|
|
|
*/ |
93
|
1 |
|
public function get_html() { |
94
|
1 |
|
return $this->html; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Modify the page item HTML. |
99
|
|
|
* |
100
|
|
|
* @param string $html The new page item HTML. |
101
|
|
|
*/ |
102
|
1 |
|
public function set_html( $html ) { |
103
|
1 |
|
$this->html = $html; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Create a new subitems collection with a single subitem |
108
|
|
|
* for the specified collection with the specified HTML and page number. |
109
|
|
|
* |
110
|
|
|
* @static |
111
|
|
|
* @param Carbon_Pagination_Collection $collection Collection of the original item. |
112
|
|
|
* @param string $html HTML of the new subitem. |
113
|
|
|
* @param int $page_number The number of the page to link the subitem to. |
114
|
|
|
* @return Carbon_Pagination_Collection $subitems_collection The new subitems collection. |
115
|
|
|
*/ |
116
|
4 |
|
public static function generate_single_subitem_collection( $collection, $html, $page_number ) { |
117
|
4 |
|
$page_item = new Carbon_Pagination_Item_Page( $collection ); |
118
|
4 |
|
$page_item->set_html( $html ); |
119
|
4 |
|
$page_item->set_page_number( $page_number ); |
120
|
|
|
|
121
|
|
|
// create and assign the subitems collection |
122
|
4 |
|
$subitems_collection = new Carbon_Pagination_Collection( $collection->get_pagination(), false ); |
123
|
4 |
|
$subitems_collection->set_items( array( $page_item ) ); |
124
|
|
|
|
125
|
4 |
|
return $subitems_collection; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |