Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.28% |
571 / 790 |
|
57.14% |
44 / 77 |
CRAP | |
0.00% |
0 / 1 |
| Search_Blocks | |
72.28% |
571 / 790 |
|
57.14% |
44 / 77 |
1785.71 | |
0.00% |
0 / 1 |
| init | |
97.22% |
35 / 36 |
|
0.00% |
0 / 1 |
12 | |||
| owns_search_results | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| is_tracking_disabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| filter__posts_pre_query | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| is_block_template_overlay_enabled | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| is_block_template_overlay_filter_on | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_free_plan | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| reset_is_free_plan_cache | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| supports_paid_search | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| set_supports_paid_search_for_testing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| reset_supports_paid_search_cache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| woocommerce_blocks_enabled | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| woocommerce_version_supported | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
3 | |||
| set_woocommerce_blocks_enabled_for_testing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reset_woocommerce_blocks_enabled_cache | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| woocommerce_search_template_override_enabled | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| is_woocommerce_product_search | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| woocommerce_only_block_names | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| is_woocommerce_only_block | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| custom_taxonomy_map | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve_taxonomy_slot | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reset_custom_taxonomy_map_cache | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| supported_custom_taxonomies | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
4.07 | |||
| get_search_param_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
3 | |||
| enqueue_editor_assets | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
6 | |||
| register_block_category | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| register_blocks | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 | |||
| register_store_script_module | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
3.00 | |||
| same_origin_script_module_src | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 | |||
| inject_filter_checkbox_variations | |
100.00% |
98 / 98 |
|
100.00% |
1 / 1 |
7 | |||
| register_patterns | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
| pattern_content_from_template | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| get_search_template_content | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| register_search_template | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| should_use_product_overlay | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get_overlay_template_content | |
76.92% |
10 / 13 |
|
0.00% |
0 / 1 |
7.60 | |||
| reset_overlay_template_content_cache | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| print_block_template_overlay | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
12 | |||
| enqueue_block_template_overlay_assets | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
12 | |||
| print_theme_token_sampler | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| block_template_overlay_inline_css | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| enqueue_search_page_assets | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| enqueue_search_layout_style | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| search_layout_inline_css | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| get_product_search_template_content | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| substitute_template_placeholders | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
2.00 | |||
| resolve_chrome_slugs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| replace_block_template | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| register_product_search_template | |
84.62% |
11 / 13 |
|
0.00% |
0 / 1 |
4.06 | |||
| get_parent_plugin_slug | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| prepend_search_template | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| route_classic_theme_search_template | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
10.04 | |||
| get_classic_theme_search_body | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| get_classic_theme_product_search_body | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| strip_top_level_template_parts | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| get_classic_theme_layout_style | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
| set_block_templates_active_for_testing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| block_templates_active | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| route_woocommerce_product_search_template | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| seed_interactivity_state | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| build_seed_state | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| collect_filter_configs_from_post | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
56 | |||
| filter_block_helpers | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
| walk_blocks_for_filter_configs | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| build_initial_state | |
100.00% |
47 / 47 |
|
100.00% |
1 / 1 |
11 | |||
| build_stock_status_labels | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
4.18 | |||
| is_initial_loading | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| reset_initial_loading_cache | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
| pre_hydration_filter_view | |
66.67% |
10 / 15 |
|
0.00% |
0 / 1 |
3.33 | |||
| emit_filter_wrapper_context | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| normalize_display_style | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| build_initial_strings | |
51.35% |
19 / 37 |
|
0.00% |
0 / 1 |
4.04 | |||
| build_ai_extended_loading_hints | |
51.43% |
18 / 35 |
|
0.00% |
0 / 1 |
2.46 | |||
| parse_url_search_query | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| has_search_param | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| parse_url_sort | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
| parse_url_price_range | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
7.03 | |||
| parse_price_bound | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
7.54 | |||
| parse_url_filters | |
96.77% |
30 / 31 |
|
0.00% |
0 / 1 |
10 | |||
| parse_url_filter_logic | |
43.75% |
7 / 16 |
|
0.00% |
0 / 1 |
27.80 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Search Blocks: Interactivity API block registration and state initialization. |
| 4 | * |
| 5 | * @package automattic/jetpack-search |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Search; |
| 9 | |
| 10 | use Automattic\Jetpack\Status; |
| 11 | |
| 12 | /** |
| 13 | * Registers Jetpack Search Interactivity API blocks and initializes their shared state. |
| 14 | */ |
| 15 | class Search_Blocks { |
| 16 | |
| 17 | /** |
| 18 | * Reserved query params (mirrors `RESERVED_PARAMS` in `store/url-state.js`). |
| 19 | * `s` is the WP search route key; `q` is what the inline blocks use on |
| 20 | * non-search pages (see `get_search_param_name()`). Neither may be parsed as a filter key. |
| 21 | */ |
| 22 | const RESERVED_QUERY_PARAMS = array( 's', 'q', 'orderby', 'min_price', 'max_price' ); |
| 23 | |
| 24 | /** |
| 25 | * Query-string param for inline search on non-search pages (e.g. `/about/?q=boots`). |
| 26 | * Not `s`, because on singular pages WP's `WP_Query::get_posts()` AND's a |
| 27 | * `post_content LIKE` clause into the lookup and 404s on refresh. See |
| 28 | * `docs/explorations/embedded-search-refresh-404.md` (RSM-1754). |
| 29 | */ |
| 30 | const NON_SEARCH_QUERY_PARAM = 'q'; |
| 31 | |
| 32 | /** |
| 33 | * Jetpack Search page template slug. Distinct from WP's `search` slug so a |
| 34 | * block theme's `search.html` doesn't dedupe ours; `search_template_hierarchy` |
| 35 | * prepends this slug so it still wins on `/?s=...`. |
| 36 | */ |
| 37 | const SEARCH_TEMPLATE_SLUG = 'jetpack-search'; |
| 38 | |
| 39 | /** |
| 40 | * Jetpack product-search template slug. Separate from `SEARCH_TEMPLATE_SLUG` |
| 41 | * so it gets its own Site Editor entry. |
| 42 | */ |
| 43 | const PRODUCT_SEARCH_TEMPLATE_SLUG = 'jetpack-search-product-results'; |
| 44 | |
| 45 | /** |
| 46 | * Mirror of `ProductSearchResultsTemplate::SLUG`, inlined to avoid a hard |
| 47 | * dependency on the WooCommerce class. |
| 48 | */ |
| 49 | const WC_PRODUCT_SEARCH_TEMPLATE_SLUG = 'product-search-results'; |
| 50 | |
| 51 | /** |
| 52 | * Lowest WC version that registers the `product-search-results` template |
| 53 | * (WC 6.5 bundled WC Blocks 7.4, the release that first added it). Below |
| 54 | * this, WC-only Search features have nothing to front and the gate stays closed. |
| 55 | */ |
| 56 | const MIN_WOOCOMMERCE_VERSION = '6.5.0'; |
| 57 | |
| 58 | /** |
| 59 | * Per-request memo for `is_initial_loading()`. Lifted out of a method-local |
| 60 | * `static` so tests can clear it via `reset_initial_loading_cache()`; |
| 61 | * otherwise URL state from the first test leaks into subsequent ones. |
| 62 | * |
| 63 | * @var bool|null |
| 64 | */ |
| 65 | private static $is_initial_loading_cache = null; |
| 66 | |
| 67 | /** |
| 68 | * Per-request memo for `get_overlay_template_content()`, keyed `default` / |
| 69 | * `product`. Lifted out of a method-local `static` so tests can clear it via |
| 70 | * `reset_overlay_template_content_cache()` — otherwise a CPT-customized |
| 71 | * overlay saved mid-test would be pinned by an earlier bundled-file read. |
| 72 | * |
| 73 | * @var array<string,string> |
| 74 | */ |
| 75 | private static $overlay_template_content_cache = array(); |
| 76 | |
| 77 | /** |
| 78 | * Per-request memo for `is_free_plan()`. Avoids the cold-cache hazard where |
| 79 | * `Plan::get_plan_info()` falls back to a synchronous WPCOM HTTP call — |
| 80 | * render callbacks hit the plan gate on every inner render. |
| 81 | * |
| 82 | * @var bool|null |
| 83 | */ |
| 84 | private static $is_free_plan_cache = null; |
| 85 | |
| 86 | /** |
| 87 | * Per-request memo for `supports_paid_search()`. Separate from |
| 88 | * `is_free_plan_cache` because the two answers can disagree: a site with |
| 89 | * no plan info is neither on the free plan nor on a paid one. |
| 90 | * |
| 91 | * @var bool|null |
| 92 | */ |
| 93 | private static $supports_paid_search_cache = null; |
| 94 | |
| 95 | /** |
| 96 | * Per-request memo for `woocommerce_blocks_enabled()`. Centralized so every |
| 97 | * gate (registration, render, editor config, IA store seed) shares one probe. |
| 98 | * |
| 99 | * @var bool|null |
| 100 | */ |
| 101 | private static $woocommerce_blocks_enabled_cache = null; |
| 102 | |
| 103 | /** |
| 104 | * Per-request memo for `supported_custom_taxonomies()`. Derived from the |
| 105 | * Sync allowlist intersected with registered taxonomies and unioned with |
| 106 | * the map's user-facing keys — same inputs every request. |
| 107 | * |
| 108 | * @var string[]|null |
| 109 | */ |
| 110 | private static $supported_custom_taxonomies_cache = null; |
| 111 | |
| 112 | /** |
| 113 | * Cached rendered overlay-template HTML. Filled during `wp_enqueue_scripts` |
| 114 | * so the embedded blocks' view-module enqueues land before |
| 115 | * `wp_print_import_map()` (footer priority 1) — see AGENTS.md |
| 116 | * § Hydration & SSR seeding. |
| 117 | * |
| 118 | * @var string|null |
| 119 | */ |
| 120 | private static $block_template_overlay_rendered_html = null; |
| 121 | |
| 122 | /** |
| 123 | * Register block types and hook into WordPress. |
| 124 | * |
| 125 | * Two gates apply: the caller (`Initializer`) gates everything behind the |
| 126 | * `jetpack_search_blocks_enabled` feature flag, and within this method the |
| 127 | * template-takeover surface (registering the Search template and prepending |
| 128 | * it to `search_template_hierarchy`) is additionally gated on the saved |
| 129 | * experience being `'embedded'` — only Embedded should override the theme's |
| 130 | * `search.html`. Block registration, editor assets, and IA state seeding |
| 131 | * always run so blocks inserted anywhere (post content, widgets, custom |
| 132 | * templates) get their base seed. |
| 133 | */ |
| 134 | public static function init() { |
| 135 | add_action( 'init', array( static::class, 'register_blocks' ) ); |
| 136 | add_filter( 'block_categories_all', array( static::class, 'register_block_category' ) ); |
| 137 | add_action( 'enqueue_block_editor_assets', array( static::class, 'enqueue_editor_assets' ) ); |
| 138 | add_action( 'wp_body_open', array( static::class, 'print_theme_token_sampler' ) ); |
| 139 | // Relativize `jetpack-search/*` Script Module URLs whose host matches |
| 140 | // the site canonical so the rendered `<script type="module">` is |
| 141 | // same-origin with the page. ES modules go through CORS even without |
| 142 | // a `crossorigin` attribute, and `wp-content/*` typically lacks the |
| 143 | // `Access-Control-Allow-Origin` header — see |
| 144 | // `same_origin_script_module_src()`. |
| 145 | add_filter( 'script_module_loader_src', array( static::class, 'same_origin_script_module_src' ), 10, 2 ); |
| 146 | Custom_Taxonomy_Slot_Mapping::init(); |
| 147 | // Both hooks needed; see AGENTS.md § Hydration & SSR seeding. |
| 148 | add_action( 'template_redirect', array( static::class, 'seed_interactivity_state' ) ); |
| 149 | add_action( 'wp_enqueue_scripts', array( static::class, 'seed_interactivity_state' ) ); |
| 150 | |
| 151 | $experience = ( new Module_Control() )->get_experience(); |
| 152 | |
| 153 | if ( Module_Control::EXPERIENCE_EMBEDDED === $experience ) { |
| 154 | if ( static::block_templates_active() ) { |
| 155 | // Block themes: register the template and front it via the FSE hierarchy filter. |
| 156 | add_action( 'init', array( static::class, 'register_search_template' ) ); |
| 157 | add_filter( 'search_template_hierarchy', array( static::class, 'prepend_search_template' ) ); |
| 158 | add_action( 'wp_enqueue_scripts', array( static::class, 'enqueue_search_page_assets' ) ); |
| 159 | Theme_Chrome_Slug_Resolver::register_hooks(); |
| 160 | } else { |
| 161 | // Classic themes: no FSE hierarchy to prepend to, so swap the |
| 162 | // resolved template path via `template_include`. The block markup |
| 163 | // renders inside the theme's `get_header()`/`get_footer()`. |
| 164 | // |
| 165 | // Priority 20: WooCommerce's `WC_Template_Loader::template_loader` |
| 166 | // hooks at priority 10 and rewrites the path to `archive-product.php` |
| 167 | // on product-archive requests — that includes product search. We |
| 168 | // need to run *after* WC so the override actually sticks; running |
| 169 | // at 10 (same priority, later registration) is order-of-load |
| 170 | // dependent. Higher priorities (anything > 20 used by chrome |
| 171 | // filters) aren't relevant — nothing else swaps the path. |
| 172 | add_filter( 'template_include', array( static::class, 'route_classic_theme_search_template' ), 20 ); |
| 173 | // No Site Editor entry on classic themes; the singleton CPTs give |
| 174 | // authors the standard block editor on hidden posts instead. Both |
| 175 | // init regardless of the WooCommerce override option so admins can |
| 176 | // pre-customize either template before activating the relevant |
| 177 | // surface — matching `Search_Template`'s "expose URLs before |
| 178 | // activation" rule. The override option still gates the actual |
| 179 | // front-end render path in `route_classic_theme_search_template()`. |
| 180 | Search_Template::init(); |
| 181 | Product_Search_Template::init(); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Inline on a classic theme: the theme renders regular searches, but a |
| 186 | // WooCommerce product search can still be routed to the Jetpack product |
| 187 | // shim. `route_classic_theme_search_template()` is product-only for |
| 188 | // Inline (it bails on regular searches unless Embedded); the block-theme |
| 189 | // Inline path is covered by the `search_template_hierarchy` route below. |
| 190 | // Init the product CPT regardless of the override so admins can |
| 191 | // pre-customize it (expose-before-activation, as in the Embedded branch). |
| 192 | if ( |
| 193 | Module_Control::EXPERIENCE_INLINE === $experience |
| 194 | && ! static::block_templates_active() |
| 195 | ) { |
| 196 | add_filter( 'template_include', array( static::class, 'route_classic_theme_search_template' ), 20 ); |
| 197 | Product_Search_Template::init(); |
| 198 | } |
| 199 | |
| 200 | // Blocks render results client-side, so the server-side search is wasted work. |
| 201 | // (Classic/Instant init is also suppressed in `Initializer::init_search_blocks()`.) |
| 202 | if ( ! is_admin() && static::owns_search_results() ) { |
| 203 | add_filter( 'posts_pre_query', array( static::class, 'filter__posts_pre_query' ), 10, 2 ); |
| 204 | } |
| 205 | |
| 206 | // Priority 20: after WC's priority-10 prepend so the result is load-order |
| 207 | // independent. Gated to server-rendered experiences (Embedded / Inline) — |
| 208 | // Overlay intercepts client-side, and a stale option from a since-switched |
| 209 | // experience must not keep rerouting the template hierarchy. |
| 210 | if ( |
| 211 | static::woocommerce_search_template_override_enabled() |
| 212 | && in_array( $experience, array( Module_Control::EXPERIENCE_EMBEDDED, Module_Control::EXPERIENCE_INLINE ), true ) |
| 213 | ) { |
| 214 | add_action( 'init', array( static::class, 'register_product_search_template' ) ); |
| 215 | add_filter( 'search_template_hierarchy', array( static::class, 'route_woocommerce_product_search_template' ), 20 ); |
| 216 | // Inline experience doesn't go through the EMBEDDED branch above, so |
| 217 | // hook the page-template CSS enqueue here too. Idempotent on EMBEDDED |
| 218 | // — `add_action` dedupes same callback at same priority. |
| 219 | add_action( 'wp_enqueue_scripts', array( static::class, 'enqueue_search_page_assets' ) ); |
| 220 | } |
| 221 | |
| 222 | // Two-tier gate: register the editable template CPT + admin-init editor |
| 223 | // handler whenever the operator filter is on, so admins can edit the |
| 224 | // overlay template *before* opting into the blocks Overlay experience |
| 225 | // (e.g. preview the editor from the Beta card while the preact Overlay |
| 226 | // is still the active arm — without this split, the editorUrl seeded |
| 227 | // into the React initial state at page load would be null and the |
| 228 | // link would become a no-op once the user switched to the Beta card |
| 229 | // without a page refresh). The front-end render hooks stay gated on |
| 230 | // the active experience — they only paint the overlay when the user |
| 231 | // has actually committed to it. |
| 232 | if ( static::is_block_template_overlay_filter_on() ) { |
| 233 | Overlay_Template::init(); |
| 234 | // The product overlay only renders on a Woo store, so its editable |
| 235 | // CPT is pointless off Woo. Init regardless of the override option |
| 236 | // (parity with `Product_Search_Template`) so admins can pre-customize |
| 237 | // before flipping it on; the front-end render path stays gated by |
| 238 | // the override option in `should_use_product_overlay()`. |
| 239 | if ( static::woocommerce_blocks_enabled() ) { |
| 240 | Product_Overlay_Template::init(); |
| 241 | } |
| 242 | } |
| 243 | if ( static::is_block_template_overlay_enabled() ) { |
| 244 | add_action( 'wp_enqueue_scripts', array( static::class, 'enqueue_block_template_overlay_assets' ) ); |
| 245 | add_action( 'wp_footer', array( static::class, 'print_block_template_overlay' ) ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Whether the Search blocks own the front-end search results for the active |
| 251 | * experience, meaning the server should run no search of its own. |
| 252 | * |
| 253 | * True for Embedded (the blocks template takes over the search page) and for |
| 254 | * the enabled blocks Overlay (a full-screen modal over the theme's search |
| 255 | * page). The Overlay arm goes through `is_block_template_overlay_enabled()` |
| 256 | * — operator filter plus saved experience — so a stale `overlay_blocks` |
| 257 | * option can't keep suppressing server search after the overlay is turned |
| 258 | * off. Drives both the Classic/Instant init suppression in |
| 259 | * `Initializer::init_search_blocks()` and the `posts_pre_query` short-circuit |
| 260 | * registered in `init()`. |
| 261 | * |
| 262 | * @return bool |
| 263 | */ |
| 264 | public static function owns_search_results(): bool { |
| 265 | return Module_Control::EXPERIENCE_EMBEDDED === ( new Module_Control() )->get_experience() |
| 266 | || static::is_block_template_overlay_enabled(); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Whether TrainTracks analytics are suppressed for this request. Mirrors |
| 271 | * instant search (Helper::get_search_options): the `?disable_tracking=1` |
| 272 | * crawler/QA param plus the `jetpack_instant_search_disable_tracking` |
| 273 | * operator filter. Gates both the `_tkq` pushes (seeded into |
| 274 | * `state.disableTracking`) and whether the Tracks consumer script loads. |
| 275 | * |
| 276 | * @return bool |
| 277 | */ |
| 278 | public static function is_tracking_disabled(): bool { |
| 279 | return ( class_exists( Helper::class ) && Helper::is_tracking_disabled() ) |
| 280 | || apply_filters( 'jetpack_instant_search_disable_tracking', false ); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Short-circuit the main front-end search query when the blocks own results |
| 285 | * (see AGENTS.md § Search experiences). Registered only when |
| 286 | * `owns_search_results()` is true and off `is_admin()`. |
| 287 | * |
| 288 | * Pagination totals are set to `1` (not `0`) so `have_posts()`-gated |
| 289 | * templates still render the shell the client hydrates — WP core skips |
| 290 | * `set_found_posts()` when `posts_pre_query` returns an array. |
| 291 | * |
| 292 | * @param array|null $posts Posts to return in place of the query (null by default). |
| 293 | * @param \WP_Query $query The WP_Query being filtered. |
| 294 | * @return array|null Empty array to short-circuit, or $posts to let it run. |
| 295 | */ |
| 296 | public static function filter__posts_pre_query( $posts, $query ) { |
| 297 | if ( ! $query->is_main_query() || ! $query->is_search() ) { |
| 298 | return $posts; |
| 299 | } |
| 300 | |
| 301 | $query->found_posts = 1; |
| 302 | $query->max_num_pages = 1; |
| 303 | |
| 304 | return array(); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Whether to replace the legacy instant-search overlay with the |
| 309 | * server-rendered Search blocks template |
| 310 | * (`templates/jetpack-search-overlay.html`). |
| 311 | * |
| 312 | * Two conditions: the operator filter `is_block_template_overlay_filter_on()` |
| 313 | * is on (defaults true), AND the site owner has chosen |
| 314 | * `Module_Control::EXPERIENCE_OVERLAY_BLOCKS` in the dashboard. When both |
| 315 | * hold, the legacy `SearchApp` is bypassed via |
| 316 | * `jetpack_search_init_instant_search` in `Initializer::init_search_blocks()`. |
| 317 | * |
| 318 | * @return bool |
| 319 | */ |
| 320 | public static function is_block_template_overlay_enabled(): bool { |
| 321 | if ( ! static::is_block_template_overlay_filter_on() ) { |
| 322 | return false; |
| 323 | } |
| 324 | return Module_Control::EXPERIENCE_OVERLAY_BLOCKS === ( new Module_Control() )->get_experience(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Whether the operator filter that exposes the blocks-powered overlay is on. |
| 329 | * |
| 330 | * Lighter than `is_block_template_overlay_enabled()` — doesn't require the |
| 331 | * user to have opted into the new overlay. Use for one-time setup that |
| 332 | * should run *before* opt-in (e.g. registering the editable template CPT |
| 333 | * so admins can preview the editor while preact Overlay is still active). |
| 334 | * |
| 335 | * @return bool |
| 336 | */ |
| 337 | public static function is_block_template_overlay_filter_on(): bool { |
| 338 | /** |
| 339 | * Opt out of the experimental Search blocks overlay. Available by |
| 340 | * default; return false to hide the Beta card from the Experience |
| 341 | * Selector and disable the editable-template CPT. |
| 342 | * |
| 343 | * @param bool $enabled Default true. |
| 344 | */ |
| 345 | return (bool) apply_filters( 'jetpack_search_overlay_block_template_enabled', true ); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Memoized `Plan::is_free_plan()`. See `$is_free_plan_cache`. |
| 350 | * |
| 351 | * @return bool |
| 352 | */ |
| 353 | public static function is_free_plan(): bool { |
| 354 | if ( null === self::$is_free_plan_cache ) { |
| 355 | self::$is_free_plan_cache = ( new Plan() )->is_free_plan(); |
| 356 | } |
| 357 | return self::$is_free_plan_cache; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Reset the `is_free_plan()` memo. Tests only. |
| 362 | */ |
| 363 | public static function reset_is_free_plan_cache() { |
| 364 | self::$is_free_plan_cache = null; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Whether the site has a paid Jetpack Search subscription. Paid-only block |
| 369 | * surfaces (AI Answer) call this on every render. |
| 370 | * |
| 371 | * Both probes are needed: `supports_instant_search()` is true on the free |
| 372 | * Search plan too ("plan supports the feature"), so it alone would let free |
| 373 | * through. `! is_free_plan()` excludes free + forced-free; `supports_instant_search()` |
| 374 | * excludes the no-plan case (which `is_free_plan()` returns false for). |
| 375 | * |
| 376 | * No `apply_filters()` wrapper by design — a filter that any plugin could |
| 377 | * flip would defeat a paid-feature gate. Tests bypass via |
| 378 | * `set_supports_paid_search_for_testing()`. |
| 379 | * |
| 380 | * @return bool |
| 381 | */ |
| 382 | public static function supports_paid_search(): bool { |
| 383 | if ( null === self::$supports_paid_search_cache ) { |
| 384 | $plan = new Plan(); |
| 385 | self::$supports_paid_search_cache = $plan->supports_instant_search() && ! $plan->is_free_plan(); |
| 386 | } |
| 387 | return self::$supports_paid_search_cache; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Force the `supports_paid_search()` answer — tests only. Pass `null` to clear. |
| 392 | * |
| 393 | * @internal |
| 394 | * @param bool|null $value Forced answer or null to clear. |
| 395 | */ |
| 396 | public static function set_supports_paid_search_for_testing( ?bool $value ): void { |
| 397 | self::$supports_paid_search_cache = $value; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Reset the `supports_paid_search()` memo. Tests only. |
| 402 | * |
| 403 | * @internal |
| 404 | */ |
| 405 | public static function reset_supports_paid_search_cache(): void { |
| 406 | self::$supports_paid_search_cache = null; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Whether Jetpack Search exposes its WooCommerce-only blocks, filter |
| 411 | * variations, and render paths. See AGENTS.md § WooCommerce gating. |
| 412 | * |
| 413 | * **Load-order contract:** call at or after `plugins_loaded`. WC includes |
| 414 | * its main class during `plugins_loaded`, so an earlier call returns false |
| 415 | * on a WC site. Existing callers all fire later (`enqueue_block_editor_assets`, |
| 416 | * `template_redirect`, `wp_enqueue_scripts`, block render). |
| 417 | * |
| 418 | * @return bool |
| 419 | */ |
| 420 | public static function woocommerce_blocks_enabled(): bool { |
| 421 | if ( null === self::$woocommerce_blocks_enabled_cache ) { |
| 422 | // `false` second arg: skip the autoloader on non-Woo sites — this |
| 423 | // gate is hit on every request and autoloader work would be wasted. |
| 424 | $probed = class_exists( 'WooCommerce', false ) && self::woocommerce_version_supported(); |
| 425 | |
| 426 | /** |
| 427 | * Whether Jetpack Search exposes its WooCommerce-only blocks, |
| 428 | * filter variations, and render paths. Default is the |
| 429 | * `class_exists( 'WooCommerce', false )` probe AND a minimum |
| 430 | * WooCommerce version check. |
| 431 | * |
| 432 | * @since 0.59.0 |
| 433 | * |
| 434 | * @param bool $enabled Defaults to the WooCommerce class + version probe. |
| 435 | */ |
| 436 | self::$woocommerce_blocks_enabled_cache = (bool) apply_filters( |
| 437 | 'jetpack_search_woocommerce_blocks_enabled', |
| 438 | $probed |
| 439 | ); |
| 440 | } |
| 441 | return self::$woocommerce_blocks_enabled_cache; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Whether the active WooCommerce is at `MIN_WOOCOMMERCE_VERSION` or newer. |
| 446 | * Older or absent WooCommerce reads as unsupported. |
| 447 | * |
| 448 | * @since 7.1.0 |
| 449 | * |
| 450 | * @param string|null $version WooCommerce version to test; defaults to the |
| 451 | * live `WC_VERSION` constant. Override is for tests pinning a version. |
| 452 | * @return bool |
| 453 | */ |
| 454 | public static function woocommerce_version_supported( ?string $version = null ): bool { |
| 455 | // `constant()` keeps static analysis happy — WC isn't a dependency here. |
| 456 | $version = $version ?? ( defined( 'WC_VERSION' ) ? (string) constant( 'WC_VERSION' ) : '' ); |
| 457 | return '' !== $version && version_compare( $version, self::MIN_WOOCOMMERCE_VERSION, '>=' ); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Force the `woocommerce_blocks_enabled()` answer to a specific boolean — |
| 462 | * tests only. Pass `null` to clear the override and revive the real |
| 463 | * `class_exists()` probe (also done by `reset_woocommerce_blocks_enabled_cache()`). |
| 464 | * |
| 465 | * @internal |
| 466 | * |
| 467 | * @param bool|null $value Forced answer or null to clear. |
| 468 | */ |
| 469 | public static function set_woocommerce_blocks_enabled_for_testing( ?bool $value ): void { |
| 470 | self::$woocommerce_blocks_enabled_cache = $value; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * Reset the `woocommerce_blocks_enabled()` memo. Tests only. |
| 475 | * |
| 476 | * @internal |
| 477 | */ |
| 478 | public static function reset_woocommerce_blocks_enabled_cache(): void { |
| 479 | self::$woocommerce_blocks_enabled_cache = null; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * The `jetpack_search_override_woocommerce_search_template` opt-in |
| 484 | * (default off), set from the Search dashboard. |
| 485 | * |
| 486 | * @return bool |
| 487 | */ |
| 488 | public static function woocommerce_search_template_override_enabled(): bool { |
| 489 | return (bool) get_option( 'jetpack_search_override_woocommerce_search_template', false ); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Whether the current request is a WooCommerce product search — a search |
| 494 | * query scoped to the `product` post-type archive on a Woo-enabled site. |
| 495 | * |
| 496 | * Theme-agnostic. Block-theme-only behavior (FSE hierarchy work) gates on |
| 497 | * {@see block_templates_active()} at the call site so this predicate also |
| 498 | * drives the classic-theme product shim. Public to keep the WC-gating |
| 499 | * surface (see AGENTS.md § WooCommerce gating) discoverable from outside |
| 500 | * the class; the in-class callers are `route_classic_theme_search_template()` |
| 501 | * and `route_woocommerce_product_search_template()`. |
| 502 | * |
| 503 | * @return bool |
| 504 | */ |
| 505 | public static function is_woocommerce_product_search(): bool { |
| 506 | return self::woocommerce_blocks_enabled() |
| 507 | && is_search() |
| 508 | && is_post_type_archive( 'product' ); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Canonical list of WooCommerce-only block names. Single source of truth |
| 513 | * for the WC gate across registration, helpers, and editor bundle — see |
| 514 | * AGENTS.md § WooCommerce gating. Add an entry and every gate picks it up. |
| 515 | * |
| 516 | * @return string[] |
| 517 | */ |
| 518 | public static function woocommerce_only_block_names(): array { |
| 519 | return array( |
| 520 | 'jetpack-search/filter-wc-attribute', |
| 521 | 'jetpack-search/filter-wc-price', |
| 522 | 'jetpack-search/filter-wc-rating', |
| 523 | 'jetpack-search/filter-wc-stock-status', |
| 524 | 'jetpack-search/filters-product', |
| 525 | ); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Whether a block name belongs to a WooCommerce-only block. Accepts either |
| 530 | * the full namespaced name or a bare directory basename (the registration |
| 531 | * loop walks basenames; helpers and editor hold full names). |
| 532 | * |
| 533 | * @param string $block_name Full block name (`jetpack-search/filter-wc-rating`) |
| 534 | * or bare directory basename (`filter-wc-rating`). |
| 535 | * @return bool |
| 536 | */ |
| 537 | public static function is_woocommerce_only_block( string $block_name ): bool { |
| 538 | $candidate = false === strpos( $block_name, '/' ) |
| 539 | ? 'jetpack-search/' . $block_name |
| 540 | : $block_name; |
| 541 | return in_array( $candidate, self::woocommerce_only_block_names(), true ); |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Built-in taxonomies that have dedicated filter-checkbox variations. |
| 546 | * Excluded from the "Custom Taxonomy" picker so authors reach for the |
| 547 | * dedicated variation. Mirrors `BUILT_IN_TAXONOMY_SLUGS` in |
| 548 | * `filter-checkbox/edit.js` — must stay in lockstep. |
| 549 | * |
| 550 | * @var string[] |
| 551 | */ |
| 552 | const BUILT_IN_CUSTOM_TAXONOMY_EXCLUSIONS = array( |
| 553 | 'category', |
| 554 | 'post_tag', |
| 555 | 'product_cat', |
| 556 | 'product_tag', |
| 557 | 'product_brand', |
| 558 | ); |
| 559 | |
| 560 | /** |
| 561 | * Back-compat proxy for `Custom_Taxonomy_Slot_Mapping::get_map()`. |
| 562 | * |
| 563 | * @return array<string, string> |
| 564 | */ |
| 565 | public static function custom_taxonomy_map(): array { |
| 566 | return Custom_Taxonomy_Slot_Mapping::get_map(); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Back-compat proxy for `Custom_Taxonomy_Slot_Mapping::resolve_slot()`. |
| 571 | * |
| 572 | * @param string $taxonomy User-facing taxonomy slug. |
| 573 | * @return string Effective ES field slug. |
| 574 | */ |
| 575 | public static function resolve_taxonomy_slot( string $taxonomy ): string { |
| 576 | return Custom_Taxonomy_Slot_Mapping::resolve_slot( $taxonomy ); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * Reset both the slot-mapping and `supported_custom_taxonomies()` memos. Tests only. |
| 581 | * |
| 582 | * @internal |
| 583 | */ |
| 584 | public static function reset_custom_taxonomy_map_cache(): void { |
| 585 | Custom_Taxonomy_Slot_Mapping::reset_cache_for_testing(); |
| 586 | self::$supported_custom_taxonomies_cache = null; |
| 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Custom-taxonomy slugs the "Custom Taxonomy" filter variation offers. |
| 591 | * |
| 592 | * Supported when registered locally AND either (a) in the Jetpack Search |
| 593 | * indexable allowlist (`Sync\Modules\Search::get_all_taxonomies()`, so |
| 594 | * aggregations actually return buckets) or (b) a key in `custom_taxonomy_map()` |
| 595 | * (queries route through a reserved slot). Built-ins with dedicated variations |
| 596 | * are stripped. The Sync `class_exists` guard is defensive — partial installs |
| 597 | * fall back to "map keys only". |
| 598 | * |
| 599 | * @return string[] Distinct, zero-indexed list of supported taxonomy slugs. |
| 600 | */ |
| 601 | public static function supported_custom_taxonomies(): array { |
| 602 | if ( null !== self::$supported_custom_taxonomies_cache ) { |
| 603 | return self::$supported_custom_taxonomies_cache; |
| 604 | } |
| 605 | |
| 606 | // Public taxonomies only — the editor's `core.getTaxonomies()` only returns |
| 607 | // REST-visible ones, and a private taxonomy in the Sync allowlist shouldn't surface. |
| 608 | $registered = function_exists( 'get_taxonomies' ) |
| 609 | ? array_values( get_taxonomies( array( 'public' => true ), 'names' ) ) |
| 610 | : array(); |
| 611 | |
| 612 | $indexed = class_exists( '\\Automattic\\Jetpack\\Sync\\Modules\\Search' ) |
| 613 | ? \Automattic\Jetpack\Sync\Modules\Search::get_all_taxonomies() |
| 614 | : array(); |
| 615 | |
| 616 | $map_keys = array_keys( self::custom_taxonomy_map() ); |
| 617 | |
| 618 | $candidates = array_unique( array_merge( $indexed, $map_keys ) ); |
| 619 | $supported = array_values( |
| 620 | array_diff( |
| 621 | array_values( array_intersect( $registered, $candidates ) ), |
| 622 | self::BUILT_IN_CUSTOM_TAXONOMY_EXCLUSIONS |
| 623 | ) |
| 624 | ); |
| 625 | |
| 626 | self::$supported_custom_taxonomies_cache = $supported; |
| 627 | return $supported; |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * URL param key the inline search experience uses for the current request. |
| 632 | * On the WP search route `s`; elsewhere `q` (see `NON_SEARCH_QUERY_PARAM`). |
| 633 | * |
| 634 | * @return string |
| 635 | */ |
| 636 | public static function get_search_param_name(): string { |
| 637 | return function_exists( 'is_search' ) && is_search() ? 's' : self::NON_SEARCH_QUERY_PARAM; |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Enqueue the client-side block registration bundle in the block editor. |
| 642 | * |
| 643 | * WP bootstraps server-side block metadata into the editor, but each block |
| 644 | * still needs a client-side `registerBlockType()` call so the editor knows |
| 645 | * how to render a preview. This script does that with ServerSideRender. |
| 646 | */ |
| 647 | public static function enqueue_editor_assets() { |
| 648 | $base_path = Package::get_installed_path() . 'build/search-blocks-editor/'; |
| 649 | $asset_file = $base_path . 'register-blocks.asset.php'; |
| 650 | if ( ! file_exists( $asset_file ) ) { |
| 651 | return; |
| 652 | } |
| 653 | $asset = require $asset_file; |
| 654 | |
| 655 | // `plugins_url()` resolves against the nearest plugin directory, which |
| 656 | // handles the `jetpack_vendor` location Composer installs into. |
| 657 | $url = plugins_url( 'register-blocks.js', $base_path . 'register-blocks.js' ); |
| 658 | |
| 659 | wp_enqueue_script( |
| 660 | 'jetpack-search-blocks-register', |
| 661 | $url, |
| 662 | $asset['dependencies'] ?? array(), |
| 663 | $asset['version'] ?? false, |
| 664 | true |
| 665 | ); |
| 666 | |
| 667 | // Surface PHP gates to the editor bundle so block edits and the |
| 668 | // registration loop branch consistently with server-side renders. |
| 669 | // `wp_add_inline_script` (not `wp_localize_script`) per core #25280 — |
| 670 | // the latter HTML-encodes ampersands inside nested values. |
| 671 | wp_add_inline_script( |
| 672 | 'jetpack-search-blocks-register', |
| 673 | 'window.JetpackSearchBlocksConfig = ' . wp_json_encode( |
| 674 | array( |
| 675 | 'isWooCommerceBlocksEnabled' => self::woocommerce_blocks_enabled(), |
| 676 | 'woocommerceOnlyBlocks' => self::woocommerce_only_block_names(), |
| 677 | 'supportsPaidSearch' => self::supports_paid_search(), |
| 678 | 'supportedCustomTaxonomies' => self::supported_custom_taxonomies(), |
| 679 | 'customTaxonomyMap' => (object) self::custom_taxonomy_map(), |
| 680 | ), |
| 681 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 682 | ) . ';', |
| 683 | 'before' |
| 684 | ); |
| 685 | } |
| 686 | |
| 687 | /** |
| 688 | * Add a "Jetpack Search" block category so our blocks appear under that |
| 689 | * heading in the inserter instead of "Uncategorized". |
| 690 | * |
| 691 | * @param array $categories Existing block categories. |
| 692 | * @return array |
| 693 | */ |
| 694 | public static function register_block_category( $categories ) { |
| 695 | foreach ( $categories as $category ) { |
| 696 | if ( 'jetpack-search' === ( $category['slug'] ?? '' ) ) { |
| 697 | return $categories; |
| 698 | } |
| 699 | } |
| 700 | $categories[] = array( |
| 701 | 'slug' => 'jetpack-search', |
| 702 | 'title' => __( 'Jetpack Search', 'jetpack-search-pkg' ), |
| 703 | ); |
| 704 | return $categories; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * Register all search blocks from their block.json files. |
| 709 | */ |
| 710 | public static function register_blocks() { |
| 711 | // Register block pattern category first so patterns can reference it. |
| 712 | if ( function_exists( 'register_block_pattern_category' ) ) { |
| 713 | register_block_pattern_category( |
| 714 | 'jetpack-search', |
| 715 | array( 'label' => __( 'Jetpack Search', 'jetpack-search-pkg' ) ) |
| 716 | ); |
| 717 | } |
| 718 | |
| 719 | self::register_store_script_module(); |
| 720 | |
| 721 | $blocks_dir = __DIR__ . '/blocks'; |
| 722 | $block_dirs = glob( $blocks_dir . '/*', GLOB_ONLYDIR ); |
| 723 | |
| 724 | if ( ! $block_dirs ) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | $wc_blocks_enabled = self::woocommerce_blocks_enabled(); |
| 729 | foreach ( $block_dirs as $block_dir ) { |
| 730 | if ( ! file_exists( $block_dir . '/block.json' ) ) { |
| 731 | continue; |
| 732 | } |
| 733 | if ( ! $wc_blocks_enabled && self::is_woocommerce_only_block( basename( $block_dir ) ) ) { |
| 734 | continue; |
| 735 | } |
| 736 | register_block_type( $block_dir ); |
| 737 | } |
| 738 | |
| 739 | add_filter( 'get_block_type_variations', array( static::class, 'inject_filter_checkbox_variations' ), 10, 2 ); |
| 740 | static::register_patterns(); |
| 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Register the shared store as the `jetpack-search/store` Script Module. |
| 745 | * See AGENTS.md § Shared store / bundles for why this is externalized. |
| 746 | */ |
| 747 | public static function register_store_script_module() { |
| 748 | if ( ! function_exists( 'wp_register_script_module' ) ) { |
| 749 | return; |
| 750 | } |
| 751 | |
| 752 | $base_path = Package::get_installed_path() . 'build/search-blocks/store/'; |
| 753 | $asset_file = $base_path . 'index.asset.php'; |
| 754 | if ( ! file_exists( $asset_file ) ) { |
| 755 | return; |
| 756 | } |
| 757 | $asset = require $asset_file; |
| 758 | |
| 759 | wp_register_script_module( |
| 760 | 'jetpack-search/store', |
| 761 | plugins_url( 'index.js', $base_path . 'index.js' ), |
| 762 | $asset['dependencies'] ?? array(), |
| 763 | $asset['version'] ?? false |
| 764 | ); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Relativize Jetpack Search Script Module URLs so the browser fetches them |
| 769 | * same-origin with the page. |
| 770 | * |
| 771 | * `wp_register_script_module()` resolves src via `plugins_url()`, which |
| 772 | * returns the canonical `site_url()` host. When a visitor is on a |
| 773 | * different host (Multisite mapped domains, www vs non-www without a |
| 774 | * canonical redirect, asset-offload plugins, reverse-proxy staging) the |
| 775 | * `<script type="module">` becomes cross-origin and is blocked with |
| 776 | * `MissingAllowOriginHeader` — ES modules always go through the CORS |
| 777 | * algorithm, even without a `crossorigin` attribute, and typical WP |
| 778 | * hosts don't send `Access-Control-Allow-Origin` for `wp-content/*`. |
| 779 | * |
| 780 | * Stripping scheme + host with `wp_make_link_relative()` lets the browser |
| 781 | * resolve against the page's actual origin. No `$_SERVER['HTTP_HOST']` |
| 782 | * trust — emitting an attacker-controllable host into a `<script src>` |
| 783 | * would be a cache-poisoning vector. |
| 784 | * |
| 785 | * No-op when the src host is a deliberately external host (CDN that |
| 786 | * doesn't match `home_url()`/`site_url()`); operators of those setups |
| 787 | * configure CORS on the CDN themselves. |
| 788 | * |
| 789 | * Identifier gate covers both shapes Jetpack Search ships: directly- |
| 790 | * registered modules with a slash (`jetpack-search/store`, |
| 791 | * `jetpack-search/overlay-bootstrap`) and the per-block view modules |
| 792 | * WP auto-registers from `block.json`'s `viewScriptModule`, which run |
| 793 | * `generate_block_asset_handle()` and emit hyphen-joined IDs like |
| 794 | * `jetpack-search-results-list-view-script-module`. |
| 795 | * |
| 796 | * @param string $src Module src URL. |
| 797 | * @param string $identifier Module identifier (e.g. `jetpack-search/results-list` |
| 798 | * or `jetpack-search-results-list-view-script-module`). |
| 799 | * @return string Relativized src on match, original otherwise. |
| 800 | */ |
| 801 | public static function same_origin_script_module_src( $src, $identifier ) { |
| 802 | if ( ! is_string( $src ) || '' === $src || ! is_string( $identifier ) ) { |
| 803 | return $src; |
| 804 | } |
| 805 | if ( 0 !== strpos( $identifier, 'jetpack-search/' ) && 0 !== strpos( $identifier, 'jetpack-search-' ) ) { |
| 806 | return $src; |
| 807 | } |
| 808 | |
| 809 | $src_host = wp_parse_url( $src, PHP_URL_HOST ); |
| 810 | if ( ! $src_host ) { |
| 811 | return $src; |
| 812 | } |
| 813 | |
| 814 | $canonical_hosts = array_map( |
| 815 | 'strtolower', |
| 816 | array_filter( |
| 817 | array( |
| 818 | wp_parse_url( home_url(), PHP_URL_HOST ), |
| 819 | wp_parse_url( site_url(), PHP_URL_HOST ), |
| 820 | ) |
| 821 | ) |
| 822 | ); |
| 823 | |
| 824 | if ( ! in_array( strtolower( $src_host ), $canonical_hosts, true ) ) { |
| 825 | return $src; |
| 826 | } |
| 827 | |
| 828 | return wp_make_link_relative( $src ); |
| 829 | } |
| 830 | |
| 831 | /** |
| 832 | * Inject named block variations for the filter-checkbox block. |
| 833 | * |
| 834 | * Uses the `get_block_type_variations` filter (WP 6.5+) rather than |
| 835 | * `register_block_variation()` — the latter is JS-only and has no PHP |
| 836 | * equivalent. Variation names + default attributes mirror the |
| 837 | * instant-search overlay so both surfaces describe the same filters. |
| 838 | * |
| 839 | * @param array $variations Variations registered on the block type. |
| 840 | * @param \WP_Block_Type $block_type Block type the filter is being applied to. |
| 841 | * @return array |
| 842 | */ |
| 843 | public static function inject_filter_checkbox_variations( $variations, $block_type ) { |
| 844 | if ( ! isset( $block_type->name ) || 'jetpack-search/filter-checkbox' !== $block_type->name ) { |
| 845 | return $variations; |
| 846 | } |
| 847 | |
| 848 | $additions = array( |
| 849 | array( |
| 850 | 'name' => 'category', |
| 851 | 'title' => __( 'Filter by Category', 'jetpack-search-pkg' ), |
| 852 | 'description' => __( 'Show category checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 853 | 'attributes' => array( |
| 854 | 'filterType' => 'taxonomy', |
| 855 | 'taxonomy' => 'category', |
| 856 | 'label' => __( 'Category', 'jetpack-search-pkg' ), |
| 857 | ), |
| 858 | 'isActive' => array( 'filterType', 'taxonomy' ), |
| 859 | ), |
| 860 | array( |
| 861 | 'name' => 'post_tag', |
| 862 | 'title' => __( 'Filter by Tag', 'jetpack-search-pkg' ), |
| 863 | 'description' => __( 'Show tag checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 864 | 'attributes' => array( |
| 865 | 'filterType' => 'taxonomy', |
| 866 | 'taxonomy' => 'post_tag', |
| 867 | 'label' => __( 'Tag', 'jetpack-search-pkg' ), |
| 868 | ), |
| 869 | 'isActive' => array( 'filterType', 'taxonomy' ), |
| 870 | ), |
| 871 | array( |
| 872 | 'name' => 'post_type', |
| 873 | 'title' => __( 'Filter by Post Type', 'jetpack-search-pkg' ), |
| 874 | 'description' => __( 'Show post type checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 875 | 'attributes' => array( |
| 876 | 'filterType' => 'post_type', |
| 877 | 'label' => __( 'Post Type', 'jetpack-search-pkg' ), |
| 878 | ), |
| 879 | 'isActive' => array( 'filterType' ), |
| 880 | ), |
| 881 | array( |
| 882 | 'name' => 'author', |
| 883 | 'title' => __( 'Filter by Author', 'jetpack-search-pkg' ), |
| 884 | 'description' => __( 'Show author checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 885 | 'attributes' => array( |
| 886 | 'filterType' => 'author', |
| 887 | 'label' => __( 'Author', 'jetpack-search-pkg' ), |
| 888 | ), |
| 889 | 'isActive' => array( 'filterType' ), |
| 890 | ), |
| 891 | ); |
| 892 | |
| 893 | // WC-only product-taxonomy variations. `product_brand` gets an extra |
| 894 | // `taxonomy_exists()` probe — it isn't core WC, it ships via extensions |
| 895 | // (WC Brands, Perfect Brands) or recent bundled WC versions. |
| 896 | if ( self::woocommerce_blocks_enabled() ) { |
| 897 | $additions[] = array( |
| 898 | 'name' => 'product_cat', |
| 899 | 'title' => __( 'Filter by Product Category', 'jetpack-search-pkg' ), |
| 900 | 'description' => __( 'Show product category checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 901 | 'attributes' => array( |
| 902 | 'filterType' => 'taxonomy', |
| 903 | 'taxonomy' => 'product_cat', |
| 904 | 'label' => __( 'Product Category', 'jetpack-search-pkg' ), |
| 905 | ), |
| 906 | 'isActive' => array( 'filterType', 'taxonomy' ), |
| 907 | ); |
| 908 | $additions[] = array( |
| 909 | 'name' => 'product_tag', |
| 910 | 'title' => __( 'Filter by Product Tag', 'jetpack-search-pkg' ), |
| 911 | 'description' => __( 'Show product tag checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 912 | 'attributes' => array( |
| 913 | 'filterType' => 'taxonomy', |
| 914 | 'taxonomy' => 'product_tag', |
| 915 | 'label' => __( 'Product Tag', 'jetpack-search-pkg' ), |
| 916 | ), |
| 917 | 'isActive' => array( 'filterType', 'taxonomy' ), |
| 918 | ); |
| 919 | if ( taxonomy_exists( 'product_brand' ) ) { |
| 920 | $additions[] = array( |
| 921 | 'name' => 'product_brand', |
| 922 | 'title' => __( 'Filter by Product Brand', 'jetpack-search-pkg' ), |
| 923 | 'description' => __( 'Show product brand checkboxes with live result counts.', 'jetpack-search-pkg' ), |
| 924 | 'attributes' => array( |
| 925 | 'filterType' => 'taxonomy', |
| 926 | 'taxonomy' => 'product_brand', |
| 927 | 'label' => __( 'Product Brand', 'jetpack-search-pkg' ), |
| 928 | ), |
| 929 | 'isActive' => array( 'filterType', 'taxonomy' ), |
| 930 | ); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | $additions[] = array( |
| 935 | 'name' => 'custom_taxonomy', |
| 936 | 'title' => __( 'Filter by Custom Taxonomy', 'jetpack-search-pkg' ), |
| 937 | 'description' => __( 'Show checkboxes for a custom taxonomy. Pick which taxonomy in the block settings after inserting.', 'jetpack-search-pkg' ), |
| 938 | 'attributes' => array( |
| 939 | 'filterType' => 'taxonomy', |
| 940 | 'taxonomy' => '', |
| 941 | 'label' => '', |
| 942 | ), |
| 943 | // Match on filterType only so identity survives the author picking a |
| 944 | // slug. The dedicated variations pin `taxonomy` in their isActive |
| 945 | // arrays, so WP's most-specific-match resolution still routes named |
| 946 | // slugs to those; Custom Taxonomy claims every other taxonomy. |
| 947 | 'isActive' => array( 'filterType' ), |
| 948 | ); |
| 949 | |
| 950 | // Merge by `name` so an upstream variation (block.json or earlier filter) |
| 951 | // wins over our preset of the same name; plain `array_merge` would |
| 952 | // append duplicates and the inserter would render two cards. |
| 953 | $variations = (array) $variations; |
| 954 | $existing_keys = array_flip( array_column( $variations, 'name' ) ); |
| 955 | foreach ( $additions as $variation ) { |
| 956 | if ( ! isset( $existing_keys[ $variation['name'] ] ) ) { |
| 957 | $variations[] = $variation; |
| 958 | } |
| 959 | } |
| 960 | return $variations; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Register block patterns. Files prefixed `wc-` compose WooCommerce-only |
| 965 | * blocks and load only when WC is active (mirrors `filter-wc-*` blocks). |
| 966 | */ |
| 967 | protected static function register_patterns() { |
| 968 | $patterns_dir = __DIR__ . '/patterns'; |
| 969 | if ( ! is_dir( $patterns_dir ) ) { |
| 970 | return; |
| 971 | } |
| 972 | $pattern_files = glob( $patterns_dir . '/*.php' ); |
| 973 | if ( ! $pattern_files ) { |
| 974 | return; |
| 975 | } |
| 976 | $wc_blocks_enabled = self::woocommerce_blocks_enabled(); |
| 977 | foreach ( $pattern_files as $pattern_file ) { |
| 978 | if ( ! $wc_blocks_enabled && 0 === strpos( basename( $pattern_file ), 'wc-' ) ) { |
| 979 | continue; |
| 980 | } |
| 981 | require_once $pattern_file; |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Derive a block-pattern's content from a chrome-free layout template (the |
| 987 | * overlay templates, which already ship without header/footer/main page |
| 988 | * chrome), so patterns stay in sync with the template they mirror instead of |
| 989 | * carrying a hand-copied second copy of the layout. |
| 990 | * |
| 991 | * @param string $template_file Template basename under `templates/`. |
| 992 | * @return string Block markup ready for `register_block_pattern()`, or '' when unreadable. |
| 993 | */ |
| 994 | public static function pattern_content_from_template( string $template_file ): string { |
| 995 | $template_path = __DIR__ . '/templates/' . basename( $template_file ); |
| 996 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local, bundled template file. |
| 997 | $raw = is_readable( $template_path ) ? (string) file_get_contents( $template_path ) : ''; |
| 998 | if ( '' === $raw ) { |
| 999 | return ''; |
| 1000 | } |
| 1001 | return trim( static::substitute_template_placeholders( $raw ) ); |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Build the full search page template content. |
| 1006 | * |
| 1007 | * Markup lives in `templates/jetpack-search.html` with a `{{FILTER_HEADING}}` |
| 1008 | * placeholder so the sidebar heading still goes through `esc_html__()`. |
| 1009 | * |
| 1010 | * @return string Block markup for a complete page template. |
| 1011 | */ |
| 1012 | protected static function get_search_template_content(): string { |
| 1013 | $template_path = __DIR__ . '/templates/jetpack-search.html'; |
| 1014 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local, bundled template file. |
| 1015 | $raw = is_readable( $template_path ) ? (string) file_get_contents( $template_path ) : ''; |
| 1016 | return static::substitute_template_placeholders( $raw ); |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Register the Jetpack Search page template so it surfaces in the Site |
| 1021 | * Editor and resolves via the template hierarchy. DB-stored customizations |
| 1022 | * still win automatically — the `custom` source beats `plugin`. Classic |
| 1023 | * themes are skipped: the registry is only consulted by block themes. |
| 1024 | */ |
| 1025 | public static function register_search_template() { |
| 1026 | if ( ! function_exists( 'register_block_template' ) || ! static::block_templates_active() ) { |
| 1027 | return; |
| 1028 | } |
| 1029 | $content = static::get_search_template_content(); |
| 1030 | // Bail on missing/unreadable file: our slug is prepended to the search |
| 1031 | // hierarchy, so registering empty content would render a blank page on |
| 1032 | // `/?s=…`. Falling through lets core resolve the theme's `search.html`. |
| 1033 | if ( '' === $content ) { |
| 1034 | return; |
| 1035 | } |
| 1036 | static::replace_block_template( |
| 1037 | static::get_parent_plugin_slug() . '//' . self::SEARCH_TEMPLATE_SLUG, |
| 1038 | array( |
| 1039 | 'title' => __( 'Jetpack Search Results', 'jetpack-search-pkg' ), |
| 1040 | 'description' => __( 'Displays search results with Jetpack Search filters.', 'jetpack-search-pkg' ), |
| 1041 | 'content' => $content, |
| 1042 | ) |
| 1043 | ); |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * Whether the overlay should paint the WooCommerce product variant for the |
| 1048 | * current request. True only when the override option is on and the request |
| 1049 | * is a product search — mirrors the embedded/inline interception |
| 1050 | * (`is_woocommerce_product_search()` already folds in the WC probe). The |
| 1051 | * overlay opens client-side from any search box, so this only flips on the |
| 1052 | * server-rendered product-search request (deep link or product-archive |
| 1053 | * search), not on a live form intercept from a non-product page. |
| 1054 | * |
| 1055 | * @return bool |
| 1056 | */ |
| 1057 | protected static function should_use_product_overlay(): bool { |
| 1058 | return static::woocommerce_search_template_override_enabled() |
| 1059 | && static::is_woocommerce_product_search(); |
| 1060 | } |
| 1061 | |
| 1062 | /** |
| 1063 | * Read the dedicated overlay-template markup. |
| 1064 | * |
| 1065 | * Distinct from `get_search_template_content()`: a modal isn't a page, so |
| 1066 | * the overlay markup ships without `header`/`main`/`footer` template-parts |
| 1067 | * rather than runtime-stripping them. |
| 1068 | * |
| 1069 | * Picks the product variant on a WooCommerce product search (see |
| 1070 | * `should_use_product_overlay()`). Source of truth, in order: |
| 1071 | * 1. Customized singleton CPT (`Overlay_Template` / `Product_Overlay_Template`). |
| 1072 | * 2. The bundled `jetpack-search-overlay{-product}.html`. |
| 1073 | * |
| 1074 | * @return string Block markup for the overlay body. |
| 1075 | */ |
| 1076 | protected static function get_overlay_template_content(): string { |
| 1077 | $is_product = static::should_use_product_overlay(); |
| 1078 | $key = $is_product ? 'product' : 'default'; |
| 1079 | if ( isset( self::$overlay_template_content_cache[ $key ] ) ) { |
| 1080 | return self::$overlay_template_content_cache[ $key ]; |
| 1081 | } |
| 1082 | $cpt_class = $is_product ? Product_Overlay_Template::class : Overlay_Template::class; |
| 1083 | $customized = $cpt_class::get_customized_content(); |
| 1084 | if ( null !== $customized ) { |
| 1085 | self::$overlay_template_content_cache[ $key ] = $customized; |
| 1086 | return self::$overlay_template_content_cache[ $key ]; |
| 1087 | } |
| 1088 | $file = $is_product ? 'jetpack-search-overlay-product.html' : 'jetpack-search-overlay.html'; |
| 1089 | $template_path = __DIR__ . '/templates/' . $file; |
| 1090 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local, bundled template file; wp_remote_get() is for remote URLs. |
| 1091 | self::$overlay_template_content_cache[ $key ] = is_readable( $template_path ) ? (string) file_get_contents( $template_path ) : ''; |
| 1092 | return self::$overlay_template_content_cache[ $key ]; |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Reset the `get_overlay_template_content()` memo. Tests only — PHPUnit |
| 1097 | * reuses a single process, so a CPT-customized overlay saved in one test |
| 1098 | * would otherwise be pinned (or a bundled read would mask it) in the next. |
| 1099 | * Guarded against accidental production use. |
| 1100 | */ |
| 1101 | public static function reset_overlay_template_content_cache(): void { |
| 1102 | if ( defined( 'ABSPATH' ) && ! defined( 'PHPUNIT_COMPOSER_INSTALL' ) ) { |
| 1103 | return; |
| 1104 | } |
| 1105 | self::$overlay_template_content_cache = array(); |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * Echo the Search-blocks overlay shell into `wp_footer`. Block markup |
| 1110 | * carries `data-wp-interactive` so the IA's standard `DOMContentLoaded` |
| 1111 | * hydration picks it up — no client-side fetch needed. The caller (`init()`) |
| 1112 | * gates registration on `is_block_template_overlay_enabled()`. |
| 1113 | */ |
| 1114 | public static function print_block_template_overlay() { |
| 1115 | $rendered = self::$block_template_overlay_rendered_html; |
| 1116 | if ( null === $rendered || '' === $rendered ) { |
| 1117 | return; |
| 1118 | } |
| 1119 | $config = wp_json_encode( |
| 1120 | array( |
| 1121 | 'searchInputSelector' => 'input[name="s"]:not(.jetpack-search-input__field), #searchform input.search-field, .search-form input.search-field, .searchform input.search-field', |
| 1122 | 'overlayTriggerSelector' => '.jetpack-search-block-overlay-trigger, .jetpack-instant-search__open-overlay-button, header#site-header .search-toggle[data-toggle-target]', |
| 1123 | ), |
| 1124 | JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP |
| 1125 | ); |
| 1126 | ?> |
| 1127 | <script id="jetpack-search-block-overlay-config">window.JetpackSearchBlockOverlay=<?php echo $config; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_json_encode + JSON_HEX_* flags. ?>;</script> |
| 1128 | <?php |
| 1129 | // `<template>` keeps the region out of `document.querySelectorAll` so |
| 1130 | // the IA runtime's DOMContentLoaded walk skips it. The bootstrap clones |
| 1131 | // into the shell on first open and hydrates there. |
| 1132 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- do_blocks output. |
| 1133 | printf( '<template id="jetpack-search-block-overlay-template">%s</template>', $rendered ); |
| 1134 | ?> |
| 1135 | <div |
| 1136 | id="jetpack-search-block-overlay" |
| 1137 | class="jetpack-search-block-overlay" |
| 1138 | role="dialog" |
| 1139 | aria-modal="true" |
| 1140 | aria-label="<?php echo esc_attr__( 'Search', 'jetpack-search-pkg' ); ?>" |
| 1141 | hidden |
| 1142 | > |
| 1143 | <div class="jetpack-search-block-overlay__card"> |
| 1144 | <button |
| 1145 | type="button" |
| 1146 | class="jetpack-search-block-overlay__close" |
| 1147 | aria-label="<?php echo esc_attr__( 'Close search', 'jetpack-search-pkg' ); ?>" |
| 1148 | > |
| 1149 | <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"> |
| 1150 | <path d="M18.3 5.71a1 1 0 0 0-1.41 0L12 10.59 7.11 5.7A1 1 0 0 0 5.7 7.11L10.59 12 5.7 16.89a1 1 0 1 0 1.41 1.41L12 13.41l4.89 4.89a1 1 0 0 0 1.41-1.41L13.41 12l4.89-4.89a1 1 0 0 0 0-1.4z" fill="currentColor" /> |
| 1151 | </svg> |
| 1152 | </button> |
| 1153 | <div class="jetpack-search-block-overlay__content"></div> |
| 1154 | </div> |
| 1155 | </div> |
| 1156 | <?php |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * Register + enqueue the overlay-bootstrap Script Module that wires |
| 1161 | * theme-defined search triggers to the rendered shell. Inline-CSS for the |
| 1162 | * modal chrome. Config emits alongside the overlay HTML in |
| 1163 | * `print_block_template_overlay()`. |
| 1164 | */ |
| 1165 | public static function enqueue_block_template_overlay_assets() { |
| 1166 | if ( ! function_exists( 'wp_register_script_module' ) ) { |
| 1167 | return; |
| 1168 | } |
| 1169 | $base_path = Package::get_installed_path() . 'build/search-blocks/overlay-bootstrap/'; |
| 1170 | $asset_file = $base_path . 'index.asset.php'; |
| 1171 | if ( ! file_exists( $asset_file ) ) { |
| 1172 | return; |
| 1173 | } |
| 1174 | $asset = require $asset_file; |
| 1175 | wp_register_script_module( |
| 1176 | 'jetpack-search/overlay-bootstrap', |
| 1177 | plugins_url( 'index.js', $base_path . 'index.js' ), |
| 1178 | $asset['dependencies'] ?? array(), |
| 1179 | $asset['version'] ?? false |
| 1180 | ); |
| 1181 | wp_enqueue_script_module( 'jetpack-search/overlay-bootstrap' ); |
| 1182 | |
| 1183 | wp_register_style( 'jetpack-search-block-overlay', false, array(), $asset['version'] ?? false ); |
| 1184 | wp_enqueue_style( 'jetpack-search-block-overlay' ); |
| 1185 | wp_add_inline_style( 'jetpack-search-block-overlay', static::block_template_overlay_inline_css() ); |
| 1186 | |
| 1187 | // Shared responsive layout CSS (narrow-width sidebar collapse + |
| 1188 | // in-header popover toggle). Same rules ship to the embedded / |
| 1189 | // WC-product page templates via `enqueue_search_page_assets()`. |
| 1190 | static::enqueue_search_layout_style(); |
| 1191 | |
| 1192 | // Render here (during `wp_enqueue_scripts`) so view-module enqueues |
| 1193 | // from `do_blocks()` land before the importmap prints — see |
| 1194 | // AGENTS.md § Hydration & SSR seeding. |
| 1195 | self::$block_template_overlay_rendered_html = trim( |
| 1196 | do_blocks( static::get_overlay_template_content() ) |
| 1197 | ); |
| 1198 | } |
| 1199 | |
| 1200 | /** |
| 1201 | * Print the body-sampler `<script>` that sets `--jp-search-page-ink` / |
| 1202 | * `--jp-search-page-surface` on `:root` from the body's resolved `color` / |
| 1203 | * `backgroundColor`. Skips writing surface when bg is transparent (the |
| 1204 | * theme paints on the browser canvas) or when bg equals ink (vintage |
| 1205 | * frame-themes like Twenty Sixteen use body as a colored border around a |
| 1206 | * lighter `.site` content wrapper). See AGENTS.md § Theme tokens. |
| 1207 | */ |
| 1208 | public static function print_theme_token_sampler(): void { |
| 1209 | if ( is_admin() ) { |
| 1210 | return; |
| 1211 | } |
| 1212 | echo "<script id='jetpack-search-theme-token-sampler'>(function(){try{var c=getComputedStyle(document.body),r=document.documentElement,ink=c.color,bg=c.backgroundColor;if(ink){r.style.setProperty('--jp-search-page-ink',ink);}if(bg&&bg!==ink&&bg!=='rgba(0, 0, 0, 0)'&&bg!=='transparent'){r.style.setProperty('--jp-search-page-surface',bg);}}catch(e){}})();</script>"; |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Inline CSS for the overlay modal chrome. Block content brings its own |
| 1217 | * theme styling; this is just the scrim, centered card, 60px header strip, |
| 1218 | * close button, mobile padding tweaks, and scroll lock. The responsive |
| 1219 | * sidebar-collapse + in-header popover rules shared with the page |
| 1220 | * templates live in `search_layout_inline_css()`. |
| 1221 | * |
| 1222 | * Surface/ink hoist `--jp-search-page-*` (with the legacy |
| 1223 | * `--wp--preset--color--*` chain as fallback — see AGENTS.md § Theme |
| 1224 | * tokens) onto two custom props so in-card surfaces share one source. |
| 1225 | * Hairlines use `color-mix(--jp-search-overlay-ink, --jp-search-overlay-surface)`. |
| 1226 | * |
| 1227 | * @return string |
| 1228 | */ |
| 1229 | protected static function block_template_overlay_inline_css(): string { |
| 1230 | return <<<'CSS' |
| 1231 | .jetpack-search-block-overlay { |
| 1232 | position: fixed; |
| 1233 | inset: 0; |
| 1234 | z-index: 100000; |
| 1235 | display: flex; |
| 1236 | justify-content: center; |
| 1237 | align-items: flex-start; |
| 1238 | background: rgba(31, 31, 31, 0.7); |
| 1239 | overflow-y: auto; |
| 1240 | padding: 3em 1em; |
| 1241 | transition: opacity 0.1s ease-in; |
| 1242 | } |
| 1243 | .jetpack-search-block-overlay[hidden] { |
| 1244 | display: none; |
| 1245 | } |
| 1246 | @media (prefers-reduced-motion: reduce) { |
| 1247 | .jetpack-search-block-overlay { |
| 1248 | transition: none; |
| 1249 | } |
| 1250 | } |
| 1251 | .jetpack-search-block-overlay__card { |
| 1252 | position: relative; |
| 1253 | width: 100%; |
| 1254 | max-width: 1080px; |
| 1255 | --jp-search-overlay-surface: var(--jp-search-page-surface, var(--wp--preset--color--base, var(--wp--preset--color--background, #fff))); |
| 1256 | --jp-search-overlay-ink: var(--jp-search-page-ink, var(--wp--preset--color--contrast, var(--wp--preset--color--foreground, #1d2327))); |
| 1257 | /* Single source for the content group's inset. The corner-join in |
| 1258 | * search_layout_inline_css() zeroes the block-start/block-end of this |
| 1259 | * padding on the group and re-adds the same tokens on the columns, so the |
| 1260 | * sidebar hairline reaches both card edges — keep them as var()s so the |
| 1261 | * two sites can't drift. */ |
| 1262 | --jp-search-overlay-content-pad-block-start: 0.5em; |
| 1263 | --jp-search-overlay-content-pad-inline: 2em; |
| 1264 | --jp-search-overlay-content-pad-block-end: 2em; |
| 1265 | background: var(--jp-search-overlay-surface); |
| 1266 | color: var(--jp-search-overlay-ink); |
| 1267 | border: 1px solid rgba(128, 128, 128, 0.25); |
| 1268 | border-radius: 4px; |
| 1269 | box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18); |
| 1270 | padding-top: 60px; |
| 1271 | } |
| 1272 | /* Token-aware card / scrim separation (SEARCH-270): tint the resolved surface |
| 1273 | * ~5% toward ink and paint the hairline border with the same ink-over-surface |
| 1274 | * mix used for the header `::before`. Both auto-invert polarity per theme, so |
| 1275 | * dark themes get a card that visibly layers above the scrim without losing |
| 1276 | * the themed surface color. The static `rgba(128,128,128,.25)` border + the |
| 1277 | * un-tinted token chain stay as the fallback for browsers without `color-mix`. */ |
| 1278 | @supports (background: color-mix(in sRGB, black 50%, white)) { |
| 1279 | .jetpack-search-block-overlay__card { |
| 1280 | --jp-search-overlay-surface: color-mix(in sRGB, var(--jp-search-page-ink, var(--wp--preset--color--contrast, var(--wp--preset--color--foreground, #1d2327))) 5%, var(--jp-search-page-surface, var(--wp--preset--color--base, var(--wp--preset--color--background, #fff)))); |
| 1281 | border-color: color-mix(in sRGB, var(--jp-search-overlay-ink) 20%, var(--jp-search-overlay-surface)); |
| 1282 | /* Ink-derived shadow inverts polarity per theme (SEARCH-289): a dark drop |
| 1283 | * shadow on light cards, a soft light halo on dark — a flat black shadow is |
| 1284 | * invisible against the dark scrim. Static black above is the fallback. */ |
| 1285 | box-shadow: 0 8px 32px color-mix(in sRGB, var(--jp-search-overlay-ink) 22%, transparent); |
| 1286 | } |
| 1287 | } |
| 1288 | /* Single hairline over the full 60px header strip — siblings paint with a seam (SEARCH-260). */ |
| 1289 | .jetpack-search-block-overlay__card::before { |
| 1290 | content: ""; |
| 1291 | position: absolute; |
| 1292 | top: 60px; |
| 1293 | left: 0; |
| 1294 | right: 0; |
| 1295 | height: 1px; |
| 1296 | background: transparent; |
| 1297 | pointer-events: none; |
| 1298 | } |
| 1299 | @supports (background: color-mix(in sRGB, black 50%, white)) { |
| 1300 | .jetpack-search-block-overlay__card::before { |
| 1301 | background: color-mix(in sRGB, var(--jp-search-overlay-ink) 15%, var(--jp-search-overlay-surface)); |
| 1302 | } |
| 1303 | } |
| 1304 | .jetpack-search-block-overlay__close { |
| 1305 | position: absolute; |
| 1306 | top: 0; |
| 1307 | right: 0; |
| 1308 | width: 60px; |
| 1309 | height: 60px; |
| 1310 | display: flex; |
| 1311 | align-items: center; |
| 1312 | justify-content: center; |
| 1313 | background: transparent; |
| 1314 | border: 0; |
| 1315 | cursor: pointer; |
| 1316 | color: inherit; |
| 1317 | } |
| 1318 | /* Opaque ink-over-surface mix — `color-mix(currentColor, transparent)` collapses to full-opacity ink on Safari <16.4 and swallows the X icon. */ |
| 1319 | @supports (background: color-mix(in sRGB, black 50%, white)) { |
| 1320 | .jetpack-search-block-overlay__close:hover, |
| 1321 | .jetpack-search-block-overlay__close:focus-visible { |
| 1322 | background: color-mix(in sRGB, var(--jp-search-overlay-ink) 14%, var(--jp-search-overlay-surface)); |
| 1323 | } |
| 1324 | } |
| 1325 | .jetpack-search-block-overlay__close svg { |
| 1326 | width: 24px; |
| 1327 | height: 24px; |
| 1328 | } |
| 1329 | /* Pin in-overlay button `color` against host-theme `button:hover|:focus` overrides |
| 1330 | * (fieldguide and similar legacy themes flip button color to a brand accent on hover). |
| 1331 | * Our block-level rules set `color: inherit` at (0,1,0); a stray `button:hover` rule |
| 1332 | * at (0,1,1) outranks it on `:hover`, and the `color-mix(currentColor X%, …)` hover |
| 1333 | * affordances on the filters-popover trigger, results-sort trigger, suggestions |
| 1334 | * options, etc. then resolve against the host's hover color (often white-on-white). |
| 1335 | * Card-scoped `:hover|:focus|[aria-expanded=true]` lands at (0,2,1) — beats the |
| 1336 | * theme rule without escalating to `!important`. */ |
| 1337 | .jetpack-search-block-overlay__card button:hover, |
| 1338 | .jetpack-search-block-overlay__card button:focus, |
| 1339 | .jetpack-search-block-overlay__card button:focus-visible, |
| 1340 | .jetpack-search-block-overlay__card button[aria-expanded="true"] { |
| 1341 | color: var(--jp-search-overlay-ink); |
| 1342 | } |
| 1343 | /* Load More is a solid theme `core/button` on the search page, which is right |
| 1344 | * there. Inside the overlay card it sits on the resolved card surface, where the |
| 1345 | * theme's solid button background (and its accent `:hover`, e.g. Twenty Sixteen's |
| 1346 | * #007acc) reads as a heavy slab that clashes with the card's otherwise |
| 1347 | * `currentColor`-ghost controls (close, sort/filter triggers, active-filter |
| 1348 | * pills). Card-scoped (0,2,0 / 0,2,1) so it only restyles the in-overlay button |
| 1349 | * to the same ghost affordance — the page button is untouched. */ |
| 1350 | .jetpack-search-block-overlay__card .jetpack-search-load-more__button { |
| 1351 | background: transparent; |
| 1352 | border: 1px solid; |
| 1353 | border-color: color-mix(in sRGB, currentColor 20%, transparent); |
| 1354 | color: var(--jp-search-overlay-ink); |
| 1355 | } |
| 1356 | .jetpack-search-block-overlay__card .jetpack-search-load-more__button:hover:not(:disabled), |
| 1357 | .jetpack-search-block-overlay__card .jetpack-search-load-more__button:focus-visible { |
| 1358 | background: color-mix(in sRGB, currentColor 8%, transparent); |
| 1359 | } |
| 1360 | /* Promote the first child (search-input) to a 60px header strip flush with |
| 1361 | * the close button, matching the legacy `__box` header. Suppress the input |
| 1362 | * block's own border-bottom — the card's `::before` hairline handles the |
| 1363 | * separator across the whole strip. */ |
| 1364 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input { |
| 1365 | position: absolute; |
| 1366 | top: 0; |
| 1367 | left: 0; |
| 1368 | right: 60px; |
| 1369 | height: 60px; |
| 1370 | margin: 0; |
| 1371 | padding: 0; |
| 1372 | } |
| 1373 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input .jetpack-search-input__inside-wrapper { |
| 1374 | height: 100%; |
| 1375 | display: flex; |
| 1376 | align-items: stretch; |
| 1377 | gap: 0; |
| 1378 | padding: 0; |
| 1379 | border-bottom: 0; |
| 1380 | } |
| 1381 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input .jetpack-search-input__icon { |
| 1382 | flex: 0 0 60px; |
| 1383 | width: 60px; |
| 1384 | height: 60px; |
| 1385 | padding: 18px; |
| 1386 | box-sizing: border-box; |
| 1387 | opacity: 0.5; |
| 1388 | } |
| 1389 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input .jetpack-search-input__field { |
| 1390 | flex: 1 1 auto; |
| 1391 | min-width: 0; |
| 1392 | height: 100%; |
| 1393 | font-size: 18px; |
| 1394 | line-height: 1; |
| 1395 | margin: 0; |
| 1396 | padding: 0; |
| 1397 | background: transparent; |
| 1398 | } |
| 1399 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input .jetpack-search-input__clear { |
| 1400 | flex: 0 0 60px; |
| 1401 | width: 60px; |
| 1402 | height: 60px; |
| 1403 | padding: 0; |
| 1404 | font-size: 0.875rem; |
| 1405 | font-weight: 400; |
| 1406 | line-height: 1; |
| 1407 | } |
| 1408 | /* Suggestions panel covers the full card width (cancel the input's `right: 60px` |
| 1409 | * offset) and sits above `results-sort` / `filters-popover` (both `z-index: 20`). |
| 1410 | * Background reads from `--jp-search-overlay-surface` so the panel tracks the |
| 1411 | * resolved card surface — a hardcoded `#fff` would re-introduce the white-on-dark |
| 1412 | * bug on legacy `--background`/`--foreground` themes. */ |
| 1413 | .jetpack-search-block-overlay__card .wp-block-jetpack-search-search-input .jetpack-search-input__suggestions { |
| 1414 | right: -60px; |
| 1415 | z-index: 30; |
| 1416 | background: var(--jp-search-overlay-surface, #fff); |
| 1417 | } |
| 1418 | /* Top padding clears the absolutely-positioned 60px header strip (SEARCH-243). */ |
| 1419 | .jetpack-search-block-overlay__content > .wp-block-group:first-child { |
| 1420 | padding: var(--jp-search-overlay-content-pad-block-start) var(--jp-search-overlay-content-pad-inline) var(--jp-search-overlay-content-pad-block-end); |
| 1421 | } |
| 1422 | @media (max-width: 781px) { |
| 1423 | .jetpack-search-block-overlay { |
| 1424 | padding: 0; |
| 1425 | } |
| 1426 | .jetpack-search-block-overlay__card { |
| 1427 | min-height: 100vh; |
| 1428 | border: 0; |
| 1429 | border-radius: 0; |
| 1430 | box-shadow: none; |
| 1431 | --jp-search-overlay-content-pad-inline: 1em; |
| 1432 | --jp-search-overlay-content-pad-block-end: 1em; |
| 1433 | } |
| 1434 | } |
| 1435 | /* Mirror legacy `$break-lg: 992px → $modal-max-width-lg: 95%` from `instant-search/components/search-results.scss`. */ |
| 1436 | @media (min-width: 992px) { |
| 1437 | .jetpack-search-block-overlay__card { |
| 1438 | max-width: 95%; |
| 1439 | } |
| 1440 | } |
| 1441 | /* Body-scroll lock while open. JS side stashes/restores scrollY on toggle. */ |
| 1442 | body.jetpack-search-block-overlay-open { |
| 1443 | position: fixed; |
| 1444 | left: 0; |
| 1445 | right: 0; |
| 1446 | width: 100%; |
| 1447 | overflow: hidden; |
| 1448 | } |
| 1449 | CSS; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * Register + enqueue the shared responsive layout CSS on the embedded / |
| 1454 | * WC-product page templates (`jetpack-search.html`, |
| 1455 | * `jetpack-search-product-results.html`). Self-gates on `is_search()` so |
| 1456 | * non-search requests skip the work entirely; the overlay path enqueues |
| 1457 | * the same handle unconditionally from its own asset hook. |
| 1458 | */ |
| 1459 | public static function enqueue_search_page_assets() { |
| 1460 | if ( ! is_search() ) { |
| 1461 | return; |
| 1462 | } |
| 1463 | static::enqueue_search_layout_style(); |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * Register + enqueue the inline CSS that drives the shared responsive |
| 1468 | * layout pattern across all three Search Blocks templates — narrow-width |
| 1469 | * sidebar collapse and in-header popover toggle. Called from both the |
| 1470 | * overlay enqueue path and the page-template enqueue path. |
| 1471 | * |
| 1472 | * `wp_register_style` / `wp_enqueue_style` are idempotent, but |
| 1473 | * `wp_add_inline_style` is **not** — it appends to an internal array on |
| 1474 | * every call, so a second invocation would double the inline payload. |
| 1475 | * The `wp_style_is( …, 'enqueued' )` short-circuit makes the helper safe |
| 1476 | * to call from multiple sites in one request. |
| 1477 | */ |
| 1478 | public static function enqueue_search_layout_style() { |
| 1479 | // No src — this handle exists only as a target for `wp_add_inline_style`. |
| 1480 | // Version tracks the package so a release bust cache-invalidates any |
| 1481 | // reusing site's inline-style cache. |
| 1482 | wp_register_style( 'jetpack-search-layout', false, array(), Package::VERSION ); |
| 1483 | if ( wp_style_is( 'jetpack-search-layout', 'enqueued' ) ) { |
| 1484 | return; |
| 1485 | } |
| 1486 | wp_enqueue_style( 'jetpack-search-layout' ); |
| 1487 | wp_add_inline_style( 'jetpack-search-layout', static::search_layout_inline_css() ); |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * Inline CSS for the responsive search-results layout shared across the |
| 1492 | * overlay, embedded (`jetpack-search.html`), and WC product |
| 1493 | * (`jetpack-search-product-results.html`) templates. |
| 1494 | * |
| 1495 | * Below 992px the right-column filter sidebar collapses to a popover |
| 1496 | * trigger docked next to results-sort; at >= 992px the sidebar is the |
| 1497 | * sole filter UI and the in-header popover is hidden so the two don't |
| 1498 | * double up. Same breakpoint as the legacy Instant Search overlay |
| 1499 | * (`.jetpack-instant-search__search-results-secondary { display: none }` |
| 1500 | * below `$break-lg`). Lives next to `block_template_overlay_inline_css()` |
| 1501 | * (which keeps overlay-only chrome) because the rules target the |
| 1502 | * templates' outer columns + results-header, none of which any single |
| 1503 | * block owns. |
| 1504 | * |
| 1505 | * @return string |
| 1506 | */ |
| 1507 | protected static function search_layout_inline_css(): string { |
| 1508 | return <<<'CSS' |
| 1509 | /* The block group already carries `layout.type:flex` which makes the WordPress |
| 1510 | * block-layout system emit `display:flex` / `flex-wrap:nowrap` / |
| 1511 | * `justify-content:space-between`. Restating them is defensive (decouples us |
| 1512 | * from block-layout CSS being present); the operative net-new rule is |
| 1513 | * `align-items: center`, which centers `results-count` against the controls |
| 1514 | * cluster. */ |
| 1515 | .jetpack-search-layout__results-header { |
| 1516 | display: flex; |
| 1517 | flex-wrap: nowrap; |
| 1518 | justify-content: space-between; |
| 1519 | align-items: center; |
| 1520 | } |
| 1521 | /* Right-side controls cluster: sort + filters-popover trigger. Without this |
| 1522 | * the three `__results-header` children get spread evenly by the parent's |
| 1523 | * `space-between`; nesting sort + popover here pins them as one block on the |
| 1524 | * trailing edge. */ |
| 1525 | .jetpack-search-layout__results-header-controls { |
| 1526 | display: flex; |
| 1527 | flex-wrap: nowrap; |
| 1528 | align-items: center; |
| 1529 | gap: 0.75rem; |
| 1530 | } |
| 1531 | /* Name the columns row as the layout container so the sidebar/popover flip |
| 1532 | * tracks its inline-size. The `@media` rules below are the universal base — |
| 1533 | * they fire in every browser (including legacy ones without container-query |
| 1534 | * support) and they drive standalone usage outside the named container (no |
| 1535 | * container ancestor → only `@media` applies). The `@container` rule further |
| 1536 | * down overrides `@media` via source-order cascade at equal specificity when |
| 1537 | * the named container is in scope AND narrower than 992px. The override has |
| 1538 | * to undo `@media (min-width: 992px)`'s `popover { display: none }` |
| 1539 | * explicitly: in the "wide viewport, narrow container" case `@media |
| 1540 | * (min-width: 992px)` keeps firing on the viewport width and would otherwise |
| 1541 | * leave the visitor with no filter UI at all. `@container (min-width: 992px)` |
| 1542 | * isn't defined — container width is bounded by viewport width in practice, |
| 1543 | * so the matching `@media (min-width: 992px)` already covers the wide case. */ |
| 1544 | .wp-block-columns:has(> .jetpack-search-layout__filters-column) { |
| 1545 | container-type: inline-size; |
| 1546 | container-name: jetpack-search-layout; |
| 1547 | } |
| 1548 | /* Below 992px the right-column filter sidebar collapses to a popover trigger |
| 1549 | * docked next to results-sort. The trigger comes from the |
| 1550 | * `jetpack-search/filters-popover` block that ships in each template. The |
| 1551 | * selector is scoped to the named outer column so nested `wp-block-column`s |
| 1552 | * inside result-card templates aren't affected. */ |
| 1553 | @media (max-width: 991.98px) { |
| 1554 | .jetpack-search-layout__filters-column { |
| 1555 | display: none; |
| 1556 | } |
| 1557 | /* `!important` defends against the parent `wp:columns` block-layout CSS |
| 1558 | * that pins `.wp-block-column` to its inline `flex-basis` (or to an even |
| 1559 | * split when no width is set). Once the filter column is `display:none`, |
| 1560 | * the results column has to be able to claim the full row at any |
| 1561 | * specificity. */ |
| 1562 | .jetpack-search-layout__results-column { |
| 1563 | flex-basis: 100% !important; |
| 1564 | } |
| 1565 | } |
| 1566 | /* Sidebar left divider tracks `currentColor` so the hairline stays subtle on |
| 1567 | * light themes and visible on dark themes, matching the search-input |
| 1568 | * underline. We only set color; each template's column block sets |
| 1569 | * `border-left-width: 1px` inline. Fallback to `transparent` so themes/UAs |
| 1570 | * without `color-mix` support get an invisible divider rather than a hard |
| 1571 | * grey rule. */ |
| 1572 | .jetpack-search-layout__filters-column { |
| 1573 | border-left-color: transparent; |
| 1574 | } |
| 1575 | @supports (border-color: color-mix(in sRGB, black 50%, white)) { |
| 1576 | .jetpack-search-layout__filters-column { |
| 1577 | border-left-color: color-mix(in sRGB, currentColor 15%, transparent); |
| 1578 | } |
| 1579 | } |
| 1580 | /* Sidebar-showing rules (>= 992px). The corner-join is structural: the |
| 1581 | * columns row is pulled flush to the search-input hairline and breathing |
| 1582 | * room re-added as internal column padding, so the filters column's |
| 1583 | * `border-left` runs the row's full height — hairline (top) to end-of-div |
| 1584 | * (bottom). Three vertical gaps are neutralised: |
| 1585 | * |
| 1586 | * a) `margin-block-start` on the row (outer group's `spacing.blockGap` or |
| 1587 | * the theme's default block-gap) — zeroed. |
| 1588 | * |
| 1589 | * b) `.is-layout-flex { align-items: center }` (theme/core default), which |
| 1590 | * centres the shorter filters column and drops its top edge. Overridden |
| 1591 | * to `stretch` (not `flex-start`) so the column also grows to full row |
| 1592 | * height; it's flow layout, so its content stays top-aligned regardless. |
| 1593 | * |
| 1594 | * c) Overlay-only: SEARCH-243's content-group inset sits outside the |
| 1595 | * columns, so the stretched column stops short of the card edges (below |
| 1596 | * the `::before` hairline at the top, and short of the bottom). Zeroed |
| 1597 | * on the group's block axis and re-added on the columns, both sides |
| 1598 | * reading the same `--jp-search-overlay-content-pad-*` tokens the group |
| 1599 | * itself uses — so the divider reaches both edges while content keeps |
| 1600 | * its breathing room, and the two sites can't drift. |
| 1601 | * |
| 1602 | * `.is-layout-flex` bumps the columns selector to (0,3,0) to outrank |
| 1603 | * per-container `blockGap` CSS; `:has(> filters-column)` scopes it to our |
| 1604 | * rows. These target the row/group, which `@container` can't reach from |
| 1605 | * inside the named container, so they stay viewport-driven — harmless when |
| 1606 | * the sidebar collapses below 992px. (b) is also a no-op wherever WP core's |
| 1607 | * `.wp-block-columns { align-items: normal !important }` is present; see |
| 1608 | * AGENTS.md. */ |
| 1609 | @media (min-width: 992px) { |
| 1610 | /* Sidebar shown, popover-in-results-header hidden. The `@container |
| 1611 | * (max-width: 991.98px)` block below re-shows the popover when the |
| 1612 | * named container is narrower than the viewport. */ |
| 1613 | .jetpack-search-layout__results-header .jetpack-search-filters-popover { |
| 1614 | display: none; |
| 1615 | } |
| 1616 | .wp-block-columns.is-layout-flex:has(> .jetpack-search-layout__filters-column) { |
| 1617 | align-items: stretch; |
| 1618 | margin-block-start: 0; |
| 1619 | } |
| 1620 | .jetpack-search-block-overlay__content > .wp-block-group:first-child:has(.jetpack-search-layout__filters-column) { |
| 1621 | padding-top: 0; |
| 1622 | padding-bottom: 0; |
| 1623 | } |
| 1624 | .wp-block-columns:has(> .jetpack-search-layout__filters-column) > .wp-block-column { |
| 1625 | padding-top: var(--jp-search-overlay-content-pad-block-start, 0.5em); |
| 1626 | } |
| 1627 | .jetpack-search-block-overlay__content .wp-block-columns:has(> .jetpack-search-layout__filters-column) > .wp-block-column { |
| 1628 | padding-bottom: var(--jp-search-overlay-content-pad-block-end, 2em); |
| 1629 | } |
| 1630 | } |
| 1631 | /* @container override: applies when the named container exists. Placed AFTER |
| 1632 | * the `@media` rules so source-order cascade lets it win over them at equal |
| 1633 | * specificity. In "wide viewport, narrow container" (the case the change is |
| 1634 | * meant to fix), `@media (max-width: 991.98px)` doesn't fire but `@media |
| 1635 | * (min-width: 992px)` does — this block undoes the latter's `popover { |
| 1636 | * display: none }` via `display: inline-block` and hides the sidebar that |
| 1637 | * the `@media (max-width)` rule wouldn't have hidden at this viewport. Same |
| 1638 | * `!important` reasoning on `flex-basis: 100%` as the @media block. */ |
| 1639 | @container jetpack-search-layout (max-width: 991.98px) { |
| 1640 | .jetpack-search-layout__filters-column { |
| 1641 | display: none; |
| 1642 | } |
| 1643 | .jetpack-search-layout__results-column { |
| 1644 | flex-basis: 100% !important; |
| 1645 | } |
| 1646 | .jetpack-search-layout__results-header .jetpack-search-filters-popover { |
| 1647 | display: inline-block; |
| 1648 | } |
| 1649 | } |
| 1650 | CSS; |
| 1651 | } |
| 1652 | |
| 1653 | /** |
| 1654 | * Product-search counterpart of `get_search_template_content()`. |
| 1655 | * |
| 1656 | * @return string Block markup for the product-search template. |
| 1657 | */ |
| 1658 | protected static function get_product_search_template_content(): string { |
| 1659 | $template_path = __DIR__ . '/templates/jetpack-search-product-results.html'; |
| 1660 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local, bundled template file. |
| 1661 | $raw = is_readable( $template_path ) ? (string) file_get_contents( $template_path ) : ''; |
| 1662 | return static::substitute_template_placeholders( $raw ); |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * Substitute `{{FILTER_HEADING}}` / `{{HEADER_SLUG}}` / `{{FOOTER_SLUG}}` in a |
| 1667 | * bundled template. Empty input passes through. |
| 1668 | * |
| 1669 | * @param string $raw Raw template-file contents. |
| 1670 | * @return string |
| 1671 | */ |
| 1672 | protected static function substitute_template_placeholders( string $raw ): string { |
| 1673 | if ( '' === $raw ) { |
| 1674 | return $raw; |
| 1675 | } |
| 1676 | $slugs = static::resolve_chrome_slugs(); |
| 1677 | return str_replace( |
| 1678 | array( '{{FILTER_HEADING}}', '{{HEADER_SLUG}}', '{{FOOTER_SLUG}}' ), |
| 1679 | array( |
| 1680 | esc_html__( 'Filter options', 'jetpack-search-pkg' ), |
| 1681 | $slugs['header'], |
| 1682 | $slugs['footer'], |
| 1683 | ), |
| 1684 | $raw |
| 1685 | ); |
| 1686 | } |
| 1687 | |
| 1688 | /** |
| 1689 | * Active theme's chrome slugs. Test seam; resolver lives on |
| 1690 | * `Theme_Chrome_Slug_Resolver`. |
| 1691 | * |
| 1692 | * @return array{header:string,footer:string} |
| 1693 | */ |
| 1694 | protected static function resolve_chrome_slugs(): array { |
| 1695 | return Theme_Chrome_Slug_Resolver::resolve(); |
| 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * Idempotent wrapper around `register_block_template`. Unregisters first so |
| 1700 | * a stale entry from a prior init (long-lived PHP-FPM worker) is replaced |
| 1701 | * rather than triggering `doing_it_wrong`. |
| 1702 | * |
| 1703 | * @param string $name Fully-qualified template name. |
| 1704 | * @param array<string,mixed> $args Args for register_block_template(). |
| 1705 | */ |
| 1706 | protected static function replace_block_template( string $name, array $args ) { |
| 1707 | if ( class_exists( '\WP_Block_Templates_Registry' ) ) { |
| 1708 | $registry = \WP_Block_Templates_Registry::get_instance(); |
| 1709 | if ( $registry->is_registered( $name ) ) { |
| 1710 | $registry->unregister( $name ); |
| 1711 | } |
| 1712 | } |
| 1713 | register_block_template( $name, $args ); |
| 1714 | } |
| 1715 | |
| 1716 | /** |
| 1717 | * Counterpart of `register_search_template()` for product search. |
| 1718 | */ |
| 1719 | public static function register_product_search_template() { |
| 1720 | if ( ! function_exists( 'register_block_template' ) || ! static::block_templates_active() ) { |
| 1721 | return; |
| 1722 | } |
| 1723 | $content = static::get_product_search_template_content(); |
| 1724 | if ( '' === $content ) { |
| 1725 | return; |
| 1726 | } |
| 1727 | static::replace_block_template( |
| 1728 | static::get_parent_plugin_slug() . '//' . self::PRODUCT_SEARCH_TEMPLATE_SLUG, |
| 1729 | array( |
| 1730 | 'title' => __( 'Jetpack Search Product Results', 'jetpack-search-pkg' ), |
| 1731 | 'description' => __( 'Displays WooCommerce product search results with Jetpack Search filters.', 'jetpack-search-pkg' ), |
| 1732 | 'content' => $content, |
| 1733 | ) |
| 1734 | ); |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * Directory slug of the plugin that owns the template in the Site Editor UI. |
| 1739 | * Picked by preference so the more-specific "Jetpack Search" label wins |
| 1740 | * when both the standalone plugin and the Jetpack monolith are active: |
| 1741 | * `jetpack-search` → `jetpack` → `jetpack-search` fallback. |
| 1742 | * |
| 1743 | * @return string |
| 1744 | */ |
| 1745 | protected static function get_parent_plugin_slug(): string { |
| 1746 | $active = Helper::get_active_plugins(); |
| 1747 | $preferred = array( |
| 1748 | 'jetpack-search' => 'jetpack-search/jetpack-search.php', |
| 1749 | 'jetpack' => 'jetpack/jetpack.php', |
| 1750 | ); |
| 1751 | foreach ( $preferred as $slug => $plugin_file ) { |
| 1752 | if ( in_array( $plugin_file, $active, true ) ) { |
| 1753 | return $slug; |
| 1754 | } |
| 1755 | } |
| 1756 | return 'jetpack-search'; |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * Prepend the Jetpack Search slug to the search template hierarchy on |
| 1761 | * block-theme search requests. Existing occurrences are stripped first |
| 1762 | * so a second init pass / another filter on the same hook can't dup. |
| 1763 | * |
| 1764 | * WooCommerce product-search carve-out: override off → leave to WC's |
| 1765 | * prepend; override on → fall through here, then |
| 1766 | * `route_woocommerce_product_search_template()` swaps WC's slug for ours. |
| 1767 | * |
| 1768 | * @param string[] $templates Template hierarchy slugs. |
| 1769 | * @return string[] |
| 1770 | */ |
| 1771 | public static function prepend_search_template( $templates ) { |
| 1772 | if ( ! is_search() || ! static::block_templates_active() ) { |
| 1773 | return $templates; |
| 1774 | } |
| 1775 | if ( ! static::woocommerce_search_template_override_enabled() && static::is_woocommerce_product_search() ) { |
| 1776 | return $templates; |
| 1777 | } |
| 1778 | $templates = array_values( |
| 1779 | array_filter( |
| 1780 | (array) $templates, |
| 1781 | static function ( $slug ) { |
| 1782 | return self::SEARCH_TEMPLATE_SLUG !== $slug; |
| 1783 | } |
| 1784 | ) |
| 1785 | ); |
| 1786 | array_unshift( $templates, self::SEARCH_TEMPLATE_SLUG ); |
| 1787 | return $templates; |
| 1788 | } |
| 1789 | |
| 1790 | /** |
| 1791 | * Classic-theme counterpart to `prepend_search_template()`. Block-theme |
| 1792 | * hierarchy filters are a no-op on classic themes — `locate_template()` |
| 1793 | * walks slugs as `{slug}.php` and a classic theme doesn't ship one for |
| 1794 | * our slug. So let the hierarchy resolve normally, then swap the path |
| 1795 | * via `template_include` to our bundled PHP shim, which renders the same |
| 1796 | * block markup inside the theme's `get_header()`/`get_footer()`. |
| 1797 | * |
| 1798 | * @param string $template Resolved template path. |
| 1799 | * @return string |
| 1800 | */ |
| 1801 | public static function route_classic_theme_search_template( $template ) { |
| 1802 | if ( ! is_search() ) { |
| 1803 | return $template; |
| 1804 | } |
| 1805 | $is_product_search = static::is_woocommerce_product_search(); |
| 1806 | // Override off: leave product search to WooCommerce / the theme's own |
| 1807 | // archive routing — we don't impose the product shim without opt-in. |
| 1808 | if ( ! static::woocommerce_search_template_override_enabled() && $is_product_search ) { |
| 1809 | return $template; |
| 1810 | } |
| 1811 | // Override on + product search: route to the product-results shim. |
| 1812 | // Bail back to the theme's template if neither a customization nor a |
| 1813 | // bundled body is available — rendering header/footer around an empty |
| 1814 | // body looks broken. |
| 1815 | if ( $is_product_search ) { |
| 1816 | if ( null === Product_Search_Template::get_customized_content() && '' === static::get_classic_theme_product_search_body() ) { |
| 1817 | return $template; |
| 1818 | } |
| 1819 | return __DIR__ . '/templates/classic-theme-product-search.php'; |
| 1820 | } |
| 1821 | // Regular (non-product) search: only Embedded takes over the whole search |
| 1822 | // page. Inline registers this router too (for the product shim above) but |
| 1823 | // leaves regular searches to the theme, so bail here unless Embedded. |
| 1824 | if ( Module_Control::EXPERIENCE_EMBEDDED !== ( new Module_Control() )->get_experience() ) { |
| 1825 | return $template; |
| 1826 | } |
| 1827 | // Same empty-body bail-out for the generic shim. A saved empty |
| 1828 | // customization ('' vs null) is honored as intentional. |
| 1829 | if ( null === Search_Template::get_customized_content() && '' === static::get_classic_theme_search_body() ) { |
| 1830 | return $template; |
| 1831 | } |
| 1832 | return __DIR__ . '/templates/classic-theme-search.php'; |
| 1833 | } |
| 1834 | |
| 1835 | /** |
| 1836 | * Classic-theme search body — same markup as the block-theme path with |
| 1837 | * top-level `core/template-part` references stripped so the theme's |
| 1838 | * `get_header()` / `get_footer()` drive the chrome. |
| 1839 | * |
| 1840 | * Source of truth: customized `Search_Template` CPT → bundled `jetpack-search.html`. |
| 1841 | * Public because `templates/classic-theme-search.php` calls it from outside the class. |
| 1842 | * |
| 1843 | * @return string Block markup, no template-part wrappers. |
| 1844 | */ |
| 1845 | public static function get_classic_theme_search_body(): string { |
| 1846 | $customized = Search_Template::get_customized_content(); |
| 1847 | if ( null !== $customized ) { |
| 1848 | return $customized; |
| 1849 | } |
| 1850 | return static::strip_top_level_template_parts( static::get_search_template_content() ); |
| 1851 | } |
| 1852 | |
| 1853 | /** |
| 1854 | * Product-search counterpart to {@see get_classic_theme_search_body()} — |
| 1855 | * source of truth for the classic-theme product-results shim. Customized |
| 1856 | * `Product_Search_Template` CPT → bundled `jetpack-search-product-results.html`. |
| 1857 | * Public because `templates/classic-theme-product-search.php` calls it from |
| 1858 | * outside the class. |
| 1859 | * |
| 1860 | * @return string Block markup, no template-part wrappers. |
| 1861 | */ |
| 1862 | public static function get_classic_theme_product_search_body(): string { |
| 1863 | $customized = Product_Search_Template::get_customized_content(); |
| 1864 | if ( null !== $customized ) { |
| 1865 | return $customized; |
| 1866 | } |
| 1867 | return static::strip_top_level_template_parts( static::get_product_search_template_content() ); |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Strip top-level `core/template-part` self-closing comments — classic |
| 1872 | * themes can't resolve their slugs. Non-greedy `.*?` capped by `-->` keeps |
| 1873 | * matching cleanly across template revisions and across nested attribute |
| 1874 | * payloads. |
| 1875 | * |
| 1876 | * @param string $content Block markup, possibly empty. |
| 1877 | * @return string |
| 1878 | */ |
| 1879 | protected static function strip_top_level_template_parts( string $content ): string { |
| 1880 | if ( '' === $content ) { |
| 1881 | return ''; |
| 1882 | } |
| 1883 | return (string) preg_replace( '#<!--\s*wp:template-part\s+.*?/-->\s*#s', '', $content ); |
| 1884 | } |
| 1885 | |
| 1886 | /** |
| 1887 | * Inline layout `<style>` block the classic-theme shims emit before the |
| 1888 | * bundled block markup. Classic themes don't emit core's block-supports |
| 1889 | * layout CSS, so two traits the bundled templates rely on collapse on |
| 1890 | * classic themes: the inner group's 1.5rem `blockGap` vanishes (search |
| 1891 | * input runs straight into the results row), and `alignwide` has no |
| 1892 | * effect (content stretches edge-to-edge because `template_include` |
| 1893 | * bypasses the theme's own content wrapper). Reapplying both, scoped to |
| 1894 | * `<main class="wp-block-group">`, restores parity without leaking |
| 1895 | * outside the shim. Shared by both shims so a future layout tweak |
| 1896 | * touches one place. The `<style>` `id` is unique per render — routing |
| 1897 | * ensures only one shim runs per request, so duplicate IDs can't occur. |
| 1898 | * |
| 1899 | * Public because both `templates/classic-theme-search.php` and |
| 1900 | * `templates/classic-theme-product-search.php` call it from outside the |
| 1901 | * class. |
| 1902 | * |
| 1903 | * @return string Inline `<style>` element ready to echo. |
| 1904 | */ |
| 1905 | public static function get_classic_theme_layout_style(): string { |
| 1906 | return <<<'HTML' |
| 1907 | <style id="jetpack-search-classic-theme-layout"> |
| 1908 | main.wp-block-group { |
| 1909 | max-width: var(--wp--style--global--wide-size, 1280px); |
| 1910 | margin-inline: auto; |
| 1911 | padding-inline: clamp(1rem, 4vw, 2rem); |
| 1912 | } |
| 1913 | main.wp-block-group .is-layout-flow > * + * { |
| 1914 | margin-block-start: var(--wp--style--block-gap, 1.5rem); |
| 1915 | } |
| 1916 | </style> |
| 1917 | HTML; |
| 1918 | } |
| 1919 | |
| 1920 | /** |
| 1921 | * Test override for `block_templates_active()`. Null = read the live state. |
| 1922 | * |
| 1923 | * @var bool|null |
| 1924 | */ |
| 1925 | private static $block_templates_active_for_testing = null; |
| 1926 | |
| 1927 | /** |
| 1928 | * Test seam. Set true/false to force `block_templates_active()`, or null to clear. |
| 1929 | * |
| 1930 | * @param bool|null $active Forced value, or null to clear. |
| 1931 | */ |
| 1932 | public static function set_block_templates_active_for_testing( ?bool $active ): void { |
| 1933 | self::$block_templates_active_for_testing = $active; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * Whether the theme resolves block templates. Overridable seam over |
| 1938 | * `wp_is_block_theme()` for tests. |
| 1939 | * |
| 1940 | * @return bool |
| 1941 | */ |
| 1942 | protected static function block_templates_active(): bool { |
| 1943 | if ( null !== self::$block_templates_active_for_testing ) { |
| 1944 | return self::$block_templates_active_for_testing; |
| 1945 | } |
| 1946 | return wp_is_block_theme(); |
| 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * Front the `jetpack-search-product-results` template for WC product |
| 1951 | * search. Drops WC's `product-search-results` and unshifts ours so it |
| 1952 | * resolves before any `jetpack-search` prepend for the generic route. |
| 1953 | * |
| 1954 | * @param string[] $templates Template hierarchy slugs. |
| 1955 | * @return string[] |
| 1956 | */ |
| 1957 | public static function route_woocommerce_product_search_template( $templates ) { |
| 1958 | // FSE-hierarchy work — classic themes resolve template slugs as `{slug}.php` |
| 1959 | // and there's no `jetpack-search-product-results.php`. The classic-theme |
| 1960 | // equivalent runs through `route_classic_theme_search_template()` instead. |
| 1961 | if ( ! static::block_templates_active() || ! static::is_woocommerce_product_search() ) { |
| 1962 | return $templates; |
| 1963 | } |
| 1964 | $templates = array_values( |
| 1965 | array_filter( |
| 1966 | (array) $templates, |
| 1967 | static function ( $slug ) { |
| 1968 | return self::WC_PRODUCT_SEARCH_TEMPLATE_SLUG !== $slug |
| 1969 | && self::PRODUCT_SEARCH_TEMPLATE_SLUG !== $slug; |
| 1970 | } |
| 1971 | ) |
| 1972 | ); |
| 1973 | array_unshift( $templates, self::PRODUCT_SEARCH_TEMPLATE_SLUG ); |
| 1974 | return $templates; |
| 1975 | } |
| 1976 | |
| 1977 | /** |
| 1978 | * Seed the Interactivity API store with initial state. Per-block render |
| 1979 | * callbacks deep-merge their own entries on top (e.g. filter-checkbox |
| 1980 | * writes its filterConfig). See AGENTS.md § Hydration & SSR seeding. |
| 1981 | */ |
| 1982 | public static function seed_interactivity_state() { |
| 1983 | if ( ! function_exists( 'wp_interactivity_state' ) ) { |
| 1984 | return; |
| 1985 | } |
| 1986 | wp_interactivity_state( |
| 1987 | 'jetpack-search', |
| 1988 | static::build_seed_state( static::collect_filter_configs_from_post() ) |
| 1989 | ); |
| 1990 | } |
| 1991 | |
| 1992 | /** |
| 1993 | * Compose the final seeded state for `wp_interactivity_state()`. |
| 1994 | * |
| 1995 | * @param array<string, array<string, mixed>> $filter_configs Map of filter configs. |
| 1996 | * @return array<string, mixed> |
| 1997 | */ |
| 1998 | public static function build_seed_state( array $filter_configs ): array { |
| 1999 | $state = static::build_initial_state(); |
| 2000 | $state['filterConfigs'] = $filter_configs; |
| 2001 | return $state; |
| 2002 | } |
| 2003 | |
| 2004 | /** |
| 2005 | * Walk the current post's block tree for filter blocks and build the |
| 2006 | * filterConfigs map. Template-part scans are not performed — a filter |
| 2007 | * inside a template part still works, but its config isn't available to |
| 2008 | * the search-results SSR until hydration. |
| 2009 | * |
| 2010 | * @return array<string, array<string, mixed>> |
| 2011 | */ |
| 2012 | protected static function collect_filter_configs_from_post(): array { |
| 2013 | if ( ! function_exists( 'get_post' ) || ! function_exists( 'parse_blocks' ) ) { |
| 2014 | return array(); |
| 2015 | } |
| 2016 | // Bail if any helper is missing — half-loaded feature would ship inconsistent filterConfigs. |
| 2017 | foreach ( static::filter_block_helpers() as $helper ) { |
| 2018 | if ( ! class_exists( $helper ) ) { |
| 2019 | return array(); |
| 2020 | } |
| 2021 | } |
| 2022 | $post = get_post(); |
| 2023 | if ( ! $post || empty( $post->post_content ) ) { |
| 2024 | return array(); |
| 2025 | } |
| 2026 | $configs = array(); |
| 2027 | static::walk_blocks_for_filter_configs( parse_blocks( $post->post_content ), $configs ); |
| 2028 | return $configs; |
| 2029 | } |
| 2030 | |
| 2031 | /** |
| 2032 | * Map of filter block name → helper class. Add a new filter block type |
| 2033 | * by appending one entry here. |
| 2034 | * |
| 2035 | * @return array<string, class-string> |
| 2036 | */ |
| 2037 | protected static function filter_block_helpers(): array { |
| 2038 | $helpers = array( |
| 2039 | 'jetpack-search/filter-checkbox' => Filter_Checkbox::class, |
| 2040 | 'jetpack-search/filter-date' => Filter_Date::class, |
| 2041 | 'jetpack-search/filter-wc-rating' => Filter_Wc_Rating::class, |
| 2042 | 'jetpack-search/filter-wc-attribute' => Filter_Wc_Attribute::class, |
| 2043 | 'jetpack-search/filter-wc-stock-status' => Search_Product_Filter_Status::class, |
| 2044 | ); |
| 2045 | if ( self::woocommerce_blocks_enabled() ) { |
| 2046 | return $helpers; |
| 2047 | } |
| 2048 | // Non-Woo sites: drop WC-only entries so the filter-config walk stays |
| 2049 | // symmetric with what `register_blocks()` actually registered. |
| 2050 | foreach ( array_keys( $helpers ) as $name ) { |
| 2051 | if ( self::is_woocommerce_only_block( $name ) ) { |
| 2052 | unset( $helpers[ $name ] ); |
| 2053 | } |
| 2054 | } |
| 2055 | return $helpers; |
| 2056 | } |
| 2057 | |
| 2058 | /** |
| 2059 | * Recursively walk a parsed block tree, pushing each filter block's |
| 2060 | * config into `$configs` by reference. |
| 2061 | * |
| 2062 | * @param array $blocks Parsed block tree from parse_blocks(). |
| 2063 | * @param array $configs Accumulator map keyed by filterKey. |
| 2064 | * @return void |
| 2065 | */ |
| 2066 | protected static function walk_blocks_for_filter_configs( array $blocks, array &$configs ): void { |
| 2067 | $helpers = static::filter_block_helpers(); |
| 2068 | foreach ( $blocks as $block ) { |
| 2069 | if ( ! is_array( $block ) ) { |
| 2070 | continue; |
| 2071 | } |
| 2072 | $block_name = (string) ( $block['blockName'] ?? '' ); |
| 2073 | if ( isset( $helpers[ $block_name ] ) ) { |
| 2074 | $helper = $helpers[ $block_name ]; |
| 2075 | $attrs = (array) ( $block['attrs'] ?? array() ); |
| 2076 | $key = $helper::derive_filter_key( $attrs ); |
| 2077 | if ( '' !== $key ) { |
| 2078 | $configs[ $key ] = $helper::build_config( $attrs, $key ); |
| 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) { |
| 2083 | static::walk_blocks_for_filter_configs( $block['innerBlocks'], $configs ); |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /** |
| 2089 | * Build the initial state array for the jetpack-search Interactivity API store. |
| 2090 | * |
| 2091 | * @return array<string, mixed> |
| 2092 | */ |
| 2093 | public static function build_initial_state() { |
| 2094 | $is_private = class_exists( Status::class ) ? ( new Status() )->is_private_site() : false; |
| 2095 | $is_wpcom = class_exists( Helper::class ) ? Helper::is_wpcom() : false; |
| 2096 | $site_id = class_exists( Helper::class ) ? Helper::get_wpcom_site_id() : 0; |
| 2097 | $search_query = static::parse_url_search_query(); |
| 2098 | $active_filters = static::parse_url_filters(); |
| 2099 | $filter_logic = static::parse_url_filter_logic( $active_filters ); |
| 2100 | $price_range = static::parse_url_price_range(); |
| 2101 | $is_initial_loading = static::is_initial_loading(); |
| 2102 | $searching_text = function_exists( '__' ) ? __( 'Searching…', 'jetpack-search-pkg' ) : 'Searching…'; |
| 2103 | |
| 2104 | return array( |
| 2105 | // Connection / routing config. |
| 2106 | 'siteId' => $site_id, |
| 2107 | 'apiRoot' => function_exists( 'rest_url' ) ? esc_url_raw( rest_url() ) : '', |
| 2108 | 'nonce' => function_exists( 'wp_create_nonce' ) ? wp_create_nonce( 'wp_rest' ) : '', |
| 2109 | 'isPrivateSite' => $is_private, |
| 2110 | 'isWpcom' => $is_wpcom, |
| 2111 | // TrainTracks gate, mirroring instant search's `disableTracking` |
| 2112 | // (Helper::get_search_options): suppresses `_tkq` pushes for |
| 2113 | // `?disable_tracking=1` crawlers/QA and the filter override. |
| 2114 | 'disableTracking' => static::is_tracking_disabled(), |
| 2115 | // Threaded through url-state so `?orderby=price_asc` round-trips on Woo only. |
| 2116 | 'isWooCommerceBlocksEnabled' => self::woocommerce_blocks_enabled(), |
| 2117 | 'homeUrl' => function_exists( 'home_url' ) ? home_url() : '', |
| 2118 | // Blog locale (not viewer's profile locale) for consistent |
| 2119 | // logged-out formatting. BCP47-ish (`en-US`). |
| 2120 | 'locale' => function_exists( 'get_locale' ) |
| 2121 | ? str_replace( '_', '-', get_locale() ) |
| 2122 | : 'en-US', |
| 2123 | // PHP-style token string parsed client-side by `wp-date-format.js` |
| 2124 | // (IA view bundle can't import `@wordpress/date`). Empty → Intl fallback. |
| 2125 | 'dateFormat' => function_exists( 'get_option' ) |
| 2126 | ? (string) get_option( 'date_format', '' ) |
| 2127 | : '', |
| 2128 | |
| 2129 | // URL-seeded so deep links render on first paint. |
| 2130 | 'searchQuery' => $search_query, |
| 2131 | // `?s=` (empty value) must still fire the initial fetch; `searchQuery` |
| 2132 | // alone collapses present-but-empty and missing to `''`. |
| 2133 | 'hasSearchParam' => static::has_search_param(), |
| 2134 | 'searchParamName' => static::get_search_param_name(), |
| 2135 | 'sortOrder' => static::parse_url_sort(), |
| 2136 | 'activeFilters' => $active_filters, |
| 2137 | 'filterLogic' => $filter_logic, |
| 2138 | 'priceRange' => $price_range, |
| 2139 | // Scalar `?filter_id=value`; seeded as `{}` so JS readers see a defined shape. |
| 2140 | 'staticFilterSelections' => (object) array(), |
| 2141 | |
| 2142 | // Each filter block's render.php deep-merges its entry. Shape: |
| 2143 | // `{ [key]: { filterKey, filterType, taxonomy, effectiveSlug, label, showCount, maxItems } }`. |
| 2144 | 'filterConfigs' => array(), |
| 2145 | |
| 2146 | // JS hydration fills these. `aggregations` is stdClass so JS sees `{}`. |
| 2147 | 'results' => array(), |
| 2148 | 'aggregations' => (object) array(), |
| 2149 | // See AGENTS.md § Filter bucket lifecycle. |
| 2150 | 'retainedFilterOptions' => (object) array(), |
| 2151 | 'totalResults' => 0, |
| 2152 | 'pageHandle' => null, |
| 2153 | |
| 2154 | // `isLoading` true on deep links keeps the empty-state hidden until |
| 2155 | // JS fires the initial fetch (otherwise "No results found" flashes). |
| 2156 | 'isLoading' => $is_initial_loading, |
| 2157 | 'isLoadingMore' => false, |
| 2158 | 'hasError' => false, |
| 2159 | |
| 2160 | // Seeded so SSR resolves `data-wp-text` on first paint. |
| 2161 | 'resultsCountText' => $is_initial_loading ? $searching_text : '', |
| 2162 | |
| 2163 | 'strings' => static::build_initial_strings(), |
| 2164 | 'priceCurrencySymbol' => '$', |
| 2165 | |
| 2166 | // Top-level (not under `strings`) — keeps Phan's `array<string,string>` |
| 2167 | // contract on `strings` intact. |
| 2168 | 'aiExtendedLoadingHints' => static::build_ai_extended_loading_hints(), |
| 2169 | |
| 2170 | 'wcStockStatusLabels' => static::build_stock_status_labels(), |
| 2171 | ); |
| 2172 | } |
| 2173 | |
| 2174 | /** |
| 2175 | * Slug → display-label map for `wc_stock_status` selections, used by the |
| 2176 | * active-filters block for product-aware chips. RSM-1932 will swap this |
| 2177 | * for WC's translated labels (`wc_get_product_stock_status_options()`) |
| 2178 | * without changing the shape. Empty when the status helper isn't loaded. |
| 2179 | * |
| 2180 | * @return array<string, string> |
| 2181 | */ |
| 2182 | protected static function build_stock_status_labels(): array { |
| 2183 | if ( ! class_exists( Search_Product_Filter_Status::class ) ) { |
| 2184 | return array(); |
| 2185 | } |
| 2186 | $labels = array(); |
| 2187 | foreach ( Search_Product_Filter_Status::get_options() as $option ) { |
| 2188 | $value = (string) ( $option['value'] ?? '' ); |
| 2189 | if ( '' === $value ) { |
| 2190 | continue; |
| 2191 | } |
| 2192 | $labels[ $value ] = (string) ( $option['label'] ?? $value ); |
| 2193 | } |
| 2194 | return $labels; |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * Whether the URL carries a search query, filter, or price range — i.e. |
| 2199 | * the JS store will fire an initial fetch on hydration. Render callbacks |
| 2200 | * use this to emit pre-hydration affordances (skeleton, "Searching…"). |
| 2201 | * |
| 2202 | * URL-derived rather than read back from `wp_interactivity_state()` |
| 2203 | * because FSE pre-resolves block attributes before `wp_enqueue_scripts` |
| 2204 | * fires, so a state-read would silently return false on the very pages |
| 2205 | * this is meant to flag. Mirrors the `isLoading` seed exactly. |
| 2206 | * |
| 2207 | * @return bool |
| 2208 | */ |
| 2209 | public static function is_initial_loading(): bool { |
| 2210 | if ( null !== self::$is_initial_loading_cache ) { |
| 2211 | return self::$is_initial_loading_cache; |
| 2212 | } |
| 2213 | // `has_search_param()` not `parse_url_search_query() !== ''` — an |
| 2214 | // explicit `?s=` (empty value) still means "visitor landed on a |
| 2215 | // search page" and should fire an unfiltered initial fetch. |
| 2216 | if ( static::has_search_param() ) { |
| 2217 | self::$is_initial_loading_cache = true; |
| 2218 | return true; |
| 2219 | } |
| 2220 | if ( ! empty( static::parse_url_filters() ) ) { |
| 2221 | self::$is_initial_loading_cache = true; |
| 2222 | return true; |
| 2223 | } |
| 2224 | self::$is_initial_loading_cache = null !== static::parse_url_price_range(); |
| 2225 | return self::$is_initial_loading_cache; |
| 2226 | } |
| 2227 | |
| 2228 | /** |
| 2229 | * Reset the `is_initial_loading()` memo. Tests only — PHPUnit reuses a |
| 2230 | * single process so `$_GET` from an earlier test would pin the value. |
| 2231 | * Guarded against accidental production use. |
| 2232 | */ |
| 2233 | public static function reset_initial_loading_cache(): void { |
| 2234 | if ( defined( 'ABSPATH' ) && ! defined( 'PHPUNIT_COMPOSER_INSTALL' ) ) { |
| 2235 | return; |
| 2236 | } |
| 2237 | self::$is_initial_loading_cache = null; |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * Pre-hydration view state for a filter block's wrapper. Centralizes the |
| 2242 | * seeded-state read shared by filter-checkbox and filter-date so each |
| 2243 | * render.php branches on a single struct rather than re-deriving the |
| 2244 | * same flags inline. |
| 2245 | * |
| 2246 | * @param string $filter_key The filter key (e.g. `category`, `post_type`). |
| 2247 | * @return array{has_buckets:bool,is_initial_loading:bool,show_wrapper:bool} |
| 2248 | */ |
| 2249 | public static function pre_hydration_filter_view( string $filter_key ): array { |
| 2250 | if ( ! function_exists( 'wp_interactivity_state' ) ) { |
| 2251 | return array( |
| 2252 | 'has_buckets' => false, |
| 2253 | 'is_initial_loading' => false, |
| 2254 | 'show_wrapper' => false, |
| 2255 | ); |
| 2256 | } |
| 2257 | // `aggregations` is seeded as `stdClass` when empty (so JS sees `{}`, |
| 2258 | // not `[]`); cast before subscripting so the read works in either shape. |
| 2259 | $state = wp_interactivity_state( 'jetpack-search' ); |
| 2260 | $aggs = (array) ( $state['aggregations'] ?? array() ); |
| 2261 | $has_buckets = ! empty( $aggs[ $filter_key ]['buckets'] ?? array() ); |
| 2262 | $is_initial_loading = static::is_initial_loading(); |
| 2263 | return array( |
| 2264 | 'has_buckets' => $has_buckets, |
| 2265 | 'is_initial_loading' => $is_initial_loading, |
| 2266 | 'show_wrapper' => $has_buckets || $is_initial_loading, |
| 2267 | ); |
| 2268 | } |
| 2269 | |
| 2270 | /** |
| 2271 | * Emit the `data-wp-context` attribute for a filter block's wrapper. The |
| 2272 | * seeded `wrapperHidden` value is what the IA SSR pass evaluates |
| 2273 | * `data-wp-bind--hidden="context.wrapperHidden"` against, and what the |
| 2274 | * `syncFilterWrapperVisibility` callback updates after hydration. |
| 2275 | * |
| 2276 | * @param string $filter_key The filter key. |
| 2277 | * @param bool $show_wrapper Whether the wrapper should be visible on first paint. |
| 2278 | */ |
| 2279 | public static function emit_filter_wrapper_context( string $filter_key, bool $show_wrapper ): void { |
| 2280 | if ( ! function_exists( 'wp_interactivity_data_wp_context' ) ) { |
| 2281 | return; |
| 2282 | } |
| 2283 | echo wp_kses_data( |
| 2284 | wp_interactivity_data_wp_context( |
| 2285 | array( |
| 2286 | 'filterKey' => $filter_key, |
| 2287 | 'wrapperHidden' => ! $show_wrapper, |
| 2288 | ) |
| 2289 | ) |
| 2290 | ); |
| 2291 | } |
| 2292 | |
| 2293 | /** |
| 2294 | * Normalize the shared `displayStyle` attribute to one of the two CSS |
| 2295 | * variants. `filter-wc-stock-status` and `filter-wc-rating` deliberately |
| 2296 | * don't ship a chip variant and don't call this helper. |
| 2297 | * |
| 2298 | * @param mixed $value Raw attribute value. |
| 2299 | * @return string Either 'checkbox-list' or 'chips'. |
| 2300 | */ |
| 2301 | public static function normalize_display_style( $value ): string { |
| 2302 | return 'chips' === $value ? 'chips' : 'checkbox-list'; |
| 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * Seed translated view-bundle strings for the Interactivity API store. |
| 2307 | * |
| 2308 | * @return array<string, string> |
| 2309 | */ |
| 2310 | protected static function build_initial_strings(): array { |
| 2311 | if ( ! function_exists( '__' ) || ! function_exists( '_n' ) ) { |
| 2312 | return array( |
| 2313 | 'searching' => 'Searching…', |
| 2314 | 'resultsCountSingle' => 'Found %d result', |
| 2315 | 'resultsCountPlural' => 'Found %d results', |
| 2316 | 'removeFilter' => 'Remove %s', |
| 2317 | 'ratingStarsTop' => '5 stars', |
| 2318 | 'ratingStarsAndUpSingle' => '%d star and up', |
| 2319 | 'ratingStarsAndUpPlural' => '%d stars and up', |
| 2320 | 'priceRangeFromTo' => '%1$s – %2$s', |
| 2321 | 'priceRangeFrom' => '%s+', |
| 2322 | 'priceRangeUpTo' => 'Under %s', |
| 2323 | 'priceLabel' => 'Price', |
| 2324 | 'suggestionLabelQuery' => 'Suggestions', |
| 2325 | 'suggestionLabelTaxonomy' => 'Popular Filters', |
| 2326 | 'suggestionLabelPost' => 'Articles', |
| 2327 | 'aiErrorMessage' => 'Sorry, an error occurred while generating an answer.', |
| 2328 | 'aiErrorCode' => 'Error code: %s', |
| 2329 | ); |
| 2330 | } |
| 2331 | return array( |
| 2332 | 'searching' => __( 'Searching…', 'jetpack-search-pkg' ), |
| 2333 | /* translators: %d: number of results. */ |
| 2334 | 'resultsCountSingle' => _n( 'Found %d result', 'Found %d results', 1, 'jetpack-search-pkg' ), |
| 2335 | /* translators: %d: number of results. */ |
| 2336 | 'resultsCountPlural' => _n( 'Found %d result', 'Found %d results', 2, 'jetpack-search-pkg' ), |
| 2337 | /* translators: %s: filter label (e.g. "Category: News"). Announced by screen readers when focus lands on a filter pill's remove button. */ |
| 2338 | 'removeFilter' => __( 'Remove %s', 'jetpack-search-pkg' ), |
| 2339 | /* translators: Active-filter chip label for the 5-star row. The 5-star row is "exactly 5 stars" — no "& up" affordance — because there is no higher rating. Mirrors the row's aria-label in filter-wc-rating/render.php. */ |
| 2340 | 'ratingStarsTop' => __( '5 stars', 'jetpack-search-pkg' ), |
| 2341 | /* translators: %d: rating threshold (singular form, i.e. 1). Active-filter chip label for the "1 star and up" threshold row. Mirrors the row's aria-label in filter-wc-rating/render.php. */ |
| 2342 | 'ratingStarsAndUpSingle' => _n( '%d star and up', '%d stars and up', 1, 'jetpack-search-pkg' ), |
| 2343 | /* translators: %d: rating threshold (plural form, i.e. 2-4). Active-filter chip label for the "X stars and up" threshold rows. Mirrors the row's aria-label in filter-wc-rating/render.php. */ |
| 2344 | 'ratingStarsAndUpPlural' => _n( '%d star and up', '%d stars and up', 2, 'jetpack-search-pkg' ), |
| 2345 | /* translators: 1: minimum price (already includes the currency symbol). 2: maximum price (already includes the currency symbol). Renders an active "Price: $10 – $50" filter pill. */ |
| 2346 | 'priceRangeFromTo' => __( '%1$s – %2$s', 'jetpack-search-pkg' ), |
| 2347 | /* translators: %s: minimum price (already includes the currency symbol). Renders an active "Price: $10+" filter pill (no upper bound) — compact "and above" form aligned with mainstream e-commerce filter chips. */ |
| 2348 | 'priceRangeFrom' => __( '%s+', 'jetpack-search-pkg' ), |
| 2349 | /* translators: %s: maximum price (already includes the currency symbol). Renders an active "Price: Under $50" filter pill (no lower bound) — mirrors Amazon/eBay/Walmart's "Under $X" convention. */ |
| 2350 | 'priceRangeUpTo' => __( 'Under %s', 'jetpack-search-pkg' ), |
| 2351 | /* translators: Group label for the price filter pill ("Price: $10 – $50"). Mirrors the price block's default heading; falls back to this when no price block is on the page. */ |
| 2352 | 'priceLabel' => __( 'Price', 'jetpack-search-pkg' ), |
| 2353 | /* translators: Group label for the typed-query suggestions section of the Search Input autocomplete dropdown. */ |
| 2354 | 'suggestionLabelQuery' => __( 'Suggestions', 'jetpack-search-pkg' ), |
| 2355 | /* translators: Group label for the taxonomy (category / tag) section of the Search Input autocomplete dropdown. */ |
| 2356 | 'suggestionLabelTaxonomy' => __( 'Popular Filters', 'jetpack-search-pkg' ), |
| 2357 | /* translators: Group label for the post-title section of the Search Input autocomplete dropdown. */ |
| 2358 | 'suggestionLabelPost' => __( 'Articles', 'jetpack-search-pkg' ), |
| 2359 | /* translators: Heading shown on the AI Answer panel when the agent endpoint returns an error. The technical message + HTTP/JSON-RPC code render below this string. */ |
| 2360 | 'aiErrorMessage' => __( 'Sorry, an error occurred while generating an answer.', 'jetpack-search-pkg' ), |
| 2361 | /* translators: %s: numeric error code. Surfaces the HTTP / JSON-RPC code that came back with the AI Answer failure, under the technical message. */ |
| 2362 | 'aiErrorCode' => __( 'Error code: %s', 'jetpack-search-pkg' ), |
| 2363 | ); |
| 2364 | } |
| 2365 | |
| 2366 | /** |
| 2367 | * Rotating loading hints for the "Show more" extended AI answer. |
| 2368 | * Mirrors the overlay verbatim so visitors switching surfaces see |
| 2369 | * the same copy. |
| 2370 | * |
| 2371 | * @return array<int, string> |
| 2372 | */ |
| 2373 | protected static function build_ai_extended_loading_hints(): array { |
| 2374 | // Strings omit trailing `…` — render.php appends an animated ellipsis, |
| 2375 | // so a static one would double up. Overlay does the same. |
| 2376 | if ( ! function_exists( '__' ) ) { |
| 2377 | return array( |
| 2378 | 'Searching harder', |
| 2379 | 'Looking deeper into this', |
| 2380 | 'Finding a more complete answer', |
| 2381 | 'Analyzing additional sources', |
| 2382 | 'Gathering more details', |
| 2383 | 'Pulling in more context', |
| 2384 | 'Expanding the search', |
| 2385 | 'Rolling up my virtual sleeves', |
| 2386 | 'Digging through the archives', |
| 2387 | 'Putting on my reading glasses', |
| 2388 | 'Checking under the digital couch cushions', |
| 2389 | 'Consulting the oracle', |
| 2390 | 'Asking a smarter algorithm', |
| 2391 | 'Brewing a fresh batch of insights', |
| 2392 | 'Unleashing the full power of search', |
| 2393 | ); |
| 2394 | } |
| 2395 | return array( |
| 2396 | __( 'Searching harder', 'jetpack-search-pkg' ), |
| 2397 | __( 'Looking deeper into this', 'jetpack-search-pkg' ), |
| 2398 | __( 'Finding a more complete answer', 'jetpack-search-pkg' ), |
| 2399 | __( 'Analyzing additional sources', 'jetpack-search-pkg' ), |
| 2400 | __( 'Gathering more details', 'jetpack-search-pkg' ), |
| 2401 | __( 'Pulling in more context', 'jetpack-search-pkg' ), |
| 2402 | __( 'Expanding the search', 'jetpack-search-pkg' ), |
| 2403 | __( 'Rolling up my virtual sleeves', 'jetpack-search-pkg' ), |
| 2404 | __( 'Digging through the archives', 'jetpack-search-pkg' ), |
| 2405 | __( 'Putting on my reading glasses', 'jetpack-search-pkg' ), |
| 2406 | __( 'Checking under the digital couch cushions', 'jetpack-search-pkg' ), |
| 2407 | __( 'Consulting the oracle', 'jetpack-search-pkg' ), |
| 2408 | __( 'Asking a smarter algorithm', 'jetpack-search-pkg' ), |
| 2409 | __( 'Brewing a fresh batch of insights', 'jetpack-search-pkg' ), |
| 2410 | __( 'Unleashing the full power of search', 'jetpack-search-pkg' ), |
| 2411 | ); |
| 2412 | } |
| 2413 | |
| 2414 | /** |
| 2415 | * Parse the search query from the URL using whichever key |
| 2416 | * `get_search_param_name()` returns (`s` on search routes, `q` elsewhere). |
| 2417 | * Public so render templates can seed their input from the same source. |
| 2418 | * |
| 2419 | * @return string |
| 2420 | */ |
| 2421 | public static function parse_url_search_query(): string { |
| 2422 | $key = self::get_search_param_name(); |
| 2423 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- read-only URL state; coerced to string + sanitize_text_field( wp_unslash( ... ) ) on the next line. |
| 2424 | $raw = $_GET[ $key ] ?? ''; |
| 2425 | if ( ! is_scalar( $raw ) ) { |
| 2426 | return ''; |
| 2427 | } |
| 2428 | return trim( sanitize_text_field( wp_unslash( (string) $raw ) ) ); |
| 2429 | } |
| 2430 | |
| 2431 | /** |
| 2432 | * Whether the search-query key is present in `$_GET` (any value). |
| 2433 | * Distinguishes `?s=` (blank search) from a URL that omits the key — |
| 2434 | * `parse_url_search_query()` collapses both to `''`. Array-shaped |
| 2435 | * `?s[]=foo` reads as "not present" to stay in lockstep. |
| 2436 | * |
| 2437 | * @return bool |
| 2438 | */ |
| 2439 | public static function has_search_param(): bool { |
| 2440 | $key = self::get_search_param_name(); |
| 2441 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only URL presence check; the value is never read here. |
| 2442 | return isset( $_GET[ $key ] ) && is_scalar( $_GET[ $key ] ); |
| 2443 | } |
| 2444 | |
| 2445 | /** |
| 2446 | * Parse the sort order from the URL, defaulting to 'relevance'. Allowed |
| 2447 | * values track `Results_Sort::get_all_option_keys()` — on non-Woo sites |
| 2448 | * a `?orderby=price_asc` deep link collapses to `relevance` (mirrors |
| 2449 | * `store/url-state.js`). |
| 2450 | * |
| 2451 | * @return string |
| 2452 | */ |
| 2453 | protected static function parse_url_sort(): string { |
| 2454 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only URL state. |
| 2455 | $orderby = isset( $_GET['orderby'] ) ? sanitize_key( wp_unslash( $_GET['orderby'] ) ) : ''; |
| 2456 | $allowed = array_values( |
| 2457 | array_filter( |
| 2458 | Results_Sort::get_all_option_keys(), |
| 2459 | static function ( $key ) { |
| 2460 | return 'relevance' !== $key; |
| 2461 | } |
| 2462 | ) |
| 2463 | ); |
| 2464 | return in_array( $orderby, $allowed, true ) ? $orderby : 'relevance'; |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * Parse the price range from the URL. Mirrors `store/url-state.js`. |
| 2469 | * Either bound may be null for a half-open range; non-numeric or |
| 2470 | * negative values null out. Returns null entirely on non-Woo sites — |
| 2471 | * `min_price`/`max_price` are WC-only and a stray param shouldn't drive |
| 2472 | * the API into a `range` clause for a field the index doesn't have. |
| 2473 | * |
| 2474 | * @return array{min: float|null, max: float|null}|null |
| 2475 | */ |
| 2476 | protected static function parse_url_price_range(): ?array { |
| 2477 | if ( ! self::woocommerce_blocks_enabled() ) { |
| 2478 | return null; |
| 2479 | } |
| 2480 | // phpcs:disable WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- coerced to float in parse_price_bound(). |
| 2481 | $min = self::parse_price_bound( $_GET['min_price'] ?? null ); |
| 2482 | $max = self::parse_price_bound( $_GET['max_price'] ?? null ); |
| 2483 | // phpcs:enable |
| 2484 | |
| 2485 | if ( null === $min && null === $max ) { |
| 2486 | return null; |
| 2487 | } |
| 2488 | // Inverted bounds → empty ES `range` clause / zero results silently. |
| 2489 | // Treat as garbage and bail so the page falls back to unfiltered search. |
| 2490 | if ( null !== $min && null !== $max && $min > $max ) { |
| 2491 | return null; |
| 2492 | } |
| 2493 | return array( |
| 2494 | 'min' => $min, |
| 2495 | 'max' => $max, |
| 2496 | ); |
| 2497 | } |
| 2498 | |
| 2499 | /** |
| 2500 | * Coerce a single price-range URL value into a finite, non-negative float. |
| 2501 | * |
| 2502 | * @param mixed $raw Raw value pulled from $_GET. |
| 2503 | * @return float|null |
| 2504 | */ |
| 2505 | private static function parse_price_bound( $raw ): ?float { |
| 2506 | if ( null === $raw || '' === $raw || ! is_scalar( $raw ) ) { |
| 2507 | return null; |
| 2508 | } |
| 2509 | // `is_numeric` keeps PHP in lockstep with JS's `Number()`: rejects |
| 2510 | // partially-numeric strings ("1.5.3") that `(float)` would silently |
| 2511 | // extract as `1.5` while `Number()` returns `NaN`. |
| 2512 | $raw = wp_unslash( $raw ); |
| 2513 | if ( ! is_numeric( $raw ) ) { |
| 2514 | return null; |
| 2515 | } |
| 2516 | $num = (float) $raw; |
| 2517 | if ( ! is_finite( $num ) || $num < 0 ) { |
| 2518 | return null; |
| 2519 | } |
| 2520 | return $num; |
| 2521 | } |
| 2522 | |
| 2523 | /** |
| 2524 | * Parse `?<filterKey>[]=<value>` URL params into `{ [filterKey]: string[] }`. |
| 2525 | * Mirrors the shape `store/url-state.js` writes (see AGENTS.md § URL format). |
| 2526 | * No registered-key filtering here — `filterConfigs` aren't available until |
| 2527 | * blocks render. The JS layer gates on hydration. |
| 2528 | * |
| 2529 | * Scalar `?post_type=<slug>` is also accepted as a shortcut for |
| 2530 | * `?post_types[]=<slug>` — matches WP/WC's own URL convention. Merged into |
| 2531 | * any existing array selections so `?post_type=foo&post_types[]=bar` reads |
| 2532 | * as `[foo, bar]`. Singular-form-on-an-array-key keeps its existing |
| 2533 | * "ignored noise" behaviour for every other filter. |
| 2534 | * |
| 2535 | * @return array<string, string[]> |
| 2536 | */ |
| 2537 | protected static function parse_url_filters(): array { |
| 2538 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- read-only URL state; sanitized per-value below. |
| 2539 | $raw = wp_unslash( $_GET ); |
| 2540 | if ( ! is_array( $raw ) ) { |
| 2541 | return array(); |
| 2542 | } |
| 2543 | |
| 2544 | $out = array(); |
| 2545 | foreach ( $raw as $key => $values ) { |
| 2546 | $filter_key = sanitize_key( (string) $key ); |
| 2547 | if ( '' === $filter_key || in_array( $filter_key, self::RESERVED_QUERY_PARAMS, true ) ) { |
| 2548 | continue; |
| 2549 | } |
| 2550 | if ( 'post_type' === $filter_key ) { |
| 2551 | // `is_string` (not `is_scalar`) keeps the gate consistent with |
| 2552 | // `parse_url_filter_logic`'s value check — `$_GET` only ever |
| 2553 | // carries strings or arrays, and the array case takes the |
| 2554 | // `is_array( $values )` branch immediately below. |
| 2555 | if ( ! is_string( $values ) ) { |
| 2556 | continue; |
| 2557 | } |
| 2558 | // `sanitize_key`, not `sanitize_text_field` — post-type slugs are |
| 2559 | // always lowercase + `[a-z0-9_-]`; the lowercase pass keeps a |
| 2560 | // `?post_type=Product` URL from reaching ES with the wrong case |
| 2561 | // and silently returning zero results. |
| 2562 | $slug = sanitize_key( $values ); |
| 2563 | if ( '' === $slug ) { |
| 2564 | continue; |
| 2565 | } |
| 2566 | $existing = $out['post_types'] ?? array(); |
| 2567 | $out['post_types'] = array_values( array_unique( array_merge( $existing, array( $slug ) ) ) ); |
| 2568 | continue; |
| 2569 | } |
| 2570 | if ( ! is_array( $values ) ) { |
| 2571 | continue; |
| 2572 | } |
| 2573 | $clean = array_values( |
| 2574 | array_filter( |
| 2575 | array_map( 'sanitize_text_field', $values ), |
| 2576 | static function ( $v ) { |
| 2577 | return '' !== $v; |
| 2578 | } |
| 2579 | ) |
| 2580 | ); |
| 2581 | if ( $clean ) { |
| 2582 | $existing = $out[ $filter_key ] ?? array(); |
| 2583 | $out[ $filter_key ] = array_values( array_unique( array_merge( $existing, $clean ) ) ); |
| 2584 | } |
| 2585 | } |
| 2586 | return $out; |
| 2587 | } |
| 2588 | |
| 2589 | /** |
| 2590 | * Parse `?query_type_<key>=and` overrides into `{ [filterKey]: 'and' }`. |
| 2591 | * Only literal `'and'` is honoured — anything else is dropped so it |
| 2592 | * can't round-trip back through `pushStateToUrl`. Mirrors |
| 2593 | * `store/url-state.js`. |
| 2594 | * |
| 2595 | * @param array<string, string[]> $active_filters Result of parse_url_filters(). |
| 2596 | * @return array<string, string> |
| 2597 | */ |
| 2598 | protected static function parse_url_filter_logic( array $active_filters ): array { |
| 2599 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- read-only URL state; sanitized per-value below. |
| 2600 | $raw = wp_unslash( $_GET ); |
| 2601 | if ( ! is_array( $raw ) ) { |
| 2602 | return array(); |
| 2603 | } |
| 2604 | |
| 2605 | $out = array(); |
| 2606 | foreach ( $raw as $key => $value ) { |
| 2607 | if ( ! is_string( $key ) || 0 !== strpos( $key, 'query_type_' ) ) { |
| 2608 | continue; |
| 2609 | } |
| 2610 | if ( ! is_string( $value ) || 'and' !== $value ) { |
| 2611 | continue; |
| 2612 | } |
| 2613 | $filter_key = sanitize_key( substr( $key, strlen( 'query_type_' ) ) ); |
| 2614 | if ( '' === $filter_key || in_array( $filter_key, self::RESERVED_QUERY_PARAMS, true ) ) { |
| 2615 | continue; |
| 2616 | } |
| 2617 | if ( empty( $active_filters[ $filter_key ] ) ) { |
| 2618 | continue; |
| 2619 | } |
| 2620 | $out[ $filter_key ] = 'and'; |
| 2621 | } |
| 2622 | return $out; |
| 2623 | } |
| 2624 | } |