PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 2.5.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v2.5.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / Request.php

Request.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 2.5.2, at includes/Core/Request.php

208 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6
7 class Request extends Base {
8 /**
9 * Flag for already parsed or not
10 *
11 * Specially needed for those who don't update pro yet.
12 * @var boolean
13 */
14 private static $already_parsed = false;
15
16 /**
17 * List of BetterDocs Perma Structure
18 * @var array
19 */
20 private $perma_structure = [];
21
22 /**
23 * List of BetterDocs Query Vars Agains Page Structure.
24 * @var array
25 */
26 private $query_vars = [];
27
28 /**
29 * List of Query Variables from $wp->query_vars.
30 * @var array
31 */
32 private $wp_query_vars = [];
33
34 /**
35 * Rewrite Class Reference of BetterDocs
36 * @var Rewrite
37 */
38 protected $rewrite;
39
40 /**
41 * Settings Class Reference of BetterDocs
42 * @var Settings
43 */
44 protected $settings;
45
46 public function __construct( Rewrite $rewrite, Settings $settings ){
47 $this->rewrite = $rewrite;
48 $this->settings = $settings;
49 }
50
51 public function init() {
52 if( is_admin() ) {
53 return;
54 }
55
56 $this->perma_structure = [
57 'is_docs' => trim( $this->rewrite->get_base_slug(), '/' ),
58 'is_docs_category' => trim( $this->settings->get( 'category_slug', 'docs-category' ), '/' ) . '/%doc_category%',
59 'is_docs_tag' => trim( $this->settings->get( 'tag_slug', 'docs-tag' ), '/' ) . '/%doc_tag%',
60 'is_single_docs' => trim( $this->settings->get( 'permalink_structure', 'docs' ), '/' ) . '/%name%',
61 ];
62
63 $this->query_vars = [
64 'is_docs' => [ 'post_type' ],
65 'is_docs_category' => [ 'doc_category' ],
66 'is_docs_tag' => [ 'doc_tag' ],
67 'is_single_docs' => [ 'name', 'docs', 'post_type' ],
68 ];
69
70 add_action( 'parse_request', [$this, 'parse'] );
71
72 /**
73 * This is for Backward compatibility if pro not updated.
74 */
75 add_action( 'parse_request', [$this, 'backward_compability'], 11 );
76 }
77
78 protected function is_docs( $query_vars ){
79 return $query_vars;
80 }
81
82 protected function is_single_docs( $query_vars ){
83 if( ! isset( $query_vars[ 'name' ] ) ) {
84 return false;
85 }
86
87 global $wpdb;
88 $name = $query_vars[ 'name' ];
89
90 $_post_id = (int) $wpdb->get_var(
91 $wpdb->prepare(
92 "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s LIMIT 1",
93 esc_sql( $name )
94 )
95 );
96
97 return $_post_id > 0;
98 }
99
100 protected function is_docs_category( $query_vars ){
101 return $this->term_exists( $query_vars, 'doc_category' );
102 }
103
104 protected function is_docs_tag( $query_vars ){
105 return $this->term_exists( $query_vars, 'doc_tag' );
106 }
107
108 protected function term_exists( $query_vars, $taxonomy ){
109 if( ! isset( $query_vars[ $taxonomy ] ) ) {
110 return false;
111 }
112
113 return term_exists( $query_vars[ $taxonomy ], $taxonomy );
114 }
115
116 public function set_perma_structure( $structures = [] ){
117 $this->perma_structure = array_merge( $this->perma_structure, $structures );
118 }
119 public function set_query_vars( $query_vars = [] ){
120 $this->query_vars = array_merge( $this->query_vars, $query_vars );
121 }
122
123 public function backward_compability( $wp ){
124 if( static::$already_parsed ) {
125 return;
126 }
127
128 $this->permalink_magic( $wp );
129 }
130
131 public function parse( $wp ){
132 static::$already_parsed = true;
133
134 $this->permalink_magic( $wp );
135 }
136
137 protected function permalink_magic( $wp ){
138 $this->wp_query_vars = $wp->query_vars;
139
140 if( ! empty( $this->perma_structure ) ) {
141 $_valid = [];
142
143 foreach( $this->perma_structure as $_type => $structure ) {
144 $_perma_vars = $this->is_perma_valid_for( $structure, $wp->request );
145
146 $_valid = empty( $_valid ) && $_perma_vars ? [ 'type' => $_type, 'query_vars' => $_perma_vars ] : $_valid;
147 if( ( $_perma_vars && method_exists($this, $_type) && call_user_func([$this, $_type], $_perma_vars) ) ) {
148 if( $_type === 'is_single_docs' ) {
149 $_perma_vars['post_type'] = 'docs';
150 }
151 $_valid = [ 'type' => $_type, 'query_vars' => $_perma_vars ];
152 }
153 }
154
155 $type = isset( $_valid['type'] ) ? $_valid['type'] : '';
156 $query_vars = isset( $_valid['query_vars'] ) ? $_valid['query_vars'] : [];
157
158 if( ! empty( $type ) ) {
159 unset( $this->query_vars[ $type ] );
160 array_map( function( $_vars ) use( &$wp ) {
161 array_map( function( $_var ) use( &$wp ) {
162 unset( $wp->query_vars[ $_var ] );
163 }, $_vars );
164 }, $this->query_vars );
165 }
166
167 $wp->query_vars = is_array( $query_vars ) ? array_merge( $wp->query_vars, $query_vars ) : $wp->query_vars;
168 }
169 }
170
171 /**
172 * This method is responsible for checking a structure is valid again a request.
173 *
174 * @param string $structure
175 * @param string $request
176 * @return array|bool
177 */
178 private function is_perma_valid_for( $structure, $request ) {
179 $_tags = explode( '/', trim( $structure, '/' ) );
180 $_replace_matched_tags = [];
181
182 $_replace_tags = array_filter( $_tags, function( $item ) use( &$_replace_matched_tags ) {
183 $_is_valid = strpos( $item, '%' ) !== false;
184 if( $_is_valid ) {
185 $_replace_matched_tags[] = trim($item, '%');
186 }
187 return $_is_valid;
188 } );
189
190 $_perma_structure = preg_quote($structure, '/');
191 $_perma_structure = str_replace($_replace_tags, '([^\/]+)', $_perma_structure);
192
193 preg_match("/^$_perma_structure$/", $request, $matches);
194
195 if( empty( $matches ) || ! is_array( $matches ) ) {
196 return false;
197 }
198
199 if( count( $matches ) === 1 ) {
200 return [ 'post_type' => 'docs' ];
201 }
202
203 unset( $matches[0] );
204
205 return array_combine( $_replace_matched_tags, $matches );
206 }
207 }
208