| 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 | setcookie( 'wp_share_code', $share_code, time() + 3600, '/', '', is_ssl(), true ); |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | $should_show = ( (int) get_option( 'wpcom_public_coming_soon' ) === 1 ); |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | if ( is_user_logged_in() && current_user_can( 'read' ) ) { |
| 37 | $should_show = false; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | return apply_filters( 'a8c_show_coming_soon_page', $should_show ); |
| 43 | } |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | function get_share_code() { |
| 51 | if ( isset( $_GET['share'] ) ) { |
| 52 | return sanitize_key( wp_unslash( $_GET['share'] ) ); |
| 53 | } elseif ( isset( $_COOKIE['wp_share_code'] ) ) { |
| 54 | return sanitize_key( wp_unslash( $_COOKIE['wp_share_code'] ) ); |
| 55 | } |
| 56 | return ''; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | function is_accessed_by_valid_share_link( $share_code ) { |
| 71 | $preview_links_option = get_option( 'wpcom_public_preview_links' ); |
| 72 | |
| 73 | if ( ! is_array( $preview_links_option ) || array() === $preview_links_option || ! $share_code ) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | if ( ! in_array( $share_code, array_column( $preview_links_option, 'code' ), true ) ) { |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | function maybe_add_x_robots_headers( $headers ) { |
| 91 | if ( get_share_code() ) { |
| 92 | $headers['X-Robots-Tag'] = 'noindex, nofollow'; |
| 93 | } |
| 94 | return $headers; |
| 95 | } |
| 96 | add_filter( 'wp_headers', __NAMESPACE__ . '\maybe_add_x_robots_headers' ); |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | function track_preview_link_event() { |
| 104 | $event_name = 'wpcom_site_previewed'; |
| 105 | if ( function_exists( 'wpcomsh_record_tracks_event' ) ) { |
| 106 | wpcomsh_record_tracks_event( $event_name, array() ); |
| 107 | } else { |
| 108 | require_lib( 'tracks/client' ); |
| 109 | tracks_record_event( get_current_user_id(), $event_name, array() ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | function coming_soon_share_image() { |
| 119 | return 'https://s1.wp.com/home.logged-out/images/wpcom-og-image.jpg'; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | function render_fallback_coming_soon_page() { |
| 126 | if ( ! defined( 'GRAVATAR_HOVERCARDS__DISABLE' ) ) { |
| 127 | define( 'GRAVATAR_HOVERCARDS__DISABLE', true ); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | remove_action( 'wp_enqueue_scripts', 'wpcom_actionbar_enqueue_scripts', 101 ); |
| 132 | add_filter( 'jetpack_action_bar_enabled', '__return_false' ); |
| 133 | remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); |
| 134 | remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 135 | remove_action( 'wp_head', 'header_js', 5 ); |
| 136 | remove_action( 'wp_head', 'global_css', 5 ); |
| 137 | remove_action( 'wp_footer', 'stats_footer', 101 ); |
| 138 | add_filter( 'infinite_scroll_archive_supported', '__return_false', 99 ); |
| 139 | add_filter( 'jetpack_disable_eu_cookie_law_widget', '__return_true', 1 ); |
| 140 | add_filter( 'wpcom_disable_logged_out_follow', '__return_true', 10, 1 ); |
| 141 | add_filter( 'wpl_is_enabled_sitewide', '__return_false', 10, 1 ); |
| 142 | add_filter( 'woocommerce_demo_store', '__return_false' ); |
| 143 | add_filter( 'jetpack_open_graph_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); |
| 144 | add_filter( 'jetpack_twitter_cards_image_default', __NAMESPACE__ . '\coming_soon_share_image' ); |
| 145 | wp_enqueue_style( 'recoleta-font', '//s1.wp.com/i/fonts/recoleta/css/400.min.css', array(), Jetpack_Mu_Wpcom::PACKAGE_VERSION ); |
| 146 | |
| 147 | include __DIR__ . '/fallback-coming-soon-page.php'; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | function add_public_coming_soon_to_settings_endpoint_get( $options ) { |
| 158 | $wpcom_public_coming_soon = (int) get_option( 'wpcom_public_coming_soon' ); |
| 159 | $options['wpcom_public_coming_soon'] = $wpcom_public_coming_soon; |
| 160 | |
| 161 | return $options; |
| 162 | } |
| 163 | add_filter( 'site_settings_endpoint_get', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_get' ); |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | function add_public_coming_soon_to_settings_endpoint_post( $input, $unfiltered_input ) { |
| 174 | if ( is_array( $unfiltered_input ) && array_key_exists( 'wpcom_public_coming_soon', $unfiltered_input ) ) { |
| 175 | $input['wpcom_public_coming_soon'] = (int) $unfiltered_input['wpcom_public_coming_soon']; |
| 176 | |
| 177 | |
| 178 | remove_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10 ); |
| 179 | } |
| 180 | return $input; |
| 181 | } |
| 182 | add_filter( 'rest_api_update_site_settings', __NAMESPACE__ . '\add_public_coming_soon_to_settings_endpoint_post', 10, 2 ); |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | function disable_coming_soon_on_privacy_change( $old_value, $value ) { |
| 194 | if ( 0 !== (int) $old_value || 0 === (int) $value ) { |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | update_option( 'wpcom_public_coming_soon', 0 ); |
| 199 | return true; |
| 200 | } |
| 201 | add_action( 'update_option_blog_public', __NAMESPACE__ . '\disable_coming_soon_on_privacy_change', 10, 2 ); |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | function add_option_to_new_site( $blog_id, $user_id, $domain, $path, $network_id, $meta ) { |
| 215 | if ( isset( $meta['public'] ) |
| 216 | && 0 === $meta['public'] |
| 217 | && isset( $meta['options']['wpcom_public_coming_soon'] ) |
| 218 | && 1 === (int) $meta['options']['wpcom_public_coming_soon'] |
| 219 | ) { |
| 220 | if ( function_exists( 'add_blog_option' ) ) { |
| 221 | add_blog_option( $blog_id, 'wpcom_public_coming_soon', 1 ); |
| 222 | } |
| 223 | return true; |
| 224 | } |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | add_action( 'wpmu_new_blog', __NAMESPACE__ . '\add_option_to_new_site', 10, 6 ); |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | function coming_soon_page( $template ) { |
| 237 | if ( ! should_show_coming_soon_page() ) { |
| 238 | return $template; |
| 239 | } |
| 240 | |
| 241 | render_fallback_coming_soon_page(); |
| 242 | die( 0 ); |
| 243 | } |
| 244 | add_filter( 'template_include', __NAMESPACE__ . '\coming_soon_page' ); |