PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/wp-paginate/wp-paginate.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 432 lines | 301 code | 42 blank | 89 comment | 49 complexity | 73215b358c377b5a1decedf846dc5436 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. /*
  3. Plugin Name: WP-Paginate
  4. Plugin URI: http://www.ericmmartin.com/projects/wp-paginate/
  5. Description: A simple and flexible pagination plugin for WordPress posts and comments.
  6. Author: Eric Martin
  7. Version: 1.2.4
  8. Author URI: http://www.ericmmartin.com
  9. Revision: $Id: wp-paginate.php 467949 2011-11-26 20:03:29Z emartin24 $
  10. */
  11. /* Copyright 2011 Eric Martin (eric@ericmmartin.com)
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program; if not, write to the Free Software
  22. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * Set the wp-content and plugin urls/paths
  26. */
  27. if (!defined('WP_CONTENT_URL'))
  28. define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
  29. if (!defined('WP_CONTENT_DIR'))
  30. define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
  31. if (!defined('WP_PLUGIN_URL') )
  32. define('WP_PLUGIN_URL', WP_CONTENT_URL. '/plugins');
  33. if (!defined('WP_PLUGIN_DIR') )
  34. define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins');
  35. if (!class_exists('WPPaginate')) {
  36. class WPPaginate {
  37. /**
  38. * @var string The plugin version
  39. */
  40. var $version = '1.2.4';
  41. /**
  42. * @var string The options string name for this plugin
  43. */
  44. var $optionsName = 'wp_paginate_options';
  45. /**
  46. * @var string $localizationDomain Domain used for localization
  47. */
  48. var $localizationDomain = 'wp-paginate';
  49. /**
  50. * @var string $pluginurl The url to this plugin
  51. */
  52. var $pluginurl = '';
  53. /**
  54. * @var string $pluginpath The path to this plugin
  55. */
  56. var $pluginpath = '';
  57. /**
  58. * @var array $options Stores the options for this plugin
  59. */
  60. var $options = array();
  61. var $type = 'posts';
  62. /**
  63. * PHP 4 Compatible Constructor
  64. */
  65. function WPPaginate() {$this->__construct();}
  66. /**
  67. * PHP 5 Constructor
  68. */
  69. function __construct() {
  70. $name = dirname(plugin_basename(__FILE__));
  71. //Language Setup
  72. load_plugin_textdomain($this->localizationDomain, false, "$name/I18n/");
  73. //"Constants" setup
  74. $this->pluginurl = plugins_url($name) . "/";
  75. $this->pluginpath = WP_PLUGIN_DIR . "/$name/";
  76. //Initialize the options
  77. $this->get_options();
  78. //Actions
  79. add_action('admin_menu', array(&$this, 'admin_menu_link'));
  80. if ($this->options['css'])
  81. add_action('wp_print_styles', array(&$this, 'wp_paginate_css'));
  82. }
  83. /**
  84. * Pagination based on options/args
  85. */
  86. function paginate($args = false) {
  87. if ($this->type === 'comments' && !get_option('page_comments'))
  88. return;
  89. $r = wp_parse_args($args, $this->options);
  90. extract($r, EXTR_SKIP);
  91. if (!isset($page) && !isset($pages)) {
  92. global $wp_query;
  93. if ($this->type === 'posts') {
  94. $page = get_query_var('paged');
  95. $posts_per_page = intval(get_query_var('posts_per_page'));
  96. $pages = intval(ceil($wp_query->found_posts / $posts_per_page));
  97. }
  98. else {
  99. $page = get_query_var('cpage');
  100. $comments_per_page = get_option('comments_per_page');
  101. $pages = get_comment_pages_count();
  102. }
  103. $page = !empty($page) ? intval($page) : 1;
  104. }
  105. $prevlink = ($this->type === 'posts')
  106. ? esc_url(get_pagenum_link($page - 1))
  107. : get_comments_pagenum_link($page - 1);
  108. $nextlink = ($this->type === 'posts')
  109. ? esc_url(get_pagenum_link($page + 1))
  110. : get_comments_pagenum_link($page + 1);
  111. $output = stripslashes($before);
  112. if ($pages > 1) {
  113. $output .= sprintf('<ol class="wp-paginate%s">', ($this->type === 'posts') ? '' : ' wp-paginate-comments');
  114. $output .= sprintf('<li><span class="title">%s</span></li>', stripslashes($title));
  115. $ellipsis = "<li><span class='gap'>...</span></li>";
  116. if ($page > 1 && !empty($previouspage)) {
  117. $output .= sprintf('<li><a href="%s" class="prev">%s</a></li>', $prevlink, stripslashes($previouspage));
  118. }
  119. $min_links = $range * 2 + 1;
  120. $block_min = min($page - $range, $pages - $min_links);
  121. $block_high = max($page + $range, $min_links);
  122. $left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
  123. $right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;
  124. if ($left_gap && !$right_gap) {
  125. $output .= sprintf('%s%s%s',
  126. $this->paginate_loop(1, $anchor),
  127. $ellipsis,
  128. $this->paginate_loop($block_min, $pages, $page)
  129. );
  130. }
  131. else if ($left_gap && $right_gap) {
  132. $output .= sprintf('%s%s%s%s%s',
  133. $this->paginate_loop(1, $anchor),
  134. $ellipsis,
  135. $this->paginate_loop($block_min, $block_high, $page),
  136. $ellipsis,
  137. $this->paginate_loop(($pages - $anchor + 1), $pages)
  138. );
  139. }
  140. else if ($right_gap && !$left_gap) {
  141. $output .= sprintf('%s%s%s',
  142. $this->paginate_loop(1, $block_high, $page),
  143. $ellipsis,
  144. $this->paginate_loop(($pages - $anchor + 1), $pages)
  145. );
  146. }
  147. else {
  148. $output .= $this->paginate_loop(1, $pages, $page);
  149. }
  150. if ($page < $pages && !empty($nextpage)) {
  151. $output .= sprintf('<li><a href="%s" class="next">%s</a></li>', $nextlink, stripslashes($nextpage));
  152. }
  153. $output .= "</ol>";
  154. }
  155. $output .= stripslashes($after);
  156. if ($pages > 1 || $empty) {
  157. echo $output;
  158. }
  159. }
  160. /**
  161. * Helper function for pagination which builds the page links.
  162. */
  163. function paginate_loop($start, $max, $page = 0) {
  164. $output = "";
  165. for ($i = $start; $i <= $max; $i++) {
  166. $p = ($this->type === 'posts') ? esc_url(get_pagenum_link($i)) : get_comments_pagenum_link($i);
  167. $output .= ($page == intval($i))
  168. ? "<li><span class='page current'>$i</span></li>"
  169. : "<li><a href='$p' title='$i' class='page'>$i</a></li>";
  170. }
  171. return $output;
  172. }
  173. function wp_paginate_css() {
  174. $name = "wp-paginate.css";
  175. if (false !== @file_exists(TEMPLATEPATH . "/$name")) {
  176. $css = get_template_directory_uri() . "/$name";
  177. }
  178. else {
  179. $css = $this->pluginurl . $name;
  180. }
  181. wp_enqueue_style('wp-paginate', $css, false, $this->version, 'screen');
  182. if (function_exists('is_rtl') && is_rtl()) {
  183. $name = "wp-paginate-rtl.css";
  184. if (false !== @file_exists(TEMPLATEPATH . "/$name")) {
  185. $css = get_template_directory_uri() . "/$name";
  186. }
  187. else {
  188. $css = $this->pluginurl . $name;
  189. }
  190. wp_enqueue_style('wp-paginate-rtl', $css, false, $this->version, 'screen');
  191. }
  192. }
  193. /**
  194. * Retrieves the plugin options from the database.
  195. * @return array
  196. */
  197. function get_options() {
  198. if (!$options = get_option($this->optionsName)) {
  199. $options = array(
  200. 'title' => 'Pages:',
  201. 'nextpage' => '&raquo;',
  202. 'previouspage' => '&laquo;',
  203. 'css' => true,
  204. 'before' => '<div class="navigation">',
  205. 'after' => '</div>',
  206. 'empty' => true,
  207. 'range' => 3,
  208. 'anchor' => 1,
  209. 'gap' => 3
  210. );
  211. update_option($this->optionsName, $options);
  212. }
  213. $this->options = $options;
  214. }
  215. /**
  216. * Saves the admin options to the database.
  217. */
  218. function save_admin_options(){
  219. return update_option($this->optionsName, $this->options);
  220. }
  221. /**
  222. * @desc Adds the options subpanel
  223. */
  224. function admin_menu_link() {
  225. add_options_page('WP-Paginate', 'WP-Paginate', 'manage_options', basename(__FILE__), array(&$this, 'admin_options_page'));
  226. add_filter('plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'filter_plugin_actions'), 10, 2 );
  227. }
  228. /**
  229. * @desc Adds the Settings link to the plugin activate/deactivate page
  230. */
  231. function filter_plugin_actions($links, $file) {
  232. $settings_link = '<a href="options-general.php?page=' . basename(__FILE__) . '">' . __('Settings', $this->localizationDomain) . '</a>';
  233. array_unshift($links, $settings_link); // before other links
  234. return $links;
  235. }
  236. /**
  237. * Adds settings/options page
  238. */
  239. function admin_options_page() {
  240. if (isset($_POST['wp_paginate_save'])) {
  241. if (wp_verify_nonce($_POST['_wpnonce'], 'wp-paginate-update-options')) {
  242. $this->options['title'] = $_POST['title'];
  243. $this->options['previouspage'] = $_POST['previouspage'];
  244. $this->options['nextpage'] = $_POST['nextpage'];
  245. $this->options['before'] = $_POST['before'];
  246. $this->options['after'] = $_POST['after'];
  247. $this->options['empty'] = (isset($_POST['empty']) && $_POST['empty'] === 'on') ? true : false;
  248. $this->options['css'] = (isset($_POST['css']) && $_POST['css'] === 'on') ? true : false;
  249. $this->options['range'] = intval($_POST['range']);
  250. $this->options['anchor'] = intval($_POST['anchor']);
  251. $this->options['gap'] = intval($_POST['gap']);
  252. $this->save_admin_options();
  253. echo '<div class="updated"><p>' . __('Success! Your changes were successfully saved!', $this->localizationDomain) . '</p></div>';
  254. }
  255. else {
  256. echo '<div class="error"><p>' . __('Whoops! There was a problem with the data you posted. Please try again.', $this->localizationDomain) . '</p></div>';
  257. }
  258. }
  259. ?>
  260. <div class="wrap">
  261. <div class="icon32" id="icon-options-general"><br/></div>
  262. <h2>WP-Paginate</h2>
  263. <form method="post" id="wp_paginate_options">
  264. <?php wp_nonce_field('wp-paginate-update-options'); ?>
  265. <table class="form-table">
  266. <tr valign="top">
  267. <th scope="row"><?php _e('Pagination Label:', $this->localizationDomain); ?></th>
  268. <td><input name="title" type="text" id="title" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['title'])); ?>"/>
  269. <span class="description"><?php _e('The text/HTML to display before the list of pages.', $this->localizationDomain); ?></span></td>
  270. </tr>
  271. <tr valign="top">
  272. <th scope="row"><?php _e('Previous Page:', $this->localizationDomain); ?></th>
  273. <td><input name="previouspage" type="text" id="previouspage" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['previouspage'])); ?>"/>
  274. <span class="description"><?php _e('The text/HTML to display for the previous page link.', $this->localizationDomain); ?></span></td>
  275. </tr>
  276. <tr valign="top">
  277. <th scope="row"><?php _e('Next Page:', $this->localizationDomain); ?></th>
  278. <td><input name="nextpage" type="text" id="nextpage" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['nextpage'])); ?>"/>
  279. <span class="description"><?php _e('The text/HTML to display for the next page link.', $this->localizationDomain); ?></span></td>
  280. </tr>
  281. </table>
  282. <p>&nbsp;</p>
  283. <h3><?php _e('Advanced Settings', $this->localizationDomain); ?></h3>
  284. <table class="form-table">
  285. <tr valign="top">
  286. <th scope="row"><?php _e('Before Markup:', $this->localizationDomain); ?></th>
  287. <td><input name="before" type="text" id="before" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['before'])); ?>"/>
  288. <span class="description"><?php _e('The HTML markup to display before the pagination code.', $this->localizationDomain); ?></span></td>
  289. </tr>
  290. <tr valign="top">
  291. <th scope="row"><?php _e('After Markup:', $this->localizationDomain); ?></th>
  292. <td><input name="after" type="text" id="after" size="40" value="<?php echo stripslashes(htmlspecialchars($this->options['after'])); ?>"/>
  293. <span class="description"><?php _e('The HTML markup to display after the pagination code.', $this->localizationDomain); ?></span></td>
  294. </tr>
  295. <tr valign="top">
  296. <th scope="row"><?php _e('Markup Display:', $this->localizationDomain); ?></th>
  297. <td><label for="empty">
  298. <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>
  299. </tr>
  300. <tr valign="top">
  301. <th scope="row"><?php _e('WP-Paginate CSS File:', $this->localizationDomain); ?></th>
  302. <td><label for="css">
  303. <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>
  304. </tr>
  305. <tr valign="top">
  306. <th scope="row"><?php _e('Page Range:', $this->localizationDomain); ?></th>
  307. <td>
  308. <select name="range" id="range">
  309. <?php for ($i=1; $i<=10; $i++) : ?>
  310. <option value="<?php echo $i; ?>" <?php echo ($i == $this->options['range']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
  311. <?php endfor; ?>
  312. </select>
  313. <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>
  314. </tr>
  315. <tr valign="top">
  316. <th scope="row"><?php _e('Page Anchors:', $this->localizationDomain); ?></th>
  317. <td>
  318. <select name="anchor" id="anchor">
  319. <?php for ($i=0; $i<=10; $i++) : ?>
  320. <option value="<?php echo $i; ?>" <?php echo ($i == $this->options['anchor']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
  321. <?php endfor; ?>
  322. </select>
  323. <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>
  324. </tr>
  325. <tr valign="top">
  326. <th scope="row"><?php _e('Page Gap:', $this->localizationDomain); ?></th>
  327. <td>
  328. <select name="gap" id="gap">
  329. <?php for ($i=1; $i<=10; $i++) : ?>
  330. <option value="<?php echo $i; ?>" <?php echo ($i == $this->options['gap']) ? "selected='selected'" : ""; ?>><?php echo $i; ?></option>
  331. <?php endfor; ?>
  332. </select>
  333. <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>
  334. </tr>
  335. </table>
  336. <p class="submit">
  337. <input type="submit" value="Save Changes" name="wp_paginate_save" class="button-primary" />
  338. </p>
  339. </form>
  340. <h2><?php _e('Need Support?', $this->localizationDomain); ?></h2>
  341. <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>
  342. <h2><?php _e('Like To Contribute?', $this->localizationDomain); ?></h2>
  343. <p><?php _e('If you would like to contribute, the following is a list of ways you can help:', $this->localizationDomain); ?></p>
  344. <ul>
  345. <li>&raquo; <?php _e('Translate WP-Paginate into your language', $this->localizationDomain); ?></li>
  346. <li>&raquo; <?php _e('Blog about or link to WP-Paginate so others can find out about it', $this->localizationDomain); ?></li>
  347. <li>&raquo; <?php _e('Report issues, provide feedback, request features, etc.', $this->localizationDomain); ?></li>
  348. <li>&raquo; <a href="http://wordpress.org/extend/plugins/wp-paginate/"><?php _e('Rate WP-Paginate on the WordPress Plugins Page', $this->localizationDomain); ?></a></li>
  349. <li>&raquo; <a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=KUL9VQ6U5VYCE&lc=US&item_name=Eric%20Martin%20%28ericmmartin%2ecom%29&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted"><?php _e('Make a donation', $this->localizationDomain); ?></a></li>
  350. </ul>
  351. <h2><?php _e('Other Links', $this->localizationDomain); ?></h2>
  352. <ul>
  353. <li>&raquo; <a href="http://twitter.com/ericmmartin">@ericmmartin</a> on Twitter</li>
  354. <li>&raquo; <a href="http://www.ericmmartin.com">EricMMartin.com</a></li>
  355. <li>&raquo; <a href="http://www.ericmmartin.com/projects/smcf/">SimpleModal Contact Form (SMCF) - WordPress Plugin</a></li>
  356. <li>&raquo; <a href="http://www.ericmmartin.com/projects/simplemodal-login/">SimpleModal Login - WordPress Plugin</a></li>
  357. </ul>
  358. </div>
  359. <?php
  360. }
  361. }
  362. }
  363. //instantiate the class
  364. if (class_exists('WPPaginate')) {
  365. $wp_paginate = new WPPaginate();
  366. }
  367. /**
  368. * Pagination function to use for posts
  369. */
  370. function wp_paginate($args = false) {
  371. global $wp_paginate;
  372. $wp_paginate->type = 'posts';
  373. return $wp_paginate->paginate($args);
  374. }
  375. /**
  376. * Pagination function to use for post comments
  377. */
  378. function wp_paginate_comments($args = false) {
  379. global $wp_paginate;
  380. $wp_paginate->type = 'comments';
  381. return $wp_paginate->paginate($args);
  382. }
  383. /*
  384. * The format of this plugin is based on the following plugin template:
  385. * http://pressography.com/plugins/wordpress-plugin-template/
  386. */
  387. ?>