| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace A8C\FSE\Coming_soon; |
| 9 | |
| 10 | use Automattic\Jetpack\Constants; |
| 11 | use Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 12 | |
| 13 | require_once __DIR__ . '/../../utils.php'; |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | function should_show_coming_soon_page() { |
| 21 | if ( ! is_singular() && ! is_archive() && ! is_search() && ! is_front_page() && ! is_home() && ! is_404() ) { |
| 22 | return false; |
| 23 | } |
| 24 | |
| 25 | $share_code = get_share_code(); |
| 26 | if ( is_accessed_by_valid_share_link( $share_code ) ) { |
| 27 | track_preview_link_event(); |
| 28 | setcookie( 'wp_share_code', $share_code, time() + 3600, '/', '', is_ssl(), true ); |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | $should_show = ( (int) get_option( 'wpcom_public_coming_soon' ) === 1 ); |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | if ( is_user_logged_in() && current_user_can( 'read' ) ) { |
| 38 | $should_show = false; |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | $should_show = apply_filters( 'a8c_show_coming_soon_page', $should_show ); |
| 44 | |
| 45 | if ( $should_show && is_unlaunched_big_sky_site_seen_from_a8c_proxy() ) { |
| 46 | |
| 47 | if ( ! defined( 'DONOTCACHEPAGE' ) ) { |
| 48 | define( 'DONOTCACHEPAGE', true ); |
| 49 | } |
| 50 | nocache_headers(); |
| 51 | add_action( 'wp_footer', __NAMESPACE__ . '\render_unlaunched_site_banner' ); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | return $should_show; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | function is_unlaunched_big_sky_site_seen_from_a8c_proxy() { |
| 67 | if ( ! Constants::is_true( 'AT_PROXIED_REQUEST' ) || ! is_woa_site() ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | if ( '1' !== (string) get_option( 'big_sky_enable' ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | return ! isset( $_GET['wpcom_show_coming_soon'] ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | function render_unlaunched_site_banner() { |
| 85 | $coming_soon_url = add_query_arg( 'wpcom_show_coming_soon', '1' ); |
| 86 | ?> |
| 87 | <div role="status" style="position:fixed;inset:auto 16px 16px 16px;z-index:100000;display:flex;gap:12px;align-items:center;justify-content:space-between;flex-wrap:wrap;padding:12px 16px;border-radius:8px;background:#1e1e1e;color:#fff;font:14px/1.4 -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;box-shadow:0 4px 16px rgba(0,0,0,.3);"> |
| 88 | <span> |
| 89 | <strong><?php esc_html_e( 'This site is not launched.', 'jetpack-mu-wpcom' ); ?></strong> |
| 90 | <?php esc_html_e( 'You see it because you are on the Automattic proxy. Visitors see Coming Soon.', 'jetpack-mu-wpcom' ); ?> |
| 91 | </span> |
| 92 | <a href="<?php echo esc_url( $coming_soon_url ); ?>" style="padding:6px 12px;border-radius:4px;background:#fff;color:#1e1e1e;text-decoration:none;font-weight:600;"><?php esc_html_e( 'See what visitors see', 'jetpack-mu-wpcom' ); ?></a> |
| 93 | </div> |
| 94 | <?php |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | function get_share_code() { |
| 103 | if ( isset( $_GET['share'] ) ) { |
| 104 | return sanitize_key( wp_unslash( $_GET['share'] ) ); |
| 105 | } elseif ( isset( $_COOKIE['wp_share_code'] ) ) { |
| 106 | return sanitize_key( wp_unslash( $_COOKIE['wp_share_code'] ) ); |
| 107 | } |
| 108 | return ''; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | function is_accessed_by_valid_share_link( $share_code ) { |
| 123 | $preview_links_option = get_option( 'wpcom_public_preview_links' ); |
| 124 | |
| 125 | if ( ! is_array( $preview_links_option ) || array() === $preview_links_option || ! $share_code ) { |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | if ( ! in_array( $share_code, array_column( $preview_links_option, 'code' ), true ) ) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | function maybe_add_x_robots_headers( $headers ) { |
| 143 | if ( get_share_code() ) { |
| 144 | $headers['X-Robots-Tag'] = 'noindex, nofollow'; |
| 145 | } |
| 146 | return $headers; |
| 147 | } |
| 148 | add_filter( 'wp_headers', __NAMESPACE__ . '\maybe_add_x_robots_headers' ); |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | function track_preview_link_event() { |
| 156 | $event_name = 'wpcom_site_previewed'; |
| 157 | if ( function_exists( 'wpcomsh_record_tracks_event' ) ) { |
| 158 | wpcomsh_record_tracks_event( $event_name, array() ); |
| 159 | } else { |
| 160 | require_lib( 'tracks/client' ); |
| 161 | tracks_record_event( get_current_user_id(), $event_name, array() ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | function coming_soon_share_image() { |
| 171 | return 'https://s1.wp.com/home.logged-out/images/wpcom-og-image.jpg'; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | function render_fallback_coming_soon_page() { |
| 178 | if ( ! defined( 'GRAVATAR_HOVERCARDS__DISABLE' ) ) { |
| 179 | define( 'GRAVATAR_HOVERCARDS__DISABLE', true ); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | remove_action( 'wp_enqueue_scripts', 'wpcom_actionbar_enqueue_scripts', 101 ); |
| 184 | add_filter( 'jetpack_action_bar_enabled', '__return_false' ); |
| 185 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 186 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 187 | remove_action( 'wp_head', 'header_js', 5 ); |
| 188 | remove_action( 'wp_head', 'global_css', 5 ); |
| 189 | remove_action( 'wp_footer', 'stats_footer', 101 ); |
| 190 | add_filter( 'infinite_scroll_archive_supported', '__return_false', 99 ); |
| 191 | add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true', 1 ); |
| 192 | add_filter( 'wpcom_disable_logged_out_follow', '__return_true', 10, 1 ); |
| 193 | add_filter( 'wpl_is_enabled_sitewide', '__return_false', 10, 1 ); |
| 194 | add_filter( 'woocommerce_demo_store', '__return_false' ); |
| 195 | add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); |
| 196 | add_filter( 'jetpack_twitter_cards_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); |
| 197 | wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Jetpack_Mu_Wpcom::PACKAGE_VERSION ); |
| 198 | |
| 199 | include __DIR__ . '/fallback-coming-soon-page.php'; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | function add_public_coming_soon_to_settings_endpoint_get( $options ) { |
| 210 | $wpcom_public_coming_soon = (int) get_option( 'wpcom_public_coming_soon' ); |
| 211 | $options['wpcom_public_coming_soon'] = $wpcom_public_coming_soon; |
| 212 | |
| 213 | return $options; |
| 214 | } |
| 215 | add_filter( 'site_settings_endpoint_get', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_get' ); |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | function add_public_coming_soon_to_settings_endpoint_post( $input, $unfiltered_input ) { |
| 226 | if ( is_array( $unfiltered_input ) && array_key_exists( 'wpcom_public_coming_soon', $unfiltered_input ) ) { |
| 227 | $input['wpcom_public_coming_soon'] = (int) $unfiltered_input['wpcom_public_coming_soon']; |
| 228 | |
| 229 | |
| 230 | remove_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10 ); |
| 231 | } |
| 232 | return $input; |
| 233 | } |
| 234 | add_filter( 'rest_api_update_site_settings', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_post', 10, 2 ); |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | function disable_coming_soon_on_privacy_change( $old_value, $value ) { |
| 246 | if ( 0 !== (int) $old_value || 0 === (int) $value ) { |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | update_option( 'wpcom_public_coming_soon', 0 ); |
| 251 | return true; |
| 252 | } |
| 253 | add_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10, 2 ); |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | function add_option_to_new_site( $blog_id, $user_id, $domain, $path, $network_id, $meta ) { |
| 267 | if ( isset( $meta['public'] ) |
| 268 | && 0 === $meta['public'] |
| 269 | && isset( $meta['options']['wpcom_public_coming_soon'] ) |
| 270 | && 1 === (int) $meta['options']['wpcom_public_coming_soon'] |
| 271 | ) { |
| 272 | if ( function_exists( 'add_blog_option' ) ) { |
| 273 | add_blog_option( $blog_id, 'wpcom_public_coming_soon', 1 ); |
| 274 | } |
| 275 | return true; |
| 276 | } |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | add_action( 'wpmu_new_blog', __NAMESPACE__ . '\add_option_to_new_site', 10, 6 ); |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | function coming_soon_page( $template ) { |
| 289 | if ( ! should_show_coming_soon_page() ) { |
| 290 | return $template; |
| 291 | } |
| 292 | |
| 293 | render_fallback_coming_soon_page(); |
| 294 | die( 0 ); |
| 295 | } |
| 296 | add_filter( 'template_include', __NAMESPACE__ . '\coming_soon_page' ); |