1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Carbon Pagination renderer class. |
4
|
|
|
* Handles rendering of the pagination. |
5
|
|
|
*/ |
6
|
|
|
class Carbon_Pagination_Renderer { |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @var Carbon_Pagination_Collection |
10
|
|
|
* |
11
|
|
|
* The pagination collection object. |
12
|
|
|
*/ |
13
|
|
|
protected $collection; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Constructor. |
17
|
|
|
* Creates and configures a new pagination renderer for the provided pagination collection. |
18
|
|
|
* |
19
|
|
|
* @param Carbon_Pagination_Collection $collection Pagination collection object. |
20
|
|
|
*/ |
21
|
1 |
|
public function __construct( Carbon_Pagination_Collection $collection ) { |
22
|
1 |
|
$this->set_collection( $collection ); |
23
|
1 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Retrieve the collection object. |
27
|
|
|
* |
28
|
|
|
* @return Carbon_Pagination_Collection $collection The collection object. |
29
|
|
|
*/ |
30
|
1 |
|
public function get_collection() { |
31
|
1 |
|
return $this->collection; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Modify the collection object. |
36
|
|
|
* |
37
|
|
|
* @param Carbon_Pagination_Collection $collection The new collection object. |
38
|
|
|
*/ |
39
|
1 |
|
public function set_collection( Carbon_Pagination_Collection $collection ) { |
40
|
1 |
|
$this->collection = $collection; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Parse all tokens within a string. |
45
|
|
|
* |
46
|
|
|
* Tokens should be passed in the array in the following way: |
47
|
|
|
* array( 'TOKENNAME' => 'tokenvalue' ) |
48
|
|
|
* |
49
|
|
|
* Tokens should be used in the string in the following way: |
50
|
|
|
* 'lorem {TOKENNAME} ipsum' |
51
|
|
|
* |
52
|
|
|
* @param string $string The unparsed string. |
53
|
|
|
* @param array $tokens An array of tokens and their values. |
54
|
|
|
* @return string $string The parsed string. |
55
|
|
|
*/ |
56
|
9 |
|
public function parse_tokens( $string, $tokens = array() ) { |
57
|
9 |
|
foreach ( $tokens as $find => $replace ) { |
58
|
9 |
|
$string = str_replace( '{' . $find . '}', $replace, $string ); |
59
|
9 |
|
} |
60
|
|
|
|
61
|
9 |
|
return $string; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Prepare the items for rendering. |
66
|
|
|
* If no items are specified, fetches the ones from the collection. |
67
|
|
|
* Filters out incorrect items. |
68
|
|
|
* |
69
|
|
|
* @return array $ready_items The prepared items. |
70
|
|
|
*/ |
71
|
4 |
|
public function prepare_items( $items = array() ) { |
72
|
|
|
// if no items are specified, use the ones from the collection |
73
|
4 |
|
if ( empty( $items ) ) { |
74
|
1 |
|
$items = $this->get_collection()->get_items(); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
4 |
|
$ready_items = array(); |
78
|
|
|
|
79
|
|
|
// allow only Carbon_Pagination_Item instances here |
80
|
4 |
|
foreach ( $items as $item ) { |
81
|
4 |
|
if ( $item instanceof Carbon_Pagination_Item ) { |
82
|
4 |
|
$ready_items[] = $item; |
83
|
4 |
|
} |
84
|
4 |
|
} |
85
|
|
|
|
86
|
4 |
|
return $ready_items; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Render the current collection items. |
91
|
|
|
* Each item can have sub items, which are rendered recursively. |
92
|
|
|
* |
93
|
|
|
* @param array $items Items to render. If not specified, will render the collection items. |
94
|
|
|
* @param bool $echo Whether to display or return the output. True will display, false will return. |
95
|
|
|
*/ |
96
|
6 |
|
public function render( $items = array(), $echo = true ) { |
97
|
|
|
// allow developers to filter the items before rendering |
98
|
6 |
|
$items = apply_filters( 'carbon_pagination_items_before_render', $items, $this ); |
99
|
6 |
|
$items = $this->prepare_items( $items ); |
100
|
|
|
|
101
|
|
|
// loop through all items and get their output |
102
|
6 |
|
$output = $this->render_items( $items ); |
103
|
|
|
|
104
|
|
|
// allow developers to filter the output before it is rendered |
105
|
6 |
|
$output = apply_filters( 'carbon_pagination_renderer_output', $output, $this ); |
106
|
|
|
|
107
|
6 |
|
if ( ! $echo ) { |
108
|
6 |
|
return $output; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
echo wp_kses( $output, wp_kses_allowed_html( 'post' ) ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Render a set of pagination items, recursively. |
116
|
|
|
* |
117
|
|
|
* @param array $items Items to render. |
118
|
|
|
* @return string $output The HTML output of all items. |
119
|
|
|
*/ |
120
|
4 |
|
public function render_items( $items ) { |
121
|
4 |
|
$output = ''; |
122
|
|
|
|
123
|
|
|
// loop through items |
124
|
4 |
|
foreach ( $items as $item ) { |
125
|
4 |
|
$subitems_collection = $item->get_subitems_collection(); |
126
|
4 |
|
if ( $subitems_collection && $items = $subitems_collection->get_items() ) { |
127
|
|
|
// loop the subitem collection items |
128
|
3 |
|
$output .= $this->render_items( $items ); |
129
|
3 |
|
} else { |
130
|
|
|
// add item HTML to output |
131
|
4 |
|
$output .= $this->render_item( $item ); |
132
|
|
|
} |
133
|
4 |
|
} |
134
|
|
|
|
135
|
4 |
|
return $output; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Setup, parse tokens & render a specific item. |
140
|
|
|
* |
141
|
|
|
* @param Carbon_Pagination_Item $item Item to render. |
142
|
|
|
* @return string $output The HTML of the item. |
143
|
|
|
*/ |
144
|
4 |
|
public function render_item( Carbon_Pagination_Item $item ) { |
145
|
|
|
// allow item to be modified before setup |
146
|
4 |
|
do_action( 'carbon_pagination_before_setup_item', $item ); |
147
|
|
|
|
148
|
|
|
// setup the item |
149
|
4 |
|
$item->setup(); |
150
|
|
|
|
151
|
|
|
// allow item to be modified after setup |
152
|
4 |
|
do_action( 'carbon_pagination_after_setup_item', $item ); |
153
|
|
|
|
154
|
|
|
// render the item |
155
|
4 |
|
$html = $this->parse_tokens( $item->render(), $item->get_tokens() ); |
156
|
|
|
|
157
|
|
|
// allow item HTML to be filtered |
158
|
4 |
|
$html = apply_filters( 'carbon_pagination_render_item_html', $html, $item ); |
159
|
|
|
|
160
|
4 |
|
return $html; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
} |