PluginProbe ʕ •ᴥ•ʔ
Nested Pages / 1.3.4
Nested Pages v1.3.4
3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.2 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.3.1 1.6.3.2 1.6.4 1.6.5 1.6.5.1 1.6.5.2 1.6.6 1.6.7 1.6.8 1.7.0 1.7.1 2.0.1 2.0.2 2.0.3 2.0.4 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.21 3.1.22 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7
wp-nested-pages / app / Entities / NavMenu / NavMenuSyncMenu.php
wp-nested-pages / app / Entities / NavMenu Last commit date
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