1) { $occurrence = $fqbpro_runtime_query_ids[$id]; $parsed_block['attrs']['queryId'] = 100000000 + (abs(crc32($id . ':' . $occurrence)) % 900000000); } return $parsed_block; } /** Reset request-local identity state for focused tests. */ function reset_runtime_query_ids() { global $fqbpro_runtime_query_ids; $fqbpro_runtime_query_ids = []; unset($GLOBALS['fqbpro_url_parameter_scope']); } /** * Carry the defining Filter Header configuration through Query descendants. * * WordPress builds Query Loop arguments while rendering inner blocks such as * core/post-template, not while rendering core/query itself. The parent block * is available on render_block_context, so this adds a private runtime context * value to the variation's descendants without duplicating configuration in * saved Query attributes. * * @param array $context Current block context. * @param array $parsed_block Block being rendered. * @param \WP_Block|null $parent_block Parent block instance. * @return array */ function filter_block_context($context, $parsed_block, $parent_block) { $context = is_array($context) ? $context : []; if (($parsed_block['blockName'] ?? '') === 'core/query') { // A nested Query owns its own configuration; never inherit an outer one. unset($context[CONTEXT_KEY]); if ($parent_block instanceof \WP_Block && is_legacy_wrapper($parent_block)) { $context[CONTEXT_KEY] = [ 'contract' => 'legacy-v3', 'attributes' => (array) ($parent_block->parsed_block['attrs'] ?? []), ]; } return $context; } if (!$parent_block instanceof \WP_Block) { return $context; } $attributes = null; if (is_filter_query_block($parent_block)) { $attributes = [ 'contract' => 'v4', 'attributes' => find_filter_header_attributes($parent_block), ]; if (is_array($attributes['attributes']) && !empty($parent_block->parsed_block['attrs']['query']['inherit'])) { global $wp_query; $attributes['baseQuery'] = $wp_query instanceof \WP_Query ? $wp_query->query_vars : ['post__in' => [0]]; $type = $attributes['baseQuery']['post_type'] ?? 'post'; if (is_array($type)) $type = count($type) === 1 ? reset($type) : 'any'; $type = is_string($type) && $type !== '' ? sanitize_key($type) : 'post'; $attributes['inheritedPostType'] = $type; if ($type === 'any') $attributes['baseQuery']['post__in'] = [0]; } } elseif (($parent_block->parsed_block['blockName'] ?? '') === 'core/query') { $parent_envelope = $parent_block->context[CONTEXT_KEY] ?? null; if (is_array($parent_envelope) && ($parent_envelope['contract'] ?? '') === 'legacy-v3') { $attributes = $parent_envelope; } else { unset($context[CONTEXT_KEY]); return $context; } } elseif (isset($parent_block->context[CONTEXT_KEY]) && is_array($parent_block->context[CONTEXT_KEY])) { $attributes = $parent_block->context[CONTEXT_KEY]; } if (is_array($attributes)) { $context[CONTEXT_KEY] = $attributes; if (($attributes['contract'] ?? '') === 'v4' && isset($attributes['baseQuery'])) { // Core otherwise bypasses query_loop_block_query_vars for inherited loops. // Only the descendant runtime context changes; saved attributes do not. $context['query'] = is_array($context['query'] ?? null) ? $context['query'] : []; $context['query']['inherit'] = false; $context['query']['postType'] = $attributes['inheritedPostType']; $context['query']['perPage'] = (int) ($attributes['baseQuery']['posts_per_page'] ?? get_option('posts_per_page')); } } return $context; } /** * Apply one Filter Header request to its parent native Query Loop. * * @param array $query Native Query Loop arguments. * @param \WP_Block $block Query block instance. * @param int $page Current Query Loop page. * @param bool $initial_only Compile saved restrictions without visitor state for term counts. * @return array */ function filter_query_vars($query, $block, $page, $initial_only = false) { if (!$block instanceof \WP_Block) { return $query; } $envelope = $block->context[CONTEXT_KEY] ?? null; if (!is_array($envelope) || !is_array($envelope['attributes'] ?? null)) { return $query; } $attributes = $envelope['attributes']; $is_legacy = ($envelope['contract'] ?? '') === 'legacy-v3'; if (!$is_legacy && is_array($envelope['baseQuery'] ?? null)) { // Start from the archive/search restrictions, never core's unrelated default // post-type/order arguments, and give this Query its own requested page. $query = $envelope['baseQuery']; $query['paged'] = max(1, absint($page)); $per_page = (int) ($query['posts_per_page'] ?? get_option('posts_per_page')); if (isset($query['offset']) && is_numeric($query['offset'])) { $query['offset'] = max(0, (int) $query['offset']) + ($per_page > 0 ? ($query['paged'] - 1) * $per_page : 0); } else { unset($query['offset']); } if (($query['post__in'] ?? null) === [0]) return $query; } $post_type = resolve_post_type($query, $block); if ($is_legacy) { // A saved legacy placement grandfathers only its registered post type. // Track it before the shared advanced compiler performs its allowlist check. Service\track_referenced_post_type($post_type); } $is_free = defined('FQBPRO_EDITION') && FQBPRO_EDITION === 'free'; $tracked = $is_free ? in_array($post_type, Service\get_tracked_post_types(), true) : Advanced\is_tracked_type($post_type); if (!$is_legacy && !$tracked) { $query['post__in'] = [0]; return $query; } $context_query = isset($block->context['query']) && is_array($block->context['query']) ? $block->context['query'] : []; if ($is_free) { // Paid saved constraints must never be silently discarded on a downgrade. if (!empty($context_query['fqbp'])) { $query['post__in'] = [0]; return $query; } } else { $saved_advanced = $is_legacy ? LegacyV3\advanced_query($attributes['query'] ?? $context_query, $post_type) : ($context_query[Advanced\KEY] ?? null); $query = Advanced\compile($query, $saved_advanced, $post_type, $is_legacy, !$is_legacy && (int) ($attributes['nativeVersion'] ?? 0) === 1); } if (($query['post__in'] ?? null) === [0]) { return $query; } $query_id = absint($block->context['queryId'] ?? 0); $sources = !$is_legacy && !$is_free ? FilterHeader\get_catalog_sources((array) ($query['post_type'] ?? $post_type)) : FilterHeader\get_catalog_sources($post_type, $is_legacy); $configuration = $is_legacy ? LegacyV3\configuration($attributes, $post_type, $query_id) : FilterHeader\normalize_configuration($attributes, $post_type, $query_id, $sources); if (($configuration['queryId'] ?? 0) === 0) { // An unavailable saved control stays closed even when its Query lacks a URL ID. return $is_free ? Compiler\compile($query, $configuration, [], FilterHeader\get_catalog_sources($post_type)) : $query; } if ($initial_only) return Compiler\compile($query, $configuration, [], $sources); // Public, bookmarkable GET filtering is read-only. Each control validates its own state below. // phpcs:ignore WordPress.Security.NonceVerification.Recommended $request = isset($_GET) && is_array($_GET) ? wp_unslash($_GET) : []; $state = $is_legacy ? LegacyV3\request_state($request, $configuration) : Request\normalize_state($request, $configuration); if ($is_legacy) { // Keep legacy search on its existing native Query Loop settings. unset($configuration['search']['columns']); } return Compiler\compile($query, $configuration, $state, $sources); } /** Whether a block is the supported version 1-3 wrapper. */ function is_legacy_wrapper($block) { return (!defined('FQBPRO_EDITION') || FQBPRO_EDITION === 'pro') && $block instanceof \WP_Block && ($block->parsed_block['blockName'] ?? '') === LegacyV3\BLOCK_NAME; } /** * Check that a core/query parent is the Filter Query variation. * * @param \WP_Block $block Query block. * @return bool */ function is_filter_query_block($block) { $parsed = isset($block->parsed_block) && is_array($block->parsed_block) ? $block->parsed_block : []; if (($parsed['blockName'] ?? '') !== 'core/query') { return false; } if (($parsed['attrs']['namespace'] ?? '') === QUERY_NAMESPACE) { return true; } return find_filter_header_attributes($block) !== null; } /** * Find the defining Filter Header within supported Query layout blocks. * * @param \WP_Block $block Query block. * @return array|null */ function find_filter_header_attributes($block) { $parsed = $block instanceof \WP_Block ? $block->parsed_block : []; $header = find_filter_header_block($parsed); return $header !== null ? FilterHeader\resolve_header_attributes($header) : null; } /** First Header in document order belongs to this Query, including either column. */ function find_filter_header_block($parsed_query) { $headers = query_header_blocks($parsed_query); return $headers[0]['block'] ?? null; } /** * Return Header definitions and paths relative to this Query's children. Only * layout blocks pass ownership through; nested Queries own separate definitions. */ function query_header_blocks($parsed_query) { $headers = []; $nodes = 0; $walk = static function ($blocks, $path = [], $depth = 0) use (&$walk, &$headers, &$nodes) { if ($depth > 8) return; foreach ((array) $blocks as $index => $block) { if (++$nodes > 200) return; if (!is_array($block)) continue; $name = $block['blockName'] ?? ''; $position = array_merge($path, [$index]); if ($name === 'filter-query-block-pro/filter-header') { $headers[] = ['block' => $block, 'path' => $position]; } elseif (in_array($name, HEADER_LAYOUT_BLOCKS, true)) { $walk($block['innerBlocks'] ?? [], $position, $depth + 1); } } }; $walk(is_array($parsed_query) ? ($parsed_query['innerBlocks'] ?? []) : []); return $headers; } /** * Resolve the single Query Loop post type. * * @param array $query Query arguments. * @param \WP_Block $block Query block. * @return string */ function resolve_post_type($query, $block) { $context_query = isset($block->context['query']) && is_array($block->context['query']) ? $block->context['query'] : []; $envelope = $block->context[CONTEXT_KEY] ?? []; $post_type = isset($envelope['baseQuery']) && ($envelope['contract'] ?? '') === 'v4' ? ($envelope['inheritedPostType'] ?? ($query['post_type'] ?? 'post')) : ($context_query['postType'] ?? ($query['post_type'] ?? 'post')); if (is_array($post_type)) { $post_type = reset($post_type) ?: 'post'; } return sanitize_key($post_type) ?: 'post'; } /** Catalog sources for all selected types; Free keeps its single-type contract. */ function query_catalog_sources($query) { $primary = sanitize_key($query['postType'] ?? 'post') ?: 'post'; if (defined('FQBPRO_EDITION') && FQBPRO_EDITION === 'free') return FilterHeader\get_catalog_sources($primary); $config = Advanced\normalize($query['fqbp'] ?? []); $types = is_wp_error($config) ? [$primary] : array_merge([$primary], $config['postTypes']); return FilterHeader\get_catalog_sources($types); }