-
Notifications
You must be signed in to change notification settings - Fork 0
/
FrontRoute.php
156 lines (130 loc) · 5.75 KB
/
FrontRoute.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
// Setup FrontRoute
namespace Balise\Bridge;
class FrontRoute extends Route
{
public static $routes = array();
public static function start()
{
add_action('init', array('Balise\Bridge\FrontRoute', 'onInit'));
add_filter('theme_templates', array('Balise\AnchorFramework\Anchor', 'loadThemeTemplates'), 10, 4);
$types = array('index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment');
foreach ($types as $type) {
add_filter("{$type}_template", array('Balise\AnchorFramework\Anchor', 'getThemeTemplate'), 10, 3);
}
add_action('parse_query', array('Balise\Bridge\FrontRoute', 'bypassQuery'));
$types = array('index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date', 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', 'attachment');
foreach ($types as $type) {
add_filter("{$type}_template_hierarchy", array('Balise\Bridge\FrontRoute', 'loadThemeTemplates'), 1, 4);
}
}
public static function loadThemeTemplates($post_templates)
{
$resolved = self::routesResolver(self::$routes);
array_unshift($post_templates, isset($resolved[3]) ? $resolved[3] : 'index' . ".php");
return $post_templates;
}
public static function routesResolver($routes, $prefix = "")
{
$selected = null;
foreach ($routes as $route) {
if ($_SERVER['REQUEST_METHOD'] === $route[0]) {
// Store the keys used
preg_match_all('/{(\w+)}/i', $route[1], $keys);
// Make the attibutes value as wildcard
$resolver = preg_replace('/{\w+}/i', '([^\/]+)', $route[1]);
// If the route match
$request = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$remove = substr(get_bloginfo('url'), strpos(get_bloginfo('url'), '://') + 3);
if (preg_match('#^/' . $prefix . $resolver . '/?$#i', str_replace($remove, "", $request), $data)) {
$selected = $route;
// Make sure the attribute 2 is a callback
$selected[2] = self::convertToCallable($selected[2]);
// Match the value to the keys and add it to the selected route
$values = array();
foreach ($keys[1] as $key => $id) {
$values[$id] = $data[$key + 1];
}
$selected[] = $values;
// Return selected route with data
return $selected;
break;
}
}
}
return $_SERVER['REQUEST_URI'];
}
public static function onInit()
{
/* This is where we hook our routes to the */
global $wp_query;
add_rewrite_tag('%frontendroute%', '([^&]+)');
foreach (self::$routes as $route) {
$resolver = preg_replace('/{\w+}/i', '([^\/]+)', $route[1]);
add_rewrite_rule('^' . $resolver . '$', 'index.php?frontendroute=1', 'top');
}
// This is added to work with the AnchorFramework and boilerplate
\add_filter('balise-anchor-getData', function () {
if (get_query_var("frontendroute", false)) {
global $wp_query, $post;
$post->ID = 1;
$post->post_category = array();
$post->post_content = '';
$post->post_status = 'publish';
$post->post_title = '';
$post->post_type = 'page';
$post->comment_status = 'closed';
setup_postdata($post);
// Resolve the route
$resolved = self::routesResolver(self::$routes);
// Call the callback
$post->post_content = call_user_func_array($resolved[2], $resolved[4]);
return new \Balise\AnchorFramework\PostWrapper($post, true);
}
});
}
public static function bypassQuery()
{
if (get_query_var("frontendroute", false) === 1) {
global $wp_query, $post;
// Make a fake query to ensure
// the data is passed to a template
$post = new \stdClass();
$post->ID = 1;
$post->post_category = array();
$post->post_content = '';
$post->post_status = 'publish';
$post->post_title = '';
$post->post_type = 'page';
$post->comment_status = 'closed';
setup_postdata($post);
$wp_query->queried_object = array($post);
$wp_query->posts = array($post);
$wp_query->post = $post;
$wp_query->found_posts = 1;
$wp_query->current_post = -1;
$wp_query->in_the_loop = false;
$wp_query->post_count = 1;
$wp_query->max_num_pages = 1;
$wp_query->is_single = 1;
$wp_query->is_singular = true;
$wp_query->is_404 = false;
$wp_query->is_posts_page = 0;
$wp_query->is_home = 0;
$wp_query->page = 0;
$wp_query->is_post = false;
$wp_query->is_page = true;
$wp_query->page = false;
// Resolve the route
$resolved = self::routesResolver(self::$routes);
// Call the callback
$post->post_content = call_user_func_array($resolved[2], $resolved[4]);
// Put the data in the template
if ($overridden_template = locate_template('index.php')) {
load_template($overridden_template);
die();
}
}
}
}
FrontRoute::start();