Activation
3 years ago
Config
2 years ago
Entities
1 year ago
Form
3 years ago
Views
1 year ago
Bootstrap.php
3 years ago
FrontEndBootstrap.php
4 years ago
Helpers.php
7 years ago
NestedPages.php
1 year ago
Redirects.php
5 years ago
RedirectsFrontEnd.php
8 years ago
RedirectsFrontEnd.php
90 lines
| 1 | <?php |
| 2 | namespace NestedPages; |
| 3 | |
| 4 | /** |
| 5 | * Fixes issue when pages are nested under non-page post types (np-redirect) |
| 6 | */ |
| 7 | class RedirectsFrontEnd |
| 8 | { |
| 9 | public function __construct() |
| 10 | { |
| 11 | add_filter('page_link', [$this, 'pageLinks'],10,2); |
| 12 | add_action('parse_request', [$this, 'parseRequest']); |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Remove np-redirect slugs from links |
| 17 | */ |
| 18 | public function pageLinks( $post_link, $id = 0 ) |
| 19 | { |
| 20 | $post = get_post($id); |
| 21 | $post_link = $this->renameLinkSlugs($post, $post_link); |
| 22 | return $post_link; |
| 23 | } |
| 24 | |
| 25 | public function parseRequest($wp) |
| 26 | { |
| 27 | if ( isset($wp->query_vars['error']) ) $slug = basename($wp->request); |
| 28 | if ( isset($wp->query_vars['pagename']) && ! empty($wp->query_vars['pagename']) ) $slug = $wp->query_vars['pagename']; |
| 29 | if ( isset($wp->query_vars['name']) && ! empty($wp->query_vars['name']) ) $slug = $wp->query_vars['name']; |
| 30 | if ( isset($wp->query_vars['attachment']) && ! empty($wp->query_vars['attachment']) ) $slug = $wp->query_vars['attachment']; |
| 31 | if ( !isset($slug) ) return; |
| 32 | |
| 33 | $segments = explode('/', $wp->request); |
| 34 | $slug = basename($slug); |
| 35 | |
| 36 | // If this is a redirect link, strip out the np-r and go to the original |
| 37 | if ( substr($slug, -4) == 'np-r' ){ |
| 38 | $slug = substr($slug, 0, -5); |
| 39 | $wp->request = $slug; |
| 40 | $wp->query_vars['pagename'] = $slug; |
| 41 | $wp->query_vars['name'] = $slug; |
| 42 | return; |
| 43 | }; |
| 44 | |
| 45 | $redirect = false; |
| 46 | if ( count($segments) == 1 ) return; |
| 47 | |
| 48 | $parent_slug = $segments[count($segments) - 2]; |
| 49 | if ( substr($parent_slug, -4) == 'np-r' ){ |
| 50 | $redirect = true; |
| 51 | $parent_slug = substr($parent_slug, 0, -5); |
| 52 | } |
| 53 | $parent_args = [ |
| 54 | 'name' => sanitize_text_field($parent_slug), |
| 55 | 'posts_per_page' => 1 |
| 56 | ]; |
| 57 | $parent_args['post_type'] = ( $redirect ) ? 'np-redirect' : 'any'; |
| 58 | $parent_post = get_posts($parent_args); |
| 59 | |
| 60 | $page_args = [ |
| 61 | 'name' => sanitize_text_field($slug), |
| 62 | 'post_type' => 'any', |
| 63 | 'posts_per_page' => 1 |
| 64 | ]; |
| 65 | $page_args['post_parent'] = ( isset($parent_post) && $redirect ) ? $parent_post[0]->ID : null; |
| 66 | $page = get_posts($page_args); |
| 67 | if ( !$page ) return; |
| 68 | |
| 69 | unset($wp->query_vars['attachment']); |
| 70 | unset($wp->query_vars['error']); |
| 71 | |
| 72 | $wp->query_vars['page_id'] = $page[0]->ID; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Recursive function removes non-page parent slugs |
| 77 | */ |
| 78 | private function renameLinkSlugs($post, $slug) |
| 79 | { |
| 80 | if ( is_admin() ) return $slug; |
| 81 | if ( $post->post_parent > 0 ) { |
| 82 | $parent_post = get_post($post->post_parent); |
| 83 | if ( $parent_post->post_type == 'np-redirect' ){ |
| 84 | $slug = str_replace($parent_post->post_name . '/', $parent_post->post_name . '-np-r/', $slug); |
| 85 | } |
| 86 | return $this->renameLinkSlugs($parent_post, $slug); |
| 87 | } |
| 88 | return $slug; |
| 89 | } |
| 90 | } |