AdminRouteService.php
2 years ago
AdminRouteServiceProvider.php
3 years ago
AdminURLService.php
3 years ago
PermalinkService.php
3 years ago
PermalinkServiceProvider.php
3 years ago
PermalinkSettingService.php
3 years ago
PermalinksSettingsService.php
3 years ago
RouteConditionsServiceProvider.php
3 years ago
PermalinkService.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Routing; |
| 4 | |
| 5 | /** |
| 6 | * A service for registering custom routes. |
| 7 | */ |
| 8 | class PermalinkService { |
| 9 | /** |
| 10 | * The regex url. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $url = ''; |
| 15 | |
| 16 | /** |
| 17 | * The regex url. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $query = ''; |
| 22 | |
| 23 | /** |
| 24 | * Holds the params we care about for this route. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $params = []; |
| 29 | |
| 30 | /** |
| 31 | * Query vars. |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | protected $query_vars = []; |
| 36 | |
| 37 | /** |
| 38 | * The priority of the new rule. |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | protected $priority = 'top'; |
| 43 | |
| 44 | /** |
| 45 | * Set the url. |
| 46 | * |
| 47 | * @param string $url The url. |
| 48 | * |
| 49 | * @return $this |
| 50 | */ |
| 51 | public function url( $url ) { |
| 52 | $this->url = $url; |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set query. |
| 58 | * |
| 59 | * @param string $query The query. |
| 60 | * |
| 61 | * @return $this |
| 62 | */ |
| 63 | public function query( $query ) { |
| 64 | $this->query = $query; |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add any params we will use. |
| 70 | * |
| 71 | * @param array $params Array of params. |
| 72 | * |
| 73 | * @return $this |
| 74 | */ |
| 75 | public function params( $params = [] ) { |
| 76 | $this->params = $params; |
| 77 | return $this; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set the priority of the rule. |
| 82 | * |
| 83 | * @param "top"|"bottom" $priority The priority. |
| 84 | * |
| 85 | * @return $this |
| 86 | */ |
| 87 | public function priority( $priority ) { |
| 88 | $this->priority = $priority; |
| 89 | return $this; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Add the rewrite rule. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public function addRewriteRule() { |
| 98 | $rules = get_option( 'rewrite_rules' ); |
| 99 | add_rewrite_rule( $this->url, $this->query, $this->priority ); |
| 100 | if ( ! isset( $rules[ $this->url ] ) ) { |
| 101 | flush_rewrite_rules(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Add query vars. |
| 107 | * |
| 108 | * @param array $query_vars The query vars. |
| 109 | * |
| 110 | * @return array |
| 111 | */ |
| 112 | public function addQueryVars( $query_vars ) { |
| 113 | return array_merge( $query_vars, $this->params ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Create the permalink. |
| 118 | * This handles adding the rewrite rule and query vars. |
| 119 | * |
| 120 | * @return mixed |
| 121 | */ |
| 122 | public function create() { |
| 123 | // add the query vars. |
| 124 | add_filter( 'query_vars', [ $this, 'addQueryVars' ] ); |
| 125 | // add the rewrite rule. |
| 126 | add_action( 'init', [ $this, 'addRewriteRule' ] ); |
| 127 | } |
| 128 | } |
| 129 |