1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pagination Class. Adapted from http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/ |
4
|
|
|
* Original author: Victor De la Rocha |
5
|
|
|
* Date: 28/11/2015 |
6
|
|
|
*/ |
7
|
|
|
if ( ! class_exists( 'FooGalleryNextGenPagination' ) ) { |
8
|
|
|
|
9
|
|
|
class FooGalleryNextGenPagination { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/* Default values */ |
12
|
|
|
var $item_count = 100; |
|
|
|
|
13
|
|
|
var $limit = 10; |
|
|
|
|
14
|
|
|
var $target = ""; |
|
|
|
|
15
|
|
|
var $page = 1; |
|
|
|
|
16
|
|
|
var $adjacents = 2; |
|
|
|
|
17
|
|
|
var $parameterName = "paged"; |
|
|
|
|
18
|
|
|
var $urlF = false;//urlFriendly |
|
|
|
|
19
|
|
|
var $start = -1; |
|
|
|
|
20
|
|
|
var $end = -1; |
|
|
|
|
21
|
|
|
var $page_count = 10; |
|
|
|
|
22
|
|
|
var $url = false; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/* Next and previous buttons */ |
25
|
|
|
var $nextI = "›"; |
|
|
|
|
26
|
|
|
var $prevI = "‹"; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/* Processing variables */ |
29
|
|
|
var $calculate = false; |
|
|
|
|
30
|
|
|
var $pagination = ''; |
|
|
|
|
31
|
|
|
var $errors = false; |
|
|
|
|
32
|
|
|
|
33
|
|
|
#Total items |
34
|
|
|
function items( $value ) { |
|
|
|
|
35
|
|
|
$this->item_count = (int) $value; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
#how many items to show per page |
39
|
|
|
function limit( $value ) { |
|
|
|
|
40
|
|
|
$this->limit = (int) $value; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
#Page to sent the page value |
44
|
|
|
function target( $value ) { |
|
|
|
|
45
|
|
|
$this->target = $value; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
#Current page |
49
|
|
|
function currentPage( $value ) { |
|
|
|
|
50
|
|
|
$this->page = (int) $value; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
#How many adjacent pages should be shown on each side of the current page? |
54
|
|
|
function adjacents( $value ) { |
|
|
|
|
55
|
|
|
$this->adjacents = (int) $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
#show counter? |
59
|
|
|
function showCounter( $value = "" ) { |
|
|
|
|
60
|
|
|
$this->showCounter = ( $value === true ) ? true : false; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
#to change the class name of the pagination div |
64
|
|
|
function changeClass( $value = "" ) { |
|
|
|
|
65
|
|
|
$this->className = $value; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function nextLabel( $value ) { |
|
|
|
|
69
|
|
|
$this->nextT = $value; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
function nextIcon( $value ) { |
|
|
|
|
73
|
|
|
$this->nextI = $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
function prevLabel( $value ) { |
|
|
|
|
77
|
|
|
$this->prevT = $value; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
function prevIcon( $value ) { |
|
|
|
|
81
|
|
|
$this->prevI = $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
#to change the class name of the pagination div |
85
|
|
|
function parameterName( $value = "" ) { |
|
|
|
|
86
|
|
|
$this->parameterName = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function render($echo = true) { |
|
|
|
|
90
|
|
|
if ( ! $this->calculate ) { |
91
|
|
|
$this->calculate(); |
92
|
|
|
} |
93
|
|
|
if ( $echo ) { |
94
|
|
|
echo $this->pagination; |
95
|
|
|
} else { |
96
|
|
|
return $this->pagination; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
function get_page_number_url( $page_number ) { |
|
|
|
|
101
|
|
|
return esc_url( add_query_arg( $this->parameterName, $page_number, $this->url ) ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
function calculate() { |
|
|
|
|
105
|
|
|
$this->pagination = ""; |
106
|
|
|
$this->calculate = true; |
107
|
|
|
$this->errors = false; |
108
|
|
|
|
109
|
|
|
if ( $this->urlF and $this->urlF != '%' and strpos( $this->target, $this->urlF ) === false ) { |
|
|
|
|
110
|
|
|
//Es necesario especificar el comodin para sustituir |
111
|
|
|
$this->errors = "Especificaste un wildcard para sustituir, pero no existe en el target"; |
|
|
|
|
112
|
|
|
return; |
113
|
|
|
} elseif ( $this->urlF and $this->urlF == '%' and strpos( $this->target, $this->urlF ) === false ) { |
|
|
|
|
114
|
|
|
$this->errors = "Es necesario especificar en el target el comodin % para sustituir el n�mero de p�gina"; |
|
|
|
|
115
|
|
|
return; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$n = $this->nextI; |
119
|
|
|
$p = $this->prevI; |
120
|
|
|
|
121
|
|
|
/* Setup vars for query. */ |
122
|
|
|
if ( $this->page ) { |
123
|
|
|
$this->start = ( $this->page - 1 ) * $this->limit; //first item to display on this page |
124
|
|
|
$this->end = $this->start + $this->limit - 1; |
125
|
|
|
} else { |
126
|
|
|
$this->start = 0; //if no page var is given, set start to 0 |
127
|
|
|
$this->end = $this->limit - 1; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/* Setup page vars for display. */ |
131
|
|
|
$counter = 1; |
132
|
|
|
$prev = $this->page - 1; //previous page is page - 1 |
133
|
|
|
$next = $this->page + 1; //next page is page + 1 |
134
|
|
|
$this->page_count = ceil( $this->item_count / $this->limit ); //lastpage is = total pages / items per page, rounded up. |
|
|
|
|
135
|
|
|
$lpm1 = $this->page_count - 1; //last page minus 1 |
136
|
|
|
|
137
|
|
|
/* |
138
|
|
|
Now we apply our rules and draw the pagination object. |
139
|
|
|
We're actually saving the code to a variable in case we want to draw it more than once. |
140
|
|
|
*/ |
141
|
|
|
|
142
|
|
|
if ( $this->page_count > 1 ) { |
143
|
|
|
if ( $this->page ) { |
144
|
|
|
//anterior button |
145
|
|
|
if ( $this->page > 1 ) { |
146
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $prev ) . "\" class=\"prev\">$p</a>"; |
147
|
|
|
} else { |
148
|
|
|
$this->pagination .= "<span class=\"disabled\">$p</span>"; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
//pages |
152
|
|
|
if ( $this->page_count < 7 + ( $this->adjacents * 2 ) ) {//not enough pages to bother breaking it up |
153
|
|
|
for ( $counter = 1; $counter <= $this->page_count; $counter ++ ) { |
154
|
|
|
if ( $counter == $this->page ) { |
155
|
|
|
$this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
156
|
|
|
} else { |
157
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} elseif ( $this->page_count > 5 + ( $this->adjacents * 2 ) ) {//enough pages to hide some |
161
|
|
|
//close to beginning; only hide later pages |
162
|
|
|
if ( $this->page < 1 + ( $this->adjacents * 2 ) ) { |
163
|
|
|
for ( $counter = 1; $counter < 4 + ( $this->adjacents * 2 ); $counter ++ ) { |
164
|
|
|
if ( $counter == $this->page ) { |
165
|
|
|
$this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
166
|
|
|
} else { |
167
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$this->pagination .= "..."; |
171
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $lpm1 ) . "\">$lpm1</a>"; |
172
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $this->page_count ) . "\">$this->page_count</a>"; |
173
|
|
|
} //in middle; hide some front and some back |
174
|
|
|
elseif ( $this->page_count - ( $this->adjacents * 2 ) > $this->page && $this->page > ( $this->adjacents * 2 ) ) { |
175
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( 1 ) . "\">1</a>"; |
176
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( 2 ) . "\">2</a>"; |
177
|
|
|
$this->pagination .= "..."; |
178
|
|
|
for ( $counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter ++ ) { |
179
|
|
|
if ( $counter == $this->page ) { |
180
|
|
|
$this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
181
|
|
|
} else { |
182
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
$this->pagination .= "..."; |
186
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $lpm1 ) . "\">$lpm1</a>"; |
187
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $this->page_count ) . "\">$this->page_count</a>"; |
188
|
|
|
} //close to end; only hide early pages |
189
|
|
|
else { |
190
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( 1 ) . "\">1</a>"; |
191
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( 2 ) . "\">2</a>"; |
192
|
|
|
$this->pagination .= "..."; |
193
|
|
|
for ( $counter = $this->page_count - ( 2 + ( $this->adjacents * 2 ) ); $counter <= $this->page_count; $counter ++ ) { |
194
|
|
|
if ( $counter == $this->page ) { |
195
|
|
|
$this->pagination .= "<span class=\"selected-page\">$counter</span>"; |
196
|
|
|
} else { |
197
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $counter ) . "\">$counter</a>"; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
if ( $this->page ) { |
203
|
|
|
//siguiente button |
204
|
|
|
if ( $this->page < $counter - 1 ) { |
205
|
|
|
$this->pagination .= "<a href=\"" . $this->get_page_number_url( $next ) . "\" class=\"next\">$n</a>"; |
206
|
|
|
} else { |
207
|
|
|
$this->pagination .= "<span class=\"disabled\">$n</span>"; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.