PostDataFactory.php
11 years ago
PostFactory.php
11 years ago
PostRepository.php
11 years ago
PostTrashActions.php
11 years ago
PostUpdateRepository.php
11 years ago
PostTrashActions.php
93 lines
| 1 | <?php namespace NestedPages\Entities\Post; |
| 2 | |
| 3 | use NestedPages\Entities\User\UserRepository; |
| 4 | use NestedPages\Entities\NavMenu\NavMenuSyncListing; |
| 5 | use NestedPages\Entities\NavMenu\NavMenuRemoveItem; |
| 6 | use NestedPages\Entities\NavMenu\NavMenuRepository; |
| 7 | |
| 8 | /** |
| 9 | * WP Actions tied to a Post |
| 10 | */ |
| 11 | class PostTrashActions { |
| 12 | |
| 13 | /** |
| 14 | * User Repository |
| 15 | * @var object |
| 16 | */ |
| 17 | private $user_repo; |
| 18 | |
| 19 | /** |
| 20 | * Nav Menu Repository |
| 21 | */ |
| 22 | private $nav_menu_repo; |
| 23 | |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->user_repo = new UserRepository; |
| 28 | $this->nav_menu_repo = new NavMenuRepository; |
| 29 | add_action( 'trashed_post', array( $this, 'trashHook' ) ); |
| 30 | add_action( 'delete_post', array( $this, 'removeLinkNavItem'), 10 ); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Trash hook - make sure child pages of trashed page are visible |
| 36 | * @since 1.3.4 |
| 37 | */ |
| 38 | public function trashHook($post_id) |
| 39 | { |
| 40 | $post_type = get_post_type($post_id); |
| 41 | $this->resetToggles($post_id, $post_type); |
| 42 | $this->removeNavMenuItem($post_id); |
| 43 | if ( $post_type == 'page' ){ |
| 44 | $sync = new NavMenuSyncListing; |
| 45 | $sync->sync(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Link Post Types are immediately deleted, so trash hook isn't called |
| 51 | * Must remove nav menu items when they're deleted |
| 52 | */ |
| 53 | public function removeLinkNavItem($post_id) |
| 54 | { |
| 55 | $post_type = get_post_type($post_id); |
| 56 | if ( $post_type == 'nav_menu_item' ) return; |
| 57 | if ( $post_type == 'np-redirect' ) $this->removeNavMenuItem($post_id); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * Remove the nav menu item |
| 63 | */ |
| 64 | private function removeNavMenuItem($post_id) |
| 65 | { |
| 66 | $nav_item_id = $this->nav_menu_repo->getMenuItemID($post_id); |
| 67 | if ( $nav_item_id ) new NavMenuRemoveItem($nav_item_id); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * Make sure children of trashed pages are viewable in Nested Pages |
| 73 | */ |
| 74 | private function resetToggles($post_id, $post_type) |
| 75 | { |
| 76 | $visible_pages = $this->user_repo->getVisiblePages(); |
| 77 | $visible_pages = $visible_pages[$post_type]; |
| 78 | |
| 79 | $child_pages = array(); |
| 80 | |
| 81 | $children = new \WP_Query(array('post_type'=>$post_type, 'posts_per_page'=>-1, 'post_parent'=>$post_id)); |
| 82 | if ( $children->have_posts() ) : while ( $children->have_posts() ) : $children->the_post(); |
| 83 | array_push($child_pages, get_the_id()); |
| 84 | endwhile; endif; wp_reset_postdata(); |
| 85 | |
| 86 | foreach($child_pages as $child_page){ |
| 87 | if ( !in_array($child_page, $visible_pages) ) array_push($visible_pages, $child_page); |
| 88 | } |
| 89 | |
| 90 | $this->user_repo->updateVisiblePages($post_type, $visible_pages); |
| 91 | } |
| 92 | |
| 93 | } |