ComingSoonAdminBarBadge.php
1 month ago
ComingSoonCacheInvalidator.php
1 year ago
ComingSoonHelper.php
1 year ago
ComingSoonRequestHandler.php
2 weeks ago
ComingSoonRequestHandler.php
300 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Internal\ComingSoon; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\BlockTemplatesController; |
| 5 | use Automattic\WooCommerce\Blocks\BlockTemplatesRegistry; |
| 6 | use Automattic\WooCommerce\Blocks\Package as BlocksPackage; |
| 7 | use Automattic\Jetpack\Constants; |
| 8 | |
| 9 | /** |
| 10 | * Handles the template_include hook to determine whether the current page needs |
| 11 | * to be replaced with a coming soon screen. |
| 12 | */ |
| 13 | class ComingSoonRequestHandler { |
| 14 | |
| 15 | /** |
| 16 | * Coming soon helper. |
| 17 | * |
| 18 | * @var ComingSoonHelper |
| 19 | */ |
| 20 | private $coming_soon_helper = null; |
| 21 | |
| 22 | /** |
| 23 | * Whether the coming soon screen should be shown. Cache the result to avoid multiple calls to the helper. |
| 24 | * |
| 25 | * @var bool |
| 26 | */ |
| 27 | private static $show_coming_soon = false; |
| 28 | |
| 29 | /** |
| 30 | * Sets up the hook. |
| 31 | * |
| 32 | * @internal |
| 33 | * |
| 34 | * @param ComingSoonHelper $coming_soon_helper Dependency. |
| 35 | */ |
| 36 | final public function init( ComingSoonHelper $coming_soon_helper ) { |
| 37 | $this->coming_soon_helper = $coming_soon_helper; |
| 38 | // Hook into plugins_loaded to ensure features are initialized to determine coming soon status. |
| 39 | add_action( |
| 40 | 'plugins_loaded', |
| 41 | function () { |
| 42 | // Skip if the site is live. |
| 43 | if ( $this->coming_soon_helper->is_site_live() ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | add_filter( 'template_include', array( $this, 'handle_template_include' ) ); |
| 48 | add_filter( 'wp_theme_json_data_theme', array( $this, 'experimental_filter_theme_json_theme' ) ); |
| 49 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 50 | add_action( 'after_setup_theme', array( $this, 'possibly_init_block_templates' ), 999 ); |
| 51 | } |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Initializes block templates so we can show coming soon page in non-FSE themes. |
| 57 | */ |
| 58 | public function possibly_init_block_templates() { |
| 59 | // No need to initialize block templates since we've already initialized them in the Block Bootstrap. |
| 60 | if ( wp_is_block_theme() || current_theme_supports( 'block-template-parts' ) ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $container = BlocksPackage::container(); |
| 65 | $container->get( BlockTemplatesRegistry::class )->init(); |
| 66 | $container->get( BlockTemplatesController::class )->init(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Replaces the page template with a 'coming soon' when the site is in coming soon mode. |
| 71 | * |
| 72 | * @internal |
| 73 | * |
| 74 | * @param string $template The path to the previously determined template. |
| 75 | * @return string The path to the 'coming soon' template or any empty string to prevent further template loading in FSE themes. |
| 76 | */ |
| 77 | public function handle_template_include( $template ) { |
| 78 | if ( ! $this->should_show_coming_soon() ) { |
| 79 | return $template; |
| 80 | } |
| 81 | |
| 82 | // A coming soon page needs to be displayed. Set a short cache duration to prevents ddos attacks. |
| 83 | header( 'Cache-Control: max-age=60' ); |
| 84 | |
| 85 | $is_fse_theme = wp_is_block_theme(); |
| 86 | $is_store_coming_soon = $this->coming_soon_helper->is_store_coming_soon(); |
| 87 | add_theme_support( 'block-templates' ); |
| 88 | |
| 89 | $coming_soon_template = get_query_template( 'coming-soon' ); |
| 90 | |
| 91 | if ( ! $is_fse_theme && $is_store_coming_soon ) { |
| 92 | get_header(); |
| 93 | } |
| 94 | |
| 95 | add_action( |
| 96 | 'wp_head', |
| 97 | function () { |
| 98 | echo "<meta name='woo-coming-soon-page' content='yes'>"; |
| 99 | } |
| 100 | ); |
| 101 | |
| 102 | if ( ! empty( $coming_soon_template ) && file_exists( $coming_soon_template ) ) { |
| 103 | if ( ! $is_fse_theme && $is_store_coming_soon && function_exists( 'get_the_block_template_html' ) ) { |
| 104 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 105 | echo get_the_block_template_html(); |
| 106 | } else { |
| 107 | include $coming_soon_template; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | if ( ! $is_fse_theme && $is_store_coming_soon ) { |
| 112 | get_footer(); |
| 113 | } |
| 114 | |
| 115 | if ( $is_fse_theme ) { |
| 116 | // Since we've already rendered a template, return empty string to ensure no other template is rendered. |
| 117 | return ''; |
| 118 | } else { |
| 119 | // In non-FSE themes, other templates will still be rendered. |
| 120 | // We need to exit to prevent further processing. |
| 121 | exit(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Determines whether the coming soon screen should be shown. |
| 127 | * |
| 128 | * @return bool |
| 129 | */ |
| 130 | private function should_show_coming_soon() { |
| 131 | // Early exit if already determined that the coming soon screen should be shown. |
| 132 | if ( self::$show_coming_soon ) { |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // Early exit if the user is logged in as administrator / shop manager. |
| 137 | if ( current_user_can( 'manage_woocommerce' ) ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | // Do not show coming soon on 404 pages when applied to store pages only. |
| 142 | if ( $this->coming_soon_helper->is_store_coming_soon() && is_404() ) { |
| 143 | return false; |
| 144 | } |
| 145 | |
| 146 | // Early exit if the current page doesn't need a coming soon screen. |
| 147 | if ( ! $this->coming_soon_helper->is_current_page_coming_soon() ) { |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Check if there is an exclusion. |
| 153 | * |
| 154 | * @since 9.1.0 |
| 155 | * |
| 156 | * @param bool $is_excluded If the request should be excluded from Coming soon mode. Defaults to false. |
| 157 | */ |
| 158 | if ( apply_filters( 'woocommerce_coming_soon_exclude', false ) ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // Check if the private link option is enabled. |
| 163 | if ( get_option( 'woocommerce_private_link' ) === 'yes' ) { |
| 164 | // Exclude users with a private link. |
| 165 | if ( isset( $_GET['woo-share'] ) && get_option( 'woocommerce_share_key' ) === $_GET['woo-share'] ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 166 | // Persist the share link with a cookie for 90 days. |
| 167 | setcookie( 'woo-share', sanitize_text_field( wp_unslash( $_GET['woo-share'] ) ), time() + 60 * 60 * 24 * 90, '/' ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 168 | return false; |
| 169 | } |
| 170 | if ( isset( $_COOKIE['woo-share'] ) && get_option( 'woocommerce_share_key' ) === $_COOKIE['woo-share'] ) { |
| 171 | return false; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | self::$show_coming_soon = true; |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Filters the theme.json data to add Coming Soon fonts. |
| 181 | * This runs after child theme merging to ensure parent theme fonts are included. |
| 182 | * |
| 183 | * @param WP_Theme_JSON_Data $theme_json The theme json data object. |
| 184 | * @return WP_Theme_JSON_Data The filtered theme json data. |
| 185 | */ |
| 186 | public function experimental_filter_theme_json_theme( $theme_json ) { |
| 187 | $theme_data = $theme_json->get_data(); |
| 188 | $font_data = $theme_data['settings']['typography']['fontFamilies']['theme'] ?? array(); |
| 189 | |
| 190 | // Check if the current theme is a child theme. And if so, merge the parent theme fonts with the existing fonts. |
| 191 | if ( wp_get_theme()->parent() ) { |
| 192 | $parent_theme = wp_get_theme()->parent(); |
| 193 | $parent_theme_json_file = $parent_theme->get_file_path( 'theme.json' ); |
| 194 | |
| 195 | if ( is_readable( $parent_theme_json_file ) ) { |
| 196 | $parent_theme_json_data = json_decode( file_get_contents( $parent_theme_json_file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 197 | |
| 198 | if ( isset( $parent_theme_json_data['settings']['typography']['fontFamilies'] ) ) { |
| 199 | $parent_fonts = $parent_theme_json_data['settings']['typography']['fontFamilies']; |
| 200 | |
| 201 | // Merge parent theme fonts with existing fonts. |
| 202 | foreach ( $parent_fonts as $parent_font ) { |
| 203 | $found = false; |
| 204 | foreach ( $font_data as $existing_font ) { |
| 205 | if ( isset( $parent_font['name'] ) && isset( $existing_font['name'] ) && |
| 206 | $parent_font['name'] === $existing_font['name'] ) { |
| 207 | $found = true; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if ( ! $found ) { |
| 213 | $font_data[] = $parent_font; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | $fonts_to_add = array( |
| 221 | array( |
| 222 | 'fontFamily' => '"Inter", sans-serif', |
| 223 | 'name' => 'Inter', |
| 224 | 'slug' => 'inter', |
| 225 | 'fontFace' => array( |
| 226 | array( |
| 227 | 'fontFamily' => 'Inter', |
| 228 | 'fontStretch' => 'normal', |
| 229 | 'fontStyle' => 'normal', |
| 230 | 'fontWeight' => '300 900', |
| 231 | 'src' => array( WC()->plugin_url() . '/assets/fonts/Inter-VariableFont_slnt,wght.woff2' ), |
| 232 | ), |
| 233 | ), |
| 234 | ), |
| 235 | array( |
| 236 | 'fontFamily' => 'Cardo', |
| 237 | 'name' => 'Cardo', |
| 238 | 'slug' => 'cardo', |
| 239 | 'fontFace' => array( |
| 240 | array( |
| 241 | 'fontFamily' => 'Cardo', |
| 242 | 'fontStyle' => 'normal', |
| 243 | 'fontWeight' => '400', |
| 244 | 'src' => array( WC()->plugin_url() . '/assets/fonts/cardo_normal_400.woff2' ), |
| 245 | ), |
| 246 | ), |
| 247 | ), |
| 248 | ); |
| 249 | |
| 250 | // Add WooCommerce fonts if they don't already exist. |
| 251 | foreach ( $fonts_to_add as $font_to_add ) { |
| 252 | $found = false; |
| 253 | foreach ( $font_data as $font ) { |
| 254 | if ( isset( $font['name'] ) && $font['name'] === $font_to_add['name'] ) { |
| 255 | $found = true; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if ( ! $found ) { |
| 261 | $font_data[] = $font_to_add; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | $new_data = array( |
| 266 | 'version' => 1, |
| 267 | 'settings' => array( |
| 268 | 'typography' => array( |
| 269 | 'fontFamilies' => array( |
| 270 | 'theme' => $font_data, |
| 271 | ), |
| 272 | ), |
| 273 | ), |
| 274 | ); |
| 275 | $theme_json->update_with( $new_data ); |
| 276 | return $theme_json; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Enqueues the coming soon banner styles. |
| 281 | */ |
| 282 | public function enqueue_styles() { |
| 283 | // Early exit if the user is not logged in as administrator / shop manager. |
| 284 | if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | if ( $this->coming_soon_helper->is_site_live() ) { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | wp_enqueue_style( |
| 293 | 'woocommerce-coming-soon', |
| 294 | WC()->plugin_url() . '/assets/css/coming-soon' . ( is_rtl() ? '-rtl' : '' ) . '.css', |
| 295 | array(), |
| 296 | Constants::get_constant( 'WC_VERSION' ) |
| 297 | ); |
| 298 | } |
| 299 | } |
| 300 |