-
Notifications
You must be signed in to change notification settings - Fork 4
/
Router.php
68 lines (58 loc) · 2.4 KB
/
Router.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
<?php
namespace TheFold\WordPress;
class Router {
static function routes(array $url_callbacks, $priority = 5) {
//Inform WordPress that these routes ( rewrites ) exist by adding to the rewrite_rules_array
add_filter('rewrite_rules_array', function($rules) use ($url_callbacks) {
return array_reduce( array_keys($url_callbacks), function($rules, $route) {
$newRule = ['^'.trim($route,'/').'/?$' => 'index.php?'.static::query_var_name($route).'=1'];
return $newRule + $rules;
}, $rules );
});
add_filter('query_vars', function($qvars) use ($url_callbacks) {
return array_reduce(array_keys($url_callbacks), function($qvars, $route) {
$qvars[] = static::query_var_name($route);
return $qvars;
},$qvars);
});
add_action( 'template_redirect', function() use ($url_callbacks) {
global $wp_query;
foreach ($url_callbacks as $route => $callback) {
if ($wp_query->get( static::query_var_name($route)) ) {
$wp_query->is_home = false;
$params = null;
preg_match('#'.trim($route,'/').'#',$_SERVER['REQUEST_URI'],$params);
$res = call_user_func_array($callback,$params);
if($res === false) {
static::send_404();
} else {
exit();
}
}
}
}, get_option('thefold/router-priority',$priority) );
add_action('init', function() use ($url_callbacks) {
static::maybe_flush_rewrites($url_callbacks);
}, 99);
}
static protected function maybe_flush_rewrites($url_callbacks) {
$current = md5(json_encode(array_keys($url_callbacks)));
$cached = get_option(get_called_class(), null );
if ( empty( $cached ) || $current !== $cached ) {
flush_rewrite_rules();
update_option(get_called_class(), $current );
}
}
static protected function query_var_name($route) {
static $cache;
if (!isset($cache[$route])) {
$cache[$route] = md5($route);
}
return $cache[$route];
}
static protected function send_404() {
global $wp_query;
status_header('404');
$wp_query->set_404();
}
}