-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwp-paginate.php
More file actions
428 lines (384 loc) · 16.7 KB
/
wp-paginate.php
File metadata and controls
428 lines (384 loc) · 16.7 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/*
Plugin Name: WP-Paginate
Plugin URI: http://www.ericmmartin.com/projects/wp-paginate/
Description: A simple and flexible pagination plugin for WordPress posts and comments.
Author: Eric Martin
Version: 1.2.5
Author URI: http://www.ericmmartin.com
Revision: $Id: wp-paginate.php 943040 2014-07-03 18:30:39Z emartin24 $
*/
/* Copyright 2014 Eric Martin (eric@ericmmartin.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Set the wp-content and plugin urls/paths
*/
if (!defined('WP_CONTENT_URL'))
define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
if (!defined('WP_CONTENT_DIR'))
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
if (!defined('WP_PLUGIN_URL') )
define('WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins');
if (!defined('WP_PLUGIN_DIR') )
define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
if (!class_exists('WPPaginate')) {
class WPPaginate {
/**
* @var string The plugin version
*/
var $version = '1.2.5';
/**
* @var string The options string name for this plugin
*/
var $optionsName = 'wp_paginate_options';
/**
* @var string $localizationDomain Domain used for localization
*/
var $localizationDomain = 'wp-paginate';
/**
* @var string $pluginurl The url to this plugin
*/
var $pluginurl = '';
/**
* @var string $pluginpath The path to this plugin
*/
var $pluginpath = '';
/**
* @var array $options Stores the options for this plugin
*/
var $options = array();
var $type = 'posts';
/**
* Constructor
*/
function __construct() {
$name = dirname(plugin_basename(__FILE__));
//Language Setup
load_plugin_textdomain($this->localizationDomain, false, "$name/I18n/");
//"Constants" setup
$this->pluginurl = plugins_url($name) . "/";
$this->pluginpath = WP_PLUGIN_DIR . "/$name/";
//Initialize the options
$this->get_options();
//Actions
add_action('admin_menu', array(&$this, 'admin_menu_link'));
if ($this->options['css'])
add_action('wp_print_styles', array(&$this, 'wp_paginate_css'));
}
/**
* Pagination based on options/args
*/
function paginate($args = false) {
if ($this->type === 'comments' && !get_option('page_comments'))
return;
$r = wp_parse_args($args, $this->options);
extract($r, EXTR_SKIP);
if (!isset($page) && !isset($pages)) {
global $wp_query;
if ($this->type === 'posts') {
$page = get_query_var('paged');
$posts_per_page = intval(get_query_var('posts_per_page'));
$pages = intval(ceil($wp_query->found_posts / $posts_per_page));
}
else {
$page = get_query_var('cpage');
$comments_per_page = get_option('comments_per_page');
$pages = get_comment_pages_count();
}
$page = !empty($page) ? intval($page) : 1;
}
$prevlink = ($this->type === 'posts')
? esc_url(get_pagenum_link($page - 1))
: get_comments_pagenum_link($page - 1);
$nextlink = ($this->type === 'posts')
? esc_url(get_pagenum_link($page + 1))
: get_comments_pagenum_link($page + 1);
$output = stripslashes($before);
if ($pages > 1) {
$output .= sprintf('<ol class="wp-paginate%s">', ($this->type === 'posts') ? '' : ' wp-paginate-comments');
if (strlen(stripslashes($title)) > 0) {
$output .= sprintf('<li><span class="title">%s</span></li>', stripslashes($title));
}
$ellipsis = "<li><span class='gap'>...</span></li>";
if ($page > 1 && !empty($previouspage)) {
$output .= sprintf('<li><a href="%s" class="prev">%s</a></li>', $prevlink, stripslashes($previouspage));
}
$min_links = $range * 2 + 1;
$block_min = min($page - $range, $pages - $min_links);
$block_high = max($page + $range, $min_links);
$left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
$right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;
if ($left_gap && !$right_gap) {
$output .= sprintf('%s%s%s',
$this->paginate_loop(1, $anchor),
$ellipsis,
$this->paginate_loop($block_min, $pages, $page)
);
}
else if ($left_gap && $right_gap) {
$output .= sprintf('%s%s%s%s%s',
$this->paginate_loop(1, $anchor),
$ellipsis,
$this->paginate_loop($block_min, $block_high, $page),
$ellipsis,
$this->paginate_loop(($pages - $anchor + 1), $pages)
);
}
else if ($right_gap && !$left_gap) {
$output .= sprintf('%s%s%s',
$this->paginate_loop(1, $block_high, $page),
$ellipsis,
$this->paginate_loop(($pages - $anchor + 1), $pages)
);
}
else {
$output .= $this->paginate_loop(1, $pages, $page);
}
if ($page < $pages && !empty($nextpage)) {
$output .= sprintf('<li><a href="%s" class="next">%s</a></li>', $nextlink, stripslashes($nextpage));
}
$output .= "</ol>";
}
$output .= stripslashes($after);
if ($pages > 1 || $empty) {
echo $output;
}
}
/**
* Helper function for pagination which builds the page links.
*/
function paginate_loop($start, $max, $page = 0) {
$output = "";
for ($i = $start; $i <= $max; $i++) {
$p = ($this->type === 'posts') ? esc_url(get_pagenum_link($i)) : get_comments_pagenum_link($i);
$output .= ($page == intval($i))
? "<li><span class='page current'>$i</span></li>"
: "<li><a href='$p' title='$i' class='page'>$i</a></li>";
}
return $output;
}
function wp_paginate_css() {
$name = "wp-paginate.css";
if (false !== @file_exists(TEMPLATEPATH . "/$name")) {
$css = get_template_directory_uri() . "/$name";
}
else {
$css = $this->pluginurl . $name;
}
wp_enqueue_style('wp-paginate', $css, false, $this->version, 'screen');
if (function_exists('is_rtl') && is_rtl()) {
$name = "wp-paginate-rtl.css";
if (false !== @file_exists(TEMPLATEPATH . "/$name")) {
$css = get_template_directory_uri() . "/$name";
}
else {
$css = $this->pluginurl . $name;
}
wp_enqueue_style('wp-paginate-rtl', $css, false, $this->version, 'screen');
}
}
/**
* Retrieves the plugin options from the database.
* @return array
*/
function get_options() {
if (!$options = get_option($this->optionsName)) {
$options = array(
'title' => 'Pages:',
'nextpage' => '»',
'previouspage' => '«',
'css' => true,
'before' => '<div class="navigation">',
'after' => '</div>',
'empty' => true,
'range' => 3,
'anchor' => 1,
'gap' => 3
);
update_option($this->optionsName, $options);
}
$this->options = $options;
}
/**
* Saves the admin options to the database.
*/
function save_admin_options(){
return update_option($this->optionsName, $this->options);
}
/**
* @desc Adds the options subpanel
*/
function admin_menu_link() {
add_options_page('WP-Paginate', 'WP-Paginate', 'manage_options', basename(__FILE__), array(&$this, 'admin_options_page'));
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), 10, 2 );
}
/**
* @desc Adds the Settings link to the plugin activate/deactivate page
*/
function filter_plugin_actions($links, $file) {
$settings_link = '<a href="options-general.php?page=' . basename(__FILE__) . '">' . __('Settings', $this->localizationDomain) . '</a>';
array_unshift($links, $settings_link); // before other links
return $links;
}
/**
* Adds settings/options page
*/
function admin_options_page() {
if (isset($_POST['wp_paginate_save'])) {
if (wp_verify_nonce($_POST['_wpnonce'], 'wp-paginate-update-options')) {
$this->options['title'] = $_POST['title'];
$this->options['previouspage'] = $_POST['previouspage'];
$this->options['nextpage'] = $_POST['nextpage'];
$this->options['before'] = $_POST['before'];
$this->options['after'] = $_POST['after'];
$this->options['empty'] = (isset($_POST['empty']) && $_POST['empty'] === 'on') ? true : false;
$this->options['css'] = (isset($_POST['css']) && $_POST['css'] === 'on') ? true : false;
$this->options['range'] = intval($_POST['range']);
$this->options['anchor'] = intval($_POST['anchor']);
$this->options['gap'] = intval($_POST['gap']);
$this->save_admin_options();
echo '<div class="updated"><p>' . __('Success! Your changes were successfully saved!', $this->localizationDomain) . '</p></div>';
}
else {
echo '<div class="error"><p>' . __('Whoops! There was a problem with the data you posted. Please try again.', $this->localizationDomain) . '</p></div>';
}
}
?>
<div class="wrap">
<div class="icon32" id="icon-options-general"><br/></div>
<h2>WP-Paginate</h2>
<form method="post" id="wp_paginate_options">
<?php wp_nonce_field('wp-paginate-update-options'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Pagination Label:', $this->localizationDomain); ?></th>
<td><input name="title" type="text" id="title" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['title'])); ?>"/>
<span class="description"><?php _e('The text/HTML to display before the list of pages.', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Previous Page:', $this->localizationDomain); ?></th>
<td><input name="previouspage" type="text" id="previouspage" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['previouspage'])); ?>"/>
<span class="description"><?php _e('The text/HTML to display for the previous page link.', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Next Page:', $this->localizationDomain); ?></th>
<td><input name="nextpage" type="text" id="nextpage" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['nextpage'])); ?>"/>
<span class="description"><?php _e('The text/HTML to display for the next page link.', $this->localizationDomain); ?></span></td>
</tr>
</table>
<p> </p>
<h3><?php _e('Advanced Settings', $this->localizationDomain); ?></h3>
<table class="form-table">
<tr valign="top">
<th scope="row"><?php _e('Before Markup:', $this->localizationDomain); ?></th>
<td><input name="before" type="text" id="before" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['before'])); ?>"/>
<span class="description"><?php _e('The HTML markup to display before the pagination code.', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('After Markup:', $this->localizationDomain); ?></th>
<td><input name="after" type="text" id="after" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['after'])); ?>"/>
<span class="description"><?php _e('The HTML markup to display after the pagination code.', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Markup Display:', $this->localizationDomain); ?></th>
<td><label for="empty">
<input type="checkbox" id="empty" name="empty" <?php echo ($this->options['empty'] === true) ? "checked='checked'" : ""; ?>/> <?php _e('Show Before Markup and After Markup, even if the page list is empty?', $this->localizationDomain); ?></label></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('WP-Paginate CSS File:', $this->localizationDomain); ?></th>
<td><label for="css">
<input type="checkbox" id="css" name="css" <?php echo ($this->options['css'] === true) ? "checked='checked'" : ""; ?>/> <?php printf(__('Include the default stylesheet wp-paginate.css? WP-Paginate will first look for <code>wp-paginate.css</code> in your theme directory (<code>themes/%s</code>).', $this->localizationDomain), get_template()); ?></label></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Page Range:', $this->localizationDomain); ?></th>
<td>
<select name="range" id="range">
<?php for ($i=1; $i<=10; $i++) : ?>
<option value="<?php echo $i; ?>" <?php echo ($i == $this->options['range']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
<?php endfor; ?>
</select>
<span class="description"><?php _e('The number of page links to show before and after the current page. Recommended value: 3', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Page Anchors:', $this->localizationDomain); ?></th>
<td>
<select name="anchor" id="anchor">
<?php for ($i=0; $i<=10; $i++) : ?>
<option value="<?php echo $i; ?>" <?php echo ($i == $this->options['anchor']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
<?php endfor; ?>
</select>
<span class="description"><?php _e('The number of links to always show at beginning and end of pagination. Recommended value: 1', $this->localizationDomain); ?></span></td>
</tr>
<tr valign="top">
<th scope="row"><?php _e('Page Gap:', $this->localizationDomain); ?></th>
<td>
<select name="gap" id="gap">
<?php for ($i=1; $i<=10; $i++) : ?>
<option value="<?php echo $i; ?>" <?php echo ($i == $this->options['gap']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
<?php endfor; ?>
</select>
<span class="description"><?php _e('The minimum number of pages in a gap before an ellipsis (...) is added. Recommended value: 3', $this->localizationDomain); ?></span></td>
</tr>
</table>
<p class="submit">
<input type="submit" value="Save Changes" name="wp_paginate_save" class="button-primary" />
</p>
</form>
<h2><?php _e('Need Support?', $this->localizationDomain); ?></h2>
<p><?php printf(__('For questions, issues or feature requests, please post them in the %s and make sure to tag the post with wp-paginate.', $this->localizationDomain), '<a href="http://wordpress.org/tags/wp-paginate?forum_id=10#postform">WordPress Forum</a>'); ?></p>
<h2><?php _e('Like To Contribute?', $this->localizationDomain); ?></h2>
<p><?php _e('If you would like to contribute, the following is a list of ways you can help:', $this->localizationDomain); ?></p>
<ul>
<li>» <?php _e('Translate WP-Paginate into your language', $this->localizationDomain); ?></li>
<li>» <?php _e('Blog about or link to WP-Paginate so others can find out about it', $this->localizationDomain); ?></li>
<li>» <?php _e('Report issues, provide feedback, request features, etc.', $this->localizationDomain); ?></li>
<li>» <a href="http://wordpress.org/extend/plugins/wp-paginate/"><?php _e('Rate WP-Paginate on the WordPress Plugins Page', $this->localizationDomain); ?></a></li>
<li>» <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=KUL9VQ6U5VYCE&lc=US&item_name=Eric%20Martin%20%28ericmmartin%2ecom%29¤cy_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"><?php _e('Make a donation', $this->localizationDomain); ?></a></li>
</ul>
<h2><?php _e('Other Links', $this->localizationDomain); ?></h2>
<ul>
<li>» <a href="http://twitter.com/ericmmartin">@ericmmartin</a> on Twitter</li>
<li>» <a href="http://www.ericmmartin.com">EricMMartin.com</a></li>
<li>» <a href="http://www.ericmmartin.com/projects/smcf/">SimpleModal Contact Form (SMCF) - WordPress Plugin</a></li>
<li>» <a href="http://www.ericmmartin.com/projects/simplemodal-login/">SimpleModal Login - WordPress Plugin</a></li>
</ul>
</div>
<?php
}
}
}
//instantiate the class
if (class_exists('WPPaginate')) {
$wp_paginate = new WPPaginate();
}
/**
* Pagination function to use for posts
*/
function wp_paginate($args = false) {
global $wp_paginate;
$wp_paginate->type = 'posts';
return $wp_paginate->paginate($args);
}
/**
* Pagination function to use for post comments
*/
function wp_paginate_comments($args = false) {
global $wp_paginate;
$wp_paginate->type = 'comments';
return $wp_paginate->paginate($args);
}
/*
* The format of this plugin is based on the following plugin template:
* http://pressography.com/plugins/wordpress-plugin-template/
*/