Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
55.56% |
35 / 63 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Endpoint_Send_Email_Preview | |
58.33% |
35 / 60 |
|
25.00% |
1 / 4 |
25.23 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
2 | |||
| permissions_check | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
6.06 | |||
| send_email_preview | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Handles the sending of email previews via the WordPress.com REST API. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\Connection\Manager; |
| 9 | use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request; |
| 10 | use Automattic\Jetpack\Status\Host; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview |
| 18 | * Handles the sending of email previews via the WordPress.com REST API |
| 19 | */ |
| 20 | class WPCOM_REST_API_V2_Endpoint_Send_Email_Preview extends WP_REST_Controller { |
| 21 | |
| 22 | use WPCOM_REST_API_Proxy_Request; |
| 23 | |
| 24 | /** |
| 25 | * Constructor. |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | $this->base_api_path = 'wpcom'; |
| 29 | $this->version = 'v2'; |
| 30 | $this->namespace = $this->base_api_path . '/' . $this->version; |
| 31 | $this->rest_base = '/send-email-preview'; |
| 32 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 33 | $this->wpcom_is_site_specific_endpoint = true; |
| 34 | |
| 35 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Registers the routes for blogging prompts. |
| 40 | * |
| 41 | * @see register_rest_route() |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | $options = array( |
| 45 | 'show_in_index' => true, |
| 46 | 'methods' => 'POST', |
| 47 | // if this is not a wpcom site, we need to proxy the request to wpcom |
| 48 | 'callback' => ( ( new Host() )->is_wpcom_simple() ) ? array( |
| 49 | $this, |
| 50 | 'send_email_preview', |
| 51 | ) : array( $this, 'proxy_request_to_wpcom_as_user' ), |
| 52 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 53 | 'args' => array( |
| 54 | 'id' => array( |
| 55 | 'description' => __( 'Unique identifier for the post.', 'jetpack' ), |
| 56 | 'type' => 'integer', |
| 57 | ), |
| 58 | ), |
| 59 | ); |
| 60 | |
| 61 | register_rest_route( |
| 62 | $this->namespace, |
| 63 | $this->rest_base, |
| 64 | $options |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Checks if the user is connected and has access to edit the post |
| 70 | * |
| 71 | * @param WP_REST_Request $request Full data about the request. |
| 72 | * |
| 73 | * @return true|WP_Error True if the request has edit access, WP_Error object otherwise. |
| 74 | */ |
| 75 | public function permissions_check( $request ) { |
| 76 | if ( ! ( new Host() )->is_wpcom_simple() ) { |
| 77 | if ( ! ( new Manager() )->is_user_connected() ) { |
| 78 | return new WP_Error( |
| 79 | 'rest_cannot_send_email_preview', |
| 80 | __( 'Please connect your user account to WordPress.com', 'jetpack' ), |
| 81 | array( 'status' => rest_authorization_required_code() ) |
| 82 | ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | $post = get_post( $request->get_param( 'id' ) ); |
| 87 | |
| 88 | if ( is_wp_error( $post ) ) { |
| 89 | return $post; |
| 90 | } |
| 91 | |
| 92 | if ( $post && ! current_user_can( 'edit_post', $post->ID ) ) { |
| 93 | return new WP_Error( |
| 94 | 'rest_forbidden_context', |
| 95 | __( 'Please connect your user account to WordPress.com', 'jetpack' ), |
| 96 | array( 'status' => rest_authorization_required_code() ) |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Sends an email preview of a post to the current user. |
| 105 | * |
| 106 | * @param WP_REST_Request $request Full data about the request. |
| 107 | * |
| 108 | * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
| 109 | */ |
| 110 | public function send_email_preview( $request ) { |
| 111 | $post_id = $request['id']; |
| 112 | $post = get_post( $post_id ); |
| 113 | |
| 114 | // Return error if the post cannot be retrieved |
| 115 | if ( is_wp_error( $post ) ) { |
| 116 | return $post; |
| 117 | } |
| 118 | |
| 119 | // Check if the user's email is verified |
| 120 | if ( Email_Verification::is_email_unverified() ) { |
| 121 | return new WP_Error( 'unverified', __( 'Your email address must be verified.', 'jetpack' ), array( 'status' => rest_authorization_required_code() ) ); |
| 122 | } |
| 123 | |
| 124 | $current_user = wp_get_current_user(); |
| 125 | $email = $current_user->user_email; |
| 126 | |
| 127 | // Try to create a new subscriber with the user's email |
| 128 | $subscriber = Blog_Subscriber::create( $email ); |
| 129 | if ( ! $subscriber ) { |
| 130 | return new WP_Error( 'unverified', __( 'Could not create subscriber.', 'jetpack' ), array( 'status' => rest_authorization_required_code() ) ); |
| 131 | } |
| 132 | |
| 133 | // Send the post to the subscriber |
| 134 | require_once ABSPATH . 'wp-content/mu-plugins/email-subscriptions/subscription-mailer.php'; |
| 135 | $mailer = new Subscription_Mailer( $subscriber ); |
| 136 | $subscription = $subscriber->get_subscription( get_current_blog_id() ); |
| 137 | $mailer->send_post( $post, $subscription ); |
| 138 | |
| 139 | // Return a response |
| 140 | return new WP_REST_Response( 'Email preview sent successfully.', 200 ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Send_Email_Preview' ); |