PluginProbe
Menu In Post / 1.2.6
Menu In Post v1.2.6
trunk 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.4.0 1.4.1 1.5.0
menu-in-post / menu-in-post.php

menu-in-post.php in Menu In Post 1.2.6, at menu-in-post.php

365 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: Menu In Post
4 * Description: A simple but flexible plugin to add menus to a post or page.
5 * Author: linux4me2
6 * Author URI: https://profiles.wordpress.org/linux4me2
7 * Text Domain: menu-in-post
8 * Version: 1.2.6
9 * License: GPL3
10 * License URI: https://www.gnu.org/licenses/gpl-3.0-standalone.html
11 */
12
13 defined('ABSPATH') or die('No direct access.');
14
15 define('MENUINPOST_PLUGIN', __FILE__);
16 define('MENUINPOST_PLUGIN_DIR', untrailingslashit(dirname(MENUINPOST_PLUGIN)));
17
18 if (is_admin()) {
19 include_once MENUINPOST_PLUGIN_DIR . '/admin/help-tabs.php';
20 include_once MENUINPOST_PLUGIN_DIR . '/admin/admin.php';
21 }
22
23 add_shortcode('menu_in_post_menu', 'outputMenuInPostMenu');
24
25 /**
26 * Initiates the output of a Menu In Post Menu.
27 *
28 * @param array $atts The attributes of the menu.
29 *
30 * @return Returns the menu via wp_nav_menu()
31 */
32 function outputMenuInPostMenu($atts = array())
33 {
34 if (isset($atts['menu'])) {
35 $menu = absint($atts['menu']);
36 } else {
37 $menu = 0;
38 }
39 if ($menu == 0) {
40 return fallbackMenuInPost();
41 } else {
42 $args = array(
43 'menu'=>$menu,
44 'fallback_cb'=>'fallbackMenuInPost',
45 'echo'=>false
46 );
47 }
48 /*
49 If menu_id is empty, don't pass a value, and the menu slug with an
50 incremented value added will be used.
51
52 If container_class is empty, don't pass a value and
53 'menu-{menu slug}-container' will be used.
54 */
55 $defaults = array(
56 'menu_class'=>'menu',
57 'menu_id'=>'',
58 'container'=>'div',
59 'container_class'=>'',
60 'container_id'=>'',
61 'style'=>'list',
62 'placeholder_text'=>esc_html(__('Select...', 'menu-in-post')),
63 'append_to_url'=>'',
64 'depth'=>0
65 );
66 foreach ($defaults as $att=>$default) {
67 switch($att) {
68 case 'depth':
69 if (isset($atts[$att])) {
70 $passed_depth = absint($atts[$att]);
71 if ($passed_depth > 0) {
72 $args['depth'] = $passed_depth;
73 }
74 } else {
75 $atts['depth'] = $default;
76 }
77 break;
78 // These should be only strings.
79 default:
80 if (isset($atts[$att])) {
81 $passed_att = sanitize_text_field($atts[$att]);
82 if ($passed_att != '') {
83 $args[$att] = $passed_att;
84 }
85 } else {
86 $atts[$att] = $default;
87 }
88 }
89 }
90 if ($atts['style'] == 'dropdown') {
91 $select = '<select class="mip-drop-nav"';
92 if ($atts['menu_id'] != '') {
93 $select .= ' id="' . $args['menu_id'] . '"';
94 }
95 $select .= '>';
96 $args['items_wrap'] = $select . '<option value="#">' .
97 $atts['placeholder_text'] . '</option>%3$s</select>';
98 $args['walker'] = new MIPWalkerNavMenuDropdownBuilder();
99 } else {
100 if ($atts['append_to_url'] != '') {
101 $args['walker'] = new MIPWalkerNavMenuListBuilder();
102 }
103 }
104 if ($atts['append_to_url'] != '') {
105 $args['append_to_url'] = $atts['append_to_url'];
106 }
107 return wp_nav_menu($args);
108 }
109
110 /**
111 * Menu In Post fallback function.
112 *
113 * @return Nothing
114 */
115 function fallbackMenuInPost()
116 {
117 return;
118 }
119
120 add_action('wp_enqueue_scripts', 'enqueueMenuInPostFrontEndJS');
121
122 /**
123 * Selectively enqueues the JavaScript for Menu In Post
124 *
125 * @return Nothing, but it runs wp_enqueue_script().
126 */
127 function enqueueMenuInPostFrontEndJS()
128 {
129 $options = get_option(
130 'mip_options',
131 array(
132 'miploadjs' => 'always',
133 'miponlypages' => '',
134 'mipminimizejs' => 'yes'
135 )
136 );
137
138 $load = false;
139 $loadjs = $options['miploadjs'];
140 if ($options['mipminimizejs'] === 'yes') {
141 $file = 'main-min.js';
142 } else {
143 $file = 'main.js';
144 }
145
146 switch($loadjs) {
147 case 'always':
148 $load = true;
149 break;
150 case 'onlypages':
151 $pagestr = trim(str_replace(' ', '', $options['miponlypages']));
152 if ($pagestr !== '') {
153 $pages = explode(',', $pagestr);
154 if (is_array($pages)) {
155 $pageid = get_queried_object_id();
156 if (in_array($pageid, $pages)) {
157 $load = true;
158 }
159 }
160 }
161 break;
162 }
163
164 if ($load === true) {
165 wp_enqueue_script(
166 'menu_in_post_frontend_script',
167 plugins_url('js/' . $file, __FILE__),
168 array('jquery')
169 );
170 }
171 }
172
173 class MIPWalkerNavMenuDropdownBuilder extends Walker_Nav_Menu
174 {
175 /**
176 * Starts the list before the elements are added.
177 *
178 * @param string $output Used to append additional content (passed by ref).
179 * @param int $depth Depth of menu item. Used for padding.
180 * @param stdClass $args An object of wp_nav_menu() arguments.
181 *
182 * @return Nothing here.
183 */
184 function start_lvl(&$output, $depth = 0, $args = null)
185 {
186 }
187
188 /**
189 * Ends the list of after the elements are added.
190 *
191 * @param string $output Used to append additional content (passed by ref).
192 * @param int $depth Depth of menu item. Used for padding.
193 * @param stdClass $args An object of wp_nav_menu() arguments.
194 *
195 * @return Nothing here.
196 */
197 function end_lvl(&$output, $depth = 0, $args = null)
198 {
199 }
200
201 /**
202 * Starts the element output.
203 *
204 * @param string $output Appends additional content (passed by reference).
205 * @param WP_Post $item Menu item data object.
206 * @param int $depth Depth of menu item. Used for padding.
207 * @param stdClass $args An object of wp_nav_menu() arguments.
208 * @param int $id The current menu item. Default 0.
209 *
210 * @return Returns the HTML output for the element.
211 */
212 function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
213 {
214 // Create each option.
215 $item_output = '';
216
217 // Add spacing to the title based on the depth.
218 $item->title = str_repeat(' - ', $depth * 1) . $item->title;
219
220 // Get the link.
221 if (!empty($args->append_to_url)) {
222 $attributes = !empty($item->url) ? ' value="' . esc_attr($item->url) .
223 $args->append_to_url .'"' : '';
224 } else {
225 $attributes = !empty($item->url) ? ' value="' . esc_attr($item->url) .
226 '"' : '';
227 }
228
229 // Add selected attribute if menu item is the current page.
230 if ($item->current) {
231 $attributes .= ' selected="selected"';
232 }
233
234 // Add the HTML.
235 $item_output .= '<option'. $attributes .'>';
236 $item_output .= apply_filters('the_title_attribute', $item->title);
237
238 // Add the new item to the output string.
239 $output .= $item_output;
240 }
241
242 /**
243 * Ends the element output, if needed.
244 *
245 * @param string $output Used to append additional content (passed by ref).
246 * @param WP_Post $item Menu item data object. Not used.
247 * @param int $depth Depth of page. Not Used.
248 * @param stdClass $args An object of wp_nav_menu() arguments.
249 *
250 * @return Returns the closing tag, if needed.
251 */
252 function end_el(&$output, $item, $depth = 0, $args = null)
253 {
254 // Close the item.
255 $output .= "</option>\n";
256
257 }
258
259 }
260
261 class MIPWalkerNavMenuListBuilder extends Walker_Nav_Menu
262 {
263 /**
264 * Starts the element output.
265 *
266 * @param string $output Appends additional content (passed by reference).
267 * @param WP_Post $item Menu item data object.
268 * @param int $depth Depth of menu item. Used for padding.
269 * @param stdClass $args An object of wp_nav_menu() arguments.
270 * @param int $id The current menu item. Default 0.
271 *
272 * @return Returns the HTML output for the element.
273 */
274 function start_el(&$output, $item, $depth = 0, $args = null, $id = 0)
275 {
276 if (isset($args->item_spacing) && 'discard' === $args->item_spacing) {
277 $t = '';
278 $n = '';
279 } else {
280 $t = "\t";
281 $n = "\n";
282 }
283 $indent = ( $depth ) ? str_repeat($t, $depth) : '';
284
285 $classes = empty($item->classes) ? array() : (array) $item->classes;
286 $classes[] = 'menu-item-' . $item->ID;
287
288 $args = apply_filters('nav_menu_item_args', $args, $item, $depth);
289
290 $class_names = implode(
291 ' ',
292 apply_filters(
293 'nav_menu_css_class',
294 array_filter($classes),
295 $item,
296 $args,
297 $depth
298 )
299 );
300 $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';
301
302 $id = apply_filters(
303 'nav_menu_item_id',
304 'menu-item-' . $item->ID,
305 $item,
306 $args,
307 $depth
308 );
309 $id = $id ? ' id="' . esc_attr($id) . '"' : '';
310
311 $output .= $indent . '<li' . $id . $class_names . '>';
312
313 $atts = array();
314 $atts['title'] = !empty($item->attr_title) ? $item->attr_title : '';
315 $atts['target'] = !empty($item->target) ? $item->target : '';
316 if ('_blank' === $item->target && empty($item->xfn) ) {
317 $atts['rel'] = 'noopener';
318 } else {
319 $atts['rel'] = $item->xfn;
320 }
321 if (!empty($args->append_to_url)) {
322 $atts['href'] = !empty($item->url) ? $item->url .
323 $args->append_to_url : '';
324 } else {
325 $atts['href'] = !empty($item->url) ? $item->url : '';
326 }
327 $atts['aria-current'] = $item->current ? 'page' : '';
328
329 $atts = apply_filters(
330 'nav_menu_link_attributes',
331 $atts,
332 $item,
333 $args,
334 $depth
335 );
336
337 $attributes = '';
338 foreach ( $atts as $attr => $value ) {
339 if (is_scalar($value) && '' !== $value && false !== $value ) {
340 $value = ( 'href' === $attr ) ? esc_url($value) : esc_attr($value);
341 $attributes .= ' ' . $attr . '="' . $value . '"';
342 }
343 }
344
345 $title = apply_filters('the_title', $item->title, $item->ID);
346
347 $title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth);
348
349 $item_output = $args->before;
350 $item_output .= '<a' . $attributes . '>';
351 $item_output .= $args->link_before . $title . $args->link_after;
352 $item_output .= '</a>';
353 $item_output .= $args->after;
354
355 $output .= apply_filters(
356 'walker_nav_menu_start_el',
357 $item_output,
358 $item,
359 $depth,
360 $args
361 );
362 }
363 }
364 ?>
365