Advertisement
alchymyth

mod - simple_section_nav.php

Jun 23rd, 2011
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.62 KB | None | 0 0
  1. <?php
  2. /**
  3.  Plugin Name: Simple Section Navigation Widget
  4.  Plugin URI: http://www.get10up.com/plugins/simple-section-navigation/
  5.  Description: Adds a <strong>widget</strong> for <strong>section (or top level page) based navigation</strong>... essential for <strong>CMS</strong> implementations! The <strong>title of the widget is the top level page</strong> within the current page hierarchy. Shows all page siblings (except on the top level page), all parents and grandparents (and higher), the siblings of all parents and grandparents (up to top level page), and any immediate children of the current page. Can also be called by a function inside template files. May <strong>exclude any pages or sections</strong>. Uses standard WordPress navigation classes for easy styling.
  6.  Version: 2.1
  7.  Author: Jake Goldman (10up)
  8.  Author URI: http://www.get10up.com
  9.  
  10.     Plugin: Copyright 2011 10up  (email : jake@get10up.com)
  11.  
  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.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  25. */
  26.  
  27. class SimpleSectionNav extends WP_Widget
  28. {
  29.     function SimpleSectionNav() {
  30.         $widget_ops = array('classname' => 'simple-section-nav', 'description' => __( "Shows page ancestory (parents, grandparents, etc), siblings of ancestory and current page, and immediate children of the current page beneath the current top level page.") );
  31.         $this->WP_Widget('simple-section-nav', __('Simple Section Navigation'), $widget_ops);
  32.     }
  33.  
  34.     function widget($args, $instance) {
  35.         extract($args);
  36.         global $post;
  37.        
  38.         if ( is_search() || is_404() ) return false; //doesn't apply to search or 404 page
  39.         if ( is_front_page() && !$instance['show_on_home'] ) return false;  //if we're on the front page and we haven't chosen to show this anyways, leave
  40.        
  41.         if ( is_page() ) {
  42.             if ( isset($post) && is_object($post) ) _get_post_ancestors($post);   //workaround for occassional problems
  43.         } else {
  44.             if ($post_page = get_option("page_for_posts")) $post = get_page($post_page); //treat the posts page as the current page if applicable
  45.             elseif ($instance['show_on_home']) $sub_front_page = true;  //if want to show on home, and home is the posts page
  46.             else return false;
  47.         }
  48.        
  49.         if ( is_front_page() || isset($sub_front_page )) {
  50.             echo $before_widget.$before_title.get_bloginfo('name').$after_title."<ul>";
  51.             $children = wp_list_pages(array( 'title_li' => '', 'depth' => 1, 'sort_column' => $instance['sort_by'], 'exclude' => $instance['exclude'], 'echo' => false ));
  52.             echo apply_filters('simple_section_page_list',$children);
  53.             echo "</ul>".$after_widget;
  54.             return true;
  55.         }
  56.        
  57.         $exclude_list = $instance['exclude'];
  58.         $excluded = explode(',', $exclude_list); //convert list of excluded pages to array
  59.         if ( in_array($post->ID,$excluded) && $instance['hide_on_excluded'] ) return false; //if on excluded page, and setup to hide on excluded pages
  60.        
  61.         $post_ancestors = ( isset($post->ancestors) ) ? $post->ancestors : get_post_ancestors($post); //get the current page's ancestors either from existing value or by executing function
  62.         $top_page = $post_ancestors ? end($post_ancestors) : $post->ID; //get the top page id
  63.        
  64.         $thedepth = 0; //initialize default variables
  65.        
  66.         if( !$instance['show_all'] )
  67.         {  
  68.             $ancestors_me = implode( ',', $post_ancestors ) . ',' . $post->ID;
  69.            
  70.             //exclude pages not in direct hierarchy
  71.             foreach ($post_ancestors as $anc_id)
  72.             {
  73.                 if ( in_array($anc_id,$excluded) && $instance['hide_on_excluded'] ) return false; //if ancestor excluded, and hide on excluded, leave
  74.                
  75.                 $pageset = get_pages(array( 'child_of' => $anc_id, 'parent' => $anc_id, 'exclude' => $ancestors_me ));
  76.                 foreach ($pageset as $page) {
  77.                     $excludeset = get_pages(array( 'child_of' => $page->ID, 'parent' => $page->ID ));
  78.                     foreach ($excludeset as $expage) { $exclude_list .= ',' . $expage->ID; }
  79.                 }
  80.             }
  81.            
  82.             $thedepth = min(2,count($post_ancestors)+1); //limit the levels to 2 generations; prevents improper grandchildren from showing
  83.         }      
  84.        
  85.         $children = wp_list_pages(array( 'title_li' => '', 'echo' => 0, 'depth' => $thedepth, 'child_of' => $top_page, 'sort_column' => $instance['sort_by'], 'exclude' => $exclude_list ));    //get the list of pages, including only those in our page list
  86.         if( !$children && !$instance['show_empty'] ) return false;  //if there are no pages in this section, and use hasnt chosen to display widget anyways, leave the function
  87.        
  88.         $sect_title = ( $instance['title'] ) ? apply_filters( 'the_title', $instance['title'] ) : apply_filters( 'the_title', get_the_title($top_page), $top_page );
  89.         $sect_title = apply_filters( 'simple_section_nav_title', $sect_title );
  90.         if ($instance['a_heading']) {
  91.             $headclass = ( $post->ID == $top_page ) ? "current_page_item" : "current_page_ancestor";
  92.             if ( $post->post_parent == $top_page ) $headclass .= " current_page_parent";
  93.             $sect_title = '<a href="' . get_page_link($top_page) . '" id="toppage-' . $top_page . '" class="' . $headclass . '">' . $sect_title . '</a>';  
  94.         }
  95.        
  96.         echo $before_widget.$before_title.$sect_title.$after_title."<ul>";
  97.         echo apply_filters( 'simple_section_page_list', $children );
  98.         echo "</ul>".$after_widget;
  99.     }
  100.  
  101.     function update($new_instance, $old_instance) {
  102.         $instance = $old_instance;
  103.         $instance['title'] = trim( strip_tags( $new_instance['title'] ) );
  104.         $instance['show_all'] = ( $new_instance['show_all'] ) ? true : false;
  105.         $instance['exclude'] = str_replace( " ", "", strip_tags($new_instance['exclude']) ); //remove spaces from list
  106.         $instance['hide_on_excluded'] = ( $new_instance['hide_on_excluded'] ) ? true : false;
  107.         $instance['show_on_home'] = ( $new_instance['show_on_home'] ) ? true : false;
  108.         $instance['show_empty'] = ( $new_instance['show_empty'] ) ? true : false;
  109.         $instance['sort_by'] = ( in_array( $new_instance['sort_by'], array( 'post_title', 'menu_order', 'ID' ) ) ) ? $new_instance['sort_by'] : 'menu_order';
  110.         $instance['a_heading'] = ( $new_instance['a_heading'] ) ? true : false;
  111.         return $instance;
  112.     }
  113.  
  114.     function form($instance){
  115.         //Defaults
  116.         $instance = wp_parse_args( (array) $instance, array( 'show_all' => false, 'exclude' => '', 'hide_on_excluded' => true, 'show_on_home' => false, 'show_empty' => false, 'sort_by' => 'menu_order', 'a_heading' => false, 'title' => '' ));
  117.     ?>
  118.         <p>
  119.             <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Override Title:'); ?></label>
  120.             <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title']); ?>" size="7" class="widefat" /><br />
  121.             <small>Leave blank to use top level page title.</small>        
  122.         </p>
  123.         <p>
  124.             <label for="<?php echo $this->get_field_id('sort_by'); ?>"><?php _e('Sort pages by:'); ?></label>
  125.             <select name="<?php echo $this->get_field_name('sort_by'); ?>" id="<?php echo $this->get_field_id('sort_by'); ?>" class="widefat">
  126.                 <option value="post_title"<?php selected( $instance['sort_by'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
  127.                 <option value="menu_order"<?php selected( $instance['sort_by'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
  128.                 <option value="ID"<?php selected( $instance['sort_by'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  129.             </select>
  130.         </p>
  131.         <p>
  132.             <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e('Exclude:'); ?></label>
  133.             <input type="text" id="<?php echo $this->get_field_id('exclude'); ?>" name="<?php echo $this->get_field_name('exclude'); ?>" value="<?php echo esc_attr($instance['exclude']); ?>" size="7" class="widefat" /><br />
  134.             <small>Page IDs, separated by commas.</small>          
  135.         </p>
  136.         <p>
  137.             <input class="checkbox" type="checkbox" <?php checked($instance['show_on_home']); ?> id="<?php echo $this->get_field_id('show_on_home'); ?>" name="<?php echo $this->get_field_name('show_on_home'); ?>" />
  138.             <label for="<?php echo $this->get_field_id('show_on_home'); ?>"><?php _e('Show on home page'); ?></label><br />
  139.             <input class="checkbox" type="checkbox" <?php checked($instance['a_heading']); ?> id="<?php echo $this->get_field_id('a_heading'); ?>" name="<?php echo $this->get_field_name('a_heading'); ?>" />
  140.             <label for="<?php echo $this->get_field_id('a_heading'); ?>"><?php _e('Link heading (top level page)'); ?></label><br />
  141.             <input class="checkbox" type="checkbox" <?php checked($instance['show_all']); ?> id="<?php echo $this->get_field_id('show_all'); ?>" name="<?php echo $this->get_field_name('show_all'); ?>" />
  142.             <label for="<?php echo $this->get_field_id('show_all'); ?>"><?php _e('Show all pages in section'); ?></label><br />
  143.             <input class="checkbox" type="checkbox" <?php checked($instance['show_empty']); ?> id="<?php echo $this->get_field_id('show_empty'); ?>" name="<?php echo $this->get_field_name('show_empty'); ?>" />
  144.             <label for="<?php echo $this->get_field_id('show_empty'); ?>"><?php _e('Output even if empty section'); ?></label><br />
  145.             <input class="checkbox" type="checkbox" <?php checked($instance['hide_on_excluded']); ?> id="<?php echo $this->get_field_id('hide_on_excluded'); ?>" name="<?php echo $this->get_field_name('hide_on_excluded'); ?>" />
  146.             <label for="<?php echo $this->get_field_id('hide_on_excluded'); ?>"><?php _e('No nav on excluded pages'); ?></label>           
  147.         </p>
  148.         <p><small><a href="http://www.get10up.com/plugins/simple-section-navigation/" target="_blank">Help &amp; Support</a></small></p>
  149.     <?php
  150.     }
  151. }
  152.  
  153. add_action('widgets_init', create_function('', 'return register_widget("SimpleSectionNav");'));
  154.  
  155. /**
  156.  * Display section based navigation
  157.  *
  158.  * Arguments include: 'show_all' (boolean), 'exclude' (comma delimited list of page IDs),
  159.  * 'show_on_home' (boolean), 'show_empty' (boolean), sort_by (any valid page sort string),
  160.  * 'a_heading' (boolean), 'before_widget' (string), 'after_widget' (strong)
  161.  *
  162.  * @param array|string $args Optional. Override default arguments.
  163.  * @param NULL deprecated - so pre 2.0 implementations don't break site
  164.  * @return string HTML content, if not displaying.
  165.  */
  166. function simple_section_nav($args='',$deprecated=NULL) {
  167.     if ( !is_null($deprecated) ) {
  168.         echo 'The section navigation has been upgrade from 1.x to 2.0; this template needs to be updated to reflect major changes to the plug-in.';
  169.         return false;
  170.     }
  171.     $args = wp_parse_args($args, array(
  172.         'show_all' => false,
  173.         'exclude' => '',
  174.         'hide_on_excluded' => true,
  175.         'show_on_home' => false,
  176.         'show_empty' => false,
  177.         'sort_by' => 'menu_order',
  178.         'a_heading' => false,
  179.         'before_widget'=>'<div>',
  180.         'after_widget'=>'</div>',
  181.         'before_title'=>'<h2 class="widgettitle">',
  182.         'after_title'=>'</h2>',
  183.         'title' => ''
  184.     )); //defaults
  185.     the_widget('SimpleSectionNav',$args,array('before_widget'=>$args['before_widget'],'after_widget'=>$args['after_widget'],'before_title'=>$args['before_title'],'after_title'=>$args['after_title']));
  186. }
  187.  
  188. //********************//
  189. //upgrade from pre 2.0//
  190. //********************//
  191.  
  192. function simple_section_nav_activate()
  193. {
  194.     if (get_option('ssn_sortby') === false) return false;   //if not upgrading, leave
  195.    
  196.     $show_all = (get_option('ssn_show_all')) ? 1 : 0;
  197.     $exclude =  str_replace(" ","",get_option('ssn_exclude'));
  198.     $hide_on_excluded = (get_option('ssn_hide_on_excluded')) ? 1 : 0;
  199.     $show_on_home = (get_option('ssn_show_on_home')) ? 1 : 0;
  200.     $show_empty = (get_option('ssn_show_empty')) ? 1 : 0;
  201.     $a_heading = (get_option('ssn_a_heading')) ? 1 : 0;
  202.    
  203.     $settings = array('show_all'=>$show_all, 'exclude'=>$exclude, 'hide_on_excluded'=>$hide_on_excluded,'show_on_home'=>$show_on_home,'show_empty'=>$show_empty,'sort_by'=>get_option('ssn_sortby'),'a_heading'=>$a_heading);
  204.     wp_convert_widget_settings('simple-section-nav','widget_simple-section-nav',$settings);
  205.        
  206.     //delete old settings ... done supporting 1.x
  207.     delete_option('ssn_show_all');
  208.     delete_option('ssn_exclude');
  209.     delete_option('ssn_hide_on_excluded');
  210.     delete_option('ssn_show_on_home');
  211.     delete_option('ssn_show_empty');
  212.     delete_option('ssn_sortby');
  213.     delete_option('ssn_a_heading');
  214. }
  215. register_activation_hook(__FILE__, 'simple_section_nav_activate');
  216. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement