class-np-posttypes.php
| 1 | <?php |
| 2 | require_once('class-np-navmenu.php'); |
| 3 | /** |
| 4 | * Post Types required by Nested Pages |
| 5 | */ |
| 6 | class NP_PostTypes { |
| 7 | |
| 8 | public function __construct() |
| 9 | { |
| 10 | add_action( 'init', array( $this, 'registerRedirects') ); |
| 11 | add_action( 'trashed_post', array( $this, 'trashHook' ) ); |
| 12 | } |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Redirects Post Type |
| 17 | */ |
| 18 | public function registerRedirects() |
| 19 | { |
| 20 | $labels = array( |
| 21 | 'name' => __('Redirects'), |
| 22 | 'singular_name' => __('Redirect'), |
| 23 | 'add_new_item'=> 'Add Redirect', |
| 24 | 'edit_item' => 'Edit Redirect', |
| 25 | 'view_item' => 'View Redirect' |
| 26 | ); |
| 27 | $args = array( |
| 28 | 'labels' => $labels, |
| 29 | 'public' => false, |
| 30 | 'show_ui' => false, |
| 31 | 'menu_position' => 5, |
| 32 | 'capability_type' => 'post', |
| 33 | 'hierarchical' => true, |
| 34 | 'has_archive' => true, |
| 35 | 'supports' => array('title','editor'), |
| 36 | 'rewrite' => array('slug' => 'np-redirect', 'with_front' => false) |
| 37 | ); |
| 38 | register_post_type( 'np-redirect' , $args ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * Trash hook - unset parent pages |
| 45 | */ |
| 46 | public function trashHook($post_id) |
| 47 | { |
| 48 | $post_type = get_post_type($post_id); |
| 49 | if ( $post_type == 'page' ) $this->resetToggles($post_id); |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Make sure children of trashed pages are viewable in Nested Pages |
| 55 | */ |
| 56 | private function resetToggles($post_id) |
| 57 | { |
| 58 | $visible_pages = unserialize(get_user_meta(get_current_user_id(), 'np_visible_pages', true)); |
| 59 | $child_pages = array(); |
| 60 | $children = new WP_Query(array('post_type'=>'page', 'posts_per_page'=>-1, 'post_parent'=>$post_id)); |
| 61 | if ( $children->have_posts() ) : while ( $children->have_posts() ) : $children->the_post(); |
| 62 | array_push($child_pages, get_the_id()); |
| 63 | endwhile; endif; wp_reset_postdata(); |
| 64 | foreach($child_pages as $child_page){ |
| 65 | if ( !in_array($child_page, $visible_pages) ) array_push($visible_pages, $child_page); |
| 66 | } |
| 67 | update_user_meta(get_current_user_id(), 'np_visible_pages', serialize($visible_pages)); |
| 68 | } |
| 69 | |
| 70 | } |