| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | namespace A8C\FSE\Coming_soon; |
| 9 | |
| 10 | use Automattic\Jetpack\Jetpack_Mu_Wpcom; |
| 11 | |
| 12 | require_once __DIR__ . '/../../utils.php'; |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | function 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 | |
| 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 | |
| 46 | |
| 47 | |
| 48 | if ( is_user_logged_in() && current_user_can( 'read' ) ) { |
| 49 | $should_show = false; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | return apply_filters( 'a8c_show_coming_soon_page', $should_show ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | function get_share_code() { |
| 63 | if ( isset( $_GET['share'] ) ) { |
| 64 | return sanitize_key( wp_unslash( $_GET['share'] ) ); |
| 65 | } elseif ( isset( $_COOKIE['wp_share_code'] ) ) { |
| 66 | return sanitize_key( wp_unslash( $_COOKIE['wp_share_code'] ) ); |
| 67 | } |
| 68 | return ''; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | function 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 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | function maybe_add_x_robots_headers( $headers ) { |
| 103 | if ( get_share_code() ) { |
| 104 | $headers['X-Robots-Tag'] = 'noindex, nofollow'; |
| 105 | } |
| 106 | return $headers; |
| 107 | } |
| 108 | add_filter( 'wp_headers', __NAMESPACE__ . '\maybe_add_x_robots_headers' ); |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | function 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 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | function coming_soon_share_image() { |
| 131 | return 'https://s1.wp.com/home.logged-out/images/wpcom-og-image.jpg'; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | function render_fallback_coming_soon_page() { |
| 138 | if ( ! defined( 'GRAVATAR_HOVERCARDS__DISABLE' ) ) { |
| 139 | define( 'GRAVATAR_HOVERCARDS__DISABLE', true ); |
| 140 | } |
| 141 | |
| 142 | |
| 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 ); |
| 151 | add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true', 1 ); |
| 152 | add_filter( 'wpcom_disable_logged_out_follow', '__return_true', 10, 1 ); |
| 153 | add_filter( 'wpl_is_enabled_sitewide', '__return_false', 10, 1 ); |
| 154 | add_filter( 'woocommerce_demo_store', '__return_false' ); |
| 155 | add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); |
| 156 | add_filter( 'jetpack_twitter_cards_image_default', __NAMESPACE__ . '\coming_soon_share_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 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | function 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 | } |
| 175 | add_filter( 'site_settings_endpoint_get', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_get' ); |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | function 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 | |
| 190 | remove_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10 ); |
| 191 | } |
| 192 | return $input; |
| 193 | } |
| 194 | add_filter( 'rest_api_update_site_settings', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_post', 10, 2 ); |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | function disable_coming_soon_on_privacy_change( $old_value, $value ) { |
| 206 | if ( 0 !== (int) $old_value || 0 === (int) $value ) { |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | update_option( 'wpcom_public_coming_soon', 0 ); |
| 211 | return true; |
| 212 | } |
| 213 | add_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10, 2 ); |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | function 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 | |
| 240 | add_action( 'wpmu_new_blog', __NAMESPACE__ . '\add_option_to_new_site', 10, 6 ); |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | function 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 | } |
| 256 | add_filter( 'template_include', __NAMESPACE__ . '\coming_soon_page' ); |