/
lcp-paginator.php
152 lines (136 loc) · 5.03 KB
/
lcp-paginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Class to build pagination
* @author fernando@picandocodigo.net
*/
class LcpPaginator {
private $catlist;
private $prev_page_num;
private $next_page_num;
// Singleton implementation
private static $instance = null;
public static function get_instance(){
if( !isset( self::$instance ) ){
self::$instance = new self;
}
return self::$instance;
}
# Define if pagination should be displayed based on 'pagination' param and option.
# Check if the pagination option is set to true, and the param
# is not set to 'no' (since shortcode parameters should
# override general options).
# Receives params['pagination'] from CatList
public function get_pagination($params){
if (LcpUtils::lcp_show_pagination($params['pagination'])){
$lcp_paginator = '';
$pages_count = ceil (
$params['posts_count'] /
# Avoid dividing by 0 (pointed out by @rhj4)
max( array( 1, $params['numberposts'] ) )
);
$pag_output = '';
$this->prev_page_num = null;
$this->next_page_num = null;
$lcp_elipsis = "<span class='lcp_elipsis'>...</span>";
if ( $pages_count > 1 ) {
/* Dynamic pagination inspired by
* https://gist.github.com/shlomohass/9869e138a4fba0e7dc4c
*/
// Print first page.
$lcp_paginator .= $this->lcp_page_link(
1,
$params['page'],
$params['instance'],
$params['bookmarks']
);
// Padding around current page. How many pages will be printed
// before and after current page.
$pad = intval( $params['padding'] );
// Print opening ellipsis if needed
$params['page'] - $pad > 2 && $lcp_paginator .= $lcp_elipsis;
// Loop over pages excluding first and last page.
for( $i = 2; $i < $pages_count; $i++ ) {
if ( $i >= $params['page'] - $pad && $i <= $params['page'] + $pad ) {
$lcp_paginator .= $this->lcp_page_link(
$i,
$params['page'],
$params['instance'],
$params['bookmarks']
);
}
}
// Print closing ellipsis if needed
$params['page'] + $pad < $pages_count - 1 && $lcp_paginator .= $lcp_elipsis;
// Print last page.
$lcp_paginator .= $this->lcp_page_link(
$pages_count,
$params['page'],
$params['instance'],
$params['bookmarks']
);
$pag_output .= "<ul class='lcp_paginator'>";
// Add "Previous" link
if ($params['page'] > 1){
$this->prev_page_num = intval(intval($params['page']) - 1);
$pag_output .= $this->lcp_page_link(
$this->prev_page_num,
$params['page'],
$params['instance'],
$params['bookmarks'], $params['previous']
);
}
$pag_output .= $lcp_paginator;
// Add "Next" link
if ($params['page'] < $pages_count){
$this->next_page_num = intval($params['page'] + 1);
$pag_output .= $this->lcp_page_link(
$this->next_page_num,
$params['page'],
$params['instance'],
$params['bookmarks'], $params['next']
);
}
$pag_output .= "</ul>";
}
$pag_output = apply_filters( 'lcp_pagination_html', $pag_output, $params, $pages_count );
return $pag_output;
}
}
// `char` is the string from pagination_prev/pagination_next
private function lcp_page_link($page, $current_page, $lcp_instance, $bookmark, $char = null){
$link = '';
if ($page == $current_page){
$link = "<li class='lcp_currentpage'>$current_page</li>";
} else {
$server_vars = add_magic_quotes($_SERVER);
$request_uri = $server_vars['REQUEST_URI'];
$query = $server_vars['QUERY_STRING'];
$amp = ( strpos( $request_uri, "?") ) ? "&" : "";
$pattern = "/[&|?]?lcp_page" . preg_quote($lcp_instance) . "=([0-9]+)/";
$query = preg_replace($pattern, '', $query);
$url = strtok($request_uri,'?');
$protocol = "http";
$port = $server_vars['SERVER_PORT'];
if ( (!empty($server_vars['HTTPS']) && $server_vars['HTTPS'] !== 'off') || $port == 443){
$protocol = "https";
}
$http_host = $server_vars['HTTP_HOST'];
$page_link = "$protocol://$http_host$url?$query" .
$amp . "lcp_page" . $lcp_instance . "=". $page;
// Append a bookmark if not disabled by 'pagination_bookmarks=no'
if ($bookmark !== "no") $page_link .= "#lcp_instance_" . $lcp_instance;
$link .= "<li><a href='$page_link' title='$page'";
if ($page === $this->prev_page_num) {
$link .= " class='lcp_prevlink'";
} elseif ($page === $this->next_page_num) {
$link .= " class='lcp_nextlink'";
}
$link .= ">";
($char != null) ? ($link .= $char) : ($link .= $page);
$link .= "</a></li>";
}
// WA: Replace '?&' by '?' to avoid potential redirection problems later on
$link = str_replace('?&', '?', $link );
return $link;
}
}