NavMenuActions.php
8 years ago
NavMenuFrontEnd.php
8 years ago
NavMenuRemoveItem.php
9 years ago
NavMenuRepository.php
8 years ago
NavMenuSync.php
9 years ago
NavMenuSyncListing.php
8 years ago
NavMenuSyncMenu.php
8 years ago
NavMenuTrashActions.php
7 years ago
NavMenuSyncListing.php
121 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\NavMenu; |
| 3 | |
| 4 | use NestedPages\Entities\NavMenu\NavMenuSync; |
| 5 | use NestedPages\Helpers; |
| 6 | use NestedPages\Entities\Post\PostDataFactory; |
| 7 | |
| 8 | /** |
| 9 | * Syncs the Generated Menu to Match the Listing |
| 10 | */ |
| 11 | class NavMenuSyncListing extends NavMenuSync |
| 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 | public function __construct() |
| 31 | { |
| 32 | parent::__construct(); |
| 33 | $this->post_factory = new PostDataFactory; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Recursive function loops through pages/links and their children |
| 38 | */ |
| 39 | public function sync($parent = 0, $menu_parent = 0, $nest_level = 0) |
| 40 | { |
| 41 | $post_types = ['page']; |
| 42 | if ( !$this->integrations->plugins->wpml->installed ) $post_types[] = 'np-redirect'; |
| 43 | try { |
| 44 | $this->count = $this->count + 1; |
| 45 | $args = [ |
| 46 | 'post_type' => $post_types, |
| 47 | 'posts_per_page' => -1, |
| 48 | 'post_status' => 'publish', |
| 49 | 'orderby' => 'menu_order', |
| 50 | 'order' => 'ASC', |
| 51 | 'post_parent' => $parent |
| 52 | ]; |
| 53 | $page_q = new \WP_Query(apply_filters('nestedpages_menu_sync', $args, $nest_level)); |
| 54 | if ( $page_q->have_posts() ) : while ( $page_q->have_posts() ) : $page_q->the_post(); |
| 55 | $nest_level++; |
| 56 | global $post; |
| 57 | $this->post = $this->post_factory->build($post); |
| 58 | $this->syncPost($menu_parent, $nest_level); |
| 59 | endwhile; endif; wp_reset_postdata(); |
| 60 | } catch ( \Exception $e ){ |
| 61 | throw new \Exception($e->getMessage()); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sync an individual item |
| 67 | * @since 1.3.4 |
| 68 | */ |
| 69 | private function syncPost($menu_parent, $nest_level) |
| 70 | { |
| 71 | // Get the Menu Item |
| 72 | $query_type = ( $this->post->type == 'np-redirect' ) ? 'xfn' : 'object_id'; |
| 73 | $menu_item_id = $this->nav_menu_repo->getMenuItem($this->post->id, $query_type); |
| 74 | if ( $this->post->nav_status == 'hide' ) return $this->removeItem($menu_item_id); |
| 75 | $menu = $this->syncMenuItem($menu_parent, $menu_item_id); |
| 76 | $this->sync( $this->post->id, $menu, $nest_level ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sync Link Menu Item |
| 81 | * @since 1.1.4 |
| 82 | */ |
| 83 | private function syncMenuItem($menu_parent, $menu_item_id) |
| 84 | { |
| 85 | $type = ( $this->post->nav_type ) ? $this->post->nav_type : 'custom'; |
| 86 | $object = ( $this->post->nav_object ) ? $this->post->nav_object : 'custom'; |
| 87 | $object_id = ( $this->post->nav_object_id ) ? intval($this->post->nav_object_id) : null; |
| 88 | $url = ( $type == 'custom' ) ? esc_url($this->post->content) : ''; |
| 89 | $xfn = $this->post->id; |
| 90 | $title = ( $this->post->nav_title && $this->post->type == 'page' ) ? $this->post->nav_title : $this->post->title; |
| 91 | |
| 92 | // Compatibility for 1.4.1 - Reset Page links |
| 93 | if ( $this->post->type == 'page' ){ |
| 94 | $type = 'post_type'; |
| 95 | $object = 'page'; |
| 96 | $object_id = $this->post->id; |
| 97 | $xfn = 'page'; |
| 98 | } |
| 99 | |
| 100 | // WP 4.4 Fix, empty nav title attribute causing post_excerpt null error |
| 101 | $attr_title = ( $this->post->nav_title_attr ) ? $this->post->nav_title_attr : ''; |
| 102 | |
| 103 | $args = [ |
| 104 | 'menu-item-title' => $title, |
| 105 | 'menu-item-position' => $this->count, |
| 106 | 'menu-item-url' => $url, |
| 107 | 'menu-item-attr-title' => $attr_title, |
| 108 | 'menu-item-status' => 'publish', |
| 109 | 'menu-item-classes' => $this->post->nav_css, |
| 110 | 'menu-item-type' => $type, |
| 111 | 'menu-item-object' => $object, |
| 112 | 'menu-item-object-id' => $object_id, |
| 113 | 'menu-item-parent-id' => $menu_parent, |
| 114 | 'menu-item-xfn' => $xfn, |
| 115 | 'menu-item-target' => $this->post->link_target, |
| 116 | 'menu-item-description' => ' ' |
| 117 | ]; |
| 118 | $menu = wp_update_nav_menu_item($this->id, $menu_item_id, $args); |
| 119 | return $menu; |
| 120 | } |
| 121 | } |