Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
jetpack_user_content_link_redirection
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2/**
3 * User Content Link Redirection
4 *
5 * The purpose of this file is to track and redirect user content links in emails.
6 * This renders an iframe pointing to subscribe.wordpress.com which will track and
7 * return the destination url for the iframe parent to redirect to.
8 *
9 * @package automattic/jetpack
10 */
11
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13
14if ( ! defined( 'ABSPATH' ) ) {
15    exit( 0 );
16}
17
18/**
19 * Render a page with an iframe to track and redirect user content links in emails.
20 *
21 * Hooked to the `init` action, this function renders a page with an iframe pointing to
22 * subscribe.wordpress.com to track and return the destination URL for redirection.
23 *
24 * Redirects to the site's home page if required parameters are missing.
25 * Returns a 400 error if the request's `blog_id` doesn't match the actual `blog_id`.
26 *
27 * @return never
28 */
29function jetpack_user_content_link_redirection() {
30    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
31    if ( empty( $_SERVER['QUERY_STRING'] ) || empty( $_SERVER['HTTP_HOST'] ) || empty( $_GET['blog_id'] ) ) {
32        wp_safe_redirect( get_home_url() );
33        exit( 0 );
34    }
35
36    // phpcs:ignore WordPress.Security.NonceVerification.Recommended
37    $request_blog_id = intval( sanitize_text_field( wp_unslash( $_GET['blog_id'] ) ) );
38    $actual_blog_id  = Connection_Manager::get_site_id( true );
39
40    if ( $actual_blog_id !== $request_blog_id ) {
41        wp_die( esc_html__( 'Invalid link.', 'jetpack' ), 400 );
42        exit( 0 );
43    }
44
45    $query_params = sanitize_text_field( wp_unslash( $_SERVER['QUERY_STRING'] ) );
46    $iframe_url   = "https://subscribe.wordpress.com/?$query_params";
47
48    echo <<<'EOF'
49<!DOCTYPE html>
50<html>
51<head>
52<script>
53    let messageReceived = false;
54    window.addEventListener( 'message', function(event) {
55        if ( event.origin !== 'https://subscribe.wordpress.com' || messageReceived ) {
56            return;
57        }
58        if ( event.data.redirectUrl ) {
59            messageReceived = true;
60            window.location.href = event.data.redirectUrl;
61        }
62    } );
63</script>
64</head>
65<body>
66EOF;
67    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>';
68    echo <<<'EOF'
69</body>
70</html>
71EOF;
72    exit( 0 );
73}
74
75// The WPCOM_USER_CONTENT_LINK_REDIRECTION flag prevents this redirection logic from running
76// on Atomic in case we'd like to override the redirection logic on the Atomic end.
77
78// phpcs:ignore WordPress.Security.NonceVerification.Recommended
79if ( ! defined( 'WPCOM_USER_CONTENT_LINK_REDIRECTION' ) && isset( $_GET['action'] ) && $_GET['action'] === 'user_content_redirect' ) {
80    add_action( 'init', 'jetpack_user_content_link_redirection' );
81}