Overview

Namespaces

  • Varunsridharan
    • WordPress

Classes

  • Varunsridharan\WordPress\Endpoint
  • Overview
  • Namespace
  • Class
  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
/**
 * Simple Lib To Handle Creation of Custom Endpoints Or Rewrites in WordPress.
 *
 * @author    Varun Sridharan <varunsridharan23@gmail.com>
 * @copyright 2018 Varun Sridharan
 * @license   GPLV3 Or Greater
 */

namespace Varunsridharan\WordPress;

if ( ! class_exists( '\Varunsridharan\WordPress\Endpoint' ) ) {
    /**
     * Class Endpoint
     *
     * @package Varunsridharan\WordPress
     * @author Varun Sridharan <varunsridharan23@gmail.com>
     * @since 1.0
     */
    class Endpoint {
        /**
         * Version
         *
         * @var string
         */
        public $version = '1.5';

        /**
         * Rewrite_endpoint
         *
         * @var array
         */
        protected $rewrite_endpoint = array();

        /**
         * Rewrite_rule
         *
         * @var array
         */
        protected $rewrite_rule = array();

        /**
         * Rewrite_tag
         *
         * @var array
         */
        protected $rewrite_tag = array();

        /**
         * @var string
         */
        protected $rewrite_prefix = 'wp_router';

        /**
         * @var string
         */
        protected $parameter_pattern = '/{([\w\d]+)}/';

        /**
         * @var string
         */
        protected $value_pattern = '(?P<$1>[^/]+)';

        /**
         * @var string
         */
        protected $value_pattern_replace = '([^\/]+)';

        /**
         * Endpoint constructor.
         *
         * @param string $prefix Used to set prefeix for query args.
         */
        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 );
            #add_action( 'wp_loaded', array( $this, 'flush' ) );
        }

        /**
         * Sets Prefix.
         * Used to set prefeix for query args.
         *
         * @param string $prefix
         */
        public function prefix( $prefix = '' ) {
            $this->rewrite_prefix = $prefix;
        }

        /**
         * Parses Request And Triggers Callback / Action.
         * Based On the $callback Arg when using add_endpoint()
         *
         * @param $wp
         *
         * @uses \call_user_func()
         * @uses \call_user_func_array()
         */
        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 );
                        }
                    }
                }
            }
        }

        /**
         * Flushes WordPress's rewrite rules.
         *
         * @return void
         */
        public function flush() {
            \flush_rewrite_rules();
        }

        /**
         * On WP Init.
         */
        public function on_wp_init() {
            $this->register_rewrite_rules();
            $this->register_rewrite_tags();
            $this->register_rewrite_endpoints();
        }

        /**
         * Registers Rewrite Rules With WordPress.
         */
        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'] );
                }
            }
        }

        /**
         * Registers Rewrite Tag With WordPress.
         */
        protected function register_rewrite_tags() {
            if ( ! empty( $this->rewrite_tag ) ) {
                foreach ( $this->rewrite_tag as $param => $value ) {
                    \add_rewrite_tag( $param, $value );
                }
            }
        }

        /**
         * Registers Rewrite Endpoints With WordPress.
         */
        protected function register_rewrite_endpoints() {
            if ( ! empty( $this->rewrite_endpoint ) ) {
                foreach ( $this->rewrite_endpoint as $slug => $arr ) {
                    \add_rewrite_endpoint( $slug, $arr['type'] );
                }
            }
        }

        /**
         * Adds Custom Endpoints To Endpoints Array.
         *
         * @param string       $endpoint
         * @param int          $endpoint_type
         * @param array|string $callback
         *
         * @return $this
         *
         * @example add_endpoint('world/',EP_PAGES,array(&$this,'page_callback'))
         * @example add_endpoint('hello/',EP_PAGES,'my_page_calback')
         */
        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;
        }

        /**
         * Adds Custom Rewrite Rules.
         *
         * @param string $path
         * @param string $after
         *
         * @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;
        }

        /**
         * Adds Rewrite Tag.
         *
         * @param string  $tag
         * @param string  $regex
         * @param boolean $force
         *
         * @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;
        }

        /**
         * Adds Custom Query Vars TO WordPress.
         *
         * @param array $vars
         *
         * @return array
         */
        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;
        }
    }
}
API documentation generated by ApiGen