-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti-menu.php
More file actions
397 lines (289 loc) · 17.9 KB
/
multi-menu.php
File metadata and controls
397 lines (289 loc) · 17.9 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
namespace MultiMenu;
/*
Plugin Name: Multi Menu
Description: This is a plugin that allows you to easily replace your theme's default menu with a mega menu, fullscreen menu or slideout menu.
Version: 1.0.3
Author: Your Right Website
Author URI: https://www.yourrightwebsite.com
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class MultiMenu {
public function __construct() {
add_action('admin_enqueue_scripts', [$this, 'loadScriptsAndStyles']);
add_action('admin_init', [$this, 'renderMenuCustomMetaBox']);
add_action('wp_ajax_save_custom_menu_setting', [$this, 'saveCustomMenuSettings']);
add_filter('wp_nav_menu_args', [$this, 'assignMenuNavWalkers'], 1);
}
/*
A function to check whether the current user has the "edit_theme_options" permission.
If they don't, they don't have the proper permissions to change the menu settings.
*/
public function checkPermissions() {
if (current_user_can('edit_theme_options')) {
return true;
}
return false;
}
/*
A function for loading any CSS or Javascript needed by our plugin.
*/
public function loadScriptsAndStyles() {
// Check if we are inside of the WordPress Dashboard
if(is_admin()) {
// Get the current screen and see if we're on the nav-menus.php page
$screen = get_current_screen();
// Check if we are on the menu editor in the Dashboard
if ($screen && 'nav-menus' === $screen->id) {
// Enqueue our global level admin CSS
wp_enqueue_style('multi-menu-admin-metabox', plugin_dir_url(__FILE__) . 'assets/css/admin/admin-menu-metabox.css', [], null);
// Enqueue our Javascript for saving the custom menu settings
wp_enqueue_script(
'admin-menu-settings-save',
plugin_dir_url(__FILE__) . 'assets/js/admin/admin-menu-settings-save.js',
array('jquery'),
'1.0',
true
);
// Localize script to pass AJAX URL and nonce.
wp_localize_script(
'admin-menu-settings-save', // Must match the script handle above.
'customMenuSettings',
array(
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('custom_menu_setting_ajax_nonce'),
)
);
}
}
}
/*
Displays the Menu Type Selection interface inside of the WordPress Dashboard
*/
public function displayMenuTypeSelection() {
$this->checkPermissions();
$menus = wp_get_nav_menus(); // Used by our view file
require_once(plugin_dir_path(__FILE__) . "/views/admin-menu-style-selection.php");
}
public function renderMenuCustomMetaBox() {
add_meta_box(
'multi-menu-settings', // ID of the meta box
'Multi Menu Settings', // Title of the meta box
[$this, 'loadMenuCustomMetaBoxHTML'], // Callback function to render the meta box
'nav-menus', // Screen ID (for Appearance > Menus)
'side', // Context (side, normal, or advanced)
'high' // Priority
);
}
public function loadMenuCustomMetaBoxHTML() {
$menu_id = 0;
// Get the correct menu to edit
if ( isset($_GET['menu']) && $_GET['menu'] && is_numeric($_GET['menu'])) {
$menu_id = intval($_GET['menu']); // Get the menu id via a URL parameter
}
elseif(is_numeric(get_user_option('nav_menu_recently_edited'))) {
$menu_id = get_user_option('nav_menu_recently_edited'); // Get the menu id via the last menu viewed in WordPress
}
else {
// Default to getting the first menu ID from the database
// We should never get here unless maybe the user is creating their first menu
$menus = wp_get_nav_menus(array('orderby' => 'id', 'order' => 'DESC'));
$menu_id = !empty($menus) ? $menus[0]->term_id : 0;
}
// Retrieve existing custom settings (if any)
$multi_menu_style = get_term_meta($menu_id, 'multimenu_menu_style', true);
$multi_menu_css = get_term_meta($menu_id, 'multimenu_menu_css', true);
$multi_menu_invert_toggle_color = get_term_meta($menu_id, 'multimenu_invert_toggle_color', true);
$multi_menu_show_labels = get_term_meta($menu_id, 'multimenu_show_labels', true);
$multi_menu_preserve_classes = get_term_meta($menu_id, 'multimenu_preserve_classes', true);
$multi_menu_load_theme_specific_css = get_term_meta($menu_id, 'multimenu_load_theme_specific_css', true);
$multi_menu_additional_classes = get_term_meta($menu_id, 'multimenu_additional_classes', true);
require_once(plugin_dir_path(__FILE__) . "/views/admin-menu-custom-settings-metabox.php");
}
public function saveCustomMenuSettings() {
if (!isset($_POST['nonce']) || !check_ajax_referer('custom_menu_setting_ajax_nonce', 'nonce', false)) {
wp_send_json_error('Invalid or missing nonce.', 403);
}
// Check user capabilities.
if ( !$this->checkPermissions() ) {
wp_send_json_error('Insufficient permissions.');
}
// Get the menu ID
$menu_id = isset($_POST['menu_id']) ? intval($_POST['menu_id']) : 0;
if (!$menu_id ) {
wp_send_json_error('Invalid menu ID.');
}
// Get the term meta data
$multi_menu_style = isset($_POST['menu_style']) ? sanitize_text_field($_POST['menu_style']) : '';
$multi_menu_css = isset($_POST['menu_css']) ? sanitize_text_field($_POST['menu_css']) : '';
$multi_menu_toggle_color = isset($_POST['menu_invert_toggle_color']) ? sanitize_text_field($_POST['menu_invert_toggle_color']) : '';
$multi_menu_show_labels = isset($_POST['menu_show_labels']) ? sanitize_text_field($_POST['menu_show_labels']) : '';
$multi_menu_preserve_classes = isset($_POST['menu_preserve_classes']) ? sanitize_text_field($_POST['menu_preserve_classes']) : '';
$multi_menu_load_theme_specific_css = isset($_POST['menu_load_theme_specific_css']) ? sanitize_text_field($_POST['menu_load_theme_specific_css']) : '';
$multi_menu_additional_classes = isset($_POST['menu_additional_classes']) ? sanitize_text_field($_POST['menu_additional_classes']) : '';
// Save the term meta.
update_term_meta($menu_id, 'multimenu_menu_style', $multi_menu_style);
update_term_meta($menu_id, 'multimenu_menu_css', $multi_menu_css);
update_term_meta($menu_id, 'multimenu_invert_toggle_color', $multi_menu_toggle_color );
update_term_meta($menu_id, 'multimenu_show_labels', $multi_menu_show_labels);
update_term_meta($menu_id, 'multimenu_preserve_classes', $multi_menu_preserve_classes);
update_term_meta($menu_id, 'multimenu_load_theme_specific_css', $multi_menu_load_theme_specific_css);
update_term_meta($menu_id, 'multimenu_additional_classes', $multi_menu_additional_classes);
wp_send_json_success('Setting saved successfully.');
}
public function assignMenuNavWalkers($args) {
if(isset($args['theme_location']) && $args['theme_location'] != "") {
$current_menu_theme_location = $args['theme_location'];
$menu_locations = get_nav_menu_locations(); // Information about where each menu is located in the template
if(isset($menu_locations[$current_menu_theme_location]) && is_numeric($menu_locations[$current_menu_theme_location])) {
$menu_id = $menu_locations[$current_menu_theme_location];
$multimenu_menu_style = get_term_meta($menu_id, 'multimenu_menu_style', true);
$multimenu_menu_css = get_term_meta($menu_id, 'multimenu_menu_css', true);
$multi_menu_invert_toggle_color = get_term_meta($menu_id, 'multimenu_invert_toggle_color', true);
$multi_menu_show_labels = get_term_meta($menu_id, 'multimenu_show_labels', true);
$multi_menu_preserve_classes = get_term_meta($menu_id, 'multimenu_preserve_classes', true);
$multi_menu_load_theme_specific_css = get_term_meta($menu_id, 'multimenu_load_theme_specific_css', true);
$multi_menu_additional_classes = get_term_meta($menu_id, 'multimenu_additional_classes', true);
if($multimenu_menu_style != false && $multimenu_menu_style != "") {
$menu_params = []; // Any optional parameters we want to pass to our nav walker
$menu_params["invert_toggle"] = boolval($multi_menu_invert_toggle_color);
$menu_params["show_labels"] = boolval($multi_menu_show_labels);
$menu_params['additional_classes'] = $multi_menu_additional_classes;
$menu_params["id"] = $menu_id;
if($menu_params['additional_classes'] == "") {
$menu_params['additional_classes'] = "no-additional-classes";
}
if($multi_menu_preserve_classes != true) {
$args["menu_class"] = "";
$args["menu_id"] = "multi-menu-wrapper-" . $menu_id;
}
$current_theme = wp_get_theme();
$current_theme_name = "";
if(isset($current_theme->template) && $current_theme->template != "") {
$current_theme_name = $current_theme->template;
}
if($multimenu_menu_style == "fullscreen") {
require_once(plugin_dir_path(__FILE__) . "/fullscreen-menu/FullscreenMenuNavWalker.php");
$args['walker'] = new FullscreenMenuNavWalker($multimenu_menu_css, $menu_params);
// Load the appropriate CSS for this menu
if($multimenu_menu_css != "") {
// If there's a value, we're always loading the core styles
wp_enqueue_style('multi-menu-fullscreen-core', plugin_dir_url(__FILE__) . 'fullscreen-menu/css/fullscreen-core.css', [], null);
if($multimenu_menu_css == "light") {
wp_enqueue_style('multi-menu-fullscreen-light', plugin_dir_url(__FILE__) . 'fullscreen-menu/css/fullscreen-light.css', [], null);
}
elseif($multimenu_menu_css == "dark") {
wp_enqueue_style('multi-menu-fullscreen-dark', plugin_dir_url(__FILE__) . 'fullscreen-menu/css/fullscreen-dark.css', [], null);
}
}
if(boolval($multi_menu_load_theme_specific_css) === true && preg_match('/^[a-zA-Z0-9_-]+$/', $current_theme_name)) {
// Check if we have any theme specific CSS to load
/*
Common popular theme names include:
astra
blocksy
generatepress
kadence
neve
*/
$filepath = 'fullscreen-menu/css/theme-overrides/'. basename($current_theme_name) . '.css';
if(file_exists(plugin_dir_path(__FILE__) . $filepath)) {
wp_enqueue_style('multi-menu-overrides-' . $current_theme_name, plugin_dir_url(__FILE__) . $filepath, [], null);
}
}
// Load the appropriate JS for this menu
wp_enqueue_script(
'multi-menu-fullscreen-js',
plugin_dir_url(__FILE__) . 'fullscreen-menu/js/fullscreen-menu.js',
array('jquery'),
'1.0',
true
);
}
elseif($multimenu_menu_style == "mega") {
require_once(plugin_dir_path(__FILE__) . "/mega-menu/MegaMenuNavWalker.php");
$args['walker'] = new MegaMenuNavWalker($multimenu_menu_css, $menu_params);
// Load the appropriate CSS for this menu
if($multimenu_menu_css != "") {
// If there's a value, we're always loading the core styles
wp_enqueue_style('multi-menu-mega-core', plugin_dir_url(__FILE__) . 'mega-menu/css/mega-core.css', [], null);
if($multimenu_menu_css == "light") {
wp_enqueue_style('multi-menu-mega-light', plugin_dir_url(__FILE__) . 'mega-menu/css/mega-light.css', [], null);
}
elseif($multimenu_menu_css == "dark") {
wp_enqueue_style('multi-menu-mega-dark', plugin_dir_url(__FILE__) . 'mega-menu/css/mega-dark.css', [], null);
}
}
if(boolval($multi_menu_load_theme_specific_css) === true && preg_match('/^[a-zA-Z0-9_-]+$/', $current_theme_name)) {
// Check if we have any theme specific CSS to load
/*
Common popular theme names include:
astra
blocksy
generatepress
kadence
neve
*/
$filepath = 'mega-menu/css/theme-overrides/'. basename($current_theme_name) . '.css';
if(file_exists(plugin_dir_path(__FILE__) . $filepath)) {
wp_enqueue_style('multi-menu-overrides-' . $current_theme_name, plugin_dir_url(__FILE__) . $filepath, [], null);
}
}
// Load the appropriate JS for this menu
wp_enqueue_script(
'multi-menu-mega-js',
plugin_dir_url(__FILE__) . 'mega-menu/js/mega-menu.js',
array('jquery'),
'1.0',
true
);
}
elseif($multimenu_menu_style == "slideout") {
require_once(plugin_dir_path(__FILE__) . "/slideout-menu/SlideoutMenuNavWalker.php");
$args['walker'] = new SlideoutMenuNavWalker($multimenu_menu_css, $menu_params);
// Load the appropriate CSS for this menu
if($multimenu_menu_css != "") {
// If there's a value, we're always loading the core styles
wp_enqueue_style('multi-menu-slideout-core', plugin_dir_url(__FILE__) . 'slideout-menu/css/slideout-core.css', [], null);
if($multimenu_menu_css == "light") {
wp_enqueue_style('multi-menu-slideout-light', plugin_dir_url(__FILE__) . 'slideout-menu/css/slideout-light.css', [], null);
}
elseif($multimenu_menu_css == "dark") {
wp_enqueue_style('multi-menu-slideout-dark', plugin_dir_url(__FILE__) . 'slideout-menu/css/slideout-dark.css', [], null);
}
}
if(boolval($multi_menu_load_theme_specific_css) === true && preg_match('/^[a-zA-Z0-9_-]+$/', $current_theme_name)) {
// Check if we have any theme specific CSS to load
/*
Common popular theme names include:
astra
blocksy
generatepress
kadence
neve
*/
$filepath = 'slideout-menu/css/theme-overrides/'. basename($current_theme_name) . '.css';
if(file_exists(plugin_dir_path(__FILE__) . $filepath)) {
wp_enqueue_style('multi-menu-overrides-' . $current_theme_name, plugin_dir_url(__FILE__) . $filepath, [], null);
}
}
// Load the appropriate JS for this menu
wp_enqueue_script(
'multi-menu-slideout-js',
plugin_dir_url(__FILE__) . 'slideout-menu/js/slideout-menu.js',
array('jquery'),
'1.0',
true
);
}
}
}
}
return $args;
}
}
// Initialize the plugin
$multi_menu = new MultiMenu();
if(file_exists(plugin_dir_path(__FILE__) . "/updater/MultiMenuUpdater.php")) {
require_once(plugin_dir_path(__FILE__) . "/updater/MultiMenuUpdater.php");
}