|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace TheFold\WordPress; |
| 4 | + |
| 5 | +class Router { |
| 6 | + |
| 7 | + function __construct(array $url_callbacks, $priority = 5) { |
| 8 | + |
| 9 | + add_filter('rewrite_rules_array', function($rules) use ($url_callbacks) { |
| 10 | + |
| 11 | + foreach (array_keys($url_callbacks) as $look_for_in_url) { |
| 12 | + |
| 13 | + if(is_array($look_for_in_url)){ |
| 14 | + list($method, $route) = each($look_for_in_url); |
| 15 | + } else { |
| 16 | + $method = '*'; |
| 17 | + $route = $look_for_in_url; |
| 18 | + } |
| 19 | + |
| 20 | + $newRule = ['^'.trim($route,'/').'/?$' => 'index.php?'.$this->query_var_name($look_for_in_url).'='.$method]; |
| 21 | + $rules = $newRule + $rules; |
| 22 | + } |
| 23 | + |
| 24 | + return $rules; |
| 25 | + }); |
| 26 | + |
| 27 | + add_filter('query_vars', function($qvars) use ($url_callbacks) { |
| 28 | + |
| 29 | + foreach (array_keys($url_callbacks) as $look_for_in_url) { |
| 30 | + |
| 31 | + $var = $this->query_var_name($look_for_in_url); |
| 32 | + $qvars[] = $var; |
| 33 | + } |
| 34 | + return $qvars; |
| 35 | + }); |
| 36 | + |
| 37 | + add_action( 'template_redirect', function() use ($url_callbacks) { |
| 38 | + |
| 39 | + global $wp_query; |
| 40 | + |
| 41 | + foreach ($url_callbacks as $url_key => $callback) { |
| 42 | + |
| 43 | + if ($wp_query->get( $this->query_var_name($url_key))) { |
| 44 | + |
| 45 | + if(is_array($url_key)){ |
| 46 | + list($method, $route) = $url_key; |
| 47 | + } else { |
| 48 | + $method = '*'; |
| 49 | + $route = $url_key; |
| 50 | + } |
| 51 | + |
| 52 | + if($method == '*' || $method == $_REQUEST['REQUEST_METHOD']){ |
| 53 | + |
| 54 | + $wp_query->is_home = false; |
| 55 | + |
| 56 | + $params = null; |
| 57 | + |
| 58 | + preg_match('#'.trim($route,'/').'#',$_SERVER['REQUEST_URI'],$params); |
| 59 | + |
| 60 | + $res = call_user_func_array($callback,$params); |
| 61 | + |
| 62 | + if($res === false) |
| 63 | + $this->send_404(); |
| 64 | + else{ |
| 65 | + exit(); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + }, get_option('url_access_priority',$priority) ); |
| 71 | + |
| 72 | + add_action('init', function() use ($url_callbacks) { |
| 73 | + $this->maybe_flush_rewrites($url_callbacks); |
| 74 | + }, 99); |
| 75 | + } |
| 76 | + |
| 77 | + protected function maybe_flush_rewrites($url_callbacks) { |
| 78 | + |
| 79 | + $current = md5(json_encode(array_keys($url_callbacks))); |
| 80 | + |
| 81 | + $cached = get_option(get_called_class(), null ); |
| 82 | + |
| 83 | + if ( empty( $cached ) || $current !== $cached ) { |
| 84 | + flush_rewrite_rules(); |
| 85 | + update_option(get_called_class(), $current ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + protected function query_var_name($route) { |
| 90 | + |
| 91 | + static $cache; |
| 92 | + |
| 93 | + if (!isset($cache[$route])) { |
| 94 | + $cache[$route] = md5(json_encode($route)); |
| 95 | + } |
| 96 | + |
| 97 | + return $cache[$route]; |
| 98 | + } |
| 99 | + |
| 100 | + protected function send_404() { |
| 101 | + |
| 102 | + global $wp_query; |
| 103 | + |
| 104 | + status_header('404'); |
| 105 | + |
| 106 | + $wp_query->set_404(); |
| 107 | + } |
| 108 | +} |
0 commit comments