NavMenuActions.php
4 years ago
NavMenuFrontEnd.php
3 years ago
NavMenuRemoveItem.php
8 years ago
NavMenuRepository.php
4 years ago
NavMenuSync.php
3 years ago
NavMenuSyncListing.php
4 days ago
NavMenuSyncMenu.php
8 years ago
NavMenuTrashActions.php
7 years ago
NavMenuFrontEnd.php
59 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\NavMenu; |
| 3 | |
| 4 | use NestedPages\Entities\NavMenu\NavMenuRepository; |
| 5 | |
| 6 | /** |
| 7 | * Edits/Corrections to the generated nav menu on the front-end |
| 8 | */ |
| 9 | class NavMenuFrontEnd |
| 10 | { |
| 11 | /** |
| 12 | * Nav Menu Repository |
| 13 | */ |
| 14 | private $nav_menu_repo; |
| 15 | |
| 16 | public function __construct() |
| 17 | { |
| 18 | $this->nav_menu_repo = new NavMenuRepository; |
| 19 | add_filter('nav_menu_link_attributes', [$this, 'linkAttribute'], 10, 3); |
| 20 | add_filter('nav_menu_link_attributes', [$this, 'hrefAttribute'], 10, 3); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Filter the link attributes on the generated menu |
| 25 | */ |
| 26 | public function linkAttribute($atts, $item, $args) |
| 27 | { |
| 28 | if ( get_option('nestedpages_menusync') !== 'sync' ) return $atts; |
| 29 | if ( get_option('nestedpages_disable_menu') == 'true' ) return $atts; |
| 30 | if ( $this->nav_menu_repo->getMenuID() == null ) return $atts; |
| 31 | |
| 32 | if ( !isset($args->menu->term_id) ) return $atts; |
| 33 | if ( $args->menu->term_id !== $this->nav_menu_repo->getMenuID() ) return $atts; |
| 34 | |
| 35 | // Remove the rel= attribute created from saving the menu object for syncing |
| 36 | foreach($atts as $attribute => $value){ |
| 37 | if ( strtolower($attribute) != 'rel' ) continue; |
| 38 | if ( $value == $item->object ) unset($atts[$attribute]); |
| 39 | if ( is_numeric($value) ) unset($atts[$attribute]); |
| 40 | } |
| 41 | |
| 42 | return $atts; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Add a custom link if one is set |
| 47 | */ |
| 48 | public function hrefAttribute($atts, $item, $args) |
| 49 | { |
| 50 | if ( get_option('nestedpages_menusync') !== 'sync' ) return $atts; |
| 51 | if ( get_option('nestedpages_disable_menu') == 'true' ) return $atts; |
| 52 | if ( $this->nav_menu_repo->getMenuID() == null ) return $atts; |
| 53 | if ( !isset($item->object_id) ) return $atts; |
| 54 | $custom_url = get_post_meta($item->object_id, '_np_nav_custom_url', true); |
| 55 | if ( !$custom_url ) return $atts; |
| 56 | $atts['href'] = $custom_url; |
| 57 | return $atts; |
| 58 | } |
| 59 | } |