Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
32.22% covered (danger)
32.22%
29 / 90
41.67% covered (danger)
41.67%
5 / 12
CRAP
n/a
0 / 0
A8C\FSE\Coming_soon\should_show_coming_soon_page
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
132
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
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
5.12
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\Jetpack_Mu_Wpcom;
11
12require_once __DIR__ . '/../../utils.php';
13
14/**
15 * Determines whether the coming soon page should be shown.
16 *
17 * @return boolean
18 */
19function should_show_coming_soon_page() {
20    if ( ! is_singular() && ! is_archive() && ! is_search() && ! is_front_page() && ! is_home() && ! is_404() ) {
21        return false;
22    }
23
24    $share_code = get_share_code();
25    if ( is_accessed_by_valid_share_link( $share_code ) ) {
26        track_preview_link_event();
27        $secure = is_ssl();
28        // SameSite=None lets the share link keep working when the site is previewed in a cross-site iframe.
29        setcookie(
30            'wp_share_code',
31            $share_code,
32            array(
33                'expires'  => time() + 3600,
34                'path'     => '/',
35                'secure'   => $secure,
36                'httponly' => true,
37                'samesite' => $secure ? 'None' : 'Lax',
38            )
39        );
40        return false;
41    }
42
43    $should_show = ( (int) get_option( 'wpcom_public_coming_soon' ) === 1 );
44
45    // Everyone from Administrator to Subscriber will be able to see the site.
46    // See https://wordpress.org/support/article/roles-and-capabilities/ for all roles and capabilities.
47    // We can update to `edit_post` to be stricter, or open it up as an editable feature.
48    if ( is_user_logged_in() && current_user_can( 'read' ) ) {
49        $should_show = false;
50    }
51
52    // Allow folks to hook into this method to set their own rules.
53    // We'll use to on WordPress.com to check further user privileges.
54    return apply_filters( 'a8c_show_coming_soon_page', $should_show );
55}
56
57/**
58 * Gets share code from URL or Cookie.
59 *
60 * @return string
61 */
62function get_share_code() {
63    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.
64        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.
65    } elseif ( isset( $_COOKIE['wp_share_code'] ) ) {
66        return sanitize_key( wp_unslash( $_COOKIE['wp_share_code'] ) );
67    }
68    return '';
69}
70
71/**
72 * Determines whether the coming soon page should be bypassed.
73 *
74 * It checks if share code is provided as GET parameter, or as a cookie.
75 * Then it validates the code against blog option, and if sharing code is valid,
76 * it allows bypassing the Coming Soon page.
77 *
78 * @param string $share_code Share code to check against blog option value.
79 *
80 * @return bool
81 */
82function is_accessed_by_valid_share_link( $share_code ) {
83    $preview_links_option = get_option( 'wpcom_public_preview_links' );
84
85    if ( ! is_array( $preview_links_option ) || array() === $preview_links_option || ! $share_code ) {
86        return false;
87    }
88
89    if ( ! in_array( $share_code, array_column( $preview_links_option, 'code' ), true ) ) {
90        return false;
91    }
92
93    return true;
94}
95
96/**
97 * Add X-Robots-Tag header to prevent from indexing page that includes share code.
98 *
99 * @param array $headers Headers.
100 * @return array Headers.
101 */
102function maybe_add_x_robots_headers( $headers ) {
103    if ( get_share_code() ) {
104        $headers['X-Robots-Tag'] = 'noindex, nofollow';
105    }
106    return $headers;
107}
108add_filter( 'wp_headers', __NAMESPACE__ . '\maybe_add_x_robots_headers' );
109
110/**
111 * Track site preview event.
112 *
113 * @return void
114 */
115function track_preview_link_event() {
116    $event_name = 'wpcom_site_previewed';
117    if ( function_exists( 'wpcomsh_record_tracks_event' ) ) {
118        wpcomsh_record_tracks_event( $event_name, array() );
119    } else {
120        require_lib( 'tracks/client' );
121        tracks_record_event( get_current_user_id(), $event_name, array() );
122    }
123}
124
125/**
126 * Returns the WP.com logo
127 *
128 * @return string
129 */
130function coming_soon_share_image() {
131    return 'https://s1.wp.com/home.logged-out/images/wpcom-og-image.jpg';
132}
133
134/**
135 * Renders a fallback coming soon page
136 */
137function render_fallback_coming_soon_page() {
138    if ( ! defined( 'GRAVATAR_HOVERCARDS__DISABLE' ) ) {
139        define( 'GRAVATAR_HOVERCARDS__DISABLE', true );
140    }
141
142    // Disable WP scripts, likes, social og meta, cookie banner.
143    remove_action( 'wp_enqueue_scripts', 'wpcom_actionbar_enqueue_scripts', 101 );
144    add_filter( 'jetpack_action_bar_enabled', '__return_false' );
145    remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
146    remove_action( 'wp_print_styles', 'print_emoji_styles' );
147    remove_action( 'wp_head', 'header_js', 5 );
148    remove_action( 'wp_head', 'global_css', 5 );
149    remove_action( 'wp_footer', 'stats_footer', 101 );
150    add_filter( 'infinite_scroll_archive_supported', '__return_false', 99 ); // Disable infinite scroll feature.
151    add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true', 1 );
152    add_filter( 'wpcom_disable_logged_out_follow', '__return_true', 10, 1 ); // Disable follow actionbar.
153    add_filter( 'wpl_is_enabled_sitewide', '__return_false', 10, 1 ); // Disable likes.
154    add_filter( 'woocommerce_demo_store', '__return_false' ); // Prevent the the wocommerce demo store notice from displaying.
155    add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); // Set the default OG image.
156    add_filter( 'jetpack_twitter_cards_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); // Set the default Twitter Card image.
157    wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Jetpack_Mu_Wpcom::PACKAGE_VERSION );
158
159    include __DIR__ . '/fallback-coming-soon-page.php';
160}
161
162/**
163 * This filter makes sure it's possible to fetch `wpcom_public_coming_soon` option via public-api.wordpress.com.
164 *
165 * @param object $options array Retrieved site options, ready to be returned as API respomnse.
166 *
167 * @return array current value of `wpcom_public_coming_soon`
168 */
169function add_public_coming_soon_to_settings_endpoint_get( $options ) {
170    $wpcom_public_coming_soon            = (int) get_option( 'wpcom_public_coming_soon' );
171    $options['wpcom_public_coming_soon'] = $wpcom_public_coming_soon;
172
173    return $options;
174}
175add_filter( 'site_settings_endpoint_get', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_get' );
176
177/**
178 * This filter makes sure it's possible to change `wpcom_public_coming_soon` option via public-api.wordpress.com.
179 *
180 * @param object $input Filtered POST input.
181 * @param object $unfiltered_input Raw and unfiltered POST input.
182 *
183 * @return mixed
184 */
185function add_public_coming_soon_to_settings_endpoint_post( $input, $unfiltered_input ) {
186    if ( is_array( $unfiltered_input ) && array_key_exists( 'wpcom_public_coming_soon', $unfiltered_input ) ) {
187        $input['wpcom_public_coming_soon'] = (int) $unfiltered_input['wpcom_public_coming_soon'];
188
189        // Avoid updating the value of the `wpcom_public_coming_soon` if the request wants to change the option.
190        remove_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10 );
191    }
192    return $input;
193}
194add_filter( 'rest_api_update_site_settings', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_post', 10, 2 );
195
196/**
197 * Launch the site when the privacy mode changes from public-not-indexed
198 * This can happen due to clicking the launch button from the banner
199 * Or due to manually updating the setting in wp-admin or calypso settings page.
200 *
201 * @param  string $old_value the old value of blog_public.
202 * @param  string $value     the new value of blog_public.
203 * @return bool              whether an update occurred.
204 */
205function disable_coming_soon_on_privacy_change( $old_value, $value ) {
206    if ( 0 !== (int) $old_value || 0 === (int) $value ) {
207        // Do nothing if not moving from public-not-indexed.
208        return false;
209    }
210    update_option( 'wpcom_public_coming_soon', 0 );
211    return true;
212}
213add_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10, 2 );
214
215/**
216 * Adds the `wpcom_public_coming_soon` option to new sites  if requested by the client.
217 *
218 * @param int    $blog_id    Blog ID.
219 * @param int    $user_id    User ID.
220 * @param string $domain     Site domain.
221 * @param string $path       Site path.
222 * @param int    $network_id Network ID. Only relevant on multi-network installations.
223 * @param array  $meta       Meta data. Used to set initial site options.
224 * @return bool              whether an update occurred.
225 */
226function add_option_to_new_site( $blog_id, $user_id, $domain, $path, $network_id, $meta ) {
227    if ( isset( $meta['public'] )
228        && 0 === $meta['public']
229        && isset( $meta['options']['wpcom_public_coming_soon'] )
230        && 1 === (int) $meta['options']['wpcom_public_coming_soon']
231    ) {
232        if ( function_exists( 'add_blog_option' ) ) {
233            add_blog_option( $blog_id, 'wpcom_public_coming_soon', 1 );
234        }
235        return true;
236    }
237    return false;
238}
239
240add_action( 'wpmu_new_blog', __NAMESPACE__ . '\add_option_to_new_site', 10, 6 );
241
242/**
243 * Decides whether to render to the site's coming soon page and performs
244 * the render.
245 *
246 * @param string $template The template to render.
247 */
248function coming_soon_page( $template ) {
249    if ( ! should_show_coming_soon_page() ) {
250        return $template;
251    }
252
253    render_fallback_coming_soon_page();
254    die( 0 );
255}
256add_filter( 'template_include', __NAMESPACE__ . '\coming_soon_page' );