-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsive_sidebar_menu.php
executable file
·139 lines (108 loc) · 4.2 KB
/
responsive_sidebar_menu.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
<?php
/*
* @package ANDYP - Responsive Sidebar Menus
* @author Andy Pearson <[email protected]>
* @copyright 2020 LondonParkour
*
* @wordpress-plugin
* Plugin Name: _ANDYP - Menus - Responsive Sidebar Menus
* Plugin URI: http:https://londonparkour.com
* Description: <strong>🩳SHORTCODE</strong> | <em>[andyp_responsive_menus]</em> | Either a dynamic sidebar or a select dropdown, depending on width media query
* Version: 1.0.0
* Author: Andy Pearson
* Author URI: https://londonparkour.com
* Domain Path: /languages
*/
// ┌─────────────────────────────────────────────────────────────────────────┐
// │ Register with ANDYP Plugins │
// └─────────────────────────────────────────────────────────────────────────┘
require __DIR__.'/src/acf/andyp_plugin_register.php';
class andyp_walker extends Walker_Nav_Menu {
// Displays start of an element. E.g '<li> Item Name'
// @see Walker::start_el()
function start_el(&$output, $item, $depth=0, $args=array(), $id = 0) {
$output .= '<option value="'. $item->url .'">';
$output .= $item->title;
$output .= '</option>';
}
}
class andyp_responsive_menus {
/**
* __construct
*
* @return void
*/
public function __construct(){
$this->create_shortcode();
return;
}
/**
* create_shortcode
*
* @return void
*/
public function create_shortcode(){
add_shortcode( 'andyp_responsive_menus', array( $this, 'render_shortcode' ) );
}
/**
* render_shortcode
*
* @param mixed $atts
* @param mixed $content
* @return void
*/
public function render_shortcode($atts, $content = null){
// ┌──────────────────────────────────────┐
// │ Shortcode parameters │
// └──────────────────────────────────────┘
extract(
shortcode_atts(
array(
// Menu Name
'menu' => '',
'sidebar' => '',
'category_mobile' => '',
),
$atts
)
);
if ($menu != ''){
echo '<div class="sidemenu__mobile">';
wp_nav_menu( array(
'menu' => $menu,
'items_wrap' => '<select onChange="window.location.href=this.value">%3$s</select>',
'walker' => new andyp_walker(),
'container' => '',
) );
echo '</div>';
}
if ($sidebar != ''){
echo '<div class="sidemenu__desktop">';
dynamic_sidebar( $sidebar );
echo '</div>';
}
if ($category_mobile != ''){
echo '<div class="sidemenu__mobile">';
$categories = explode(',', $category_mobile);
echo '<select onChange="window.location.href=this.value">';
echo '<option>Wiki support</option>';
foreach ($categories as $category){
$postslist = get_posts( array( 'category' => $category, 'numberposts' => -1 ) );
foreach ($postslist as $post){
echo '<option value="'.$this->make_relative($post->guid).'">' . $post->post_title . '</option>';
}
}
echo '</select>';
echo '</div>';
}
return ;
}
public function make_relative($guid){
// remove site url
$url = str_replace(site_url(), '', $guid);
// remove any domain up to the first slash. Add a slash back in.
$url = preg_replace('/^.*?\/\/.*?\//', '/', $url);
return $url;
}
}
new andyp_responsive_menus;