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
NavMenuSyncListing.php
127 lines
| 1 | <?php namespace NestedPages\Entities\NavMenu; |
| 2 | |
| 3 | use NestedPages\Entities\NavMenu\NavMenuSync; |
| 4 | use NestedPages\Helpers; |
| 5 | use NestedPages\Entities\Post\PostDataFactory; |
| 6 | |
| 7 | /** |
| 8 | * Syncs the Generated Menu to Match the Listing |
| 9 | */ |
| 10 | class NavMenuSyncListing extends NavMenuSync implements NavMenuSyncInterface { |
| 11 | |
| 12 | |
| 13 | /** |
| 14 | * Individual Post |
| 15 | * @var array |
| 16 | */ |
| 17 | private $post; |
| 18 | |
| 19 | /** |
| 20 | * Menu Position Count |
| 21 | * @var int |
| 22 | */ |
| 23 | private $count = 0; |
| 24 | |
| 25 | /** |
| 26 | * Post Data Factory |
| 27 | */ |
| 28 | private $post_factory; |
| 29 | |
| 30 | |
| 31 | public function __construct() |
| 32 | { |
| 33 | parent::__construct(); |
| 34 | $this->post_factory = new PostDataFactory; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * Create the menu with nested pages |
| 40 | */ |
| 41 | public function sync($parent = 0, $menu_parent = 0) |
| 42 | { |
| 43 | $this->count = $this->count + 1; |
| 44 | $page_q = new \WP_Query(array( |
| 45 | 'post_type' => array('page','np-redirect'), |
| 46 | 'posts_per_page' => -1, |
| 47 | 'post_status' => 'publish', |
| 48 | 'orderby' => 'menu_order', |
| 49 | 'order' => 'ASC', |
| 50 | 'post_parent' => $parent |
| 51 | )); |
| 52 | if ( $page_q->have_posts() ) : while ( $page_q->have_posts() ) : $page_q->the_post(); |
| 53 | global $post; |
| 54 | $this->post = $this->post_factory->build($post); |
| 55 | $this->syncItem($menu_parent); |
| 56 | endwhile; endif; wp_reset_postdata(); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Sync an individual item |
| 62 | * @since 1.3.4 |
| 63 | */ |
| 64 | private function syncItem($menu_parent) |
| 65 | { |
| 66 | // Get the Menu Item ID using the post ID |
| 67 | $menu_item_id = $this->nav_menu_repo->getMenuItemID($this->post->id); |
| 68 | |
| 69 | if ( $this->post->nav_status == 'hide' ) return $this->removeItem($menu_item_id); |
| 70 | |
| 71 | $menu = ( $this->post->type == 'page' ) |
| 72 | ? $this->syncPageItem($menu_parent, $menu_item_id) |
| 73 | : $this->syncLinkItem($menu_parent, $menu_item_id); |
| 74 | |
| 75 | $this->sync( $this->post->id, $menu ); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Sync Page Menu Item |
| 82 | * @since 1.1.4 |
| 83 | */ |
| 84 | private function syncPageItem($menu_parent, $menu_item_id) |
| 85 | { |
| 86 | $menu = wp_update_nav_menu_item($this->id, $menu_item_id, array( |
| 87 | 'menu-item-title' => $this->post->nav_title, |
| 88 | 'menu-item-position' => $this->count, |
| 89 | 'menu-item-url' => $this->post->link, |
| 90 | 'menu-item-attr-title' => $this->post->nav_title_attr, |
| 91 | 'menu-item-status' => 'publish', |
| 92 | 'menu-item-classes' => $this->post->nav_css, |
| 93 | 'menu-item-type' => 'post_type', |
| 94 | 'menu-item-object' => 'page', |
| 95 | 'menu-item-object-id' => $this->post->id, |
| 96 | 'menu-item-parent-id' => $menu_parent, |
| 97 | 'menu-item-target' => $this->post->link_target |
| 98 | )); |
| 99 | return $menu; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Sync Link Menu Item |
| 105 | * @since 1.1.4 |
| 106 | */ |
| 107 | private function syncLinkItem($menu_parent, $menu_item_id) |
| 108 | { |
| 109 | $menu = wp_update_nav_menu_item($this->id, $menu_item_id, array( |
| 110 | 'menu-item-title' => $this->post->title, |
| 111 | 'menu-item-position' => $this->count, |
| 112 | 'menu-item-url' => Helpers::check_url(get_the_content($this->post->id)), |
| 113 | 'menu-item-attr-title' => $this->post->nav_title_attr, |
| 114 | 'menu-item-status' => 'publish', |
| 115 | 'menu-item-classes' => $this->post->nav_css, |
| 116 | 'menu-item-type' => 'custom', |
| 117 | 'menu-item-object' => 'np-redirect', |
| 118 | 'menu-item-object-id' => $this->post->id, |
| 119 | 'menu-item-parent-id' => $menu_parent, |
| 120 | 'menu-item-xfn' => $this->post->id, |
| 121 | 'menu-item-target' => $this->post->link_target |
| 122 | )); |
| 123 | return $menu; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | } |