MenuWidget.php 4.09 KB
<?php
class MenuWidget extends WP_Widget {
    public static function init() {
        register_widget(__CLASS__);

        if (is_admin()) {
            _enqueue_script('tz-menu-widget', TzTools::tools_url('MenuWidget.js', __FILE__), Array('addEvent'));
        }
    }

    public function __construct() {
        static $options = Array(
            'classname' => ''
          , 'description' => 'Configurable menu to site hierarchy'
        );

        parent::__construct('tz-menu-widget', 'Menu', $options);
    }

    public function widget($args, $instance) {
        static $all_pages = false;

        $display_container = false;
        foreach ($instance['pages'] as $page) {
            if ($instance['inclusive'] == 0) {
                $instance['child_of'] = $page;
                _list_pages($instance); // I should probably have the before/after from args in here . . . 
                continue;
            }
    
            if (!$all_pages) {
                $all_pages = get_pages(Array('sort_column' => 'menu_order'));
            }
    
            $instance['exclude'] = '';
            $children = Array($page);
            foreach ($all_pages as $key => $wp_page) {
                if ($wp_page->post_parent == $page || in_array($wp_page->post_parent, $children)) {
                    $children[] = $wp_page->ID;
                }
    
                if (!in_array($wp_page->ID, $children)) {
                    if (!empty($instance['exclude'])) {
                        $instance['exclude'] .= ',';
                    }
                    $instance['exclude'] .= $wp_page->ID;
                }
            }

            if (!$display_container && !empty($args['before_widget'])) {
                echo $args['before_widget'];
                $display_container = true;
            }

            _list_pages($instance);
        }

        if ($display_container) {
            echo $args['after_widget'];
        }
    }

    public function update($new_instance, $old_instance) {
        $instance = $old_instance;

        $instance['pages'] = Array();

        $instance['title_li']  = strip_tags(stripslashes($new_instance['title_li']));
        $instance['pages']     = (array)$new_instance['pages']; // (int)strip_tags(stripslashes($new_instance['page']));
        $instance['depth']     = (int)strip_tags(stripslashes($new_instance['depth']));
        $instance['inclusive'] = ($new_instance['inclusive'] == 'on' ? 1 : 0);

        return $instance;
    }

    public function form($instance) {
        $instance = _parse_args((array)$instance, Array('title_li' => '', 'pages' => Array(0 => ''), 'depth' => '', 'inclusive' => ''));
        $checked  = ($instance['inclusive'] == 1 ? ' checked="checked" ' : '');

?>
<p>
  <label for="<?php echo $this->get_field_id('title_li'); ?>">Title</label>
  <br /><input type="text" id="<?php echo $this->get_field_id('title_li'); ?>" name="<?php echo $this->get_field_name('title_li'); ?>" value="<?php echo attribute_escape($instance['title_li']); ?>" />
</p>

<p class="TzMenuPageList">
  <label>Page(s)</label> <a href="#" class="TzAddMenuItem">Add Another</a>
<?php
    foreach ($instance['pages'] as $key => $page) {
        echo '<span class="TzPageContainer">';
        _dropdown_pages(Array('name' => $this->get_field_name('pages') . '[]', 'selected' => attribute_escape($instance['pages'][$key]), 'sort_column' => 'menu_order', 'echo' => 1));
        if ($key > 0) {
            echo '<a href="#" class="TzDelMenuItem">Del</a>';
        }
        echo '</span>';
    }
?>
</p>

<p>
  <label for="<?php echo $this->get_field_id('depth'); ?>">Depth</label> &nbsp;(0 for infinite)
  <br /><input type="text" size="2" maxlength="2" id="<?php echo $this->get_field_id('depth'); ?>" name="<?php echo $this->get_field_name('depth'); ?>" value="<?php echo attribute_escape($instance['depth']); ?>" />
</p>

<p>
  <input type="checkbox" id="<?php echo $this->get_field_id('inclusive'); ?>" name="<?php echo $this->get_field_name('inclusive'); ?>" <?php echo $checked; ?> />
  <label for="<?php echo $this->get_field_id('inclusive'); ?>">Show Selected (vs just children)</label>
</p>
<?php
    }
}
?>