Advertisement
Guest User

si douglas

a guest
Jan 30th, 2010
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.08 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Next Page
  4. Plugin URI: http://sillybean.net/code/wordpress/next-page/
  5. Description: Provides shortcodes and template tags for next/previous navigation in pages.
  6. Version: 1.2
  7. Author: Stephanie Leary
  8. Author URI: http://sillybean.net/
  9.  
  10. Changelog:
  11. = 1.2 =
  12. * Added option to exclude pages by ID
  13. * Improved handling of special characters (September 16, 2009)
  14. = 1.1 =
  15. * Added security check before allowing users to manage options
  16. * Fixed typo in template tags shown on options page (August 3, 2009)
  17. = 1.0 =
  18. * First release (July 4, 2009)
  19.  
  20. Copyright 2009  Stephanie Leary  (email : steph@sillybean.net)
  21.  
  22.     This program is free software; you can redistribute it and/or modify
  23.     it under the terms of the GNU General Public License as published by
  24.     the Free Software Foundation; either version 2 of the License, or
  25.     (at your option) any later version.
  26.  
  27.     This program is distributed in the hope that it will be useful,
  28.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.     GNU General Public License for more details.
  31.  
  32.     You should have received a copy of the GNU General Public License
  33.     along with this program; if not, write to the Free Software
  34.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  35. */
  36.  
  37. // Hook for adding admin menus
  38. add_action('admin_menu', 'next_page_add_pages');
  39.  
  40. function next_page_add_pages() {
  41.     // Add a new submenu under Options:
  42.     add_options_page('Next Page', 'Next Page', 8, 'next-page', 'next_page_options');
  43.     // set defaults
  44.     add_option('next_page__before_prev_link', '<div class="alignleft">', '', 'yes');
  45.     add_option('next_page__prev_link_text', 'Previous: %title%', '', 'yes');
  46.     add_option('next_page__after_prev_link', '</div>', '', 'yes');
  47.     add_option('next_page__before_parent_link', '<div class="aligncenter">', '', 'yes');
  48.     add_option('next_page__parent_link_text', 'Up one level: %title%', '', 'yes');
  49.     add_option('next_page__after_parent_link', '</div>', '', 'yes');
  50.     add_option('next_page__before_next_link', '<div class="alignright">', '', 'yes');
  51.     add_option('next_page__next_link_text', 'Next: %title%', '', 'yes');
  52.     add_option('next_page__after_next_link', '</div>', '', 'yes');
  53.     add_option('next_page__exclude', '', '', 'yes');
  54.  
  55. }
  56.  
  57. // displays the options page content
  58. function next_page_options() {
  59.     if ( current_user_can('manage_options') ) {  
  60.     // variables for the field and option names
  61.         $hidden_field_name = 'next_page_submit_hidden';
  62.    
  63.         // See if the user has posted us some information
  64.         // If they did, this hidden field will be set to 'Y'
  65.         if( $_POST[ $hidden_field_name ] == 'Y' ) {
  66.                 // Save the posted value in the database
  67.             update_option('next_page__before_prev_link', $_POST['next_page__before_prev_link']);
  68.             update_option('next_page__prev_link_text', $_POST['next_page__prev_link_text']);
  69.             update_option('next_page__after_prev_link', $_POST['next_page__after_prev_link']);
  70.            
  71.             update_option('next_page__before_parent_link', $_POST['next_page__before_parent_link']);
  72.             update_option('next_page__parent_link_text', $_POST['next_page__parent_link_text']);
  73.             update_option('next_page__after_parent_link', $_POST['next_page__after_parent_link']);
  74.            
  75.             update_option('next_page__before_next_link', $_POST['next_page__before_next_link']);
  76.             update_option('next_page__next_link_text', $_POST['next_page__next_link_text']);
  77.             update_option('next_page__after_next_link', $_POST['next_page__after_next_link']);
  78.            
  79.             update_option('next_page__exclude', $_POST['next_page__exclude']);
  80.    
  81.             // Put an options updated message on the screen
  82.     ?>
  83.     <div class="updated"><p><strong><?php _e('Options saved.'); ?></strong></p></div>
  84.    
  85.     <?php  } // Now display the options editing screen ?>
  86.    
  87.     <div class="wrap">
  88.     <form method="post" id="next_page_form">
  89.     <?php wp_nonce_field('update-options'); ?>
  90.  
  91.     <h2><?php _e( 'Next Page Options'); ?></h2>
  92.     <input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
  93.    
  94.     <p><label><?php _e("Exclude pages: "); ?><br />
  95.     <input type="text" name="next_page__exclude" id="next_page__exclude" value="<?php echo stripslashes(htmlentities(get_option('next_page__exclude'))); ?>" /><br />
  96.     <small><?php _e("Enter page IDs separated by commas."); ?></small></label></p>
  97.        
  98.     <div style="float: left; width: 30%; margin-right: 5%;">
  99.     <h3><?php _e("Previous Page Display:"); ?></h3>
  100.     <p><label><?php _e("Before previous page link: "); ?><br />
  101.     <input type="text" name="next_page__before_prev_link" id="next_page__before_prev_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__before_prev_link'))); ?>" />  </label></p>
  102.    
  103.     <p><label><?php _e("Previous page link text: <small>Use %title% for the page title</small>"); ?><br />
  104.     <input type="text" name="next_page__prev_link_text" id="next_page__prev_link_text" value="<?php echo stripslashes(htmlentities(get_option('next_page__prev_link_text'))); ?>" />  </label></p>
  105.    
  106.     <p><label><?php _e("After previous page link: "); ?><br />
  107.     <input type="text" name="next_page__after_prev_link" id="next_page__after_prev_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__after_prev_link'))); ?>" />  </label></p>
  108.     <p>Shortcode: <strong>[previous]</strong><br />
  109.     Template tag: <strong>&lt;?php previous_link(); ?&gt;</strong></p>
  110.     </div>
  111.    
  112.     <div style="float: left; width: 30%; margin-right: 5%;">
  113.     <h3><?php _e("Parent Page Display:"); ?></h3>
  114.     <p><label><?php _e("Before parent page link: "); ?><br />
  115.     <input type="text" name="next_page__before_parent_link" id="next_page__before_parent_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__before_parent_link'))); ?>" />  </label></p>
  116.    
  117.     <p><label><?php _e("Parent page link text: <small>Use %title% for the page title</small>"); ?><br />
  118.     <input type="text" name="next_page__parent_link_text" id="next_page__parent_link_text" value="<?php echo stripslashes(htmlentities(get_option('next_page__parent_link_text'))); ?>" />  </label></p>
  119.    
  120.     <p><label><?php _e("After parent page link: "); ?><br />
  121.     <input type="text" name="next_page__after_parent_link" id="next_page__after_parent_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__after_parent_link'))); ?>" />  </label></p>
  122.     <p>Shortcode: <strong>[parent]</strong><br />
  123.     Template tag: <strong>&lt;?php parent_link(); ?&gt;</strong></p>
  124.     </div>
  125.    
  126.     <div style="float: left; width: 30%;">
  127.     <h3><?php _e("Next Page Display:"); ?></h3>
  128.     <p><label><?php _e("Before next page link: "); ?><br />
  129.     <input type="text" name="next_page__before_next_link" id="next_page__before_next_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__before_next_link'))); ?>" />  </label></p>
  130.    
  131.     <p><label><?php _e("Next page link text: <small>Use %title% for the page title</small>"); ?><br />
  132.     <input type="text" name="next_page__next_link_text" id="next_page__next_link_text" value="<?php echo stripslashes(htmlentities(get_option('next_page__next_link_text'))); ?>" />  </label></p>
  133.    
  134.     <p><label><?php _e("After next page link: "); ?><br />
  135.     <input type="text" name="next_page__after_next_link" id="next_page__after_next_link" value="<?php echo stripslashes(htmlentities(get_option('next_page__after_next_link'))); ?>" />  </label></p>
  136.     <p>Shortcode: <strong>[next]</strong><br />
  137.     Template tag: <strong>&lt;?php next_link(); ?&gt;</strong></p>
  138.     </div>
  139.    
  140.     <input type="hidden" name="action" value="update" />
  141.     <input type="hidden" name="page_options" value="next_page__before_prev_link,
  142.     next_page__prev_link_text,
  143.     next_page__after_prev_link,
  144.     next_page__before_parent_link,
  145.     next_page__parent_link_text,
  146.     next_page__before_next_link,
  147.     next_page__next_link_text,
  148.     next_page__next_link_text,
  149.     next_page__after_next_link" />
  150.    
  151.     <p class="submit">
  152.     <input type="submit" name="submit" class="button-primary" value="<?php _e('Update Options'); ?>" />
  153.     </p>
  154.     </form>
  155.     </div>
  156. <?php
  157.     } // if user can
  158. } // end function next_page_options()
  159.  
  160. // make the magic happen
  161. function flatten_page_list($exclude = '') {
  162.     global $post;
  163.     $args = "sort_column=menu_order&sort_order=asc&child_of={$post->post_parent}";
  164.     if (!empty($exclude)) $args .= '&exclude='.$exclude;
  165.     $pagelist = get_pages($args);
  166.     $pages = array();
  167.     foreach ($pagelist as $page) {
  168.        $pages[] += $page->ID;
  169.     }
  170.     return $pages;
  171. }
  172.  
  173. function next_page() {
  174.     global $post;
  175.     $exclude = get_option('next_page__exclude');
  176.     $pages = flatten_page_list($exclude);
  177.     $current = array_search($post->ID, $pages);
  178.    
  179.     if ($current+1 >= count($pages)) {
  180.         return false;
  181.     }else {
  182.    
  183.     $nextID = $pages[$current+1];
  184.    
  185.     $before_link = stripslashes(get_option('next_page__before_next_link'));
  186.     $linkurl = get_permalink($nextID);
  187.     $title = get_the_title($nextID);
  188.     $linktext = get_option('next_page__next_link_text');
  189.     if (strpos($linktext, '%title%') !== false)
  190.         $linktext = str_replace('%title%', $title, $linktext);
  191.     $after_link = stripslashes(get_option('next_page__after_next_link'));
  192.    
  193.     $link = $before_link . '<a href="' . $linkurl . '" title="' . $title . '">' . $linktext . '</a>' . $after_link;
  194.     return $link;
  195.     }
  196. }
  197.  
  198. function prev_page() {
  199.     global $post;
  200.     $exclude = get_option('next_page__exclude');
  201.     $pages = flatten_page_list($exclude);
  202.     $current = array_search($post->ID, $pages);
  203.    
  204.     if ($current-1 <0 ) {
  205.         return false;
  206.     }else {
  207.     $prevID = $pages[$current-1];
  208.    
  209.     $before_link = stripslashes(get_option('next_page__before_prev_link'));
  210.     $linkurl = get_permalink($prevID);
  211.     $title = get_the_title($prevID);
  212.     $linktext = get_option('next_page__prev_link_text');
  213.     if (strpos($linktext, '%title%') !== false)
  214.         $linktext = str_replace('%title%', $title, $linktext);
  215.     $after_link = stripslashes(get_option('next_page__after_prev_link'));
  216.    
  217.     $link = $before_link . '<a href="' . $linkurl . '" title="' . $title . '">' . $linktext . '</a>' . $after_link;
  218.     return $link;
  219.     }
  220. }
  221.  
  222. function parent_page() {
  223.     global $post;
  224.     $parentID = $post->post_parent;
  225.    
  226.     $exclude = array(get_option('next_page__exclude'));
  227.     if (in_array($parentID, $exclude)) return false;
  228.     else {
  229.         $before_link = stripslashes(get_option('next_page__before_parent_link'));
  230.         $linkurl = get_permalink($parentID);
  231.         $title = get_the_title($parentID);
  232.         $linktext = get_option('next_page__parent_link_text');
  233.         if (strpos($linktext, '%title%') !== false)
  234.             $linktext = str_replace('%title%', $title, $linktext);
  235.         $after_link = stripslashes(get_option('next_page__after_parent_link'));
  236.        
  237.         $link = $before_link . '<a href="' . $linkurl . '" title="' . $title . '">' . $linktext . '</a>' . $after_link;
  238.         return $link;
  239.     }
  240. }
  241.  
  242. // template tags
  243. function previous_link() {
  244.     return prev_page();
  245. }
  246.  
  247. function next_link() {
  248.     return next_page();
  249. }
  250.  
  251. function parent_link() {
  252.     return parent_page();
  253. }
  254.  
  255. // shortcodes
  256. add_shortcode('previous', 'prev_page');
  257. add_shortcode('next', 'next_page');
  258. add_shortcode('parent', 'parent_page');
  259. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement