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: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252:
<?php
namespace Varunsridharan\WordPress;
if ( ! class_exists( '\Varunsridharan\WordPress\Endpoint' ) ) {
class Endpoint {
public $version = '1.5';
protected $rewrite_endpoint = array();
protected $rewrite_rule = array();
protected $rewrite_tag = array();
protected $rewrite_prefix = 'wp_router';
protected $parameter_pattern = '/{([\w\d]+)}/';
protected $value_pattern = '(?P<$1>[^/]+)';
protected $value_pattern_replace = '([^\/]+)';
public function __construct( $prefix = '' ) {
$this->prefix( $prefix );
\add_action( 'init', array( &$this, 'on_wp_init' ) );
\add_action( 'parse_request', array( $this, 'parse_request' ) );
\add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
}
public function prefix( $prefix = '' ) {
$this->rewrite_prefix = $prefix;
}
public function parse_request( $wp ) {
if ( ! empty( $wp->query_vars ) ) {
foreach ( $wp->query_vars as $key => $value ) {
if ( isset( $this->rewrite_endpoint[ $key ] ) ) {
if ( isset( $this->rewrite_endpoint[ $key ]['callback'] ) && is_callable( $this->rewrite_endpoint[ $key ]['callback'] ) ) {
$is_arr = ( is_array( $this->rewrite_endpoint[ $key ]['callback'] ) ) ? true : false;
$callback = ( true === $is_arr ) ? 'call_user_func_array' : 'call_user_func';
$param = ( true === $is_arr ) ? array( $wp ) : $wp;
$callback( $this->rewrite_endpoint[ $key ]['callback'], $param );
} else {
\do_action( $this->rewrite_endpoint[ $key ]['callback'], $wp );
}
}
}
}
}
public function flush() {
\flush_rewrite_rules();
}
public function on_wp_init() {
$this->register_rewrite_rules();
$this->register_rewrite_tags();
$this->register_rewrite_endpoints();
}
protected function register_rewrite_rules() {
if ( ! empty( $this->rewrite_rule ) ) {
foreach ( $this->rewrite_rule as $value ) {
\add_rewrite_rule( $value['regex'], $value['replace'], $value['type'] );
}
}
}
protected function register_rewrite_tags() {
if ( ! empty( $this->rewrite_tag ) ) {
foreach ( $this->rewrite_tag as $param => $value ) {
\add_rewrite_tag( $param, $value );
}
}
}
protected function register_rewrite_endpoints() {
if ( ! empty( $this->rewrite_endpoint ) ) {
foreach ( $this->rewrite_endpoint as $slug => $arr ) {
\add_rewrite_endpoint( $slug, $arr['type'] );
}
}
}
public function add_endpoint( $endpoint = '', $endpoint_type = \EP_ROOT, $callback = array() ) {
if ( ! isset( $this->rewrite_endpoint[ $endpoint ] ) ) {
$this->rewrite_endpoint[ $endpoint ] = array(
'type' => $endpoint_type,
'callback' => $callback,
);
}
return $this;
}
public function add_rewrite_rule( $path = '', $after = 'top' ) {
$uri = '^' . preg_replace( $this->parameter_pattern, $this->value_pattern_replace, $path );
$url = 'index.php?';
$_url = array();
$matches = [];
if ( preg_match_all( $this->parameter_pattern, $path, $matches ) ) {
foreach ( $matches[1] as $id => $param ) {
$param = ( empty( $this->rewrite_prefix ) ) ? $param : $this->rewrite_prefix . '_' . $param;
$_url[] = "{$param}=\$matches[" . ( $id + 1 ) . ']';
$this->add_tag( '%' . $param . '%', '(.+)' );
}
}
$this->rewrite_rule[] = array(
'regex' => $uri . '/?',
'replace' => $url . '' . implode( '&', $_url ),
'type' => $after,
);
return $this;
}
public function add_tag( $tag = '', $regex = '', $force = false ) {
if ( ! isset( $this->rewrite_tag[ $tag ] ) || isset( $this->rewrite_tag[ $tag ] ) && true === $force ) {
$this->rewrite_tag[ $tag ] = $regex;
}
return $this;
}
public function add_query_vars( $vars = array() ) {
if ( ! empty( $this->rewrite_endpoint ) ) {
$keys = array_keys( $this->rewrite_endpoint );
return array_merge( $vars, $keys );
}
return $vars;
}
}
}