| 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.3 |
| 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 |
// Get the target, if any. |
| 230 |
if (!empty($item->target)) { |
| 231 |
$attributes .= ' data-target="' . esc_attr($item->target) . '"'; |
| 232 |
} |
| 233 |
|
| 234 |
// Add selected attribute if menu item is the current page. |
| 235 |
if ($item->current) { |
| 236 |
$attributes .= ' selected="selected"'; |
| 237 |
} |
| 238 |
|
| 239 |
// Add the HTML. |
| 240 |
$item_output .= '<option'. $attributes .'>'; |
| 241 |
$item_output .= apply_filters('the_title_attribute', $item->title); |
| 242 |
|
| 243 |
// Add the new item to the output string. |
| 244 |
$output .= $item_output; |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Ends the element output, if needed. |
| 249 |
* |
| 250 |
* @param string $output Used to append additional content (passed by ref). |
| 251 |
* @param WP_Post $item Menu item data object. Not used. |
| 252 |
* @param int $depth Depth of page. Not Used. |
| 253 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 254 |
* |
| 255 |
* @return Returns the closing tag, if needed. |
| 256 |
*/ |
| 257 |
function end_el(&$output, $item, $depth = 0, $args = null) |
| 258 |
{ |
| 259 |
// Close the item. |
| 260 |
$output .= "</option>\n"; |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
class MIPWalkerNavMenuListBuilder extends Walker_Nav_Menu |
| 267 |
{ |
| 268 |
/** |
| 269 |
* Starts the element output. |
| 270 |
* |
| 271 |
* @param string $output Appends additional content (passed by reference). |
| 272 |
* @param WP_Post $item Menu item data object. |
| 273 |
* @param int $depth Depth of menu item. Used for padding. |
| 274 |
* @param stdClass $args An object of wp_nav_menu() arguments. |
| 275 |
* @param int $id The current menu item. Default 0. |
| 276 |
* |
| 277 |
* @return Returns the HTML output for the element. |
| 278 |
*/ |
| 279 |
function start_el(&$output, $item, $depth = 0, $args = null, $id = 0) |
| 280 |
{ |
| 281 |
if (isset($args->item_spacing) && 'discard' === $args->item_spacing) { |
| 282 |
$t = ''; |
| 283 |
$n = ''; |
| 284 |
} else { |
| 285 |
$t = "\t"; |
| 286 |
$n = "\n"; |
| 287 |
} |
| 288 |
$indent = ( $depth ) ? str_repeat($t, $depth) : ''; |
| 289 |
|
| 290 |
$classes = empty($item->classes) ? array() : (array) $item->classes; |
| 291 |
$classes[] = 'menu-item-' . $item->ID; |
| 292 |
|
| 293 |
$args = apply_filters('nav_menu_item_args', $args, $item, $depth); |
| 294 |
|
| 295 |
$class_names = implode( |
| 296 |
' ', |
| 297 |
apply_filters( |
| 298 |
'nav_menu_css_class', |
| 299 |
array_filter($classes), |
| 300 |
$item, |
| 301 |
$args, |
| 302 |
$depth |
| 303 |
) |
| 304 |
); |
| 305 |
$class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : ''; |
| 306 |
|
| 307 |
$id = apply_filters( |
| 308 |
'nav_menu_item_id', |
| 309 |
'menu-item-' . $item->ID, |
| 310 |
$item, |
| 311 |
$args, |
| 312 |
$depth |
| 313 |
); |
| 314 |
$id = $id ? ' id="' . esc_attr($id) . '"' : ''; |
| 315 |
|
| 316 |
$output .= $indent . '<li' . $id . $class_names . '>'; |
| 317 |
|
| 318 |
$atts = array(); |
| 319 |
$atts['title'] = !empty($item->attr_title) ? $item->attr_title : ''; |
| 320 |
$atts['target'] = !empty($item->target) ? $item->target : ''; |
| 321 |
if ('_blank' === $item->target && empty($item->xfn) ) { |
| 322 |
$atts['rel'] = 'noopener'; |
| 323 |
} else { |
| 324 |
$atts['rel'] = $item->xfn; |
| 325 |
} |
| 326 |
if (!empty($args->append_to_url)) { |
| 327 |
$atts['href'] = !empty($item->url) ? $item->url . |
| 328 |
$args->append_to_url : ''; |
| 329 |
} else { |
| 330 |
$atts['href'] = !empty($item->url) ? $item->url : ''; |
| 331 |
} |
| 332 |
$atts['aria-current'] = $item->current ? 'page' : ''; |
| 333 |
|
| 334 |
$atts = apply_filters( |
| 335 |
'nav_menu_link_attributes', |
| 336 |
$atts, |
| 337 |
$item, |
| 338 |
$args, |
| 339 |
$depth |
| 340 |
); |
| 341 |
|
| 342 |
$attributes = ''; |
| 343 |
foreach ( $atts as $attr => $value ) { |
| 344 |
if (is_scalar($value) && '' !== $value && false !== $value ) { |
| 345 |
$value = ( 'href' === $attr ) ? esc_url($value) : esc_attr($value); |
| 346 |
$attributes .= ' ' . $attr . '="' . $value . '"'; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
$title = apply_filters('the_title', $item->title, $item->ID); |
| 351 |
|
| 352 |
$title = apply_filters('nav_menu_item_title', $title, $item, $args, $depth); |
| 353 |
|
| 354 |
$item_output = $args->before; |
| 355 |
$item_output .= '<a' . $attributes . '>'; |
| 356 |
$item_output .= $args->link_before . $title . $args->link_after; |
| 357 |
$item_output .= '</a>'; |
| 358 |
$item_output .= $args->after; |
| 359 |
|
| 360 |
$output .= apply_filters( |
| 361 |
'walker_nav_menu_start_el', |
| 362 |
$item_output, |
| 363 |
$item, |
| 364 |
$depth, |
| 365 |
$args |
| 366 |
); |
| 367 |
} |
| 368 |
} |
| 369 |
?> |