| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | namespace Private_Site; |
| 10 | |
| 11 | use Automattic\Jetpack\Connection\Rest_Authentication; |
| 12 | use Jetpack; |
| 13 | use WP_Error; |
| 14 | use WP_REST_Request; |
| 15 | use function esc_html_e; |
| 16 | use function get_current_blog_id; |
| 17 | use function get_option; |
| 18 | use function remove_filter; |
| 19 | use function status_header; |
| 20 | use function wp_get_current_user; |
| 21 | use function wp_send_json_error; |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | const DISABLED_JETPACK_MODULES_WHEN_PRIVATE = array( |
| 31 | 'publicize', |
| 32 | 'sharedaddy', |
| 33 | 'json-api', |
| 34 | 'enhanced-distribution', |
| 35 | 'google-analytics', |
| 36 | 'photon', |
| 37 | 'photon-cdn', |
| 38 | 'sitemaps', |
| 39 | 'verification-tools', |
| 40 | 'wordads', |
| 41 | ); |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | function is_module_active() { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | function admin_init() { |
| 56 | if ( ! is_module_active() ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | if ( ( is_jetpack_connected() || site_is_private() ) && should_update_privacy_selector() ) { |
| 67 | |
| 68 | add_filter( 'allowed_options', '\Private_Site\remove_privacy_option_from_whitelist' ); |
| 69 | } |
| 70 | |
| 71 | if ( should_override_editor_with_classic_editor() ) { |
| 72 | |
| 73 | add_action( 'load-post.php', '\Private_Site\use_classic_editor_if_requested', - 1000 ); |
| 74 | add_action( 'load-post-new.php', '\Private_Site\use_classic_editor_if_requested', - 1000 ); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | if ( site_is_private() && should_prevent_site_access() && ! is_jetpack_admin_ajax_request() ) { |
| 79 | send_access_denied_error_response(); |
| 80 | } |
| 81 | } |
| 82 | add_action( 'admin_init', '\Private_Site\admin_init' ); |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | function init() { |
| 88 | if ( ! is_module_active() ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | add_filter( 'rest_api_update_site_settings', '\Private_Site\cache_option_on_update_site_settings', 10, 2 ); |
| 94 | |
| 95 | if ( ! site_is_private() ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | add_action( 'parse_request', '\Private_Site\parse_request', 100 ); |
| 101 | |
| 102 | |
| 103 | add_filter( 'rest_dispatch_request', '\Private_Site\rest_dispatch_request', 10, 3 ); |
| 104 | |
| 105 | |
| 106 | add_action( 'wp_head', '\Private_Site\private_no_pinning' ); |
| 107 | |
| 108 | |
| 109 | add_action( 'opml_head', '\Private_Site\hide_opml' ); |
| 110 | |
| 111 | |
| 112 | add_filter( 'bloginfo', '\Private_Site\mask_site_name', 3, 2 ); |
| 113 | |
| 114 | |
| 115 | add_filter( 'preprocess_comment', '\Private_Site\preprocess_comment', 0 ); |
| 116 | |
| 117 | |
| 118 | add_filter( 'robots_txt', '\Private_Site\private_robots_txt' ); |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | } |
| 124 | add_action( 'init', '\Private_Site\init' ); |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | function muplugins_loaded() { |
| 130 | if ( ! is_module_active() ) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if ( ! site_is_private() ) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | add_filter( 'xmlrpc_methods', '\Private_Site\xmlrpc_methods_limit_to_allowed_list' ); |
| 140 | |
| 141 | |
| 142 | add_filter( 'jetpack_xmlrpc_methods', '\Private_Site\register_additional_jetpack_xmlrpc_methods' ); |
| 143 | |
| 144 | |
| 145 | add_action( 'jetpack_sync_before_send_queue_full_sync', '\Private_Site\remove_mask_site_name_filter' ); |
| 146 | add_action( 'jetpack_sync_before_send_queue_sync', '\Private_Site\remove_mask_site_name_filter' ); |
| 147 | |
| 148 | |
| 149 | add_filter( 'jetpack_active_modules', '\Private_Site\filter_jetpack_active_modules' ); |
| 150 | add_filter( 'jetpack_get_available_modules', '\Private_Site\filter_jetpack_get_available_modules' ); |
| 151 | add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' ); |
| 152 | } |
| 153 | add_action( 'muplugins_loaded', '\Private_Site\muplugins_loaded' ); |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | function fetch_option_from_wpcom( $option ) { |
| 163 | if ( ! is_jetpack_connected() ) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | $jetpack = Jetpack::init(); |
| 168 | if ( ! method_exists( $jetpack, 'get_cloud_site_options' ) ) { |
| 169 | return false; |
| 170 | } |
| 171 | $options = $jetpack->get_cloud_site_options( array( $option ) ); |
| 172 | |
| 173 | return $options[ $option ] ?? false; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | function site_is_coming_soon(): bool { |
| 186 | if ( ! site_is_private() ) { |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | $wpcom_coming_soon = wp_cache_get( 'wpcom_coming_soon', 'wpcomsh' ); |
| 191 | |
| 192 | if ( false === $wpcom_coming_soon ) { |
| 193 | $wpcom_coming_soon = (int) fetch_option_from_wpcom( 'wpcom_coming_soon' ); |
| 194 | wp_cache_set( 'wpcom_coming_soon', $wpcom_coming_soon, 'wpcomsh' ); |
| 195 | } |
| 196 | |
| 197 | return (bool) $wpcom_coming_soon; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | function site_is_public_coming_soon(): bool { |
| 212 | if ( site_is_private() ) { |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | return 1 === (int) get_option( 'wpcom_public_coming_soon' ); |
| 217 | } |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | function site_launch_status(): string { |
| 225 | |
| 226 | if ( ! site_is_private() && ! site_is_public_coming_soon() ) { |
| 227 | return ''; |
| 228 | } |
| 229 | |
| 230 | $launch_status = wp_cache_get( 'wpcom_launch_status', 'wpcomsh' ); |
| 231 | |
| 232 | if ( ! $launch_status ) { |
| 233 | $launch_status = (string) fetch_option_from_wpcom( 'launch-status' ); |
| 234 | wp_cache_set( 'wpcom_launch_status', $launch_status, 'wpcomsh' ); |
| 235 | } |
| 236 | |
| 237 | return (string) $launch_status; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | function is_launched() { |
| 246 | return 'launched' === site_launch_status(); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | function site_is_private() { |
| 260 | return defined( 'AT_PRIVACY_MODEL' ) && AT_PRIVACY_MODEL === 'wp_uploads'; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | function should_prevent_site_access() { |
| 274 | static $cached; |
| 275 | |
| 276 | if ( isset( $cached ) ) { |
| 277 | return $cached; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | if ( class_exists( 'Automattic\Jetpack\Connection\Rest_Authentication' ) && Rest_Authentication::is_signed_with_blog_token() ) { |
| 285 | return $cached = false; |
| 286 | } |
| 287 | |
| 288 | if ( |
| 289 | ( defined( 'WP_CLI' ) && WP_CLI ) || |
| 290 | ( defined( 'WP_IMPORTING' ) && WP_IMPORTING ) |
| 291 | ) { |
| 292 | |
| 293 | return $cached = false; |
| 294 | } |
| 295 | |
| 296 | return $cached = ! is_private_blog_user(); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | function register_additional_jetpack_xmlrpc_methods( $methods ) { |
| 307 | return array_merge( |
| 308 | $methods, |
| 309 | array( |
| 310 | 'jetpack.getClosestThumbnailSizeUrl' => '\Private_Site\get_closest_thumbnail_size_url', |
| 311 | 'jetpack.getReadAccessCookies' => '\Private_Site\get_read_access_cookies', |
| 312 | ) |
| 313 | ); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | function get_closest_thumbnail_size_url( $args ) { |
| 324 | if ( ! isset( $args['url'] ) || ! isset( $args['width'] ) || ! isset( $args['height'] ) ) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | $id = attachment_url_to_postid( $args['url'] ); |
| 329 | if ( ! $id ) { |
| 330 | return false; |
| 331 | } |
| 332 | |
| 333 | $result = wp_get_attachment_image_src( $id, array( $args['width'], $args['height'] ) ); |
| 334 | if ( ! $result ) { |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | return $result; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | function get_read_access_cookies( $args ) { |
| 350 | $user = get_user_by( 'id', intval( $args[0] ) ); |
| 351 | if ( ! $user ) { |
| 352 | return new WP_Error( |
| 353 | 'account_not_found', |
| 354 | 'Account not found. If you already have an account, make sure you have connected to WordPress.com.' |
| 355 | ); |
| 356 | } |
| 357 | if ( ! $user->has_cap( 'read' ) ) { |
| 358 | return new WP_Error( 'access error', 'User does not have "read" capabilities' ); |
| 359 | } |
| 360 | |
| 361 | add_filter( 'send_auth_cookies', '__return_false' ); |
| 362 | add_filter( |
| 363 | 'auth_cookie_expiration', |
| 364 | function () use ( $args ) { |
| 365 | return $args[1]; |
| 366 | }, |
| 367 | 1000 |
| 368 | ); |
| 369 | |
| 370 | $logged_in_cookie = null; |
| 371 | $logged_in_cookie_expiration = null; |
| 372 | |
| 373 | add_action( |
| 374 | 'set_logged_in_cookie', |
| 375 | function ( $_cookie, $_expires, $_expiration ) use ( &$logged_in_cookie, &$logged_in_cookie_expiration ) { |
| 376 | $logged_in_cookie = $_cookie; |
| 377 | $logged_in_cookie_expiration = $_expiration; |
| 378 | }, |
| 379 | 10, |
| 380 | 3 |
| 381 | ); |
| 382 | wp_set_auth_cookie( $user->ID, true ); |
| 383 | if ( ! $logged_in_cookie ) { |
| 384 | return new WP_Error( 'authorization_failed', 'Authorization cookie was not found' ); |
| 385 | } |
| 386 | |
| 387 | return array( |
| 388 | array( LOGGED_IN_COOKIE, $logged_in_cookie, $logged_in_cookie_expiration ), |
| 389 | ); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | function is_jetpack_admin_ajax_request() { |
| 399 | |
| 400 | return ( |
| 401 | isset( $_SERVER['REQUEST_URI'] ) && |
| 402 | substr( $_SERVER['REQUEST_URI'], 0, 24 ) === '/wp-admin/admin-ajax.php' && |
| 403 | isset( $_SERVER['HTTP_AUTHORIZATION'] ) && |
| 404 | substr( $_SERVER['HTTP_AUTHORIZATION'], 0, 9 ) === 'X_JETPACK' && |
| 405 | array_key_exists( 'action', $_POST ) && |
| 406 | substr( $_POST['action'], 0, 8 ) === 'jetpack_' |
| 407 | ); |
| 408 | |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | function send_access_denied_error_response() { |
| 418 | global $wp; |
| 419 | |
| 420 | if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || 'admin-ajax.php' === ( $wp->query_vars['pagename'] ?? '' ) ) { |
| 421 | wp_send_json_error( |
| 422 | array( |
| 423 | 'code' => 'private_site', |
| 424 | 'message' => __( |
| 425 | 'This site is private.', |
| 426 | 'wpcomsh' |
| 427 | ), |
| 428 | ), |
| 429 | null, |
| 430 | JSON_UNESCAPED_SLASHES |
| 431 | ); |
| 432 | } |
| 433 | |
| 434 | require access_denied_template_path(); |
| 435 | exit( 0 ); |
| 436 | } |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | function parse_request() { |
| 442 | if ( maybe_print_robots_txt() ) { |
| 443 | |
| 444 | exit( 0 ); |
| 445 | } |
| 446 | |
| 447 | if ( should_prevent_site_access() ) { |
| 448 | send_access_denied_error_response(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | function original_request_url() { |
| 458 | |
| 459 | $origin = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['SERVER_NAME']; |
| 460 | |
| 461 | if ( ! empty( $_SERVER['SERVER_PORT'] ) && ! in_array( $_SERVER['SERVER_PORT'], array( 80, 443 ) ) ) { |
| 462 | $origin .= ':' . $_SERVER['SERVER_PORT']; |
| 463 | } |
| 464 | |
| 465 | return $origin . strtok( $_SERVER['REQUEST_URI'], '?' ); |
| 466 | |
| 467 | } |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | function cache_option_on_update_site_settings( $input, $unfiltered_input ) { |
| 482 | if ( array_key_exists( 'wpcom_coming_soon', $unfiltered_input ) ) { |
| 483 | wp_cache_set( 'wpcom_coming_soon', $unfiltered_input['wpcom_coming_soon'], 'wpcomsh' ); |
| 484 | } |
| 485 | |
| 486 | if ( array_key_exists( 'launch-status', $unfiltered_input ) ) { |
| 487 | wp_cache_set( 'wpcom_launch_status', $unfiltered_input['launch-status'], 'wpcomsh' ); |
| 488 | } |
| 489 | |
| 490 | return $input; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | function maybe_print_robots_txt() { |
| 502 | if ( untrailingslashit( original_request_url() ) === site_url( '/robots.txt' ) ) { |
| 503 | do_action( 'do_robots' ); |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | function rest_dispatch_request( $dispatch_result, $request, $route ) { |
| 524 | |
| 525 | if ( $dispatch_result !== null ) { |
| 526 | return $dispatch_result; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | $allowed_routes = array( |
| 534 | '2fa/', |
| 535 | 'jwt-auth/', |
| 536 | 'oauth1/', |
| 537 | ); |
| 538 | |
| 539 | if ( preg_match( '#^/(' . implode( '|', $allowed_routes ) . ')#', $route ) ) { |
| 540 | return null; |
| 541 | } |
| 542 | |
| 543 | if ( should_prevent_site_access() ) { |
| 544 | return new WP_Error( 'private_site', __( 'This site is private.', 'wpcomsh' ), array( 'status' => 403 ) ); |
| 545 | } |
| 546 | |
| 547 | return null; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
| 557 | function xmlrpc_methods_limit_to_allowed_list( $methods ) { |
| 558 | if ( should_prevent_site_access() ) { |
| 559 | return array_filter( |
| 560 | $methods, |
| 561 | function ( $key ) { |
| 562 | |
| 563 | return 'demo.sayHello' === $key || preg_match( '/^jetpack\..+/', $key ); |
| 564 | }, |
| 565 | ARRAY_FILTER_USE_KEY |
| 566 | ); |
| 567 | } |
| 568 | |
| 569 | return $methods; |
| 570 | } |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | function is_private_blog_user() { |
| 578 | return blog_user_can(); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | function blog_user_can( $capability = 'read' ) { |
| 591 | $user = wp_get_current_user(); |
| 592 | if ( ! $user->ID ) { |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | $blog_id = get_current_blog_id(); |
| 597 | if ( ! $blog_id ) { |
| 598 | return false; |
| 599 | } |
| 600 | |
| 601 | |
| 602 | $the_user = clone $user; |
| 603 | $the_user->for_site( $blog_id ); |
| 604 | return $the_user->has_cap( $capability ); |
| 605 | } |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | function mask_site_name( $value, $what ) { |
| 616 | if ( ! site_is_coming_soon() && should_prevent_site_access() && in_array( $what, array( 'name', 'title' ), true ) ) { |
| 617 | return __( 'Private Site', 'wpcomsh' ); |
| 618 | } |
| 619 | |
| 620 | return $value; |
| 621 | } |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | function remove_mask_site_name_filter() { |
| 627 | remove_filter( 'bloginfo', '\Private_Site\mask_site_name' ); |
| 628 | } |
| 629 | |
| 630 | |
| 631 | |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | function preprocess_comment( $comment ) { |
| 638 | if ( should_prevent_site_access() ) { |
| 639 | require access_denied_template_path(); |
| 640 | exit( 0 ); |
| 641 | } |
| 642 | return $comment; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | function is_jetpack_connected() { |
| 651 | return class_exists( 'Jetpack' ) && Jetpack::is_connection_ready(); |
| 652 | } |
| 653 | |
| 654 | |
| 655 | |
| 656 | |
| 657 | |
| 658 | |
| 659 | function is_site_preview() { |
| 660 | return site_preview_source() !== false; |
| 661 | } |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | function site_preview_source() { |
| 669 | |
| 670 | $ua = isset( $_SERVER['HTTP_USER_AGENT'] ) |
| 671 | ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) |
| 672 | : ''; |
| 673 | $apps_ua_fragments = array( |
| 674 | 'iphone-app' => ' wp-iphone/', |
| 675 | 'android-app' => ' wp-android/', |
| 676 | 'desktop-app' => ' WordPressDesktop/', |
| 677 | ); |
| 678 | foreach ( $apps_ua_fragments as $source => $fragment ) { |
| 679 | if ( strpos( $ua, $fragment ) !== false ) { |
| 680 | return $source; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | if ( |
| 685 | ( |
| 686 | isset( $_GET['iframe'] ) |
| 687 | && 'true' === $_GET['iframe'] |
| 688 | && ( |
| 689 | ( isset( $_GET['theme_preview'] ) && 'true' === $_GET['theme_preview'] ) |
| 690 | || ( |
| 691 | isset( $_GET['preview'] ) |
| 692 | && 'true' === $_GET['preview'] |
| 693 | ) |
| 694 | ) |
| 695 | ) |
| 696 | || isset( $_GET['widgetPreview'] ) |
| 697 | || isset( $_GET['widget-preview'] ) |
| 698 | ) { |
| 699 | return 'browser-iframe'; |
| 700 | } |
| 701 | |
| 702 | return false; |
| 703 | |
| 704 | } |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | function access_denied_template_path() { |
| 710 | if ( is_site_preview() ) { |
| 711 | return __DIR__ . '/access-denied-preview-login-template.php'; |
| 712 | } |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | $calypso_domains = array( |
| 719 | 'https://wordpress.com/', |
| 720 | 'https://horizon.wordpress.com/', |
| 721 | 'https://wpcalypso.wordpress.com/', |
| 722 | 'http://calypso.localhost:3000/', |
| 723 | 'http://127.0.0.1:41050/', |
| 724 | ); |
| 725 | $should_redirect_to_login = ( ! is_user_logged_in() ) && class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'sso' ) && in_array( wp_get_referer(), $calypso_domains, true ); |
| 726 | if ( $should_redirect_to_login ) { |
| 727 | wp_safe_redirect( wp_login_url( set_url_scheme( original_request_url() ) ) ); |
| 728 | exit( 0 ); |
| 729 | } elseif ( site_is_coming_soon() ) { |
| 730 | return __DIR__ . '/access-denied-coming-soon-template.php'; |
| 731 | } else { |
| 732 | return __DIR__ . '/access-denied-private-site-template.php'; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | function remove_privacy_option_from_whitelist( $allow_list ) { |
| 748 | $blog_public_index = array_search( 'blog_public', $allow_list['reading'], true ); |
| 749 | unset( $allow_list['reading'][ $blog_public_index ] ); |
| 750 | |
| 751 | return $allow_list; |
| 752 | } |
| 753 | |
| 754 | |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | |
| 760 | function should_update_privacy_selector() { |
| 761 | return apply_filters( 'wpcom_should_update_privacy_selector', true ); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | |
| 766 | |
| 767 | |
| 768 | |
| 769 | |
| 770 | function private_robots_txt() { |
| 771 | |
| 772 | return "User-agent: *\nDisallow: /\n"; |
| 773 | } |
| 774 | |
| 775 | |
| 776 | |
| 777 | _x( 'Viewer', 'User role', 'wpcomsh' ); |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | function rename_subscriber_role_to_viewer( $roles ) { |
| 785 | if ( site_is_private() && isset( $roles->roles['subscriber'] ) ) { |
| 786 | $roles->roles['subscriber']['name'] = 'Viewer'; |
| 787 | $roles->role_names['subscriber'] = 'Viewer'; |
| 788 | } |
| 789 | } |
| 790 | add_action( 'wp_roles_init', '\Private_Site\rename_subscriber_role_to_viewer' ); |
| 791 | |
| 792 | |
| 793 | |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | |
| 799 | |
| 800 | |
| 801 | function translate_viewer_role( $translation, $text, $context, $domain ) { |
| 802 | if ( 'User role' === $context && 'default' === $domain && 'Viewer' === $text ) { |
| 803 | return translate_user_role( $text, 'wpcomsh' ); |
| 804 | } |
| 805 | |
| 806 | return $translation; |
| 807 | } |
| 808 | add_filter( 'gettext_with_context', '\Private_Site\translate_viewer_role', 10, 4 ); |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | |
| 814 | |
| 815 | function private_no_pinning() { |
| 816 | echo '<meta name="pinterest" content="nopin" />'; |
| 817 | } |
| 818 | |
| 819 | |
| 820 | |
| 821 | |
| 822 | function hide_opml() { |
| 823 | if ( should_prevent_site_access() ) { |
| 824 | status_header( 403 ); |
| 825 | ?> |
| 826 | <error><?php esc_html_e( 'This site is private.', 'wpcomsh' ); ?></error> |
| 827 | </head> |
| 828 | </opml> |
| 829 | <?php |
| 830 | exit( 0 ); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | function filter_jetpack_active_modules( $modules ) { |
| 842 | return array_filter( |
| 843 | $modules, |
| 844 | function ( $module_name ) { |
| 845 | return ! in_array( $module_name, DISABLED_JETPACK_MODULES_WHEN_PRIVATE, true ); |
| 846 | } |
| 847 | ); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | function filter_jetpack_get_available_modules( $modules ) { |
| 858 | return array_filter( |
| 859 | $modules, |
| 860 | function ( $module_name ) { |
| 861 | return ! in_array( $module_name, DISABLED_JETPACK_MODULES_WHEN_PRIVATE, true ); |
| 862 | }, |
| 863 | ARRAY_FILTER_USE_KEY |
| 864 | ); |
| 865 | } |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | |
| 871 | |
| 872 | |
| 873 | |
| 874 | |
| 875 | |
| 876 | function use_classic_editor_if_requested() { |
| 877 | if ( ! is_module_active() ) { |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | if ( ! should_override_editor_with_classic_editor() ) { |
| 882 | return; |
| 883 | } |
| 884 | |
| 885 | if ( class_exists( '\Classic_Editor' ) ) { |
| 886 | |
| 887 | return; |
| 888 | } |
| 889 | |
| 890 | add_action( |
| 891 | 'classic_editor_plugin_settings', |
| 892 | function () { |
| 893 | return array( |
| 894 | 'editor' => 'classic', |
| 895 | 'allow-users' => false, |
| 896 | ); |
| 897 | } |
| 898 | ); |
| 899 | |
| 900 | require dirname( __DIR__ ) . '/vendor/wordpress/classic-editor-plugin/classic-editor.php'; |
| 901 | \Classic_Editor::init_actions(); |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | remove_action( 'plugins_loaded', array( 'Classic_Editor', 'init_actions' ) ); |
| 908 | |
| 909 | |
| 910 | add_filter( 'get_edit_post_link', array( 'Classic_Editor', 'get_edit_post_link' ) ); |
| 911 | add_filter( 'redirect_post_location', array( 'Classic_Editor', 'redirect_location' ) ); |
| 912 | add_action( 'edit_form_top', array( 'Classic_Editor', 'add_redirect_helper' ) ); |
| 913 | add_action( 'admin_head-edit.php', array( 'Classic_Editor', 'add_edit_php_inline_style' ) ); |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | add_filter( |
| 922 | 'get_user_metadata', |
| 923 | function ( $value, $object_id, $meta_key ) { |
| 924 | if ( $meta_key === 'calypsoify' ) { |
| 925 | return 0; |
| 926 | } |
| 927 | |
| 928 | return $value; |
| 929 | }, |
| 930 | 10, |
| 931 | 3 |
| 932 | ); |
| 933 | } |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | function disable_classic_editor_plugin_when_needed( $plugins ) { |
| 943 | if ( ! is_module_active() ) { |
| 944 | return $plugins; |
| 945 | } |
| 946 | |
| 947 | if ( ! should_override_editor_with_classic_editor() ) { |
| 948 | return $plugins; |
| 949 | } |
| 950 | |
| 951 | $key = array_search( 'classic-editor/classic-editor.php', $plugins, true ); |
| 952 | if ( false !== $key ) { |
| 953 | unset( $plugins[ $key ] ); |
| 954 | } |
| 955 | |
| 956 | return $plugins; |
| 957 | } |
| 958 | add_filter( 'option_active_plugins', '\Private_Site\disable_classic_editor_plugin_when_needed', 1000 ); |
| 959 | |
| 960 | |
| 961 | |
| 962 | |
| 963 | |
| 964 | |
| 965 | function should_override_editor_with_classic_editor() { |
| 966 | if ( ! site_is_private() ) { |
| 967 | return false; |
| 968 | } |
| 969 | |
| 970 | global $pagenow; |
| 971 | if ( empty( $pagenow ) ) { |
| 972 | return false; |
| 973 | } |
| 974 | |
| 975 | if ( $pagenow !== 'post.php' && $pagenow !== 'post-new.php' ) { |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | if ( ! array_key_exists( 'classic-editor', $_REQUEST ) ) { |
| 980 | return false; |
| 981 | } |
| 982 | |
| 983 | if ( array_key_exists( 'classic-editor__forget', $_REQUEST ) ) { |
| 984 | return false; |
| 985 | } |
| 986 | |
| 987 | return true; |
| 988 | } |