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
6 days ago
NavMenuSyncMenu.php
8 years ago
NavMenuTrashActions.php
7 years ago
NavMenuSyncMenu.php
161 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\NavMenu; |
| 3 | |
| 4 | use NestedPages\Entities\NavMenu\NavMenuSync; |
| 5 | use NestedPages\Helpers; |
| 6 | use NestedPages\Entities\Post\PostUpdateRepository; |
| 7 | use NestedPages\Entities\Post\PostRepository; |
| 8 | use NestedPages\Entities\NavMenu\NavMenuRepository; |
| 9 | |
| 10 | /** |
| 11 | * Syncs the Listing to Match the Menu |
| 12 | */ |
| 13 | class NavMenuSyncMenu extends NavMenuSync |
| 14 | { |
| 15 | /** |
| 16 | * Menu Items |
| 17 | * @var array of objects |
| 18 | */ |
| 19 | private $menu_items; |
| 20 | |
| 21 | /** |
| 22 | * Menu Index |
| 23 | * @var array |
| 24 | */ |
| 25 | private $menu_index; |
| 26 | |
| 27 | /** |
| 28 | * Post Update Repository |
| 29 | * @var object |
| 30 | */ |
| 31 | private $post_update_repo; |
| 32 | |
| 33 | /** |
| 34 | * Post Repository |
| 35 | * @var object |
| 36 | */ |
| 37 | private $post_repo; |
| 38 | |
| 39 | public function __construct() |
| 40 | { |
| 41 | parent::__construct(); |
| 42 | $this->post_update_repo = new PostUpdateRepository; |
| 43 | $this->post_repo = new PostRepository; |
| 44 | $this->setMenuItems(); |
| 45 | $this->sync(); |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get the menu items from menu and set them |
| 51 | */ |
| 52 | private function setMenuItems() |
| 53 | { |
| 54 | $this->menu_items = wp_get_nav_menu_items($this->id); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Loop through the menu items and sync depending on type |
| 59 | */ |
| 60 | public function sync() |
| 61 | { |
| 62 | if ( get_option('nestedpages_menusync') !== 'sync' ) return; |
| 63 | $this->setMenuIndex(); |
| 64 | $this->updatePagesNavStatus(); |
| 65 | foreach($this->menu_items as $key => $item){ |
| 66 | $this->updatePost($item); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Set Menu Order/Parent Index |
| 72 | */ |
| 73 | private function setMenuIndex() |
| 74 | { |
| 75 | foreach($this->menu_items as $key => $item){ |
| 76 | $this->index[$item->ID] = [ |
| 77 | 'ID' => $item->object_id, |
| 78 | 'title' => $item->title |
| 79 | ]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Update the WP Post with Menu Data |
| 85 | */ |
| 86 | private function updatePost($item) |
| 87 | { |
| 88 | $parent_id = ( $item->menu_item_parent == '0' || !isset($this->index[$item->menu_item_parent]['ID']) ) ? 0 : $this->index[$item->menu_item_parent]['ID']; |
| 89 | |
| 90 | if ( $this->nav_menu_repo->isNavMenuItem($parent_id) ) { |
| 91 | $parent_id = $this->nav_menu_repo->getLinkfromTitle($this->index[$item->menu_item_parent]['title']); |
| 92 | } |
| 93 | |
| 94 | $post_id = ( $item->xfn !== 'page' ) |
| 95 | ? $item->xfn |
| 96 | : $item->object_id; |
| 97 | |
| 98 | $post_data = [ |
| 99 | 'menu_order' => $item->menu_order, |
| 100 | 'post_id' => $post_id, |
| 101 | 'link_target' => $item->target, |
| 102 | 'np_nav_title' => $item->title, |
| 103 | 'np_title_attribute' => $item->attr_title, |
| 104 | 'post_parent' => $parent_id, |
| 105 | 'np_nav_css_classes' => $item->classes |
| 106 | ]; |
| 107 | if ( $item->type == 'custom' ) { |
| 108 | $post_data['content'] = $item->url; |
| 109 | $post_data['post_id'] = $item->xfn; |
| 110 | } |
| 111 | |
| 112 | if ( is_string(get_post_status($post_id)) ){ |
| 113 | $this->post_update_repo->updateFromMenuItem($post_data); |
| 114 | } else { |
| 115 | $this->syncNewLink($item, $parent_id); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Sync a new Link |
| 121 | */ |
| 122 | private function syncNewLink($item, $parent_id) |
| 123 | { |
| 124 | if ( $this->integrations->plugins->wpml->installed ) return; |
| 125 | $post_data = [ |
| 126 | 'menuTitle' => $item->title, |
| 127 | 'np_link_title' => $item->title, |
| 128 | '_status' => 'publish', |
| 129 | 'np_link_content' => $item->url, |
| 130 | 'parent_id' => $parent_id, |
| 131 | 'post_type' => 'np-redirect', |
| 132 | 'link_target' => $item->target, |
| 133 | 'menu_order'=> $item->menu_order, |
| 134 | 'menuType' => $item->type, |
| 135 | 'objectType' => $item->object, |
| 136 | 'objectId' => $item->object_id, |
| 137 | 'titleAttribute' => $item->attr_title |
| 138 | ]; |
| 139 | $post_id = $this->post_update_repo->saveRedirect($post_data); |
| 140 | update_post_meta($item->ID, '_menu_item_xfn', absint($post_id)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Update NP Nav Status for Pages |
| 145 | * If a menu item is removed, it's nav status needs to be set to hide |
| 146 | */ |
| 147 | private function updatePagesNavStatus() |
| 148 | { |
| 149 | $visible_pages = $this->nav_menu_repo->getPagesInMenu(); |
| 150 | foreach($this->index as $key => $item){ |
| 151 | $menu_items[$key] = $item['ID']; |
| 152 | } |
| 153 | foreach($visible_pages as $page){ |
| 154 | if ( !in_array($page, $menu_items) ) { |
| 155 | $data['post_id'] = $page; |
| 156 | $data['nav_status'] = 'hide'; |
| 157 | $this->post_update_repo->updateNavStatus($data); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } |