├── Routes.php └── composer.json /Routes.php: -------------------------------------------------------------------------------- 1 | router)) { 30 | $route = $upstatement_routes->router->match(); 31 | 32 | unset($upstatement_routes->router); 33 | 34 | if ($route && isset($route['target'])) { 35 | if ( isset($route['params']) ) { 36 | call_user_func($route['target'], $route['params']); 37 | } else { 38 | call_user_func($route['target']); 39 | } 40 | } 41 | } 42 | } 43 | 44 | /** 45 | * @param string $route A string to match (ex: 'myfoo') 46 | * @param callable $callback A function to run, examples: 47 | * Routes::map('myfoo', 'my_callback_function'); 48 | * Routes::map('mybaq', array($my_class, 'method')); 49 | * Routes::map('myqux', function() { 50 | * //stuff goes here 51 | * }); 52 | */ 53 | public static function map($route, $callback, $name = '') { 54 | global $upstatement_routes; 55 | if (!isset($upstatement_routes->router)) { 56 | $upstatement_routes->router = new AltoRouter(); 57 | $site_url = get_bloginfo('url'); 58 | $site_url_parts = explode('/', $site_url); 59 | $site_url_parts = array_slice($site_url_parts, 3); 60 | $base_path = implode('/', $site_url_parts); 61 | if (!$base_path || strpos($route, $base_path) === 0) { 62 | $base_path = '/'; 63 | } else { 64 | $base_path = '/' . $base_path . '/'; 65 | } 66 | // Clean any double slashes that have resulted 67 | $base_path = str_replace( "//", "/", $base_path ); 68 | $upstatement_routes->router->setBasePath($base_path); 69 | } 70 | $route = self::convert_route($route); 71 | $upstatement_routes->router->map('GET|POST|PUT|DELETE', trailingslashit($route), $callback, $name); 72 | $upstatement_routes->router->map('GET|POST|PUT|DELETE', untrailingslashit($route), $callback, $name); 73 | } 74 | 75 | /** 76 | * @return string A string in a format for AltoRouter 77 | * ex: [:my_param] 78 | */ 79 | public static function convert_route($route_string) { 80 | if (strpos($route_string, '[') > -1) { 81 | return $route_string; 82 | } 83 | $route_string = preg_replace('/(:)\w+/', '/[$0]', $route_string); 84 | $route_string = str_replace('[[', '[', $route_string); 85 | $route_string = str_replace(']]', ']', $route_string); 86 | $route_string = str_replace('[/:', '[:', $route_string); 87 | $route_string = str_replace('//[', '/[', $route_string); 88 | if ( strpos($route_string, '/') === 0 ) { 89 | $route_string = substr($route_string, 1); 90 | } 91 | return $route_string; 92 | } 93 | 94 | /** 95 | * @param string $template A php file to load (ex: 'single.php') 96 | * @param array|bool $tparams An array of data to send to the php file. Inside the php file 97 | * this data can be accessed via: 98 | * global $params; 99 | * @param int $status_code A code for the status (ex: 200) 100 | * @param WP_Query $query Use a WP_Query object in the template file instead of 101 | * the default query 102 | * @param int $priority The priority used by the "template_include" filter 103 | * @return bool 104 | */ 105 | public static function load($template, $tparams = false, $query = false, $status_code = 200, $priority = 10) { 106 | $fullPath = is_readable($template); 107 | if (!$fullPath) { 108 | $template = locate_template($template); 109 | } 110 | if ($tparams){ 111 | global $params; 112 | $params = $tparams; 113 | } 114 | if ($status_code) { 115 | add_filter('status_header', function($status_header, $header, $text, $protocol) use ($status_code) { 116 | $text = get_status_header_desc($status_code); 117 | $header_string = "$protocol $status_code $text"; 118 | return $header_string; 119 | }, 10, 4 ); 120 | if (404 != $status_code) { 121 | add_action('parse_query', function($query) { 122 | if ($query->is_main_query()){ 123 | $query->is_404 = false; 124 | } 125 | },1); 126 | add_action('template_redirect', function(){ 127 | global $wp_query; 128 | $wp_query->is_404 = false; 129 | },1); 130 | } 131 | } 132 | 133 | if ($query) { 134 | add_action('parse_request', function() use ($query) { 135 | global $wp; 136 | if ( is_callable($query) ) 137 | $query = call_user_func($query); 138 | 139 | if ( is_array($query) ) 140 | $wp->query_vars = $query; 141 | elseif ( !empty($query) ) 142 | parse_str($query, $wp->query_vars); 143 | else 144 | return true; // Could not interpret query. Let WP try. 145 | 146 | return false; 147 | }); 148 | } 149 | if ($template) { 150 | add_filter('template_include', function($t) use ($template) { 151 | return $template; 152 | }, $priority); 153 | return true; 154 | } 155 | return false; 156 | } 157 | } 158 | 159 | global $upstatement_routes; 160 | $upstatement_routes = new Routes(); 161 | 162 | if ( file_exists($composer_autoload = __DIR__ . '/vendor/autoload.php') 163 | || file_exists($composer_autoload = WP_CONTENT_DIR.'/vendor/autoload.php')){ 164 | require_once($composer_autoload); 165 | } 166 | 167 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "upstatement/routes", 3 | "description": "Manage rewrites and routes in WordPress with this dead-simple plugin", 4 | "keywords": [ 5 | "routes", 6 | "routing", 7 | "rewrite", 8 | "redirects" 9 | ], 10 | "homepage": "https://www.upstatement.com", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Jared Novack", 15 | "email": "jared@upstatement.com", 16 | "homepage": "https://www.upstatement.com" 17 | } 18 | ], 19 | "support": { 20 | "issues": "https://github.com/Upstatement/routes/issues", 21 | "wiki": "https://github.com/Upstatement/routes/wiki", 22 | "source": "https://github.com/Upstatement/routes" 23 | }, 24 | "require": { 25 | "php": ">=7.3", 26 | "altorouter/altorouter": "^2.0.2", 27 | "composer/installers": "^1.0 || ^2.0" 28 | }, 29 | "require-dev": { 30 | "phpunit/phpunit": "5.7.16", 31 | "wp-cli/wp-cli": "*", 32 | "satooshi/php-coveralls": "*" 33 | }, 34 | "autoload": { 35 | "psr-0": { 36 | "Routes": "" 37 | } 38 | } 39 | } 40 | --------------------------------------------------------------------------------