Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
CRAP | n/a |
0 / 0 |
|
| wpcom_handle_post_like_from_email | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
182 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Post Like From Email |
| 4 | * |
| 5 | * Handles Like-button clicks originating from post-notification emails on |
| 6 | * Atomic/WoW sites by proxying the request to the wpcom/v2/email-like endpoint |
| 7 | * (which validates the HMAC and records the like server-side), then redirecting |
| 8 | * the visitor to the post permalink. |
| 9 | * |
| 10 | * @package automattic/jetpack-mu-wpcom |
| 11 | */ |
| 12 | |
| 13 | // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Request is authenticated server-side via HMAC, not via WP nonces. |
| 14 | |
| 15 | use Automattic\Jetpack\Connection\Client; |
| 16 | |
| 17 | /** |
| 18 | * Detect a like-from-email request, forward it to wpcom, and redirect to the post. |
| 19 | * |
| 20 | * The response body from the wpcom endpoint is intentionally ignored — the visitor |
| 21 | * always lands on the post permalink. |
| 22 | * |
| 23 | * @return void |
| 24 | */ |
| 25 | function wpcom_handle_post_like_from_email() { |
| 26 | if ( ! isset( $_GET['like'] ) || ! isset( $_GET['postid'] ) || ! isset( $_GET['like_actor'] ) || ! isset( $_GET['like_hmac'] ) ) { |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | if ( 1 !== (int) $_GET['like'] ) { |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | // On wpcom, the like is already handled at the same priority — bail to avoid |
| 35 | // double-firing if jetpack-mu-wpcom is loaded inside wpcom. |
| 36 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $postid = (int) $_GET['postid']; |
| 41 | $like_actor = sanitize_text_field( wp_unslash( $_GET['like_actor'] ) ); |
| 42 | $like_hmac = sanitize_text_field( wp_unslash( $_GET['like_hmac'] ) ); |
| 43 | |
| 44 | if ( $postid <= 0 || '' === $like_actor || '' === $like_hmac ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $blog_id = get_wpcom_blog_id(); |
| 49 | if ( ! $blog_id ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | Client::wpcom_json_api_request_as_blog( |
| 54 | '/sites/' . $blog_id . '/email-like', |
| 55 | '2', |
| 56 | array( |
| 57 | 'method' => 'POST', |
| 58 | 'headers' => array( |
| 59 | 'content-type' => 'application/json', |
| 60 | ), |
| 61 | ), |
| 62 | wp_json_encode( |
| 63 | array( |
| 64 | 'post_id' => $postid, |
| 65 | 'like_actor' => $like_actor, |
| 66 | 'like_hmac' => $like_hmac, |
| 67 | ), |
| 68 | JSON_UNESCAPED_SLASHES |
| 69 | ), |
| 70 | 'wpcom' |
| 71 | ); |
| 72 | |
| 73 | $permalink = get_permalink( $postid ); |
| 74 | if ( ! $permalink ) { |
| 75 | $permalink = home_url( '/' ); |
| 76 | } |
| 77 | |
| 78 | wp_safe_redirect( $permalink ); |
| 79 | exit; |
| 80 | } |
| 81 | add_action( 'template_redirect', 'wpcom_handle_post_like_from_email', 1 ); |