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