Skip to content
Newer
Older
100644 239 lines (220 sloc) 5.88 KB
9b5f441 @jbrinley most of WP_Route, plus tests
authored May 19, 2011
1 <?php
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 9, 2012
2
9b5f441 @jbrinley most of WP_Route, plus tests
authored May 19, 2011
3 class WP_Router_Page extends WP_Router_Utility {
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
4 const POST_TYPE = 'wp_router_page';
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
5
9e67a94 @jbrinley do a 403 error or a login redirect when access is denied
authored May 22, 2011
6 protected static $rewrite_slug = 'WP_Router';
7 protected static $post_id = 0; // The ID of the post this plugin uses
9b5f441 @jbrinley most of WP_Route, plus tests
authored May 19, 2011
8
9e67a94 @jbrinley do a 403 error or a login redirect when access is denied
authored May 23, 2011
9 protected $contents = '';
10 protected $title = '';
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
11 protected $template = '';
7b743f8 @jbrinley filter permalink for wp router pages where possible, provide placehol…
authored Aug 6, 2012
12 protected $meta = array();
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
13
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
14 public static function init() {
15 self::register_post_type();
16 }
17
18 /**
19 * Register a post type to use when displaying pages
20 * @static
21 * @return void
22 */
23 private static function register_post_type() {
24 // a very quiet post type
25 $args = array(
26 'public' => FALSE,
27 'show_ui' => FALSE,
28 'exclude_from_search' => TRUE,
29 'publicly_queryable' => TRUE,
30 'show_in_menu' => FALSE,
31 'show_in_nav_menus' => FALSE,
32 'supports' => array('title'),
33 'has_archive' => TRUE,
34 'rewrite' => array(
35 'slug' => self::$rewrite_slug,
36 'with_front' => FALSE,
37 'feeds' => FALSE,
38 'pages' => FALSE,
39 )
40 );
41 register_post_type(self::POST_TYPE, $args);
42 }
43
44 /**
45 * Get the ID of the placeholder post
46 *
47 * @static
48 * @return int
49 */
9e67a94 @jbrinley do a 403 error or a login redirect when access is denied
authored May 23, 2011
50 protected static function get_post_id() {
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
51 if ( !self::$post_id ) {
52 $posts = get_posts(array(
53 'post_type' => self::POST_TYPE,
54 'post_status' => 'publish',
55 'posts_per_page' => 1,
56 ));
57 if ( $posts ) {
58 self::$post_id = $posts[0]->ID;
59 } else {
60 self::$post_id = self::make_post();
61 }
62 }
63 return self::$post_id;
64 }
65
66 /**
67 * Make a new placeholder post
68 *
69 * @static
70 * @return int The ID of the new post
71 */
72 private static function make_post() {
73 $post = array(
c05fe40 @jbrinley fix erroneous usage of __ and _e wrapper function
authored Jul 5, 2012
74 'post_title' => __('WP Router Placeholder Page', 'wp-router'),
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
75 'post_status' => 'publish',
76 'post_type' => self::POST_TYPE,
77 );
78 $id = wp_insert_post($post);
79 if ( is_wp_error($id) ) {
80 return 0;
81 }
82 return $id;
83 }
84
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
85 public function __construct( $contents, $title, $template = '' ) {
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
86 $this->contents = $contents;
87 $this->title = $title;
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
88 $this->template = $template;
89 $this->add_hooks();
90 }
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
91
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
92 protected function add_hooks() {
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
93 add_action('pre_get_posts', array($this, 'edit_query'), 10, 1);
94 add_action('the_post', array($this, 'set_post_contents'), 10, 1);
95 add_filter('the_title', array($this, 'get_title'), 10, 2);
3c59e00 @jbrinley minor bugfixes and a sample page
authored May 22, 2011
96 add_filter('single_post_title', array($this, 'get_single_post_title'), 10, 2);
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
97 add_filter('redirect_canonical', array($this, 'override_redirect'), 10, 2);
7b743f8 @jbrinley filter permalink for wp router pages where possible, provide placehol…
authored Aug 6, 2012
98 add_filter('get_post_metadata', array($this, 'set_post_meta'), 10, 4);
99 add_filter('post_type_link', array($this, 'override_permalink'), 10, 4);
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
100 if ( $this->template ) {
101 add_filter('template_include', array($this, 'override_template'), 10, 1);
102 }
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
103 }
104
105 /**
106 * Edit WordPress's query so it finds our placeholder page
107 *
108 * @param WP_Query $query
109 * @return void
110 */
111 public function edit_query( WP_Query $query ) {
112 if ( isset($query->query_vars[self::QUERY_VAR]) ) {
113 // make sure we get the right post
114 $query->query_vars['post_type'] = self::POST_TYPE;
115 $query->query_vars['p'] = self::get_post_id();
116
117 // override any vars WordPress set based on the original query
118 $query->is_single = TRUE;
3c59e00 @jbrinley minor bugfixes and a sample page
authored May 22, 2011
119 $query->is_singular = TRUE;
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
120 $query->is_404 = FALSE;
121 $query->is_home = FALSE;
122 }
123 }
124
125 /**
126 * Override the global $pages array to yield our content
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
127 *
ae35c00 @jbrinley move post type registration to WP_Router_Page, and flesh out said cla…
authored May 20, 2011
128 * @param object $post
129 * @return void
130 */
131 public function set_post_contents( $post ) {
132 global $pages;
133 $pages = array($this->contents);
134 // TODO: add a facility for multi-page documents?
135 }
136
137 /**
138 * Set the title for the placeholder page
139 *
140 * @param string $title
141 * @param int $post_id
142 * @return string
143 */
144 public function get_title( $title, $post_id ) {
145 if ( $post_id == self::get_post_id() ) {
146 $title = $this->title;
147 }
148 return $title;
149 }
3c59e00 @jbrinley minor bugfixes and a sample page
authored May 22, 2011
150
a15c77e @jbrinley WP 3.0 compatibility
authored Jun 1, 2011
151 /**
152 * Set the title for the placeholder page (again)
153 *
154 * @param string $title
155 * @param object $post
156 * @return string
157 */
158 public function get_single_post_title( $title, $post = NULL ) {
159 // in WP 3.0.x, $post might be NULL. Not true in WP 3.1
160 if ( !$post ) {
161 $post = $GLOBALS['post'];
162 }
3c59e00 @jbrinley minor bugfixes and a sample page
authored May 22, 2011
163 return $this->get_title($title, $post->ID);
164 }
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
165
a15c77e @jbrinley WP 3.0 compatibility
authored Jun 2, 2011
166 /**
167 * Use the specified template file
168 *
169 * @param string $template
170 * @return string
171 */
ed2a0c5 @jbrinley add template argument
authored May 24, 2011
172 public function override_template( $template ) {
173 if ( $this->template && file_exists($template) ) { // these checks shouldn't be necessary, but no harm
174 return $this->template;
175 }
176 return $template;
177 }
d4428d1 @jbrinley If %category% is in the permastruct, WordPress will try to redirect a…
authored Aug 9, 2011
178
179 /**
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
180 * If asking for a paged router page, or if %category% is in
181 * the permastruct, WordPress will try to redirect to the URL
182 * for the dummy page. This should stop that from happening.
d4428d1 @jbrinley If %category% is in the permastruct, WordPress will try to redirect a…
authored Aug 9, 2011
183 *
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
184 * @wordpress-filter redirect_canonical
d4428d1 @jbrinley If %category% is in the permastruct, WordPress will try to redirect a…
authored Aug 9, 2011
185 * @param string $redirect_url
186 * @param string $requested_url
187 * @return bool
188 */
189 public function override_redirect( $redirect_url, $requested_url ) {
aadeb7d @jbrinley prevent redirect to placeholder page when "page" or "paged" query arg…
authored Jul 10, 2012
190 if ( $redirect_url && get_query_var('WP_Route') ) {
191 return FALSE;
192 }
193 if ( $redirect_url && get_permalink(self::get_post_id()) == $redirect_url ) {
d4428d1 @jbrinley If %category% is in the permastruct, WordPress will try to redirect a…
authored Aug 9, 2011
194 return FALSE;
195 }
196 return $redirect_url;
197 }
7b743f8 @jbrinley filter permalink for wp router pages where possible, provide placehol…
authored Aug 6, 2012
198
199 /**
200 * Some plugins expect certain data in post meta. It obviously
201 * won't be there for the placeholder page. Let's pretend
202 * it is there.
203 *
204 * @param array|null $meta
205 * @param int $post_id
206 * @param string $meta_key
207 * @param bool $single
208 * @return array
209 */
210 public function set_post_meta( $meta, $post_id, $meta_key, $single ) {
211 if ( $post_id == self::get_post_id() ) {
212 if ( empty($this->meta) ) {
213 $this->meta = array(
214 '_yoast_wpseo_title' => array($this->get_title('', $post_id)),
215 );
216 $this->meta = apply_filters('wp_router_placeholder_postmeta', $this->meta);
217 }
218 if ( $meta_key ) {
219 if ( empty($this->meta[$meta_key]) ) {
220 return NULL;
221 }
222 return $this->meta[$meta_key];
223 }
224 return $this->meta;
225 }
226 return $meta;
227 }
228
229 public function override_permalink( $post_link, $post, $leavename, $sample ) {
230 if ( $post->ID == self::get_post_id() ) {
231 if ( $post->ID == get_queried_object_id() ) {
232 global $wp;
233 return home_url($wp->request);
234 }
235 }
236 return $post_link;
237 }
9b5f441 @jbrinley most of WP_Route, plus tests
authored May 19, 2011
238 }
Something went wrong with that request. Please try again.