PluginProbe ʕ •ᴥ•ʔ
Nested Pages / trunk
Nested Pages vtrunk
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 / RedirectsFrontEnd.php
wp-nested-pages / app Last commit date
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 }