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.2.0 10.0.1 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3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.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Blocks / Domain / BlockRegistrationContext.php
woocommerce / src / Blocks / Domain Last commit date
Services 2 weeks ago BlockRegistrationContext.php 2 weeks ago Bootstrap.php 2 weeks ago Package.php 1 year ago
BlockRegistrationContext.php
229 lines
1 <?php
2 /**
3 * BlockRegistrationContext class.
4 */
5
6 declare( strict_types = 1 );
7
8 namespace Automattic\WooCommerce\Blocks\Domain;
9
10 /**
11 * Decides whether WooCommerce block types and patterns should be registered for the current request.
12 *
13 * Runs during bootstrap on `plugins_loaded`, before the main query is parsed, so it inspects only $_SERVER,
14 * $_GET and constants set before wp-load — not query-dependent helpers such as is_favicon()/is_robots().
15 *
16 * @internal
17 *
18 * @since 11.1.0
19 */
20 class BlockRegistrationContext {
21
22 /**
23 * Whether block types and patterns should be registered for the current request.
24 *
25 * @return bool True unless the request is a known non-rendering context.
26 */
27 public function should_register(): bool {
28 /**
29 * Filters whether WooCommerce should register its block types and patterns for the current request.
30 *
31 * Registration is skipped on known non-rendering contexts (the Store API and other WooCommerce REST
32 * namespaces, cron, AJAX, XML-RPC, favicon, robots.txt and XML sitemaps) as a performance optimisation.
33 * Product and variation descriptions rendered through do_blocks are already handled on demand (see the
34 * woocommerce_short_description hook in Bootstrap), so this filter is only needed to opt back in when an
35 * extension renders WooCommerce blocks some other way in one of those contexts.
36 *
37 * @since 11.1.0
38 *
39 * @param bool $should_register Whether block types and patterns should be registered for this request.
40 */
41 return (bool) apply_filters( 'woocommerce_should_register_blocks', $this->is_rendering_request() );
42 }
43
44 /**
45 * Whether the current request may render or edit blocks.
46 *
47 * Blacklist of known non-rendering contexts: an unrecognised request keeps registering (the previous
48 * behaviour), so a missed case costs a little performance but never a rendering regression. Front-end,
49 * admin and wp/v2 (block/site editor) requests therefore keep registering.
50 *
51 * @return bool True unless the request is a known non-rendering context.
52 */
53 private function is_rendering_request(): bool {
54 // The Store API returns data, not rendered pages; description blocks are registered on demand instead
55 // (see the woocommerce_short_description hook in Bootstrap).
56 if ( wc()->is_store_api_request() ) {
57 return false;
58 }
59
60 // Cron produces no output.
61 if ( wp_doing_cron() ) {
62 return false;
63 }
64
65 // AJAX (admin-ajax and wc-ajax) renders no blocks. wc-ajax's constants are set too late to use here, so
66 // detect it from the request; ! empty() matches WC_AJAX::set_wc_ajax_argument_in_query().
67 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the endpoint only, no state change.
68 if ( wp_doing_ajax() || ! empty( $_GET['wc-ajax'] ) ) {
69 return false;
70 }
71
72 // XML-RPC renders no blocks; its constant is set before wp-load, so it is reliable here.
73 if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
74 return false;
75 }
76
77 // Favicon, robots.txt and XML sitemaps render no blocks.
78 if ( $this->is_non_rendering_path_request() ) {
79 return false;
80 }
81
82 // WooCommerce REST namespaces render no blocks (Store API handled above). wp/v2 is left registering for
83 // the block and site editors.
84 if ( $this->is_woocommerce_rest_request() ) {
85 return false;
86 }
87
88 // WooCommerce's own admin pages (Settings, Status, Analytics, Orders, ...) render no blocks. Core admin
89 // screens and the block/site editor are intentionally left registering.
90 if ( $this->is_woocommerce_admin_page() ) {
91 return false;
92 }
93
94 return true;
95 }
96
97 /**
98 * Whether the request targets a WooCommerce-owned admin page (admin.php?page=wc-*).
99 *
100 * These are WooCommerce's own settings/status/analytics screens, which render no blocks. Only WooCommerce's
101 * own pages are matched; core admin screens and the block/site editor are left registering. $pagenow is set
102 * in wp-includes/vars.php before the plugins_loaded action, so it is available here.
103 *
104 * @return bool
105 */
106 private function is_woocommerce_admin_page(): bool {
107 if ( ! is_admin() ) {
108 return false;
109 }
110
111 global $pagenow;
112 if ( 'admin.php' !== $pagenow ) {
113 return false;
114 }
115
116 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the page slug only, no state change.
117 if ( ! isset( $_GET['page'] ) || ! is_string( $_GET['page'] ) ) {
118 return false;
119 }
120
121 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the page slug only, no state change.
122 $page = sanitize_key( wp_unslash( $_GET['page'] ) );
123
124 // WooCommerce-owned admin page slugs. The WooCommerce Admin SPA (home, analytics, marketing) all use the
125 // wc-admin slug with a path query parameter.
126 $woocommerce_pages = array(
127 'wc-admin',
128 'wc-settings',
129 'wc-orders',
130 'wc-reports',
131 'wc-status',
132 'wc-addons',
133 );
134
135 return in_array( $page, $woocommerce_pages, true );
136 }
137
138 /**
139 * Whether the request targets a WordPress endpoint that renders no block content: the favicon, robots.txt or
140 * core XML sitemaps. The URI path is inspected directly because is_favicon()/is_robots() are unavailable this
141 * early. WP core serves these in wp-includes/template-loader.php and the WP_Sitemaps class.
142 *
143 * @return bool
144 */
145 private function is_non_rendering_path_request(): bool {
146 if ( empty( $_SERVER['REQUEST_URI'] ) ) {
147 return false;
148 }
149 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
150 $path = wp_parse_url( '/' . ltrim( (string) wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ), PHP_URL_PATH );
151 if ( ! is_string( $path ) ) {
152 return false;
153 }
154
155 // Favicon, e.g. /favicon.ico (also matches subdirectory installs).
156 if ( '/favicon.ico' === substr( $path, -12 ) ) {
157 return true;
158 }
159
160 // robots.txt.
161 if ( '/robots.txt' === substr( $path, -11 ) ) {
162 return true;
163 }
164
165 // Core XML sitemaps, e.g. /wp-sitemap.xml or /wp-sitemap.xsl. The suffix check avoids matching a page
166 // slug that merely contains "wp-sitemap".
167 if ( false !== strpos( $path, 'wp-sitemap' ) && ( '.xml' === substr( $path, -4 ) || '.xsl' === substr( $path, -4 ) ) ) {
168 return true;
169 }
170
171 return false;
172 }
173
174 /**
175 * Whether the request targets a WooCommerce-owned REST namespace other than the Store API, in either pretty
176 * (/wp-json/<namespace>) or plain (?rest_route=/<namespace>) permalink form.
177 *
178 * @return bool
179 */
180 private function is_woocommerce_rest_request(): bool {
181 if ( empty( $_SERVER['REQUEST_URI'] ) ) {
182 return false;
183 }
184
185 // WooCommerce-owned namespaces that render no blocks; mirrors wc_rest_should_load_namespace() (add new
186 // versions here too). Store API (wc/store) is handled above; the trailing slash prevents matching a
187 // longer, unrelated namespace.
188 $namespaces = array(
189 'wc/v1/',
190 'wc/v2/',
191 'wc/v3/',
192 'wc/v4/',
193 'wc/private/',
194 'wc-admin/',
195 'wc-analytics/',
196 'wc-telemetry/',
197 );
198
199 // Match against the path only (a leading slash anchors the prefix) so a REST-like query argument such as
200 // /some-page/?arg=/wp-json/wc/v3 is not mistaken for a REST request.
201 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
202 $path = wp_parse_url( '/' . ltrim( (string) wp_unslash( $_SERVER['REQUEST_URI'] ), '/' ), PHP_URL_PATH );
203 $rest_prefix = '/' . trailingslashit( rest_get_url_prefix() );
204
205 // Pretty permalinks: /wp-json/<namespace>...
206 if ( is_string( $path ) ) {
207 foreach ( $namespaces as $namespace ) {
208 if ( false !== strpos( $path, $rest_prefix . $namespace ) ) {
209 return true;
210 }
211 }
212 }
213
214 // Plain permalinks: ?rest_route=/<namespace>...
215 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the route only, no state change.
216 if ( isset( $_GET['rest_route'] ) && is_string( $_GET['rest_route'] ) ) {
217 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading the route only, no state change.
218 $rest_route = '/' . ltrim( rawurldecode( sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ) ), '/' );
219 foreach ( $namespaces as $namespace ) {
220 if ( 0 === strpos( $rest_route, '/' . $namespace ) ) {
221 return true;
222 }
223 }
224 }
225
226 return false;
227 }
228 }
229