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
5 days ago
NavMenuSyncMenu.php
8 years ago
NavMenuTrashActions.php
7 years ago
NavMenuSyncListing.php
137 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 | * Private Pages Sync |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $private_enabled = false; |
| 30 | |
| 31 | /** |
| 32 | * Post Data Factory |
| 33 | */ |
| 34 | private $post_factory; |
| 35 | |
| 36 | public function __construct() |
| 37 | { |
| 38 | parent::__construct(); |
| 39 | $this->private_enabled = $this->settings->privateMenuEnabled(); |
| 40 | $this->post_factory = new PostDataFactory; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Recursive function loops through pages/links and their children |
| 45 | */ |
| 46 | public function sync($parent = 0, $menu_parent = 0, $nest_level = 0) |
| 47 | { |
| 48 | $post_types = ['page']; |
| 49 | if ( !$this->integrations->plugins->wpml->installed ) $post_types[] = 'np-redirect'; |
| 50 | try { |
| 51 | $this->count = $this->count + 1; |
| 52 | $args = [ |
| 53 | 'post_type' => $post_types, |
| 54 | 'posts_per_page' => -1, |
| 55 | 'post_status' => ['publish', 'pending', 'draft', 'private', 'future', 'trash'], |
| 56 | 'orderby' => 'menu_order', |
| 57 | 'order' => 'ASC', |
| 58 | 'post_parent' => $parent |
| 59 | ]; |
| 60 | $page_q = new \WP_Query(apply_filters('nestedpages_menu_sync', $args, $nest_level)); |
| 61 | if ( $page_q->have_posts() ) : while ( $page_q->have_posts() ) : $page_q->the_post(); |
| 62 | $nest_level++; |
| 63 | global $post; |
| 64 | $this->post = $this->post_factory->build($post); |
| 65 | $this->syncPost($menu_parent, $nest_level); |
| 66 | endwhile; endif; wp_reset_postdata(); |
| 67 | } catch ( \Exception $e ){ |
| 68 | throw new \Exception($e->getMessage()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sync an individual item |
| 74 | * @since 1.3.4 |
| 75 | */ |
| 76 | private function syncPost($menu_parent, $nest_level) |
| 77 | { |
| 78 | // Get the Menu Item |
| 79 | $query_type = ( $this->post->type == 'np-redirect' ) ? 'xfn' : 'object_id'; |
| 80 | $menu_item_id = $this->nav_menu_repo->getMenuItem($this->post->id, $query_type); |
| 81 | if ( $this->post->nav_status == 'hide' ) return $this->removeItem($menu_item_id); |
| 82 | if ( $this->post->post_status !== 'publish' && !$this->private_enabled ) return $this->removeItem($menu_item_id); |
| 83 | $menu = $this->syncMenuItem($menu_parent, $menu_item_id); |
| 84 | $this->sync( $this->post->id, $menu, $nest_level ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sync Link Menu Item |
| 89 | * @since 1.1.4 |
| 90 | */ |
| 91 | private function syncMenuItem($menu_parent, $menu_item_id) |
| 92 | { |
| 93 | $type = ( $this->post->nav_type ) ? $this->post->nav_type : 'custom'; |
| 94 | $object = ( $this->post->nav_object ) ? $this->post->nav_object : 'custom'; |
| 95 | $object_id = ( $this->post->nav_object_id ) ? intval($this->post->nav_object_id) : null; |
| 96 | $url = ( $type == 'custom' ) ? esc_url($this->post->content) : ''; |
| 97 | $xfn = $this->post->id; |
| 98 | $title = ( $this->post->nav_title && $this->post->type == 'page' ) ? $this->post->nav_title : $this->post->title; |
| 99 | |
| 100 | // Compatibility for 1.4.1 - Reset Page links |
| 101 | if ( $this->post->type == 'page' ){ |
| 102 | $type = 'post_type'; |
| 103 | $object = 'page'; |
| 104 | $object_id = $this->post->id; |
| 105 | $xfn = 'page'; |
| 106 | } |
| 107 | |
| 108 | // WP 4.4 Fix, empty nav title attribute causing post_excerpt null error |
| 109 | $attr_title = ( $this->post->nav_title_attr ) ? $this->post->nav_title_attr : ''; |
| 110 | |
| 111 | $args = [ |
| 112 | 'menu-item-title' => $title, |
| 113 | 'menu-item-position' => $this->count, |
| 114 | 'menu-item-url' => $url, |
| 115 | 'menu-item-attr-title' => $attr_title, |
| 116 | 'menu-item-status' => 'publish', |
| 117 | 'menu-item-classes' => ( $this->post->nav_css ) ? $this->post->nav_css : '', |
| 118 | 'menu-item-type' => $type, |
| 119 | 'menu-item-object' => $object, |
| 120 | 'menu-item-object-id' => $object_id, |
| 121 | 'menu-item-parent-id' => $menu_parent, |
| 122 | 'menu-item-xfn' => $xfn, |
| 123 | 'menu-item-target' => $this->post->link_target, |
| 124 | 'menu-item-description' => $this->nav_menu_repo->getMenuItem($menu_item_id, 'description') |
| 125 | ]; |
| 126 | |
| 127 | $menu = wp_update_nav_menu_item($this->id, $menu_item_id, $args); |
| 128 | |
| 129 | if ( $this->post->nav_custom_url ) : |
| 130 | $url = $this->post->nav_custom_url; |
| 131 | update_post_meta( $menu, '_menu_item_url', $this->post->nav_custom_url ); |
| 132 | endif; |
| 133 | |
| 134 | return $menu; |
| 135 | } |
| 136 | } |
| 137 |