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