PluginProbe
Knowledge Base documentation & wiki plugin – BasePress Docs / trunk
Knowledge Base documentation & wiki plugin – BasePress Docs vtrunk
basepress / includes / class-basepress-variables.php

class-basepress-variables.php in Knowledge Base documentation & wiki plugin – BasePress Docs trunk, at includes/class-basepress-variables.php

260 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This is the class that sets all front end variables
4 */
5
6 // Exit if called directly
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11 if ( ! class_exists( 'Basepress_Variables' ) ){
12
13 class Basepress_Variables{
14
15 private $is_pretty_permalinks = null;
16 private $is_single_product_mode = false;
17 private $entry_page = null;
18 private $is_front_page = null;
19 private $is_knowledgebase = null;
20 private $is_products_page = null;
21 private $is_product = null;
22 private $is_section = null;
23 private $is_tag = null;
24 private $is_search = null;
25 private $is_global_search = null;
26 private $is_article = null;
27
28 /**
29 * Basepress_Variables constructor.
30 *
31 * #since 2.2.0
32 *
33 * @param $query
34 */
35 public function __construct( $query ){
36
37 if( is_admin() ) return;
38
39 global $wp_rewrite, $basepress_utils;
40
41 $options = $basepress_utils->get_options();
42
43 $this->is_pretty_permalinks = !empty( $wp_rewrite->permalink_structure );
44 $this->is_single_product_mode = isset( $options['single_product_mode'] );
45 $this->entry_page = isset( $options['entry_page'] ) ? $options['entry_page'] : '';
46
47 if( $this->is_pretty_permalinks ){
48 if( $this->is_single_product_mode ){
49 $this->get_variables_pretty_permalink_single( $query );
50 }
51 else{
52 $this->get_variables_pretty_permalink_multi( $query );
53 }
54 }
55 else{
56 if( $this->is_single_product_mode ){
57 $this->get_variables_plain_permalinks_single( $query );
58 }
59 else{
60 $this->get_variables_plain_permalinks_multi( $query );
61 }
62 }
63
64 $basepress_utils->is_knowledgebase = $this->is_knowledgebase;
65 $basepress_utils->is_single_product_mode = $this->is_single_product_mode;
66 $basepress_utils->is_products_page = $this->is_products_page;
67 $basepress_utils->is_product = $this->is_product;
68 $basepress_utils->is_section = $this->is_section;
69 $basepress_utils->is_tag = $this->is_tag;
70 $basepress_utils->is_article = $this->is_article;
71 $basepress_utils->is_search = $this->is_search;
72 $basepress_utils->is_global_search = $this->is_global_search;
73 }
74
75
76 /**
77 * Variables for pretty permalinks single KB
78 *
79 * @since 2.2.0
80 *
81 * @param $query
82 */
83 private function get_variables_pretty_permalink_single( $query ){
84 global $basepress_utils;
85
86 $is_front_page = $this->is_front_page = $basepress_utils->is_kb_on_front_page() && $this->is_home_page();
87 $is_post_type = ( isset( $_GET['post_type'] ) && 'knowledgebase' == $_GET['post_type'] )
88 || ( isset( $query['post_type'] ) && 'knowledgebase' == $query['post_type'] );
89 $is_taxonomy_term = isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] );
90 $is_search = isset( $_GET['s'] ) || isset( $query['s'] );
91
92 //This must run before $is_knowledgebase is set
93 $is_kb_page = $is_post_type || $is_taxonomy_term;
94
95 $this->is_products_page = false;
96
97 $this->is_product = isset( $query['is_knowledgebase_product'] ) || $is_front_page;
98 $this->is_tag = ( isset( $_GET['knowledgebase_tag'] ) || isset( $query['knowledgebase_tag'] ) );
99 $this->is_section = ! $this->is_product && ! $is_search && ( isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] ) ) && ! $this->is_tag;
100 $this->is_article = isset( $query['knowledgebase'] );
101
102 $this->is_search = $is_kb_page && $is_search;
103 $this->is_global_search = false;
104
105 //Must run after $is_entry_page to work
106 $this->is_knowledgebase = ( $is_kb_page || $this->is_products_page ) || $is_front_page;
107 }
108
109
110 /**
111 * Variables for pretty permalinks multi KB
112 *
113 * @since 2.2.0
114 *
115 * @param $query
116 */
117 private function get_variables_pretty_permalink_multi( $query ){
118 global $basepress_utils;
119
120 $is_front_page = $this->is_front_page = $basepress_utils->is_kb_on_front_page() && $this->is_home_page();
121 $is_products_page = ( isset( $query['page_id'] ) && ( $this->entry_page == $query['page_id'] ) && ! isset( $options['single_product_mode'] ) ) || $is_front_page;
122 $is_post_type = ( isset( $_GET['post_type'] ) && 'knowledgebase' == $_GET['post_type'] )
123 || ( isset( $query['post_type'] ) && 'knowledgebase' == $query['post_type'] );
124 $is_taxonomy_term = isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] );
125
126 $is_search = isset( $_GET['s'] ) || isset( $query['s'] );
127
128 //This must run before $is_knowledgebase is set
129 $is_kb_page = $is_post_type || $is_taxonomy_term || ( $is_products_page && $is_search );
130
131 $this->is_products_page = $is_products_page && ! isset( $query['s'] );
132
133 $this->is_product = isset( $query['is_knowledgebase_product'] ) || ( isset( $query['knowledgebase_cat'] ) && isset( $query['s'] ) );
134 $this->is_tag = ( isset( $_GET['knowledgebase_tag'] ) || isset( $query['knowledgebase_tag'] ) );
135 $this->is_section = ! $this->is_product && ( isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] ) ) && ! $this->is_tag;
136 $this->is_article = isset( $query['knowledgebase'] );
137
138 $this->is_search = $is_kb_page && $is_search;
139 $this->is_global_search = $this->is_search && ! $this->is_product;
140
141 if( ! $is_kb_page ){
142 $this->is_product = false;
143 }
144 else{
145 $this->is_product = ! ( $this->is_products_page || $this->is_search || $this->is_global_search || $this->is_section || $this->is_tag || $this->is_article );
146 }
147
148 //Must run after $is_entry_page to work
149 $this->is_knowledgebase = ( $is_kb_page || $this->is_products_page ) || $is_front_page;
150 }
151
152
153 /**
154 * Variables for plain permalinks single KB
155 *
156 * @since 2.2.0
157 *
158 * @param $query
159 */
160 private function get_variables_plain_permalinks_single( $query ){
161 global $basepress_utils;
162
163 $is_front_page = $this->is_front_page = $basepress_utils->is_kb_on_front_page() && ( $this->is_home_page() || ( isset( $query['s'] ) && isset( $query['post_type'] ) && 'knowledgebase' == $query['post_type'] ) );
164 $is_products_page = false;
165 $is_taxonomy_term = isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] );
166 $is_post_type = ( isset( $_GET['post_type'] ) && 'knowledgebase' == $_GET['post_type'] )
167 || ( isset( $query['post_type'] ) && 'knowledgebase' == $query['post_type'] ) || $is_taxonomy_term;
168 $is_search = ( isset( $_GET['s'] ) || isset( $query['s'] ) ) && ( $is_post_type || $is_taxonomy_term );
169
170 $is_product = $is_front_page || ( $is_post_type && $is_search ) ? true : false;
171 $is_section = $is_taxonomy_term ? true : false;
172
173 if( ( isset( $query['page_id'] ) && ! empty( $this->entry_page ) && $query['page_id'] == $this->entry_page ) ){
174 $is_product = true;
175 }
176
177 //This must run before $is_knowledgebase is set
178 $is_kb_page = $is_post_type || $is_taxonomy_term || $is_search || $is_product;
179
180 $this->is_products_page = $is_products_page && ! isset( $query['s'] );
181 $this->is_tag = ( isset( $_GET['knowledgebase_tag'] ) || isset( $query['knowledgebase_tag'] ) );
182 $this->is_product = $is_product && ! $this->is_tag;
183 $this->is_section = $is_section && ! $this->is_tag;
184 $this->is_article = isset( $query['knowledgebase'] );
185 $this->is_search = ( $is_kb_page || $is_products_page ) && $is_search;
186 $this->is_global_search = ! $this->is_single_product_mode && $this->is_search && ! $this->is_product;
187
188 //Must run after $is_kb_page to work
189 $this->is_knowledgebase = ( $is_kb_page || $this->is_products_page ) || $is_front_page;
190 }
191
192
193 /**
194 * Variables for plain permalinks multi KB
195 *
196 * @since 2.2.0
197 *
198 * @param $query
199 */
200 private function get_variables_plain_permalinks_multi( $query ){
201 global $basepress_utils;
202
203 $is_front_page = $this->is_front_page = $basepress_utils->is_kb_on_front_page() && $this->is_home_page();
204 $is_products_page = ( ( isset( $query['page_id'] ) && ! empty( $this->entry_page ) )
205 && $query['page_id'] == $this->entry_page && ! $this->is_single_product_mode )
206 || $is_front_page;
207
208 $is_search = isset( $_GET['s'] ) || isset( $query['s'] );
209 $is_taxonomy_term = isset( $_GET['knowledgebase_cat'] ) || isset( $query['knowledgebase_cat'] );
210
211 $is_product = false;
212 $is_section = false;
213 if( $is_taxonomy_term ){
214 $term_slug = isset( $_GET['knowledgebase_cat'] ) ? sanitize_text_field( wp_unslash( $_GET['knowledgebase_cat'] ) ) : $query['knowledgebase_cat'];
215 $term = get_term_by( 'slug', $term_slug, 'knowledgebase_cat' );
216 if( 0 == $term->parent ){
217 $is_product = true;
218 }
219 else{
220 $is_section = true;
221 }
222 }
223
224 //This must run before $is_knowledgebase is set
225 $is_kb_post_type = ( isset( $_GET['post_type'] ) && 'knowledgebase' == $_GET['post_type'] )
226 || ( isset( $query['post_type'] ) && 'knowledgebase' == $query['post_type'] )
227 || isset( $query['knowledgebase_cat'] )
228 || isset( $query['knowledgebase_tag'] )
229 || ( $is_products_page && isset( $query['s'] )
230 || $is_product );
231
232 $this->is_products_page = $is_products_page && ! isset( $query['s'] );
233 $this->is_tag = ( isset( $_GET['knowledgebase_tag'] ) || isset( $query['knowledgebase_tag'] ) );
234 $this->is_product = $is_product && ! $this->is_tag;
235 $this->is_section = $is_section && ! $this->is_tag;
236 $this->is_article = isset( $query['knowledgebase'] );
237 $this->is_search = ( $is_kb_post_type || $is_products_page ) && $is_search;
238 $this->is_global_search = ! $this->is_single_product_mode && $this->is_search && ! $this->is_product;
239
240 //Must run after $is_entry_page to work
241 $this->is_knowledgebase = ( $is_kb_post_type || $this->is_products_page ) || $is_front_page;
242 }
243
244
245 /**
246 * Returns true if the current page is the home page
247 *
248 * @since 2.2.0
249 *
250 * @return bool
251 */
252 private function is_home_page(){
253 $home_url = trailingslashit( preg_replace("(^https?://)", "", get_home_url() ) );
254 $current_url = trailingslashit( $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] ); // phpcs:ignore
255
256 return $home_url == $current_url;
257 }
258 }
259 }
260