Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.84% covered (warning)
71.84%
74 / 103
50.00% covered (danger)
50.00%
4 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Send_Email_Preview
74.00% covered (warning)
74.00%
74 / 100
50.00% covered (danger)
50.00%
4 / 8
47.89
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 register_routes
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
2
 permissions_check
88.24% covered (warning)
88.24%
15 / 17
0.00% covered (danger)
0.00%
0 / 1
6.06
 prepare_post_for_akismet
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 is_akismet_available
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
 akismet_http_post
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 check_post_for_spam
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 send_email_preview
31.03% covered (danger)
31.03%
9 / 29
0.00% covered (danger)
0.00%
0 / 1
17.81
1<?php
2/**
3 * Handles the sending of email previews via the WordPress.com REST API.
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Connection\Manager;
9use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request;
10use Automattic\Jetpack\Status\Host;
11
12if ( ! 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 */
20class 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     * Build an Akismet payload describing the post content being previewed.
105     *
106     * Uses the post author as the identity signal (not the caller), because
107     * the author is what Akismet should evaluate the content against.
108     *
109     * @since 15.8
110     *
111     * @param WP_Post $post Post being previewed.
112     * @return array Associative payload suitable for http_build_query().
113     */
114    protected function prepare_post_for_akismet( WP_Post $post ): array {
115        $author = get_userdata( (int) $post->post_author );
116
117        $payload = array(
118            'comment_type'         => 'blog-post-preview',
119            'comment_content'      => $post->post_title . "\n\n" . $post->post_content,
120            'comment_author'       => $author ? $author->display_name : '',
121            'comment_author_email' => $author ? $author->user_email : '',
122            'comment_author_url'   => $author ? $author->user_url : '',
123            'permalink'            => (string) get_permalink( $post ),
124            'blog'                 => (string) get_option( 'home' ),
125            'blog_lang'            => (string) get_bloginfo( 'language' ),
126            'comment_date_gmt'     => gmdate( DATE_ATOM, time() ),
127        );
128
129        /**
130         * Filter the values sent to Akismet when checking an email preview.
131         *
132         * @module subscriptions
133         *
134         * @since 15.8
135         *
136         * @param array   $payload The values being sent to Akismet.
137         * @param WP_Post $post    The post being previewed.
138         */
139        $filtered_payload = apply_filters( 'jetpack_send_email_preview_akismet_values', $payload, $post );
140
141        return is_array( $filtered_payload ) ? $filtered_payload : $payload;
142    }
143
144    /**
145     * Whether the Akismet plugin is loaded and usable in this request.
146     *
147     * Extracted into a method so tests can override it via a subclass.
148     *
149     * @since 15.8
150     *
151     * @return bool
152     */
153    protected function is_akismet_available(): bool {
154        return function_exists( 'akismet_http_post' ) || ( class_exists( 'Akismet' ) && method_exists( 'Akismet', 'http_post' ) );
155    }
156
157    /**
158     * POST a comment-check payload to the Akismet service.
159     *
160     * Extracted into a method so tests can override it via a subclass.
161     *
162     * @since 15.8
163     *
164     * @param string $query_string URL-encoded payload.
165     * @return array Two-element array as returned by Akismet: [ headers_array, body_string ].
166     */
167    protected function akismet_http_post( string $query_string ): array {
168        if ( method_exists( 'Akismet', 'http_post' ) ) {
169            return \Akismet::http_post( $query_string, 'comment-check' );
170        }
171
172        global $akismet_api_host, $akismet_api_port;
173        return akismet_http_post( $query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port );
174    }
175
176    /**
177     * Ask Akismet whether a post being previewed looks like spam.
178     *
179     * Fail-open: any ambiguity (Akismet absent, malformed response) returns false
180     * so the author's workflow isn't blocked on service hiccups.
181     *
182     * @since 15.8
183     *
184     * @param WP_Post $post Post being previewed.
185     * @return bool True when Akismet classifies the content as spam; false otherwise.
186     */
187    protected function check_post_for_spam( WP_Post $post ): bool {
188        if ( ! $this->is_akismet_available() ) {
189            return false;
190        }
191
192        $form     = $this->prepare_post_for_akismet( $post );
193        $response = $this->akismet_http_post( http_build_query( $form ) );
194
195        if (
196            isset( $response[0]['x-akismet-pro-tip'] ) &&
197            'discard' === trim( $response[0]['x-akismet-pro-tip'] ) &&
198            '1' === get_option( 'akismet_strictness' )
199        ) {
200            return true;
201        }
202
203        return isset( $response[1] ) && 'true' === trim( $response[1] );
204    }
205
206    /**
207     * Sends an email preview of a post to the current user.
208     *
209     * @param WP_REST_Request $request Full data about the request.
210     *
211     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
212     */
213    public function send_email_preview( $request ) {
214        $post_id = $request['id'];
215        $post    = get_post( $post_id );
216
217        // Return error if the post cannot be retrieved
218        if ( is_wp_error( $post ) ) {
219            return $post;
220        }
221
222        if ( ! $post instanceof WP_Post ) {
223            return new WP_Error(
224                'rest_post_invalid_id',
225                __( 'Invalid post ID.', 'jetpack' ),
226                array( 'status' => 404 )
227            );
228        }
229
230        // Check if the user's email is verified
231        if ( Email_Verification::is_email_unverified() ) {
232            return new WP_Error( 'unverified', __( 'Your email address must be verified.', 'jetpack' ), array( 'status' => rest_authorization_required_code() ) );
233        }
234
235        if ( $this->check_post_for_spam( $post ) ) {
236            return new WP_Error(
237                'email_preview_not_sent',
238                __( 'Email preview could not be sent.', 'jetpack' ),
239                array( 'status' => 500 )
240            );
241        }
242
243        $current_user = wp_get_current_user();
244        $email        = $current_user->user_email;
245
246        // Try to create a new subscriber with the user's email
247        $subscriber = Blog_Subscriber::create( $email );
248        if ( ! $subscriber ) {
249            return new WP_Error( 'unverified', __( 'Could not create subscriber.', 'jetpack' ), array( 'status' => rest_authorization_required_code() ) );
250        }
251
252        // Send the post to the subscriber
253        require_once ABSPATH . 'wp-content/mu-plugins/email-subscriptions/subscription-mailer.php';
254        $mailer       = new Subscription_Mailer( $subscriber );
255        $subscription = $subscriber->get_subscription( get_current_blog_id() );
256
257        /**
258         * Fires immediately before an email preview is dispatched to the current user.
259         *
260         * Useful for inspecting the post content with an external classifier (e.g. an
261         * LLM-based content moderator) or for logging outbound previews. Fires only
262         * after the Akismet spam check has passed and the subscriber has been
263         * resolved, so handlers receive a post that is about to be sent.
264         *
265         * @module subscriptions
266         *
267         * @since 15.8
268         *
269         * @param WP_Post                 $post         The post being previewed.
270         * @param Blog_Subscriber         $subscriber   The subscriber receiving the preview.
271         * @param Blog_Subscription|false $subscription The subscriber's subscription for the current blog, or false if none exists.
272         */
273        do_action( 'jetpack_before_send_email_preview', $post, $subscriber, $subscription );
274
275        $mailer->send_post( $post, $subscription );
276
277        // Return a response
278        return new WP_REST_Response( 'Email preview sent successfully.', 200 );
279    }
280}
281
282wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Send_Email_Preview' );