<?php

/*

Plugin Name: Sub page navigation Widget
Description: You can add this widget to sidebars on pages to show all sub pages of the current one.
Plugin URI: http://dennishoppe.de/wordpress-plugins/sub-page-navigation
Version: 1.0.1
Author: Dennis Hoppe
Author URI: http://DennisHoppe.de

*/


If (!Class_Exists('widget_sub_page_navigation')){
Class widget_sub_page_navigation Extends WP_Widget {
  var $text_domain;
  
  Function widget_sub_page_navigation(){
    // Get ready to translate
    $this->Load_TextDomain();

    // Setup the Widget data    
    $this->WP_Widget ( False, $this->t('Sub pages'),
                       Array('description' => $this->t('You can add this widget to sidebars on pages to show all sub pages of the current one.')));

    // Register as Widget
    Add_Action ('widgets_init', Array($this, 'Register'));
  }
  
  Function Register(){
    Register_Widget(get_class($this));
  }
  
  Function Load_TextDomain(){
    $this->text_domain = get_class($this);
    load_textdomain ($this->text_domain, DirName(__FILE__).'/language/'.get_locale().'.mo');
  }
  
  Function t ($text, $context = ''){
    // Translates the string $text
    If ($context == '')
      return __($text, $this->text_domain);
    Else
      return _x($text, $context, $this->text_domain);
  }
  
  Function widget ($widget_args, $settings){
    // if this isn't a page we bail out.
    If ( !is_page() ) return False;
    
    Global $post;
    If ($post->post_parent != 0)
      $parent = get_post($post->post_parent);
    Else
      $parent = False;

    // predefine settings
    $settings = Array_Merge( Array( 'title' => $this->t('Navigation'),
                                    'sortby' => 'menu_order, post_title',
                                    'exclude' => '' ),
                             $settings );
  
    
    // Default Args for selecting sub pages
    $page_args = Array( 'title_li' => '',
                        'child_of' => $post->ID,
                        'sort_column' => $settings['sortby'],
                        'exclude'  => $settings['exclude'], 
                        'depth'    => 1,
                        'echo'     => False );

    If ($page_listing = wp_list_pages($page_args)){
      // There are some sub pages
      If ($settings['replace_widget_title'])
        $settings['title'] = $post->post_title;
    }
    Else {
      // there are no sub pages we try to show all pages in the same depth level.
      $page_args['child_of'] = ($parent ? $parent->ID : 0);
      
      // If the parent page is a real page its title will be our widget title
      If ($parent && $settings['replace_widget_title'])
        $settings['title'] = $parent->post_title;
      
      // Read the subpages again
      $page_listing = wp_list_pages($page_args);
    }
    
    // if there are no sub pages we bail out.
    If ( !$page_listing ) return False;
    
    // Widget output    
    Echo $widget_args['before_widget'];
    
      // Widget title
      If (!$settings['hide_widget_title'])
        Echo $widget_args['before_title'] . $settings['title'] . $widget_args['after_title'];
      
      // output Page listing
      Echo '<ul>';
        Echo $page_listing;
        If (!$settings['hide_upward_link'] && $parent)
          Echo '<li class="upward"><a href='.get_permalink($parent->ID).'>'.$parent->post_title.'</a></li>';
      Echo '</ul>';
    
    // Widget bottom  
    Echo $widget_args['after_widget'];
  }
 
  Function form ($settings){
    // Show Form: ?>
    <p><?php Echo $this->t('Title:'); ?> <input type="text" name="<?php  Echo $this->get_field_name('title') ?>" value="<?php Echo $settings['title'] ?>" /></p>
    <p><input type="checkbox" value="yes" name="<?php Echo $this->get_field_name('hide_widget_title') ?>"<?php checked( $settings['hide_widget_title'], 'yes' ); ?>/> <?php echo $this->t('Hide the widget title.') ?></p>
    <p><input type="checkbox" value="yes" name="<?php Echo $this->get_field_name('replace_widget_title') ?>"<?php checked( $settings['replace_widget_title'], 'yes' ); ?>/> <?php echo $this->t('Replace the widget title with the title of the parent page if possible.') ?></p>
    <p><?php _e( 'Sort by:' ); ?> <select name="<?php Echo $this->get_field_name('sortby'); ?>">
      <option value="menu_order"<?php selected( $settings['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
      <option value="post_title"<?php selected( $settings['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
      <option value="ID"<?php selected( $settings['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
    </select></p>
    <p><?php _e( 'Exclude:' ); ?> <input type="text" value="<?php echo $settings['exclude']; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" /><br />
    <small><?php _e( 'Page IDs, separated by commas.' ); ?></small></p>
    <p><input type="checkbox" value="yes" name="<?php Echo $this->get_field_name('hide_upward_link') ?>"<?php checked( $settings['hide_upward_link'], 'yes' ); ?>/> <?php Echo $this->t('Hide link to parant page.') ?></p>
    
    <?php
  }
 
  Function update ($new_settings, $old_settings){
    return $new_settings;
  }
  
  
  
} /* End of Class */
New widget_sub_page_navigation();
} /* End of If-Class-Exists-Condition */
/* End of File */