← All changes
|
modules/subscriptions/jetpack-user-content-link-redirection.php
+28
-8
13.3.3
→
16.3-a.1
View file →
| @@ -8,20 +8,41 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @package automattic/jetpack |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 13 | + | |
| 12 | 14 | /** |
| 13 | - * Render a page containing an iframe to track and redirect the user content link in emails. | |
| 15 | + * Render a page with an iframe to track and redirect user content links in emails. | |
| 16 | + * | |
| 17 | + * Hooked to the `init` action, this function renders a page with an iframe pointing to | |
| 18 | + * subscribe.wordpress.com to track and return the destination URL for redirection. | |
| 19 | + * | |
| 20 | + * Redirects to the site's home page if required parameters are missing. | |
| 21 | + * Returns a 400 error if the request's `blog_id` doesn't match the actual `blog_id`. | |
| 22 | + * | |
| 23 | + * @return never | |
| 14 | 24 | */ |
| 15 | 25 | function jetpack_user_content_link_redirection() { |
| 16 | - if ( empty( $_SERVER['QUERY_STRING'] ) ) { | |
| 17 | - return; | |
| 26 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 27 | + if ( empty( $_SERVER['QUERY_STRING'] ) || empty( $_SERVER['HTTP_HOST'] ) || empty( $_GET['blog_id'] ) ) { | |
| 28 | + wp_safe_redirect( get_home_url() ); | |
| 29 | + exit( 0 ); | |
| 18 | 30 | } |
| 31 | + | |
| 32 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 33 | + $request_blog_id = intval( sanitize_text_field( wp_unslash( $_GET['blog_id'] ) ) ); | |
| 34 | + $actual_blog_id = Connection_Manager::get_site_id( true ); | |
| 35 | + | |
| 36 | + if ( $actual_blog_id !== $request_blog_id ) { | |
| 37 | + wp_die( esc_html__( 'Invalid link.', 'jetpack' ), 400 ); | |
| 38 | + exit( 0 ); | |
| 39 | + } | |
| 40 | + | |
| 19 | 41 | $query_params = sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) ); |
| 20 | 42 | $iframe_url = "https://subscribe.wordpress.com/?$query_params"; |
| 21 | 43 | |
| 22 | - // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 23 | - echo <<<EOF | |
| 44 | + echo <<<'EOF' | |
| 24 | 45 | <!DOCTYPE html> |
| 25 | 46 | <html> |
| 26 | 47 | <head> |
| 27 | 48 | <script> |
| @@ -39,14 +60,13 @@ | ||
| 39 | 60 | </head> |
| 40 | 61 | <body> |
| 41 | 62 | EOF; |
| 42 | 63 | echo '<iframe id="user-content-link-redirection" hidden aria-hidden="true" tabindex="-1" width="0" height="0" style="display: none" src="' . esc_url( $iframe_url ) . '"></iframe>'; |
| 43 | - echo <<<EOF | |
| 64 | + echo <<<'EOF' | |
| 44 | 65 | </body> |
| 45 | 66 | </html> |
| 46 | 67 | EOF; |
| 47 | - // phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 48 | - exit; | |
| 68 | + exit( 0 ); | |
| 49 | 69 | } |
| 50 | 70 | |
| 51 | 71 | // The WPCOM_USER_CONTENT_LINK_REDIRECTION flag prevents this redirection logic from running |
| 52 | 72 | // on Atomic in case we'd like to override the redirection logic on the Atomic end. |