Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.12% covered (warning)
56.12%
55 / 98
57.14% covered (warning)
57.14%
8 / 14
CRAP
n/a
0 / 0
A8C\FSE\Coming_soon\should_show_coming_soon_page
72.22% covered (warning)
72.22%
13 / 18
0.00% covered (danger)
0.00%
0 / 1
16.62
A8C\FSE\Coming_soon\is_unlaunched_big_sky_site_seen_from_a8c_proxy
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
A8C\FSE\Coming_soon\render_unlaunched_site_banner
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
A8C\FSE\Coming_soon\get_share_code
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
A8C\FSE\Coming_soon\is_accessed_by_valid_share_link
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
A8C\FSE\Coming_soon\maybe_add_x_robots_headers
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
A8C\FSE\Coming_soon\track_preview_link_event
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
A8C\FSE\Coming_soon\coming_soon_share_image
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
A8C\FSE\Coming_soon\render_fallback_coming_soon_page
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
6
A8C\FSE\Coming_soon\add_public_coming_soon_to_settings_endpoint_get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
A8C\FSE\Coming_soon\add_public_coming_soon_to_settings_endpoint_post
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
A8C\FSE\Coming_soon\disable_coming_soon_on_privacy_change
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
A8C\FSE\Coming_soon\add_option_to_new_site
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
A8C\FSE\Coming_soon\coming_soon_page
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Coming Soon
4 *
5 * @package A8C\FSE\Coming_soon
6 */
7
8namespace A8C\FSE\Coming_soon;
9
10use Automattic\Jetpack\Constants;
11use Automattic\Jetpack\Jetpack_Mu_Wpcom;
12
13require_once __DIR__ . '/../../utils.php';
14
15/**
16 * Determines whether the coming soon page should be shown.
17 *
18 * @return boolean
19 */
20function 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    // Everyone from Administrator to Subscriber will be able to see the site.
35    // See https://wordpress.org/support/article/roles-and-capabilities/ for all roles and capabilities.
36    // We can update to `edit_post` to be stricter, or open it up as an editable feature.
37    if ( is_user_logged_in() && current_user_can( 'read' ) ) {
38        $should_show = false;
39    }
40
41    // Allow folks to hook into this method to set their own rules.
42    // We'll use to on WordPress.com to check further user privileges.
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        // A cached copy would show the unlaunched site to regular visitors.
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 * Whether an Automattician on the a8c proxy is opening an unlaunched Big Sky site on WordPress.com on Atomic.
60 *
61 * Lets us look at the Big Sky builds customers never launched, without a support session.
62 * WordPress.com writes `big_sky_enable` when it turns Big Sky on, so a missing option means no.
63 *
64 * @return bool
65 */
66function 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    // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only switch back to the Coming Soon page.
76    return ! isset( $_GET['wpcom_show_coming_soon'] );
77}
78
79/**
80 * Render the banner that tells an Automattician the site is not launched.
81 *
82 * @return void
83 */
84function 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 * Gets share code from URL or Cookie.
99 *
100 * @return string
101 */
102function get_share_code() {
103    if ( isset( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No data written to site, used in conditionally displaying coming-soon page and adding robots headers.
104        return sanitize_key( wp_unslash( $_GET['share'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- No data written to site, used in conditionally displaying coming-soon page and adding robots headers.
105    } elseif ( isset( $_COOKIE['wp_share_code'] ) ) {
106        return sanitize_key( wp_unslash( $_COOKIE['wp_share_code'] ) );
107    }
108    return '';
109}
110
111/**
112 * Determines whether the coming soon page should be bypassed.
113 *
114 * It checks if share code is provided as GET parameter, or as a cookie.
115 * Then it validates the code against blog option, and if sharing code is valid,
116 * it allows bypassing the Coming Soon page.
117 *
118 * @param string $share_code Share code to check against blog option value.
119 *
120 * @return bool
121 */
122function 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 * Add X-Robots-Tag header to prevent from indexing page that includes share code.
138 *
139 * @param array $headers Headers.
140 * @return array Headers.
141 */
142function maybe_add_x_robots_headers( $headers ) {
143    if ( get_share_code() ) {
144        $headers['X-Robots-Tag'] = 'noindex, nofollow';
145    }
146    return $headers;
147}
148add_filter( 'wp_headers', __NAMESPACE__ . '\maybe_add_x_robots_headers' );
149
150/**
151 * Track site preview event.
152 *
153 * @return void
154 */
155function 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 * Returns the WP.com logo
167 *
168 * @return string
169 */
170function coming_soon_share_image() {
171    return 'https://s1.wp.com/home.logged-out/images/wpcom-og-image.jpg';
172}
173
174/**
175 * Renders a fallback coming soon page
176 */
177function render_fallback_coming_soon_page() {
178    if ( ! defined( 'GRAVATAR_HOVERCARDS__DISABLE' ) ) {
179        define( 'GRAVATAR_HOVERCARDS__DISABLE', true );
180    }
181
182    // Disable WP scripts, likes, social og meta, cookie banner.
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 ); // Disable infinite scroll feature.
191    add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true', 1 );
192    add_filter( 'wpcom_disable_logged_out_follow', '__return_true', 10, 1 ); // Disable follow actionbar.
193    add_filter( 'wpl_is_enabled_sitewide', '__return_false', 10, 1 ); // Disable likes.
194    add_filter( 'woocommerce_demo_store', '__return_false' ); // Prevent the the wocommerce demo store notice from displaying.
195    add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); // Set the default OG image.
196    add_filter( 'jetpack_twitter_cards_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); // Set the default Twitter Card 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 * This filter makes sure it's possible to fetch `wpcom_public_coming_soon` option via public-api.wordpress.com.
204 *
205 * @param object $options array Retrieved site options, ready to be returned as API respomnse.
206 *
207 * @return array current value of `wpcom_public_coming_soon`
208 */
209function 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}
215add_filter( 'site_settings_endpoint_get', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_get' );
216
217/**
218 * This filter makes sure it's possible to change `wpcom_public_coming_soon` option via public-api.wordpress.com.
219 *
220 * @param object $input Filtered POST input.
221 * @param object $unfiltered_input Raw and unfiltered POST input.
222 *
223 * @return mixed
224 */
225function 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        // Avoid updating the value of the `wpcom_public_coming_soon` if the request wants to change the option.
230        remove_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10 );
231    }
232    return $input;
233}
234add_filter( 'rest_api_update_site_settings', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_post', 10, 2 );
235
236/**
237 * Launch the site when the privacy mode changes from public-not-indexed
238 * This can happen due to clicking the launch button from the banner
239 * Or due to manually updating the setting in wp-admin or calypso settings page.
240 *
241 * @param  string $old_value the old value of blog_public.
242 * @param  string $value     the new value of blog_public.
243 * @return bool              whether an update occurred.
244 */
245function disable_coming_soon_on_privacy_change( $old_value, $value ) {
246    if ( 0 !== (int) $old_value || 0 === (int) $value ) {
247        // Do nothing if not moving from public-not-indexed.
248        return false;
249    }
250    update_option( 'wpcom_public_coming_soon', 0 );
251    return true;
252}
253add_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10, 2 );
254
255/**
256 * Adds the `wpcom_public_coming_soon` option to new sites  if requested by the client.
257 *
258 * @param int    $blog_id    Blog ID.
259 * @param int    $user_id    User ID.
260 * @param string $domain     Site domain.
261 * @param string $path       Site path.
262 * @param int    $network_id Network ID. Only relevant on multi-network installations.
263 * @param array  $meta       Meta data. Used to set initial site options.
264 * @return bool              whether an update occurred.
265 */
266function 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
280add_action( 'wpmu_new_blog', __NAMESPACE__ . '\add_option_to_new_site', 10, 6 );
281
282/**
283 * Decides whether to render to the site's coming soon page and performs
284 * the render.
285 *
286 * @param string $template The template to render.
287 */
288function 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}
296add_filter( 'template_include', __NAMESPACE__ . '\coming_soon_page' );