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 |