PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / app / Gianism / Controller / Rewrite.php

Rewrite.php in Gianism trunk, at app/Gianism/Controller/Rewrite.php

169 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Gianism\Controller;
4
5
6 use Gianism\Pattern\AbstractController;
7 use Gianism\Service\AbstractService;
8
9 /**
10 * Rewrite rule controller
11 *
12 * @package Gianism
13 */
14 class Rewrite extends AbstractController {
15
16 /**
17 * Rewrite rules.
18 *
19 * Initialized on constructor
20 *
21 * @var array
22 */
23 private $rewrites = [];
24
25 /**
26 * URL prefixes
27 *
28 * @var array
29 */
30 private $prefixes = [];
31
32 /**
33 * @var array
34 */
35 protected $query_vars = [ 'gianism_service', 'gianism_action' ];
36
37 /**
38 * Rewrite constructor.
39 *
40 * @param array $argument
41 */
42 protected function __construct( array $argument = [] ) {
43 parent::__construct( $argument );
44 // Add query vars
45 add_filter( 'query_vars', [ $this, 'filter_vars' ] );
46 // Instance all instances
47 // and build rewrite rules
48 // Prefixes
49 foreach ( $this->service->all_services() as $service ) {
50 $instance = $this->service->get( $service );
51 if ( $instance && $instance->enabled ) {
52 $this->prefixes[ $service ] = $instance->url_prefix;
53 }
54 }
55 // Register rewrite rules
56 if ( ! empty( $this->prefixes ) ) {
57 $preg = implode( '|', $this->prefixes );
58 $rewrites = [
59 "^({$preg})/?$" => 'index.php?gianism_service=$matches[1]&gianism_action=default',
60 "^({$preg})/([^/]+)/?$" => 'index.php?gianism_service=$matches[1]&gianism_action=$matches[2]',
61 ];
62 $prefix = $this->option->get_formatted_prefix();
63 if ( $prefix ) {
64 $rewrites[ "^{$prefix}/({$preg})/?$" ] = 'index.php?gianism_service=$matches[1]&gianism_action=default';
65 $rewrites[ "^{$prefix}/({$preg})/([^/]+)/?$" ] = 'index.php?gianism_service=$matches[1]&gianism_action=$matches[2]';
66 }
67 /**
68 * Rewrite rules array for Gianism
69 *
70 * @since 3.0.0
71 * @filter
72 * @param array $rewrites Rewrite rules array for Gianism
73 * @return array
74 */
75 $this->rewrites = apply_filters( 'gianism_rewrite_rules', $rewrites );
76 // Hook for rewrite rules
77 add_filter( 'rewrite_rules_array', [ $this, 'rewrite_rules_array' ] );
78 // Check if rewrite rules are satisfied
79 add_action( 'admin_init', [ $this, 'check_rewrite' ] );
80 // WP_Query
81 add_action( 'pre_get_posts', [ $this, 'hijack_query' ] );
82 }
83 }
84
85 /**
86 * Add custom query vars
87 *
88 * @param array $original_vars
89 *
90 * @return array
91 */
92 public function filter_vars( $original_vars ) {
93 /**
94 * Register custom query vars
95 *
96 * @filter
97 * @since 3.0.0
98 * @param array $additional_vars
99 * @param array $original_vars
100 * @return array
101 */
102 $additional_vars = apply_filters( 'gianism_filter_vars', $this->query_vars, $original_vars );
103 return array_merge( $original_vars, $additional_vars );
104 }
105
106 /**
107 * Customize rewrite rules
108 *
109 * @param array $rules
110 *
111 * @return array
112 */
113 public function rewrite_rules_array( array $rules ) {
114 if ( ! empty( $this->rewrites ) ) {
115 foreach ( $this->rewrites as $rewrite => $regexp ) {
116 if ( ! isset( $rules[ $rewrite ] ) ) {
117 $rules = array_merge(
118 [
119 $rewrite => $regexp,
120 ],
121 $rules
122 );
123 }
124 }
125 }
126
127 return $rules;
128 }
129
130 /**
131 * Check rewrite rules and flush if required
132 *
133 */
134 public function check_rewrite() {
135 $registered_rewrites = $this->option->get( 'rewrite_rules' );
136 foreach ( $this->rewrites as $reg => $replaced ) {
137 if ( ! isset( $registered_rewrites[ $reg ] ) || $replaced !== $registered_rewrites[ $reg ] ) {
138 flush_rewrite_rules();
139 }
140 }
141 }
142
143 /**
144 * If endpoint matched, do parse request.
145 *
146 * @param \WP_Query $wp_query
147 */
148 public function hijack_query( \WP_Query &$wp_query ) {
149 $service = $wp_query->get( 'gianism_service' );
150 $action = $wp_query->get( 'gianism_action' );
151 if ( ! is_admin() && $wp_query->is_main_query() && $service && $action ) {
152 /**
153 * Convert rewrite rule to service name
154 *
155 * @since 3.0.0
156 * @param string $service
157 */
158 $filtered_service = apply_filters( 'gianism_filter_service_prefix', $service );
159 if ( in_array( $service, $this->prefixes, true ) && ( $this->service->get( $filtered_service ) ) ) {
160 nocache_headers();
161 // Parse Request
162 $this->service->get( $filtered_service )->parse_request( $action, $wp_query );
163 } else {
164 $wp_query->set_404();
165 }
166 }
167 }
168 }
169