|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Carbon Pagination presenter. |
|
4
|
|
|
* Handles rendering and displaying of a particular pagination. |
|
5
|
|
|
* |
|
6
|
|
|
* @abstract |
|
7
|
|
|
*/ |
|
8
|
|
|
class Carbon_Pagination_Presenter { |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var Carbon_Pagination |
|
12
|
|
|
* |
|
13
|
|
|
* The pagination object. |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $pagination; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor. |
|
19
|
|
|
* |
|
20
|
|
|
* Sets the pagination that will be displayed or rendered. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Carbon_Pagination_HTML $pagination Pagination object that will be displayed. |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function __construct( Carbon_Pagination_HTML $pagination ) { |
|
25
|
1 |
|
$this->set_pagination( $pagination ); |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Retrieve the pagination object. |
|
30
|
|
|
* |
|
31
|
|
|
* @return Carbon_Pagination_HTML $pagination The pagination object. |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function get_pagination() { |
|
34
|
1 |
|
return $this->pagination; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Modify the pagination object. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Carbon_Pagination_HTML $pagination The pagination object. |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function set_pagination( $pagination ) { |
|
43
|
1 |
|
$this->pagination = $pagination; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Verify if the pagination is ready for presentation. |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool|WP_Error $result True if everything is fine, false or WP_Error on failure. |
|
50
|
|
|
*/ |
|
51
|
4 |
|
public function verify_pagination() { |
|
52
|
|
|
// handle unexisting pagination collection classes |
|
53
|
4 |
|
if ( ! class_exists( $this->get_pagination()->get_collection() ) ) { |
|
54
|
1 |
|
return new WP_Error( 'carbon_pagination_unexisting_pagination_collection', __( 'Unexisting pagination collection class.', 'carbon_pagination' ) ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// handle unexisting pagination renderer classes |
|
58
|
3 |
|
if ( ! class_exists( $this->get_pagination()->get_renderer() ) ) { |
|
59
|
1 |
|
return new WP_Error( 'carbon_pagination_unexisting_pagination_renderer', __( 'Unexisting pagination renderer class.', 'carbon_pagination' ) ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// if there are less than 2 pages, nothing will be shown |
|
63
|
2 |
|
if ( $this->get_pagination()->get_total_pages() <= 1 ) { |
|
64
|
1 |
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Render the pagination. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string $output The output of the pagination, or WP_Error on failure. |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function render() { |
|
76
|
|
|
// get pagination and the collection & renderer class names |
|
77
|
2 |
|
$pagination = $this->get_pagination(); |
|
78
|
2 |
|
$collection_classname = $pagination->get_collection(); |
|
79
|
2 |
|
$renderer_classname = $pagination->get_renderer(); |
|
80
|
|
|
|
|
81
|
|
|
// handle unexisting pagination collection classes |
|
82
|
2 |
|
if ( ! $this->verify_pagination() ) { |
|
83
|
2 |
|
return ''; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// initialize & generate pagination item collection |
|
87
|
|
|
$collection = new $collection_classname( $pagination ); |
|
88
|
|
|
|
|
89
|
|
|
// render the pagination item collection |
|
90
|
|
|
$renderer = new $renderer_classname( $collection ); |
|
91
|
|
|
$output = $renderer->render( array(), false ); |
|
92
|
|
|
return $output; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Build, configure and display a new pagination. |
|
97
|
|
|
* |
|
98
|
|
|
* @static |
|
99
|
|
|
* @param string $pagination The pagination type, can be one of the following: |
|
100
|
|
|
* - Posts |
|
101
|
|
|
* - Post |
|
102
|
|
|
* - Comments |
|
103
|
|
|
* - Custom |
|
104
|
|
|
* @param array $args Configuration options to modify the pagination settings. |
|
105
|
|
|
* @param bool $echo Whether to display or return the output. True will display, false will return. |
|
106
|
|
|
* @see Carbon_Pagination::__construct() |
|
107
|
|
|
*/ |
|
108
|
3 |
|
public static function display( $pagination, $args = array(), $echo = true ) { |
|
109
|
3 |
|
$pagination_classname = 'Carbon_Pagination_' . $pagination; |
|
110
|
|
|
|
|
111
|
|
|
// handle unexisting pagination types |
|
112
|
3 |
|
if ( ! class_exists( $pagination_classname ) ) { |
|
113
|
1 |
|
return new WP_Error( 'carbon_pagination_unexisting_pagination_type', __( 'Unexisting pagination type class.', 'carbon_pagination' ) ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// initialize pagination |
|
117
|
2 |
|
$pagination = new $pagination_classname( $args ); |
|
118
|
2 |
|
$presenter = new self( $pagination ); |
|
119
|
2 |
|
$output = $presenter->render(); |
|
120
|
|
|
|
|
121
|
2 |
|
if ( ! $echo ) { |
|
122
|
1 |
|
return $output; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
echo wp_kses( $output, wp_kses_allowed_html( 'post' ) ); |
|
126
|
1 |
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |