-
Notifications
You must be signed in to change notification settings - Fork 13
/
gecka-submenu.class.php
232 lines (175 loc) · 7.01 KB
/
gecka-submenu.class.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* Main Plugin class
* @author lox
*
*/
class Gecka_Submenu {
const Domain = 'gecka-submenu';
/**
* Constructor
*/
public function __construct() {
load_plugin_textdomain(self::Domain, false, dirname( plugin_basename( __FILE__ ) ) . '/languages');
$this->Options = get_option( self::Domain . '_settings');
add_action('init', array($this, 'init') );
// load widgets
add_action('widgets_init', array($this, 'widgetsInit') );
// filter to show portions of nav menus
add_filter('wp_get_nav_menu_items', array($this, 'wp_get_nav_menu_items' ), 15, 3);
// filter to show the description of menu items if asked
add_filter('walker_nav_menu_start_el', array($this, 'walker_nav_menu_start_el'), 10, 4);
if( !is_admin() ) {
require_once GKSM_PATH . '/models/Shortcodes.php';
new Gecka_Submenu_Shortcodes();
}
else {
if( get_option( self::Domain . '-pro-notice', '0') == 1 )
add_action('admin_notices', array( $this, 'admin_notices') );
add_action('wp_ajax_gecka_submenu_dismiss_notice', array($this, 'dismiss_notice'));
add_action('admin_head', array($this, 'dismiss_notice_js') );
}
// Nav menu hacks
require_once GKSM_PATH . '/models/NavMenuHacks.php';
new Gecka_Submenu_NavMenuHacks();
}
public function init()
{
// remove a silly 2010 theme filter
remove_filter( 'wp_page_menu_args', 'twentyten_page_menu_args' );
}
/**
* Init widgets
*/
public function widgetsInit ()
{
// Check for the required plugin functions. This will prevent fatal
// errors occurring when you deactivate the dynamic-sidebar plugin.
if ( !function_exists('register_widget') )
return;
// Submenu widget
include_once dirname(__FILE__) . '/widgets/Custom-menu.php';
register_widget("GKSM_Widget_Custom_Menu");
}
/**
* Retrieve child navmenu items from list of menus items matching menu ID.
*
* @param int $menu_id Menu Item ID.
* @param array $items List of nav-menu items objects.
* @return array
*/
public function wp_get_nav_menu_items($items, $menu, $args) {
global $GKSM_ID, $GKSM_MENUID;
if( isset($GKSM_ID) && $GKSM_ID
&& isset($GKSM_MENUID) && $GKSM_MENUID==$menu->term_id ) {
$items = $this->wp_nav_menu_items_children( $GKSM_ID, $items );
}
return $items;
}
public function wp_nav_menu_items_children($item_id, $items) {
$item_list = array();
foreach ( (array) $items as $item ) {
if ( $item->menu_item_parent == $item_id ) {
$item_list[] = $item;
$children = $this->wp_nav_menu_items_children($item->db_id, $items);
if ( $children ) {
$item_list = array_merge($item_list, $children);
}
}
}
return $item_list;
}
/**
* Filter to show nav-menu items description
*
* @param $item_output
* @param $item
* @param $depth
* @param $args
* @return $item_output
*/
public function walker_nav_menu_start_el ($item_output, $item, $depth, $args) {
$after_link = '';
if(isset($args->show_description) && $args->show_description) {
$after_link = !empty( $item->description ) ? '<span class="description">' . esc_html( $item->description) .'</span>' : '';
}
$after_link = apply_filters('nav_menu_item_after_link', $after_link, $item, $args, $depth);
if($args->show_description == 'into_link') $after_link = $after_link . '</a>';
else $after_link = '</a>' . $after_link;
$item_output = str_replace('</a>', $after_link, $item_output);
$before_link = '';
if(isset($args->thumbnail) && $args->thumbnail) {
$size = $args->thumbnail;
if( strpos($size, ',') ) $size = explode(',',$size);
$before_link = get_the_post_thumbnail( $item->object_id, $size );
}
$before_link = apply_filters('nav_menu_item_before_link', $before_link, $item, $args, $depth);
$item_output = str_replace('<a', $before_link.'<a', $item_output);
return $item_output;
}
function admin_notices() {
echo '<div class="updated" id="gecka_submenu_notice"><div style="float: right; margin-top: 3px"><a href="#" onclick="gecka_submenu_dismiss_notice(); return false;">Dismiss</a></div>';
echo '<p>' . __('You are using Gecka Submenu.', self::Domain ) . ' '. __('<a href="http://www.gecka-apps.com" target="_blank">Discover the pro version</a> to get the most out of the Wordpress menu system.', self::Domain ). "</p></div>";
}
function dismiss_notice () {
update_option( 'gecka-submenu-pro-notice', '0');
die();
}
function dismiss_notice_js () {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
gecka_submenu_dismiss_notice = function () {
var data = {
action: 'gecka_submenu_dismiss_notice'
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
$('#gecka_submenu_notice').hide('slow');
});
}
});
</script>
<?php
}
}
/**
* Walker to show menu items as a select box, used by widgets
*/
if(!class_exists('Walker_Nav_Menu_DropDown') && is_admin() ) {
class Walker_Nav_Menu_DropDown extends Walker {
/**
* @see Walker::$tree_type
* @since 3.0.0
* @var string
*/
var $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* @see Walker::$db_fields
* @since 3.0.0
* @todo Decouple this.
* @var array
*/
var $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
/**
* @see Walker::start_el()
* @since 3.0.0
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $item Menu item data object.
* @param int $depth Depth of menu item. Used for padding.
* @param int $current_page Menu item ID.
* @param object $args
*/
function start_el(&$output, $item, $depth, $args) {
global $wp_query;
$pad = str_repeat(' ', $depth * 3);
$output .= "\t<option class=\"level-$depth\" value=\"".esc_attr($item->ID)."\"";
if ( (int)$item->ID === (int)$args['selected'] )
$output .= ' selected="selected"';
$output .= '>';
$output .= esc_html($pad . apply_filters( 'the_title', $item->title ));
$output .= "</option>\n";
}
}
}