nav-menus.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // disable direct access
  4. }
  5. if ( ! class_exists( 'Mega_Menu_Nav_Menus' ) ) :
  6. /**
  7. * Handles all admin related functionality.
  8. */
  9. class Mega_Menu_Nav_Menus {
  10. /**
  11. * Return the default settings for each menu item
  12. *
  13. * @since 1.5
  14. */
  15. public static function get_menu_item_defaults() {
  16. $defaults = array(
  17. 'type' => 'flyout',
  18. 'align' => 'bottom-left',
  19. 'icon' => 'disabled',
  20. 'hide_text' => 'false',
  21. 'disable_link' => 'false',
  22. 'hide_on_mobile' => 'false',
  23. 'hide_on_desktop' => 'false',
  24. 'hide_sub_menu_on_mobile' => 'false',
  25. 'hide_arrow' => 'false',
  26. 'item_align' => 'left',
  27. 'icon_position' => 'left',
  28. 'panel_columns' => 6, // total number of columns displayed in the panel
  29. 'mega_menu_columns' => 1, // for sub menu items, how many columns to span in the panel,
  30. 'mega_menu_order' => 0
  31. );
  32. return apply_filters( "megamenu_menu_item_defaults", $defaults );
  33. }
  34. /**
  35. * Constructor
  36. *
  37. * @since 1.0
  38. */
  39. public function __construct() {
  40. add_action( 'admin_init', array( $this, 'register_nav_meta_box' ), 9 );
  41. add_action( 'megamenu_nav_menus_scripts', array( $this, 'enqueue_menu_page_scripts' ), 10 );
  42. add_action( 'wp_ajax_mm_save_settings', array($this, 'save') );
  43. add_filter( 'hidden_meta_boxes', array( $this, 'show_mega_menu_metabox' ) );
  44. add_filter('siteorigin_panels_is_admin_page', array( $this, 'enable_site_origin_page_builder' ) );
  45. if ( function_exists( 'siteorigin_panels_admin_enqueue_scripts' ) ) {
  46. add_action( 'admin_print_scripts-nav-menus.php', array( $this, 'siteorigin_panels_admin_enqueue_scripts') );
  47. }
  48. if ( function_exists( 'siteorigin_panels_admin_enqueue_styles' ) ) {
  49. add_action( 'admin_print_styles-nav-menus.php', array( $this, 'siteorigin_panels_admin_enqueue_styles') );
  50. }
  51. }
  52. /**
  53. * Enqueue Site Origin Page Builder scripts on nav-menus page.
  54. *
  55. * @since 2.3.7
  56. */
  57. public function enable_site_origin_page_builder( $enabled ) {
  58. $screen = get_current_screen();
  59. if ($screen->base == 'nav-menus') {
  60. return true;
  61. }
  62. return $enabled;
  63. }
  64. /**
  65. * Enqueue Page Builder scripts (https://wordpress.org/plugins/siteorigin-panels/)
  66. * @since 1.9
  67. */
  68. public function siteorigin_panels_admin_enqueue_scripts() {
  69. siteorigin_panels_admin_enqueue_scripts('', true);
  70. }
  71. /**
  72. * Enqueue Page Builder styles (https://wordpress.org/plugins/siteorigin-panels/)
  73. * @since 1.9
  74. */
  75. public function siteorigin_panels_admin_enqueue_styles() {
  76. siteorigin_panels_admin_enqueue_styles('', true);
  77. }
  78. /**
  79. * By default the mega menu meta box is hidden - show it.
  80. *
  81. * @since 1.0
  82. * @param array $hidden
  83. * @return array
  84. */
  85. public function show_mega_menu_metabox( $hidden ) {
  86. if ( is_array( $hidden ) && count( $hidden ) > 0 ) {
  87. foreach ( $hidden as $key => $value ) {
  88. if ( $value == 'mega_menu_meta_box' ) {
  89. unset( $hidden[$key] );
  90. }
  91. }
  92. }
  93. return $hidden;
  94. }
  95. /**
  96. * Adds the meta box container
  97. *
  98. * @since 1.0
  99. */
  100. public function register_nav_meta_box() {
  101. global $pagenow;
  102. if ( 'nav-menus.php' == $pagenow ) {
  103. add_meta_box(
  104. 'mega_menu_meta_box',
  105. __("Max Mega Menu Settings", "megamenu"),
  106. array( $this, 'metabox_contents' ),
  107. 'nav-menus',
  108. 'side',
  109. 'high'
  110. );
  111. }
  112. }
  113. /**
  114. * Enqueue required CSS and JS for Mega Menu
  115. *
  116. * @since 1.0
  117. */
  118. public function enqueue_menu_page_scripts($hook) {
  119. if( 'nav-menus.php' != $hook )
  120. return;
  121. // Compatibility fix for SlideDeck Pro
  122. wp_deregister_script('codemirror');
  123. wp_deregister_style('codemirror');
  124. // Compatibility fix for Pinboard Theme
  125. wp_deregister_script('colorbox');
  126. wp_deregister_style('colorbox');
  127. // Compatibility fix for AGP Font Awesome Collection
  128. wp_deregister_script('colorbox-js');
  129. wp_deregister_style('colorbox-css');
  130. // Compatibility fix for purple-xmls-google-product-feed-for-woocommerce
  131. wp_deregister_script('cart-product-colorbox');
  132. wp_deregister_style('cart-product-colorstyle');
  133. // Compatibility fix for WordFence
  134. wp_deregister_script('jquery.wfcolorbox');
  135. wp_deregister_style('wordfence-colorbox-style');
  136. // Compatibility fix for Profit Builder
  137. wp_deregister_script('color-box-min');
  138. wp_deregister_script('color-box');
  139. wp_deregister_style('color-box-css');
  140. // Compatibility fix for Reamaze
  141. wp_deregister_script('jquery-colorbox');
  142. wp_deregister_style('colorbox-css');
  143. // Compatibility fix for WP Disquz media uploader
  144. wp_deregister_script('wmu-colorbox-js');
  145. wp_deregister_style('wmu-colorbox-css');
  146. wp_enqueue_style( 'colorbox', MEGAMENU_BASE_URL . 'js/colorbox/colorbox.css', false, MEGAMENU_VERSION );
  147. wp_enqueue_style( 'maxmegamenu', MEGAMENU_BASE_URL . 'css/admin/admin.css', false, MEGAMENU_VERSION );
  148. wp_enqueue_script( 'maxmegamenu', MEGAMENU_BASE_URL . 'js/admin.js', array(
  149. 'jquery',
  150. 'jquery-ui-core',
  151. 'jquery-ui-sortable',
  152. 'jquery-ui-accordion'),
  153. MEGAMENU_VERSION );
  154. wp_enqueue_script( 'colorbox', MEGAMENU_BASE_URL . 'js/colorbox/jquery.colorbox-min.js', array( 'jquery' ), MEGAMENU_VERSION );
  155. $settings = get_option( 'megamenu_settings' );
  156. $prefix = isset( $settings['prefix'] ) ? $settings['prefix'] : 'true';
  157. wp_localize_script( 'maxmegamenu', 'megamenu',
  158. array(
  159. 'debug_launched' => __("Launched for Menu ID", "megamenu"),
  160. 'launch_lightbox' => __("Mega Menu", "megamenu"),
  161. 'is_disabled_error' => __("Please enable Max Mega Menu using the settings on the left of this page.", "megamenu"),
  162. 'save_menu' => __("Please save the menu structure to enable this option.", "megamenu"),
  163. 'saving' => __("Saving", "megamenu"),
  164. 'nonce' => wp_create_nonce('megamenu_edit'),
  165. 'nonce_check_failed' => __("Oops. Something went wrong. Please reload the page.", "megamenu"),
  166. 'css_prefix' => $prefix,
  167. 'css_prefix_message' => __("Custom CSS Classes will be prefixed with 'mega-'", "megamenu"),
  168. 'row_is_full' => __("There is not enough space in this row to add a new column. Make space by reducing the width of the columns within the row or create a new row.", "megamenu")
  169. )
  170. );
  171. do_action("megamenu_enqueue_admin_scripts");
  172. }
  173. /**
  174. * Show the Meta Menu settings
  175. *
  176. * @since 1.0
  177. */
  178. public function metabox_contents() {
  179. $menu_id = $this->get_selected_menu_id();
  180. $this->print_enable_megamenu_options( $menu_id );
  181. }
  182. /**
  183. * Save the mega menu settings (submitted from Menus Page Meta Box)
  184. *
  185. * @since 1.0
  186. */
  187. public function save() {
  188. check_ajax_referer( 'megamenu_edit', 'nonce' );
  189. if ( isset( $_POST['menu'] ) && $_POST['menu'] > 0 && is_nav_menu( $_POST['menu'] ) && isset( $_POST['megamenu_meta'] ) ) {
  190. $raw_submitted_settings = $_POST['megamenu_meta'];
  191. $parsed_submitted_settings = json_decode( stripslashes( $raw_submitted_settings ), true );
  192. $submitted_settings = array();
  193. foreach ( $parsed_submitted_settings as $index => $value ) {
  194. $name = $value['name'];
  195. // find values between square brackets
  196. preg_match_all( "/\[(.*?)\]/", $name, $matches );
  197. if ( isset( $matches[1][0] ) && isset( $matches[1][1] ) ) {
  198. $location = $matches[1][0];
  199. $setting = $matches[1][1];
  200. $submitted_settings[$location][$setting] = $value['value'];
  201. }
  202. }
  203. $submitted_settings = apply_filters("megamenu_submitted_settings_meta", $submitted_settings);
  204. if ( ! get_option( 'megamenu_settings' ) ) {
  205. update_option( 'megamenu_settings', $submitted_settings );
  206. } else {
  207. $existing_settings = get_option( 'megamenu_settings' );
  208. $new_settings = array_merge( $existing_settings, $submitted_settings );
  209. update_option( 'megamenu_settings', $new_settings );
  210. }
  211. do_action( "megamenu_after_save_settings" );
  212. do_action( "megamenu_delete_cache" );
  213. }
  214. wp_die();
  215. }
  216. /**
  217. * Print the custom Meta Box settings
  218. *
  219. * @param int $menu_id
  220. * @since 1.0
  221. */
  222. public function print_enable_megamenu_options( $menu_id ) {
  223. $tagged_menu_locations = $this->get_tagged_theme_locations_for_menu_id( $menu_id );
  224. $theme_locations = get_registered_nav_menus();
  225. $saved_settings = get_option( 'megamenu_settings' );
  226. if ( ! count( $theme_locations ) ) {
  227. $link = '<a href="https://www.megamenu.com/documentation/widget/?utm_source=free&amp;utm_medium=link&amp;utm_campaign=pro" target="_blank">' . __("here", "megamenu") . '</a>';
  228. echo "<p>" . __("This theme does not register any menu locations.", "megamenu") . "</p>";
  229. echo "<p>" . __("You will need to create a new menu location and use the Max Mega Menu widget or shortcode to display the menu on your site.", "megamenu") . "</p>";
  230. echo "<p>" . str_replace( "{link}", $link, __("Click {link} for instructions.", "megamenu") ) . "</p>";
  231. } else if ( ! count ( $tagged_menu_locations ) ) {
  232. echo "<p>" . __("Please assign this menu to a theme location to enable the Mega Menu settings.", "megamenu") . "</p>";
  233. echo "<p>" . __("To assign this menu to a theme location, scroll to the bottom of this page and tag the menu to a 'Display location'.", "megamenu") . "</p>";
  234. } else { ?>
  235. <?php if ( count( $tagged_menu_locations ) == 1 ) : ?>
  236. <?php
  237. $locations = array_keys( $tagged_menu_locations );
  238. $location = $locations[0];
  239. if (isset( $tagged_menu_locations[ $location ] ) ) {
  240. $this->settings_table( $location, $saved_settings );
  241. }
  242. ?>
  243. <?php else: ?>
  244. <div id='megamenu_accordion'>
  245. <?php foreach ( $theme_locations as $location => $name ) : ?>
  246. <?php if ( isset( $tagged_menu_locations[ $location ] ) ): ?>
  247. <h3 class='theme_settings'><?php echo esc_html( $name ); ?></h3>
  248. <div class='accordion_content' style='display: none;'>
  249. <?php $this->settings_table( $location, $saved_settings ); ?>
  250. </div>
  251. <?php endif; ?>
  252. <?php endforeach;?>
  253. </div>
  254. <?php endif; ?>
  255. <?php
  256. submit_button( __( 'Save' ), 'max-mega-menu-save button-primary alignright');
  257. ?>
  258. <span class='spinner'></span>
  259. <?php
  260. }
  261. }
  262. /**
  263. * Print the list of Mega Menu settings
  264. *
  265. * @since 1.0
  266. */
  267. public function settings_table( $location, $settings ) {
  268. ?>
  269. <table>
  270. <tr>
  271. <td><?php _e("Enable", "megamenu") ?></td>
  272. <td>
  273. <input type='checkbox' class='megamenu_enabled' name='megamenu_meta[<?php echo $location ?>][enabled]' value='1' <?php checked( isset( $settings[$location]['enabled'] ) ); ?> />
  274. </td>
  275. </tr>
  276. <tr>
  277. <td><?php _e("Event", "megamenu") ?></td>
  278. <td>
  279. <select name='megamenu_meta[<?php echo $location ?>][event]'>
  280. <option value='hover' <?php selected( isset( $settings[$location]['event'] ) && $settings[$location]['event'] == 'hover'); ?>><?php _e("Hover Intent", "megamenu"); ?></option>
  281. <option value='hover_' <?php selected( isset( $settings[$location]['event'] ) && $settings[$location]['event'] == 'hover_'); ?>><?php _e("Hover", "megamenu"); ?></option>
  282. <option value='click' <?php selected( isset( $settings[$location]['event'] ) && $settings[$location]['event'] == 'click'); ?>><?php _e("Click", "megamenu"); ?></option>
  283. </select>
  284. </td>
  285. </tr>
  286. <tr>
  287. <td><?php _e("Effect", "megamenu") ?></td>
  288. <td>
  289. <select name='megamenu_meta[<?php echo $location ?>][effect]'>
  290. <?php
  291. $selected = isset( $settings[$location]['effect'] ) ? $settings[$location]['effect'] : 'fade_up';
  292. $options = apply_filters("megamenu_transition_effects", array(
  293. "disabled" => array(
  294. 'label' => __("None", "megamenu"),
  295. 'selected' => $selected == 'disabled',
  296. ),
  297. "fade" => array(
  298. 'label' => __("Fade", "megamenu"),
  299. 'selected' => $selected == 'fade',
  300. ),
  301. "fade_up" => array(
  302. 'label' => __("Fade Up", "megamenu"),
  303. 'selected' => $selected == 'fade_up' || $selected == 'fadeUp',
  304. ),
  305. "slide" => array(
  306. 'label' => __("Slide", "megamenu"),
  307. 'selected' => $selected == 'slide',
  308. ),
  309. "slide_up" => array(
  310. 'label' => __("Slide Up", "megamenu"),
  311. 'selected' => $selected == 'slide_up',
  312. )
  313. ), $selected );
  314. foreach ( $options as $key => $value ) {
  315. ?><option value='<?php echo $key ?>' <?php selected( $value['selected'] ); ?>><?php echo $value['label'] ?></option><?php
  316. }
  317. ?>
  318. </select>
  319. <select name='megamenu_meta[<?php echo $location ?>][effect_speed]'>
  320. <?php
  321. $selected = isset( $settings[$location]['effect_speed'] ) ? $settings[$location]['effect_speed'] : '200';
  322. $options = apply_filters("megamenu_effect_speed", array(
  323. "600" => __("Slow", "megamenu"),
  324. "400" => __("Med", "megamenu"),
  325. "200" => __("Fast", "megamenu")
  326. ), $selected );
  327. ksort($options);
  328. foreach ( $options as $key => $value ) {
  329. ?><option value='<?php echo $key ?>' <?php selected( $key == $selected ); ?>><?php echo $value ?></option><?php
  330. }
  331. ?>
  332. </select>
  333. </td>
  334. </tr>
  335. <tr>
  336. <td><?php _e("Effect (Mobile)", "megamenu") ?></td>
  337. <td>
  338. <select name='megamenu_meta[<?php echo $location ?>][effect_mobile]'>
  339. <?php
  340. $selected = isset( $settings[$location]['effect_mobile'] ) ? $settings[$location]['effect_mobile'] : 'disabled';
  341. $options = apply_filters("megamenu_transition_effects_mobile", array(
  342. "disabled" => array(
  343. 'label' => __("None", "megamenu"),
  344. 'selected' => $selected == 'disabled',
  345. ),
  346. "slide" => array(
  347. 'label' => __("Slide", "megamenu"),
  348. 'selected' => $selected == 'slide',
  349. )
  350. ), $selected );
  351. foreach ( $options as $key => $value ) {
  352. ?><option value='<?php echo $key ?>' <?php selected( $value['selected'] ); ?>><?php echo $value['label'] ?></option><?php
  353. }
  354. ?>
  355. </select>
  356. <select name='megamenu_meta[<?php echo $location ?>][effect_speed_mobile]'>
  357. <?php
  358. $selected = isset( $settings[$location]['effect_speed_mobile'] ) ? $settings[$location]['effect_speed_mobile'] : '200';
  359. $options = apply_filters("megamenu_effect_speed_mobile", array(
  360. "600" => __("Slow", "megamenu"),
  361. "400" => __("Med", "megamenu"),
  362. "200" => __("Fast", "megamenu")
  363. ), $selected );
  364. ksort($options);
  365. foreach ( $options as $key => $value ) {
  366. ?><option value='<?php echo $key ?>' <?php selected( $key == $selected ); ?>><?php echo $value ?></option><?php
  367. }
  368. ?>
  369. </select>
  370. </td>
  371. </tr>
  372. <tr>
  373. <td><?php _e("Theme", "megamenu"); ?></td>
  374. <td>
  375. <select name='megamenu_meta[<?php echo $location ?>][theme]'>
  376. <?php
  377. $style_manager = new Mega_Menu_Style_Manager();
  378. $themes = $style_manager->get_themes();
  379. $selected_theme = isset( $settings[$location]['theme'] ) ? $settings[$location]['theme'] : 'default';
  380. foreach ( $themes as $key => $theme ) {
  381. echo "<option value='{$key}' " . selected( $selected_theme, $key ) . ">{$theme['title']}</option>";
  382. }
  383. ?>
  384. </select>
  385. </td>
  386. </tr>
  387. <?php do_action( 'megamenu_settings_table', $location, $settings ); ?>
  388. </table>
  389. <?php
  390. }
  391. /**
  392. * Return the locations that a specific menu ID has been tagged to.
  393. *
  394. * @param $menu_id int
  395. * @return array
  396. */
  397. public function get_tagged_theme_locations_for_menu_id( $menu_id ) {
  398. $locations = array();
  399. $nav_menu_locations = get_nav_menu_locations();
  400. foreach ( get_registered_nav_menus() as $id => $name ) {
  401. if ( isset( $nav_menu_locations[ $id ] ) && $nav_menu_locations[$id] == $menu_id )
  402. $locations[$id] = $name;
  403. }
  404. return $locations;
  405. }
  406. /**
  407. * Get the current menu ID.
  408. *
  409. * Most of this taken from wp-admin/nav-menus.php (no built in functions to do this)
  410. *
  411. * @since 1.0
  412. * @return int
  413. */
  414. public function get_selected_menu_id() {
  415. $nav_menus = wp_get_nav_menus( array('orderby' => 'name') );
  416. $menu_count = count( $nav_menus );
  417. $nav_menu_selected_id = isset( $_REQUEST['menu'] ) ? (int) $_REQUEST['menu'] : 0;
  418. $add_new_screen = ( isset( $_GET['menu'] ) && 0 == $_GET['menu'] ) ? true : false;
  419. // If we have one theme location, and zero menus, we take them right into editing their first menu
  420. $page_count = wp_count_posts( 'page' );
  421. $one_theme_location_no_menus = ( 1 == count( get_registered_nav_menus() ) && ! $add_new_screen && empty( $nav_menus ) && ! empty( $page_count->publish ) ) ? true : false;
  422. // Get recently edited nav menu
  423. $recently_edited = absint( get_user_option( 'nav_menu_recently_edited' ) );
  424. if ( empty( $recently_edited ) && is_nav_menu( $nav_menu_selected_id ) )
  425. $recently_edited = $nav_menu_selected_id;
  426. // Use $recently_edited if none are selected
  427. if ( empty( $nav_menu_selected_id ) && ! isset( $_GET['menu'] ) && is_nav_menu( $recently_edited ) )
  428. $nav_menu_selected_id = $recently_edited;
  429. // On deletion of menu, if another menu exists, show it
  430. if ( ! $add_new_screen && 0 < $menu_count && isset( $_GET['action'] ) && 'delete' == $_GET['action'] )
  431. $nav_menu_selected_id = $nav_menus[0]->term_id;
  432. // Set $nav_menu_selected_id to 0 if no menus
  433. if ( $one_theme_location_no_menus ) {
  434. $nav_menu_selected_id = 0;
  435. } elseif ( empty( $nav_menu_selected_id ) && ! empty( $nav_menus ) && ! $add_new_screen ) {
  436. // if we have no selection yet, and we have menus, set to the first one in the list
  437. $nav_menu_selected_id = $nav_menus[0]->term_id;
  438. }
  439. return $nav_menu_selected_id;
  440. }
  441. }
  442. endif;