public_query_vars ?? []), (array) ($wp->private_query_vars ?? [])); foreach (array_merge(get_taxonomies([], 'objects'), get_post_types([], 'objects')) as $object) { if (is_string($object->query_var) && $object->query_var !== '') $names[] = $object->query_var; } return array_values(array_unique($names)); } function valid_url_parameter($name) { return is_string($name) && strlen($name) <= 64 && preg_match('/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/', $name) && !str_starts_with($name, 'query-') && !in_array($name, reserved_url_parameters(), true); } function url_parameter_fields($control) { $name = $control['urlParameter'] ?? ''; return array_map(static fn($suffix) => $name . ($suffix === 'value' ? '' : '-' . $suffix), native_parameter_suffixes($control)); } function url_parameter_blocked($name, $query_id, $control_id) { global $fqbpro_url_parameter_scope; if (!is_array($fqbpro_url_parameter_scope)) return false; return $fqbpro_url_parameter_scope['overflow'] || ($fqbpro_url_parameter_scope['counts'][$name] ?? 0) > 1 || empty($fqbpro_url_parameter_scope['owners'][$name][$query_id . ':' . $control_id]); } /** Expand static page/template/reusable content before any Query consumes a name. */ function prepare_url_parameter_scope($blocks) { global $fqbpro_url_parameter_scope; $scope = ['counts' => [], 'owners' => [], 'overflow' => false]; $nodes = 0; $walk = static function ($children, $stack = [], $depth = 0, $query_id = 0, $in_post_template = false) use (&$walk, &$scope, &$nodes) { if ($depth > 16) { $scope['overflow'] = true; return; } foreach ((array) $children as $block) { if (++$nodes > 4000) { $scope['overflow'] = true; return; } $name = $block['blockName'] ?? ''; $attrs = $block['attrs'] ?? []; $owner_query = $name === 'core/query' ? absint($attrs['queryId'] ?? 0) : $query_id; $dynamic_post_context = $in_post_template || $name === 'core/post-template'; if ($name === 'filter-query-block-pro/filter-header' && (int) ($attrs['nativeVersion'] ?? 0) === 1) { foreach (\Fqbpro\Plugin\Blocks\FilterHeader\native_controls_from_tree($block) as $control) { $alias = $control['urlParameter'] ?? ''; if (!valid_url_parameter($alias)) continue; if ($control['kind'] === 'date' && !isset($control['settings']['selection'])) $control['settings']['selection'] = 'range'; foreach (array_unique(array_merge([$alias], url_parameter_fields($control))) as $key) { $scope['counts'][$key] = ($scope['counts'][$key] ?? 0) + 1; $scope['owners'][$key][$owner_query . ':' . $control['controlId']] = true; } } } $content = null; $ref = ''; if ($name === 'core/block' && !empty($attrs['ref'])) { $ref = 'block:' . absint($attrs['ref']); $source = get_post(absint($attrs['ref'])); if ($source && $source->post_type === 'wp_block') $content = $source->post_content; } elseif ($name === 'core/template-part' && !empty($attrs['slug'])) { $ref = 'part:' . ($attrs['theme'] ?? get_stylesheet()) . '//' . $attrs['slug']; $source = get_block_template(substr($ref, 5), 'wp_template_part'); if ($source) $content = $source->content; } elseif ($name === 'core/post-content' && !$dynamic_post_context) { // Post Template supplies a different postId for each result. Its // content is unknown here; guessing the main queried post would // count that page's aliases again and create false collisions. $source = get_queried_object(); if ($source instanceof \WP_Post) { $ref = 'post:' . $source->ID; $content = $source->post_content; } } if ($content !== null && !in_array($ref, $stack, true)) $walk(parse_blocks($content), array_merge($stack, [$ref]), $depth + 1, $owner_query, $dynamic_post_context); $walk($block['innerBlocks'] ?? [], $stack, $depth + 1, $owner_query, $dynamic_post_context); } }; $walk($blocks); $fqbpro_url_parameter_scope = $scope; } function preflight_url_parameters($pre_render, $parsed_block) { global $fqbpro_url_parameter_scope, $_wp_current_template_content; if (!is_array($fqbpro_url_parameter_scope)) { $source = get_queried_object(); $content = !empty($_wp_current_template_content) ? $_wp_current_template_content : ($source instanceof \WP_Post ? $source->post_content : ''); prepare_url_parameter_scope($content !== '' ? parse_blocks($content) : [$parsed_block]); } return $pre_render; } function preflight_content_url_parameters($content) { global $fqbpro_url_parameter_scope; if (!is_array($fqbpro_url_parameter_scope)) prepare_url_parameter_scope(parse_blocks($content)); return $content; } add_filter('pre_render_block', __NAMESPACE__ . '\preflight_url_parameters', 1, 2); add_filter('the_content', __NAMESPACE__ . '\preflight_content_url_parameters', 8);