-
Notifications
You must be signed in to change notification settings - Fork 49
/
core.php
335 lines (274 loc) · 10.6 KB
/
core.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Template tag: Boxed Style Paging
*
* @param array $args:
* 'before': (string)
* 'after': (string)
* 'options': (string|array) Used to overwrite options set in WP-Admin -> Settings -> PageNavi
*
* @return void|string
*/
function wp_pagenavi( $args = array() ) {
if ( !is_array( $args ) ) {
$argv = func_get_args();
$args = array();
foreach ( array( 'before', 'after', 'options' ) as $i => $key ) {
$args[ $key ] = isset( $argv[ $i ]) ? $argv[ $i ] : '';
}
}
$args = wp_parse_args( $args, array(
'before' => '',
'after' => '',
'wrapper_tag' => 'div',
'wrapper_class' => 'wp-pagenavi',
'options' => array(),
'query' => $GLOBALS['wp_query'],
'type' => 'posts',
'echo' => true
) );
extract( $args, EXTR_SKIP );
$options = wp_parse_args( $options, PageNavi_Core::$options->get() );
$instance = new PageNavi_Call( $args );
list( $posts_per_page, $paged, $total_pages ) = $instance->get_pagination_args();
if ( 1 == $total_pages && !$options['always_show'] )
return;
$pages_to_show = absint( $options['num_pages'] );
$larger_page_to_show = absint( $options['num_larger_page_numbers'] );
$larger_page_multiple = absint( $options['larger_page_numbers_multiple'] );
$pages_to_show_minus_1 = $pages_to_show - 1;
$half_page_start = floor( $pages_to_show_minus_1/2 );
$half_page_end = ceil( $pages_to_show_minus_1/2 );
$start_page = $paged - $half_page_start;
if ( $start_page <= 0 )
$start_page = 1;
$end_page = $paged + $half_page_end;
if ( ( $end_page - $start_page ) != $pages_to_show_minus_1 )
$end_page = $start_page + $pages_to_show_minus_1;
if ( $end_page > $total_pages ) {
$start_page = $total_pages - $pages_to_show_minus_1;
$end_page = $total_pages;
}
if ( $start_page < 1 )
$start_page = 1;
// Support for filters to change class names
$class_names = array(
'pages' => apply_filters( 'wp_pagenavi_class_pages', 'pages'),
'first' => apply_filters( 'wp_pagenavi_class_first', 'first' ),
'previouspostslink' => apply_filters( 'wp_pagenavi_class_previouspostslink', 'previouspostslink' ),
'extend' => apply_filters( 'wp_pagenavi_class_extend', 'extend' ),
'smaller' => apply_filters( 'wp_pagenavi_class_smaller', 'smaller' ),
'page' => apply_filters( 'wp_pagenavi_class_page', 'page' ),
'current' => apply_filters( 'wp_pagenavi_class_current', 'current'),
'larger' => apply_filters( 'wp_pagenavi_class_larger', 'larger' ),
'nextpostslink' => apply_filters( 'wp_pagenavi_class_nextpostslink', 'nextpostslink'),
'last' => apply_filters( 'wp_pagenavi_class_last', 'last'),
);
$out = '';
switch ( intval( $options['style'] ) ) {
// Normal
case 1:
// Text
if ( !empty( $options['pages_text'] ) ) {
$pages_text = str_replace(
array( "%CURRENT_PAGE%", "%TOTAL_PAGES%" ),
array( number_format_i18n( $paged ), number_format_i18n( $total_pages ) ),
__( $options['pages_text'], 'wp-pagenavi' ) );
$out .= "<span class='{$class_names['pages']}'>$pages_text</span>";
}
if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
// First
$first_text = str_replace( '%TOTAL_PAGES%', number_format_i18n( $total_pages ), __( $options['first_text'], 'wp-pagenavi' ) );
$out .= $instance->get_single( 1, $first_text, array(
'class' => $class_names['first'],
'aria-label' => __('First Page'),
), '%TOTAL_PAGES%' );
}
// Previous
if ( $paged > 1 && !empty( $options['prev_text'] ) ) {
$out .= $instance->get_single( $paged - 1, $options['prev_text'], array(
'class' => $class_names['previouspostslink'],
'rel' => 'prev',
'aria-label' => __('Previous Page'),
) );
}
if ( $start_page >= 2 && $pages_to_show < $total_pages ) {
if ( !empty( $options['dotleft_text'] ) )
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>";
}
// Smaller pages
$larger_pages_array = array();
if ( $larger_page_multiple )
for ( $i = $larger_page_multiple; $i <= $total_pages; $i+= $larger_page_multiple )
$larger_pages_array[] = $i;
$larger_page_start = 0;
foreach ( $larger_pages_array as $larger_page ) {
if ( $larger_page < ($start_page - $half_page_start) && $larger_page_start < $larger_page_to_show ) {
$out .= $instance->get_single( $larger_page, $options['page_text'], array(
'class' => "{$class_names['smaller']} {$class_names['page']}",
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $larger_page ) ),
) );
$larger_page_start++;
}
}
if ( $larger_page_start )
$out .= "<span class='{$class_names['extend']}'>{$options['dotleft_text']}</span>";
// Page numbers
$timeline = 'smaller';
foreach ( range( $start_page, $end_page ) as $i ) {
if ( $i == $paged && !empty( $options['current_text'] ) ) {
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
$out .= "<span aria-current='page' class='{$class_names['current']}'>$current_page_text</span>";
$timeline = 'larger';
} else {
$out .= $instance->get_single( $i, $options['page_text'], array(
'class' => "{$class_names['page']} {$class_names[$timeline]}",
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $i ) ),
) );
}
}
// Large pages
$larger_page_end = 0;
$larger_page_out = '';
foreach ( $larger_pages_array as $larger_page ) {
if ( $larger_page > ($end_page + $half_page_end) && $larger_page_end < $larger_page_to_show ) {
$larger_page_out .= $instance->get_single( $larger_page, $options['page_text'], array(
'class' => "{$class_names['larger']} {$class_names['page']}",
'title' => sprintf( __( 'Page %s', 'wp-pagenavi' ), number_format_i18n( $larger_page ) ),
) );
$larger_page_end++;
}
}
if ( $larger_page_out ) {
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>";
}
$out .= $larger_page_out;
if ( $end_page < $total_pages ) {
if ( !empty( $options['dotright_text'] ) )
$out .= "<span class='{$class_names['extend']}'>{$options['dotright_text']}</span>";
}
// Next
if ( $paged < $total_pages && !empty( $options['next_text'] ) ) {
$out .= $instance->get_single( $paged + 1, $options['next_text'], array(
'class' => $class_names['nextpostslink'],
'rel' => 'next',
'aria-label' => __('Next Page'),
) );
}
if ( $end_page < $total_pages ) {
// Last
$out .= $instance->get_single( $total_pages, __( $options['last_text'], 'wp-pagenavi' ), array(
'class' => $class_names['last'],
'aria-label' => __('Last Page'),
), '%TOTAL_PAGES%' );
}
break;
// Dropdown
case 2:
$out .= '<form action="" method="get">'."\n";
$out .= '<select size="1" onchange="document.location.href = this.options[this.selectedIndex].value;">'."\n";
foreach ( range( 1, $total_pages ) as $i ) {
$page_num = $i;
if ( $page_num == 1 )
$page_num = 0;
if ( $i == $paged ) {
$current_page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['current_text'] );
$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'" selected="selected" class="'.$class_names['current'].'">'.$current_page_text."</option>\n";
} else {
$page_text = str_replace( '%PAGE_NUMBER%', number_format_i18n( $i ), $options['page_text'] );
$out .= '<option value="'.esc_url( $instance->get_url( $page_num ) ).'">'.$page_text."</option>\n";
}
}
$out .= "</select>\n";
$out .= "</form>\n";
break;
}
$out = $before . "<" . $wrapper_tag . " class='" . $wrapper_class . "' role='navigation'>\n$out\n</" . $wrapper_tag . ">" . $after;
$out = apply_filters( 'wp_pagenavi', $out, $args );
if ( !$echo )
return $out;
echo $out;
}
class PageNavi_Call {
protected $args;
function __construct( $args ) {
$this->args = $args;
}
function __get( $key ) {
return $this->args[ $key ];
}
function get_pagination_args() {
global $numpages;
$query = $this->query;
switch( $this->type ) {
case 'multipart':
// Multipart page
$posts_per_page = 1;
$paged = max( 1, absint( get_query_var( 'page' ) ) );
$total_pages = max( 1, $numpages );
break;
case 'users':
// WP_User_Query
$posts_per_page = $query->query_vars['number'];
$paged = max( 1, floor( $query->query_vars['offset'] / $posts_per_page ) + 1 );
$total_pages = max( 1, ceil( $query->total_users / $posts_per_page ) );
break;
default:
// WP_Query
$posts_per_page = intval( $query->get( 'posts_per_page' ) );
$paged = max( 1, absint( $query->get( 'paged' ) ) );
$total_pages = max( 1, absint( $query->max_num_pages ) );
break;
}
return array( $posts_per_page, $paged, $total_pages );
}
function get_single( $page, $raw_text, $attr, $format = '%PAGE_NUMBER%' ) {
if ( empty( $raw_text ) )
return '';
$text = str_replace( $format, number_format_i18n( $page ), $raw_text );
$attr['href'] = $this->get_url( $page );
return html( 'a', $attr, $text );
}
function get_url( $page ) {
return ( 'multipart' == $this->type ) ? get_multipage_link( $page ) : get_pagenum_link( $page );
}
}
# http://core.trac.wordpress.org/ticket/16973
if ( !function_exists( 'get_multipage_link' ) ) :
function get_multipage_link( $page = 1 ) {
global $post, $wp_rewrite;
if ( 1 == $page ) {
$url = get_permalink();
} else {
if ( '' == get_option('permalink_structure') || in_array( $post->post_status, array( 'draft', 'pending') ) )
$url = add_query_arg( 'page', $page, get_permalink() );
elseif ( 'page' == get_option( 'show_on_front' ) && get_option('page_on_front') == $post->ID )
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $wp_rewrite->pagination_base . "/$page", 'single_paged' );
else
$url = trailingslashit( get_permalink() ) . user_trailingslashit( $page, 'single_paged' );
}
return $url;
}
endif;
// Template tag: Drop Down Menu (Deprecated)
function wp_pagenavi_dropdown() {
wp_pagenavi();
}
class PageNavi_Core {
static $options;
static function init( $options ) {
self::$options = $options;
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'stylesheets' ) );
}
static function stylesheets() {
if ( !self::$options->use_pagenavi_css )
return;
if ( @file_exists( get_stylesheet_directory() . '/pagenavi-css.css' ) )
$css_file = get_stylesheet_directory_uri() . '/pagenavi-css.css';
elseif ( @file_exists( get_template_directory() . '/pagenavi-css.css' ) )
$css_file = get_template_directory_uri() . '/pagenavi-css.css';
else
$css_file = plugins_url( 'pagenavi-css.css', __FILE__ );
wp_enqueue_style( 'wp-pagenavi', $css_file, false, '2.70' );
}
}