MenuWidget.php
4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?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> (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
}
}
?>