Guest User

td_menu.php

a guest
Mar 21st, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.49 KB | None | 0 0
  1. <?php
  2. /**
  3. * Class td_menu
  4. * v 1.1 10 oct 2014
  5. */
  6. // the menu
  7.  
  8. class td_menu {
  9. var $is_header_menu_mobile = true;
  10.  
  11.  
  12. function __construct() {
  13. add_action( 'init', array($this, 'hook_init'));
  14.  
  15. if (is_admin()) {
  16. add_action('wp_update_nav_menu_item', array( $this, 'hook_wp_update_nav_menu_item'), 10, 3);
  17. add_filter('wp_edit_nav_menu_walker', array($this, 'hook_wp_edit_nav_menu_walker'));
  18. }
  19.  
  20. add_filter('wp_nav_menu_objects', array($this, 'hook_wp_nav_menu_objects'), 10, 2);
  21. }
  22.  
  23.  
  24.  
  25. function hook_wp_edit_nav_menu_walker () {
  26. //include_once('td_menu_back.php');
  27. return 'td_nav_menu_edit_walker';
  28. }
  29.  
  30. function hook_wp_update_nav_menu_item ($menu_id, $menu_item_db_id, $args) {
  31.  
  32. //mega menu category
  33. if (isset($_POST['td_mega_menu_cat'][$menu_item_db_id])) {
  34. //print_r($_POST);
  35. update_post_meta($menu_item_db_id, 'td_mega_menu_cat', $_POST['td_mega_menu_cat'][$menu_item_db_id]);
  36. }
  37. //mega menu page
  38. if (isset($_POST['td_mega_menu_page_id'][$menu_item_db_id])) {
  39. update_post_meta($menu_item_db_id, 'td_mega_menu_page_id', $_POST['td_mega_menu_page_id'][$menu_item_db_id]);
  40. }
  41.  
  42.  
  43.  
  44. }
  45.  
  46.  
  47. /**
  48. * adds mega menu support
  49. * @param $items
  50. * @param string $args
  51. * @return array
  52. */
  53. function hook_wp_nav_menu_objects($items, $args = '') {
  54.  
  55. // Internal array to keep the references of the items (ID item is the key -> item itself)
  56. // This helps to not look for an item into the $items list
  57. $_items_ref = array();
  58.  
  59. $items_buffy = array();
  60.  
  61. $td_is_firstMenu = true;
  62.  
  63.  
  64.  
  65.  
  66. //print_r($items);
  67.  
  68. foreach ($items as &$item) {
  69.  
  70. $_items_ref[$item->ID] = $item;
  71.  
  72. /**
  73. * $item
  74. * -> $item (is_mega_menu = true) - this item is a mega menu
  75. */
  76.  
  77. $item->is_mega_menu = false; // all items should have this property, we just init it here - when an item has this flag on it means that the item is the mega menu dropdown!
  78.  
  79. // first menu fix
  80. if ($td_is_firstMenu) {
  81. $item->classes[] = 'menu-item-first';
  82. $td_is_firstMenu = false;
  83. }
  84.  
  85. // fix the down arros + shortcodes
  86. if (strpos($item->title,'[') === false) {
  87.  
  88. } else {
  89. //on shortcodes [home] etc.. do not show down arrow
  90. $item->classes[] = 'td-no-down-arrow';
  91. }
  92.  
  93. //run shortcodes
  94. $item->title = do_shortcode($item->title);
  95.  
  96. //read mega menu and mega page menu settings
  97. $td_mega_menu_cat = get_post_meta($item->ID, 'td_mega_menu_cat', true);
  98. $td_mega_menu_page_id = get_post_meta($item->ID, 'td_mega_menu_page_id', true);
  99.  
  100. if ($this->is_header_menu_mobile === true) {
  101. // a item in the mobile menu
  102.  
  103. /**
  104. * we are in the mobile menu location
  105. */
  106. if ($td_mega_menu_cat != '') {
  107. //this is a mega menu, do a category check
  108.  
  109. //add the parent item
  110. $items_buffy[] = $item;
  111.  
  112.  
  113. $td_render_atts = td_api_block::get_key('td_block_mega_menu', 'render_atts');
  114.  
  115. $td_show_child_cat = 5;
  116. if (isset($td_render_atts['show_child_cat'])) {
  117. $td_show_child_cat = $td_render_atts['show_child_cat'];
  118. }
  119.  
  120. //check for subcategories
  121. $td_subcategories = get_categories(array(
  122. 'child_of' => $td_mega_menu_cat,
  123. 'number' => $td_show_child_cat //
  124. ));
  125. if (!empty($td_subcategories)) {
  126. $item->classes[] = 'menu-item-has-children'; // add the extra class for the dropdown to work
  127.  
  128. /**
  129. * - Because 'current_item_parent' (true/false) item property is not set by wp,
  130. * we use an additional flag 'td_is_parent' to mark the parent elements of the tree menu
  131. * - For the moment, the 'td_is_parent' flag is used just by the 'td_walker_mobile_menu'
  132. * walker of the mobile theme version @see td_walker_mobile_menu
  133. */
  134.  
  135. if (array_key_exists($item->ID, $_items_ref)) {
  136. $_items_ref[$item->ID]->td_is_parent = true;
  137. }
  138.  
  139. foreach ($td_subcategories as $td_category) {
  140. $new_item = $this->generate_wp_post();
  141. $new_item->is_mega_menu = false; //this is sent to the menu walkers
  142. $new_item->menu_item_parent = $item->ID;
  143. $new_item->url = get_category_link($td_category->cat_ID);
  144. $new_item->title = $td_category->name;
  145. $items_buffy[] = $new_item;
  146. }
  147. }
  148.  
  149. } else {
  150. // this item is a normal item in the mobile menu
  151. $items_buffy[] = $item;
  152. }
  153. }
  154.  
  155.  
  156.  
  157. elseif ($td_mega_menu_page_id != '' && td_api_features::is_enabled('page_mega_menu') === true) {
  158. // a item with a page - pege mega menu
  159.  
  160. // the parent item (the one that appears in the main menu)
  161. $item->classes[] = 'td-menu-item';
  162. $item->classes[] = 'td-mega-menu';
  163. $item->classes[] = 'td-mega-menu-page';
  164. $items_buffy[] = $item;
  165.  
  166. //create a new mega menu item: - this is just the dropdown menu / not the parrent
  167. $new_item = $this->generate_wp_post();
  168. $new_item->is_mega_menu = true; //this is sent to the menu walkers
  169. $new_item->menu_item_parent = $item->ID;
  170. $new_item->url = '';
  171.  
  172. //read the page content
  173. $content_post = get_post($td_mega_menu_page_id);
  174. $content = $content_post->post_content;
  175. $content = apply_filters('the_content', $content);
  176. $content = str_replace(']]>', ']]&gt;', $content);
  177.  
  178.  
  179. $new_item->title = '<div class="td-container-border"><div class="td-mega-grid">';
  180.  
  181. // the has_filter check is made for plugins, like bbpress, who think it's okay to remove all filters on 'the_content'
  182. if (!has_filter('the_content', 'do_shortcode')) {
  183. $new_item->title .= do_shortcode($content);
  184. } else {
  185. $new_item->title .= $content;
  186. }
  187.  
  188. $new_item->title .= '</div></div>';
  189. $items_buffy[] = $new_item;
  190. }
  191.  
  192.  
  193.  
  194. elseif ($td_mega_menu_cat != '') {
  195. // a item with a category mega menu
  196.  
  197. // the parent item (the one that appears in the main menu)
  198. $item->classes[] = 'td-menu-item';
  199. $item->classes[] = 'td-mega-menu';
  200. $items_buffy[] = $item;
  201.  
  202. //create a new mega menu item: - this is just the dropdown menu / not the parrent
  203. $new_item = $this->generate_wp_post();
  204. /*
  205. * it's a mega menu,
  206. * - set the is_mega_menu flag
  207. * - alter the last item classes $last_item
  208. * - change the title and url of the current item
  209. */
  210. $new_item->is_mega_menu = true; //this is sent to the menu walkers
  211. $new_item->menu_item_parent = $item->ID;
  212. $new_item->url = '';
  213. $new_item->title = '<div class="td-container-border"><div class="td-mega-grid">';
  214.  
  215. $td_render_atts = td_api_block::get_key('td_block_mega_menu', 'render_atts');
  216.  
  217.  
  218. $new_item->title .= td_global_blocks::get_instance('td_block_mega_menu')->render(
  219. array(
  220. 'limit' => '5',
  221. 'td_column_number' => 3,
  222. 'ajax_pagination' => 'next_prev',
  223. 'category_id' => $td_mega_menu_cat,
  224. 'show_child_cat' => $td_render_atts['show_child_cat'],
  225. 'td_ajax_filter_type' => 'td_category_ids_filter',
  226. 'td_ajax_preloading' => td_util::get_option('tds_mega_menu_ajax_preloading')
  227. ));
  228. $new_item->title .= '</div></div>';
  229. $items_buffy[] = $new_item;
  230. }
  231.  
  232.  
  233.  
  234. else {
  235. // normal menu item
  236. $item->classes[] = 'td-menu-item';
  237. $item->classes[] = 'td-normal-menu';
  238. $items_buffy[] = $item;
  239. }
  240.  
  241. /**
  242. * - Because 'current_item_parent' (true/false) item property is not set by wp,
  243. * we use an additional flag 'td_is_parent' to mark the parent elements of the tree menu
  244. * - For the moment, the 'td_is_parent' flag is used just by the 'td_walker_mobile_menu'
  245. * walker of the mobile theme version @see td_walker_mobile_menu
  246. */
  247.  
  248. if (isset($item->menu_item_parent) && 0 !== intval($item->menu_item_parent) && array_key_exists(intval($item->menu_item_parent), $_items_ref)) {
  249. $_items_ref[intval($item->menu_item_parent)]->td_is_parent = true;
  250. }
  251.  
  252.  
  253. } //end foreach
  254.  
  255.  
  256. // we have two header-menu locations and the fist one is the mobile menu
  257. // the second one is the header menu
  258. if ($args->theme_location == 'header-menu') {
  259. $this->is_header_menu_mobile = false;
  260. }
  261.  
  262.  
  263.  
  264. //print_r($items_buffy);
  265. //die;
  266. return $items_buffy;
  267. }
  268.  
  269.  
  270. function hook_init() {
  271. register_nav_menus(
  272. array(
  273. 'top-menu' => 'Top Header Menu',
  274. 'header-menu' => 'Header Menu (main)',
  275. 'footer-menu' => 'Footer Menu',
  276. 'custom-menu' => 'Menu for mobile'
  277. )
  278. );
  279. }
  280.  
  281.  
  282.  
  283. function generate_wp_post() {
  284. $post = new stdClass;
  285. $post->ID = 0;
  286. $post->post_author = '';
  287. $post->post_date = '';
  288. $post->post_date_gmt = '';
  289. $post->post_password = '';
  290. $post->post_type = 'menu_tds';
  291. $post->post_status = 'draft';
  292. $post->to_ping = '';
  293. $post->pinged = '';
  294. $post->comment_status = '';
  295. $post->ping_status = '';
  296. $post->post_pingback = '';
  297. //$post->post_category = '';
  298. $post->page_template = 'default';
  299. $post->post_parent = 0;
  300. $post->menu_order = 0;
  301. return new WP_Post($post);
  302. }
  303.  
  304.  
  305. }
  306.  
  307. new td_menu();
  308.  
  309.  
  310.  
  311.  
  312.  
  313. //this walker is used to remove a wrapping <a> around the megamenu
  314. class td_tagdiv_walker_nav_menu extends Walker_Nav_Menu {
  315. function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  316. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  317.  
  318. $class_names = $value = '';
  319.  
  320. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  321. $classes[] = 'menu-item-' . $item->ID;
  322.  
  323. /**
  324. * Filter the CSS class(es) applied to a menu item's <li>.
  325. *
  326. * @since 3.0.0
  327. *
  328. * @param array $classes The CSS classes that are applied to the menu item's <li>.
  329. * @param object $item The current menu item.
  330. * @param array $args An array of arguments. @see wp_nav_menu()
  331. */
  332. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
  333. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  334.  
  335. /**
  336. * Filter the ID applied to a menu item's <li>.
  337. *
  338. * @since 3.0.1
  339. *
  340. * @param string The ID that is applied to the menu item's <li>.
  341. * @param object $item The current menu item.
  342. * @param array $args An array of arguments. @see wp_nav_menu()
  343. */
  344. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  345. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  346.  
  347. $output .= $indent . '<li' . $id . $value . $class_names .'>';
  348.  
  349. $atts = array();
  350. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  351. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  352. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  353. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  354.  
  355. /**
  356. * Filter the HTML attributes applied to a menu item's <a>.
  357. *
  358. * @since 3.6.0
  359. *
  360. * @param array $atts {
  361. * The HTML attributes applied to the menu item's <a>, empty strings are ignored.
  362. *
  363. * @type string $title The title attribute.
  364. * @type string $target The target attribute.
  365. * @type string $rel The rel attribute.
  366. * @type string $href The href attribute.
  367. * }
  368. * @param object $item The current menu item.
  369. * @param array $args An array of arguments. @see wp_nav_menu()
  370. */
  371. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
  372.  
  373. $attributes = '';
  374. foreach ( $atts as $attr => $value ) {
  375. if ( ! empty( $value ) ) {
  376. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  377. $attributes .= ' ' . $attr . '="' . $value . '"';
  378. }
  379. }
  380.  
  381. $item_output = $args->before;
  382.  
  383. //tagdiv - megamenu disable link from from includes/wp_booster/td_menu.php hook_wp_nav_menu_objects
  384. if ($item->is_mega_menu == false) {
  385. $item_output .= '<a'. $attributes .'>';
  386. }
  387.  
  388. /** This filter is documented in wp-includes/post-template.php */
  389. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
  390.  
  391. //tagdiv - megamenu disable link from includes/wp_booster/td_menu.php hook_wp_nav_menu_objects
  392. if ($item->is_mega_menu == false) {
  393. $item_output .= '</a>';
  394. }
  395. $item_output .= $args->after;
  396.  
  397. /**
  398. * Filter a menu item's starting output.
  399. *
  400. * The menu item's starting output only includes $args->before, the opening <a>,
  401. * the menu item's title, the closing </a>, and $args->after. Currently, there is
  402. * no filter for modifying the opening and closing <li> for a menu item.
  403. *
  404. * @since 3.0.0
  405. *
  406. * @param string $item_output The menu item's starting HTML output.
  407. * @param object $item Menu item data object.
  408. * @param int $depth Depth of menu item. Used for padding.
  409. * @param array $args An array of arguments. @see wp_nav_menu()
  410. */
  411. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  412. }
  413. }
  414.  
  415.  
  416. class td_walker_mobile_menu extends Walker_Nav_Menu {
  417.  
  418. public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
  419. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  420.  
  421. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  422. $classes[] = 'menu-item-' . $item->ID;
  423.  
  424. /**
  425. * Filter the CSS class(es) applied to a menu item's list item element.
  426. *
  427. * @since 3.0.0
  428. * @since 4.1.0 The `$depth` parameter was added.
  429. *
  430. * @param array $classes The CSS classes that are applied to the menu item's `<li>` element.
  431. * @param object $item The current menu item.
  432. * @param array $args An array of {@see wp_nav_menu()} arguments.
  433. * @param int $depth Depth of menu item. Used for padding.
  434. */
  435. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
  436. $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
  437.  
  438. /**
  439. * Filter the ID applied to a menu item's list item element.
  440. *
  441. * @since 3.0.1
  442. * @since 4.1.0 The `$depth` parameter was added.
  443. *
  444. * @param string $menu_id The ID that is applied to the menu item's `<li>` element.
  445. * @param object $item The current menu item.
  446. * @param array $args An array of {@see wp_nav_menu()} arguments.
  447. * @param int $depth Depth of menu item. Used for padding.
  448. */
  449. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
  450. $id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
  451.  
  452. $output .= $indent . '<li' . $id . $class_names .'>';
  453.  
  454. $atts = array();
  455. $atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
  456. $atts['target'] = ! empty( $item->target ) ? $item->target : '';
  457. $atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
  458. $atts['href'] = ! empty( $item->url ) ? $item->url : '';
  459.  
  460. /**
  461. * Filter the HTML attributes applied to a menu item's anchor element.
  462. *
  463. * @since 3.6.0
  464. * @since 4.1.0 The `$depth` parameter was added.
  465. *
  466. * @param array $atts {
  467. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  468. *
  469. * @type string $title Title attribute.
  470. * @type string $target Target attribute.
  471. * @type string $rel The rel attribute.
  472. * @type string $href The href attribute.
  473. * }
  474. * @param object $item The current menu item.
  475. * @param array $args An array of {@see wp_nav_menu()} arguments.
  476. * @param int $depth Depth of menu item. Used for padding.
  477. */
  478. $atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
  479.  
  480. $attributes = '';
  481. foreach ( $atts as $attr => $value ) {
  482. if ( ! empty( $value ) ) {
  483. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  484. $attributes .= ' ' . $attr . '="' . $value . '"';
  485. }
  486. }
  487.  
  488. $item_output = $args->before;
  489. $item_output .= '<a'. $attributes .'>';
  490. /** This filter is documented in wp-includes/post-template.php */
  491. $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID );
  492.  
  493.  
  494. // TAGDIV: The $link_after of args is added for parent items
  495. if (isset($item->td_is_parent) && true === $item->td_is_parent) {
  496. $item_output .= $args->link_after;
  497. }
  498.  
  499.  
  500. $item_output .= '</a>';
  501. $item_output .= $args->after;
  502.  
  503. /**
  504. * Filter a menu item's starting output.
  505. *
  506. * The menu item's starting output only includes `$args->before`, the opening `<a>`,
  507. * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
  508. * no filter for modifying the opening and closing `<li>` for a menu item.
  509. *
  510. * @since 3.0.0
  511. *
  512. * @param string $item_output The menu item's starting HTML output.
  513. * @param object $item Menu item data object.
  514. * @param int $depth Depth of menu item. Used for padding.
  515. * @param array $args An array of {@see wp_nav_menu()} arguments.
  516. */
  517. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  518. }
  519. }
Advertisement
Add Comment
Please, Sign In to add comment