PluginProbe
Filter Everything — WordPress & WooCommerce Filters / 1.9.7
Filter Everything — WordPress & WooCommerce Filters v1.9.7
1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 All 52 releases
← All changes | src/WpManager.php +59 -298 1.9.2.21.9.7 View file →
@@ -9,12 +9,11 @@
9 9 class WpManager
10 10 {
11 11 private $requestParser;
12 12
13 - private $filterQueryVars;
13 + /** @var FilterContext */
14 + private $context;
14 15
15 - private $isFilterRequest;
16 -
17 16 private $em;
18 17 private static $fqcn;
19 18 private static $is;
20 19 private static $m;
@@ -24,11 +23,9 @@
24 23 {
25 24 global $wp_rewrite;
26 25
27 26 if ( ! defined( 'FLRT_PERMALINKS_ENABLED' ) ) {
28 - $rewrite = $wp_rewrite->wp_rewrite_rules();
29 - $permalinksEnabled = ( defined( 'FLRT_FILTERS_PRO' ) && ! empty( $rewrite ) );
30 - define( 'FLRT_PERMALINKS_ENABLED', $permalinksEnabled );
27 + define( 'FLRT_PERMALINKS_ENABLED', flrt_permalinks_enabled() );
31 28 }
32 29
33 30 if ( ! defined('FLRT_SET_TRANSIENT_ENABLED')){
34 31 define( 'FLRT_SET_TRANSIENT_ENABLED', true );
@@ -39,9 +36,10 @@
39 36 self::$m = 'identify_query_source';
40 37 self::$mg = 'get_query_builder_name';
41 38
42 39 $this->requestParser = new RequestParser( $this->prepareRequest() );
43 - $this->em = Container::instance()->getEntityManager();
40 + $this->context = Container::instance()->getFilterContext();
41 + $this->em = Container::instance()->getEntityManager();
44 42 }
45 43
46 44 public function parseRequest($WP)
47 45 {
@@ -49,9 +47,9 @@
49 47 foreach ( $this->requestParser->getQueryVars() as $key => $queryVar ) {
50 48 $this->setQueryVar( $key, $queryVar );
51 49 }
52 50
53 - $this->isFilterRequest = true;
51 + $this->context->markFilterRequest();
54 52 $this->setQueryVar('wpc_is_filter_request', true );
55 53
56 54 if ( $this->getQueryVar('error') === '404' ) {
57 55 $WP->set_query_var( 'error', '404' );
@@ -177,12 +175,10 @@
177 175
178 176 public function addFilterQueryToWpQuery( $wp_query )
179 177 {
180 178 // The main difference is that we need to detect relevantSetId:
181 - // - one time and store it into Container
179 + // - one time and store it into the FilterContext
182 180 // - do it before comparing with the current query
183 - global $wpc_not_fired;
184 -
185 181 $fqcn = self::$fqcn;
186 182 $m = self::$m;
187 183
188 184 $source = $fqcn::$m($wp_query);
@@ -188,11 +184,9 @@
188 184 $source = $fqcn::$m($wp_query);
189 185 $wp_query->set('flrt_detected_source', $source);
190 186
191 187 $this->collectWPQueries( $wp_query );
192 - if ( $wp_query->is_main_query() && $wpc_not_fired ) {
193 - global $flrt_sets;
194 -
188 + if ( $wp_query->is_main_query() && $this->context->isMainQueryPending() ) {
195 189 $filterSet = Container::instance()->getFilterSetService();
196 190
197 191 // Set global filters vars
198 192 $this->setQueryVar('wp_queried_object', $this->identifyWpQueriedObject($wp_query) );
@@ -197,10 +191,10 @@
197 191 // Set global filters vars
198 192 $this->setQueryVar('wp_queried_object', $this->identifyWpQueriedObject($wp_query) );
199 193 $sets = $filterSet->findRelevantSets( $this->getQueryVar('wp_queried_object') );
200 194
201 - // Save sets in global var
202 - $flrt_sets = $sets;
195 + // Queue the sets for the widgets (consumed by flrt_the_set())
196 + $this->context->setSets( $sets );
203 197 $this->setQueryVar('wpc_page_related_set_ids', $sets);
204 198
205 199 do_action( 'wpc_related_set_ids', $sets );
206 200
@@ -248,10 +242,10 @@
248 242 self::make_404( $wp_query, 'Invalid date/time format requested');
249 243 return true;
250 244 }
251 245 }
252 - // To will never fire this section of code again
253 - $wpc_not_fired = false;
246 + // This section must never run again for this request
247 + $this->context->markMainQueryHandled();
254 248 }
255 249
256 250 // This should be an array!
257 251 $setIds = $this->isFilteredQuery( $wp_query );
@@ -376,11 +370,11 @@
376 370 // Here was self:make_404() but I moved it.
377 371 return false;
378 372 }
379 373
380 - // Hard set of filterQueryVars
374 + // Deliberate overwrite: queried values now carry the logic separators
381 375 if (!empty($queriedValuesWithLogic)) {
382 - $this->filterQueryVars['queried_values'] = $queriedValuesWithLogic;
376 + $this->context->replace('queried_values', $queriedValuesWithLogic);
383 377 }
384 378
385 379 return true;
386 380 }
@@ -404,13 +398,30 @@
404 398 }
405 399
406 400 if ($wp_query->is_post_type_archive()) {
407 401 $post_type_object = $wp_query->get_queried_object();
408 - if (isset($post_type_object->name)) {
409 - $wp_queried_object['post_types'][] = $post_type_object->name;
402 + $post_type_name = '';
410 403
404 + if ( $post_type_object instanceof \WP_Post_Type ) {
405 + $post_type_name = $post_type_object->name;
406 + } else {
407 + // Since WooCommerce 11 the queried object on the Shop page is
408 + // the shop page WP_Post, not the "product" post type object —
409 + // fall back to the query var to keep the archive recognized.
410 + $query_post_type = $wp_query->get('post_type');
411 + if ( is_array( $query_post_type ) ) {
412 + $query_post_type = reset( $query_post_type );
413 + }
414 + if ( is_string( $query_post_type ) && $query_post_type !== '' ) {
415 + $post_type_name = $query_post_type;
416 + }
417 + }
418 +
419 + if ( $post_type_name ) {
420 + $wp_queried_object['post_types'][] = $post_type_name;
421 +
411 422 // Shop page
412 - if( $post_type_object->name === 'product' ){
423 + if( $post_type_name === 'product' ){
413 424 $wp_queried_object['common'][] = 'shop_page';
414 425 }
415 426
416 427 if( $wp_query->is_search() ){
@@ -600,23 +611,32 @@
600 611
601 612 return $where;
602 613 }
603 614
615 + /**
616 + * Thin delegates to FilterContext; kept for backward compatibility.
617 + * New code should use Container::instance()->getFilterContext() directly.
618 + */
604 619 public function getQueryVar($var, $default = false)
605 620 {
606 - if (isset($this->filterQueryVars[$var])) {
607 - return $this->filterQueryVars[$var];
608 - }
609 - return $default;
621 + return $this->ctx()->get( $var, $default );
610 622 }
611 623
612 624 public function setQueryVar($var, $value)
613 625 {
614 - if (!isset($this->filterQueryVars[$var])) {
615 - $this->filterQueryVars[$var] = $value;
616 - return true;
626 + return $this->ctx()->set( $var, $value );
627 + }
628 +
629 + /**
630 + * getQueryVar() may be called (e.g. from admin screens) before init() ran;
631 + * resolve the context lazily so those callers keep getting the default.
632 + */
633 + private function ctx()
634 + {
635 + if ( ! $this->context ) {
636 + $this->context = Container::instance()->getFilterContext();
617 637 }
618 - return false;
638 + return $this->context;
619 639 }
620 640
621 641 public static function make_404($wp_query, $message = '')
622 642 {
@@ -629,9 +649,9 @@
629 649 }
630 650
631 651 public function isFilterRequest()
632 652 {
633 - return $this->isFilterRequest;
653 + return $this->ctx()->isFilterRequest();
634 654 }
635 655
636 656 private function collectWPQueries( $wp_query )
637 657 {
@@ -832,8 +852,15 @@
832 852 }
833 853
834 854 public function customParseRequest( $do_parse_request, $WP, $extra_query_vars ){
835 855 global $wp_rewrite;
856 +
857 + // Another router (e.g. Brain\Cortex in WP User Manager) has already parsed
858 + // this request and built the query — leave it alone.
859 + if ( false === $do_parse_request ) {
860 + return $do_parse_request;
861 + }
862 +
836 863 $postData = Container::instance()->getThePost();
837 864
838 865 $WP->query_vars = array();
839 866 $post_type_query_vars = array();
@@ -1101,274 +1128,8 @@
1101 1128
1102 1129 $WP->query_posts();
1103 1130 $WP->handle_404();
1104 1131 $WP->register_globals();
1105 -
1106 - return $do_parse_request;
1107 - }
1108 -
1109 - public function customParseRequestBefore60( $do_parse_request, $WP, $extra_query_vars ){
1110 - global $wp_rewrite;
1111 - $postData = Container::instance()->getThePost();
1112 -
1113 - $WP->query_vars = [];
1114 - $post_type_query_vars = [];
1115 -
1116 - if ( is_array( $extra_query_vars ) ) {
1117 - $WP->extra_query_vars = & $extra_query_vars;
1118 - } elseif ( ! empty( $extra_query_vars ) ) {
1119 - parse_str( $extra_query_vars, $WP->extra_query_vars );
1120 - }
1121 - // Process PATH_INFO, REQUEST_URI, and 404 for permalinks.
1122 -
1123 - // Fetch the rewrite rules.
1124 - $rewrite = $wp_rewrite->wp_rewrite_rules();
1125 -
1126 - if ( ! empty( $rewrite ) ) {
1127 - // If we match a rewrite rule, this will be cleared.
1128 - $error = '404';
1129 - $WP->did_permalink = true;
1130 -
1131 - $pathinfo = isset( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : '';
1132 - list( $pathinfo ) = explode( '?', $pathinfo );
1133 - $pathinfo = str_replace( '%', '%25', $pathinfo );
1134 -
1135 - // Cleanup request path from filter segments
1136 - $request_uri = $this->getRequestUri();
1137 - $cleanedRequest = $this->requestParser->cleanUpRequestPathFromFilterSegments( $request_uri );
1138 -
1139 - list( $req_uri ) = explode( '?', $cleanedRequest );
1140 - $self = $_SERVER['PHP_SELF'];
1141 - $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' );
1142 - $home_path_regex = sprintf( '|^%s|i', preg_quote( $home_path, '|' ) );
1143 -
1144 - /*
1145 - * Trim path info from the end and the leading home path from the front.
1146 - * For path info requests, this leaves us with the requesting filename, if any.
1147 - * For 404 requests, this leaves us with the requested permalink.
1148 - */
1149 - $req_uri = str_replace( $pathinfo, '', $req_uri );
1150 - $req_uri = trim( $req_uri, '/' );
1151 - $req_uri = preg_replace( $home_path_regex, '', $req_uri );
1152 - $req_uri = trim( $req_uri, '/' );
1153 - $pathinfo = trim( $pathinfo, '/' );
1154 - $pathinfo = preg_replace( $home_path_regex, '', $pathinfo );
1155 - $pathinfo = trim( $pathinfo, '/' );
1156 - $self = trim( $self, '/' );
1157 - $self = preg_replace( $home_path_regex, '', $self );
1158 - $self = trim( $self, '/' );
1159 -
1160 - // The requested permalink is in $pathinfo for path info requests and
1161 - // $req_uri for other requests.
1162 - if ( ! empty( $pathinfo ) && ! preg_match( '|^.*' . $wp_rewrite->index . '$|', $pathinfo ) ) {
1163 - $requested_path = $pathinfo;
1164 - } else {
1165 - // If the request uri is the index, blank it out so that we don't try to match it against a rule.
1166 - if ( $req_uri == $wp_rewrite->index ) {
1167 - $req_uri = '';
1168 - }
1169 - $requested_path = $req_uri;
1170 - }
1171 - $requested_file = $req_uri;
1172 -
1173 - $WP->request = $requested_path;
1174 - $this->setQueryVar('wp_request', $requested_path);
1175 -
1176 - if( $cleanedRequest === strtolower( $request_uri ) ){
1177 - // No filter request. Let's allow WordPress and plugins continue their work
1178 - return $do_parse_request;
1179 - }
1180 -
1181 - $do_parse_request = false;
1182 - // Look for matches.
1183 - $request_match = $requested_path;
1184 -
1185 - if ( empty( $request_match ) ) {
1186 - // An empty request could only match against ^$ regex.
1187 - if ( isset( $rewrite['$'] ) ) {
1188 - $WP->matched_rule = '$';
1189 - $query = $rewrite['$'];
1190 - $matches = array( '' );
1191 - }
1192 - } else {
1193 - foreach ( (array) $rewrite as $match => $query ) {
1194 - // If the requested file is the anchor of the match, prepend it to the path info.
1195 - if ( ! empty( $requested_file ) && strpos( $match, $requested_file ) === 0 && $requested_file != $requested_path ) {
1196 - $request_match = $requested_file . '/' . $requested_path;
1197 - }
1198 -
1199 - if ( preg_match( "#^$match#", $request_match, $matches ) ||
1200 - preg_match( "#^$match#", urldecode( $request_match ), $matches ) ) {
1201 -
1202 - if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query, $varmatch ) ) {
1203 - // This is a verbose page match, let's check to be sure about it.
1204 - $page = get_page_by_path( $matches[ $varmatch[1] ] );
1205 -
1206 - if ( ! $page ) {
1207 - continue;
1208 - }
1209 -
1210 - $post_status_obj = get_post_status_object( $page->post_status );
1211 -
1212 - if ( ! $post_status_obj->public && ! $post_status_obj->protected
1213 - && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
1214 - continue;
1215 - }
1216 - }
1217 -
1218 - // Got a match.
1219 - $WP->matched_rule = $match;
1220 - break;
1221 - }
1222 - }
1223 - }
1224 -
1225 - if ( isset( $WP->matched_rule ) ) {
1226 - // Trim the query of everything up to the '?'.
1227 - $query = preg_replace( '!^.+\?!', '', $query );
1228 -
1229 - // Substitute the substring matches into the query.
1230 - $query = addslashes( \WP_MatchesMapRegex::apply( $query, $matches ) );
1231 -
1232 - $WP->matched_query = $query;
1233 -
1234 - // Parse the query.
1235 - parse_str( $query, $perma_query_vars );
1236 -
1237 - // If we're processing a 404 request, clear the error var since we found something.
1238 - if ( '404' == $error ) {
1239 - unset( $error, $_GET['error'] );
1240 - }
1241 - }
1242 -
1243 - // If req_uri is empty or if it is a request for ourself, unset error.
1244 - if (empty($requested_path) || $requested_file == $self || strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false) {
1245 - unset($error, $_GET['error']);
1246 -
1247 - if (isset($perma_query_vars) && strpos($_SERVER['PHP_SELF'], 'wp-admin/') !== false && (! isset( $postData['flrt_ajax_link'] )) ) {
1248 - unset($perma_query_vars);
1249 - }
1250 -
1251 - $WP->did_permalink = false;
1252 - }
1253 -
1254 - } else {
1255 - $do_parse_request = false;
1256 - }
1257 -
1258 - /**
1259 - * Filters the query variables whitelist before processing.
1260 - *
1261 - * Allows (publicly allowed) query vars to be added, removed, or changed prior
1262 - * to executing the query. Needed to allow custom rewrite rules using your own arguments
1263 - * to work, or any other custom query variables you want to be publicly available.
1264 - *
1265 - * @since 1.5.0
1266 - *
1267 - * @param string[] $public_query_vars The array of whitelisted query variable names.
1268 - */
1269 -
1270 - $WP->public_query_vars = apply_filters( 'query_vars', $WP->public_query_vars );
1271 -
1272 - foreach ( get_post_types( [], 'objects' ) as $post_type => $t ) {
1273 - if ( is_post_type_viewable( $t ) && $t->query_var ) {
1274 - $post_type_query_vars[ $t->query_var ] = $post_type;
1275 - }
1276 - }
1277 -
1278 - foreach ( $WP->public_query_vars as $wpvar ) {
1279 - if ( isset( $WP->extra_query_vars[ $wpvar ] ) ) {
1280 - $WP->query_vars[ $wpvar ] = $WP->extra_query_vars[ $wpvar ];
1281 - } elseif ( isset( $_GET[ $wpvar ] ) && isset( $postData[ $wpvar ] ) && $_GET[ $wpvar ] !== $postData[ $wpvar ] ) {
1282 - wp_die( esc_html__( 'A variable mismatch has been detected.' ), esc_html__( 'Sorry, you are not allowed to view this item.' ), 400 );
1283 - } elseif ( isset( $postData[ $wpvar ] ) ) {
1284 - $WP->query_vars[ $wpvar ] = $postData[ $wpvar ];
1285 - } elseif ( isset( $_GET[ $wpvar ] ) ) {
1286 - $WP->query_vars[ $wpvar ] = $_GET[ $wpvar ];
1287 - } elseif ( isset( $perma_query_vars[ $wpvar ] ) ) {
1288 - $WP->query_vars[ $wpvar ] = $perma_query_vars[ $wpvar ];
1289 - }
1290 -
1291 - if ( ! empty( $WP->query_vars[ $wpvar ] ) ) {
1292 - if ( ! is_array( $WP->query_vars[ $wpvar ] ) ) {
1293 - $WP->query_vars[ $wpvar ] = (string) $WP->query_vars[ $wpvar ];
1294 - } else {
1295 - foreach ( $WP->query_vars[ $wpvar ] as $vkey => $v ) {
1296 - if ( is_scalar( $v ) ) {
1297 - $WP->query_vars[ $wpvar ][ $vkey ] = (string) $v;
1298 - }
1299 - }
1300 - }
1301 -
1302 - if ( isset( $post_type_query_vars[ $wpvar ] ) ) {
1303 - $WP->query_vars['post_type'] = $post_type_query_vars[ $wpvar ];
1304 - $WP->query_vars['name'] = $WP->query_vars[ $wpvar ];
1305 - }
1306 - }
1307 - }
1308 -
1309 - // Convert urldecoded spaces back into '+'.
1310 - foreach ( get_taxonomies( [], 'objects' ) as $taxonomy => $t ) {
1311 - if ( $t->query_var && isset( $WP->query_vars[ $t->query_var ] ) ) {
1312 - $WP->query_vars[ $t->query_var ] = str_replace( ' ', '+', $WP->query_vars[ $t->query_var ] );
1313 - }
1314 - }
1315 -
1316 - // Don't allow non-publicly queryable taxonomies to be queried from the front end.
1317 - if ( ! is_admin() ) {
1318 - foreach ( get_taxonomies( array( 'publicly_queryable' => false ), 'objects' ) as $taxonomy => $t ) {
1319 - /*
1320 - * Disallow when set to the 'taxonomy' query var.
1321 - * Non-publicly queryable taxonomies cannot register custom query vars. See register_taxonomy().
1322 - */
1323 - if ( isset( $WP->query_vars['taxonomy'] ) && $taxonomy === $WP->query_vars['taxonomy'] ) {
1324 - unset( $WP->query_vars['taxonomy'], $WP->query_vars['term'] );
1325 - }
1326 - }
1327 - }
1328 -
1329 - // Limit publicly queried post_types to those that are 'publicly_queryable'.
1330 - if ( isset( $WP->query_vars['post_type'] ) ) {
1331 - $queryable_post_types = get_post_types( array( 'publicly_queryable' => true ) );
1332 - if ( ! is_array( $WP->query_vars['post_type'] ) ) {
1333 - if ( ! in_array( $WP->query_vars['post_type'], $queryable_post_types ) ) {
1334 - unset( $WP->query_vars['post_type'] );
1335 - }
1336 - } else {
1337 - $WP->query_vars['post_type'] = array_intersect( $WP->query_vars['post_type'], $queryable_post_types );
1338 - }
1339 - }
1340 -
1341 - // Resolve conflicts between posts with numeric slugs and date archive queries.
1342 - $WP->query_vars = wp_resolve_numeric_slug_conflicts( $WP->query_vars );
1343 -
1344 - foreach ( (array) $WP->private_query_vars as $var ) {
1345 - if ( isset( $WP->extra_query_vars[ $var ] ) ) {
1346 - $WP->query_vars[ $var ] = $WP->extra_query_vars[ $var ];
1347 - }
1348 - }
1349 -
1350 - if ( isset( $error ) ) {
1351 - $WP->query_vars['error'] = $error;
1352 - }
1353 -
1354 - /**
1355 - * Filters the array of parsed query variables.
1356 - *
1357 - * @since 2.1.0
1358 - *
1359 - * @param array $query_vars The array of requested query variables.
1360 - */
1361 - $WP->query_vars = apply_filters( 'request', $WP->query_vars );
1362 -
1363 - /**
1364 - * Fires once all query variables for the current request have been parsed.
1365 - *
1366 - * @since 2.1.0
1367 - *
1368 - * @param WP $this Current WordPress environment instance (passed by reference).
1369 - */
1370 - do_action_ref_array( 'parse_request', array( &$WP ) );
1371 1132
1372 1133 return $do_parse_request;
1373 1134 }
1374 1135