PostCloner.php
3 years ago
PostDataFactory.php
3 years ago
PostFactory.php
3 days ago
PostRepository.php
5 years ago
PostSaveActions.php
3 years ago
PostTrashActions.php
8 years ago
PostUpdateRepository.php
3 days ago
PrivatePostParent.php
3 years ago
PostTrashActions.php
94 lines
| 1 | <?php |
| 2 | namespace NestedPages\Entities\Post; |
| 3 | |
| 4 | use NestedPages\Entities\User\UserRepository; |
| 5 | use NestedPages\Entities\NavMenu\NavMenuSyncListing; |
| 6 | use NestedPages\Entities\NavMenu\NavMenuRemoveItem; |
| 7 | use NestedPages\Entities\NavMenu\NavMenuRepository; |
| 8 | |
| 9 | /** |
| 10 | * WP Actions tied to a Post |
| 11 | */ |
| 12 | class PostTrashActions |
| 13 | { |
| 14 | /** |
| 15 | * User Repository |
| 16 | * @var object |
| 17 | */ |
| 18 | private $user_repo; |
| 19 | |
| 20 | /** |
| 21 | * Nav Menu Repository |
| 22 | */ |
| 23 | private $nav_menu_repo; |
| 24 | |
| 25 | public function __construct() |
| 26 | { |
| 27 | $this->user_repo = new UserRepository; |
| 28 | $this->nav_menu_repo = new NavMenuRepository; |
| 29 | add_action( 'trashed_post', [$this, 'trashHook']); |
| 30 | add_action( 'delete_post', [$this, 'removeLinkNavItem'], 10 ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Trash hook - make sure child pages of trashed page are visible |
| 35 | * @since 1.3.4 |
| 36 | */ |
| 37 | public function trashHook($post_id) |
| 38 | { |
| 39 | $post_type = get_post_type($post_id); |
| 40 | $this->resetToggles($post_id, $post_type); |
| 41 | if ( get_option('nestedpages_menusync') !== 'sync' ) return; |
| 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 | * Remove the nav menu item |
| 62 | */ |
| 63 | private function removeNavMenuItem($post_id) |
| 64 | { |
| 65 | $query_type = ( get_post_type($post_id) == 'np-redirect' ) ? 'xfn' : 'object_id'; |
| 66 | $nav_item_id = $this->nav_menu_repo->getMenuItem($post_id, $query_type); |
| 67 | if ( $nav_item_id ) new NavMenuRemoveItem($nav_item_id); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Make sure children of trashed pages are viewable in Nested Pages |
| 72 | */ |
| 73 | private function resetToggles($post_id, $post_type) |
| 74 | { |
| 75 | $visible_pages = $this->user_repo->getVisiblePages(); |
| 76 | if ( !isset($visible_pages[$post_type]) ) return; |
| 77 | $visible_pages = $visible_pages[$post_type]; |
| 78 | |
| 79 | if ( !isset($visible_pages[$post_type]) ) return; |
| 80 | |
| 81 | $child_pages = []; |
| 82 | |
| 83 | $children = new \WP_Query(['post_type'=>$post_type, 'posts_per_page'=>-1, 'post_parent'=>$post_id]); |
| 84 | if ( $children->have_posts() ) : while ( $children->have_posts() ) : $children->the_post(); |
| 85 | array_push($child_pages, get_the_id()); |
| 86 | endwhile; endif; wp_reset_postdata(); |
| 87 | |
| 88 | foreach($child_pages as $child_page){ |
| 89 | if ( !in_array($child_page, $visible_pages) ) array_push($visible_pages, $child_page); |
| 90 | } |
| 91 | |
| 92 | $this->user_repo->updateVisiblePages($post_type, $visible_pages); |
| 93 | } |
| 94 | } |