Skip to content

Commit 12ca60c

Browse files
committed
update code from mohiohio lib
1 parent 720d42c commit 12ca60c

File tree

5 files changed

+38
-185
lines changed

5 files changed

+38
-185
lines changed

Dispatch.php

Lines changed: 0 additions & 108 deletions
This file was deleted.

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ Easily add custom URL endpoints in WordPress. Map a url to a function.
55
##Example
66

77
```
8-
new \TheFold\WordPress\Dispatch([
8+
use \TheFold\WordPress\Router;
9+
10+
Router::routes([
911
1012
'testing-a-url' => function(){
11-
echo 'Hello Ted';
13+
echo 'Hello Ted';
1214
},
13-
15+
1416
'hello-([a-z]+)' => function($request, $name){
1517
echo "Hello $name";
1618
}

Router.php

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,107 +2,66 @@
22

33
namespace TheFold\WordPress;
44

5-
class Router {
5+
class Dispatch {
66

7-
function __construct(array $url_callbacks, $priority = 5) {
7+
static function routes(array $url_callbacks, $priority = 5) {
88

99
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;
10+
return array_reduce( array_keys($url_callbacks), function($rules, $route) {
11+
$newRule = ['^'.trim($route,'/').'/?$' => 'index.php?'.static::query_var_name($route).'=1'];
12+
return $newRule + $rules;
13+
}, $rules );
2514
});
2615

2716
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;
17+
return array_reduce(array_keys($url_callbacks), function($qvars, $route) {
18+
$qvars[] = static::query_var_name($route);
19+
return $qvars;
20+
},$qvars);
3521
});
36-
37-
add_action( 'template_redirect', function() use ($url_callbacks) {
3822

23+
add_action( 'template_redirect', function() use ($url_callbacks) {
3924
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;
25+
foreach ($url_callbacks as $route => $callback) {
26+
if ($wp_query->get( static::query_var_name($route))) {
27+
$wp_query->is_home = false;
28+
$params = null;
29+
preg_match('#'.trim($route,'/').'#',$_SERVER['REQUEST_URI'],$params);
30+
$res = call_user_func_array($callback,$params);
31+
if($res === false) {
32+
static::send_404();
4733
} 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-
}
34+
exit();
6735
}
6836
}
6937
}
70-
}, get_option('url_access_priority',$priority) );
71-
38+
}, get_option('thefold/router-priority',$priority) );
39+
7240
add_action('init', function() use ($url_callbacks) {
73-
$this->maybe_flush_rewrites($url_callbacks);
41+
static::maybe_flush_rewrites($url_callbacks);
7442
}, 99);
7543
}
7644

77-
protected function maybe_flush_rewrites($url_callbacks) {
78-
45+
static protected function maybe_flush_rewrites($url_callbacks) {
7946
$current = md5(json_encode(array_keys($url_callbacks)));
80-
8147
$cached = get_option(get_called_class(), null );
82-
8348
if ( empty( $cached ) || $current !== $cached ) {
8449
flush_rewrite_rules();
8550
update_option(get_called_class(), $current );
8651
}
8752
}
88-
89-
protected function query_var_name($route) {
9053

54+
static protected function query_var_name($route) {
9155
static $cache;
92-
9356
if (!isset($cache[$route])) {
94-
$cache[$route] = md5(json_encode($route));
57+
$cache[$route] = md5($route);
9558
}
96-
97-
return $cache[$route];
59+
return $cache[$route];
9860
}
9961

100-
protected function send_404() {
101-
62+
static protected function send_404() {
10263
global $wp_query;
103-
10464
status_header('404');
105-
10665
$wp_query->set_404();
10766
}
10867
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"authors": [
66
{
77
"name": "Tim Field",
8-
"email": "tim@thefold.co.nz"
8+
"email": "field.tim@gmail.com"
99
}
1010
],
1111
"require": {

plugin.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
/**
33
* Plugin Name: WordPress Dispatcher
4-
* Plugin URI: https://github.com/timbofield/wordpress-dispatcher
4+
* Plugin URI: https://github.com/tim-field/wordpress-dispatcher
55
* Description: Create URL endpoints within WordPress
6-
* Version: 1.0
6+
* Version: 2.0
77
* Author: Tim Field
8-
* Author URI: http://www.thefold.co.nz
8+
* Author URI: http://github.com/tim-field
99
* License: MIT
1010
*/
1111

12-
require_once(__DIR__ . '/Dispatch.php');
12+
require_once(__DIR__ . '/Router.php');

0 commit comments

Comments
 (0)