Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
35.53% covered (danger)
35.53%
27 / 76
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Email_Preview
37.50% covered (danger)
37.50%
27 / 72
0.00% covered (danger)
0.00%
0 / 4
28.78
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
75.00% covered (warning)
75.00%
27 / 36
0.00% covered (danger)
0.00%
0 / 1
2.06
 permissions_check
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
30
 email_preview
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Email Preview endpoint for 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\Extensions\Premium_Content\Subscription_Service\Abstract_Token_Subscription_Service;
11use Automattic\Jetpack\Status\Host;
12
13if ( ! defined( 'ABSPATH' ) ) {
14    exit( 0 );
15}
16
17require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/premium-content/_inc/subscription-service/include.php';
18
19/**
20 * Class WPCOM_REST_API_V2_Endpoint_Email_Preview
21 *
22 * Returns an email preview given a post id.
23 */
24class WPCOM_REST_API_V2_Endpoint_Email_Preview extends WP_REST_Controller {
25
26    use WPCOM_REST_API_Proxy_Request;
27
28    /**
29     * Constructor.
30     */
31    public function __construct() {
32        $this->base_api_path                   = 'wpcom';
33        $this->version                         = 'v2';
34        $this->namespace                       = $this->base_api_path . '/' . $this->version;
35        $this->rest_base                       = '/email-preview';
36        $this->wpcom_is_wpcom_only_endpoint    = true;
37        $this->wpcom_is_site_specific_endpoint = true;
38
39        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
40    }
41
42    /**
43     * Registers the routes for email preview.
44     *
45     * @see register_rest_route()
46     */
47    public function register_routes() {
48        $options = array(
49            'show_in_index'       => true,
50            'methods'             => 'GET',
51            // if this is not a wpcom site, we need to proxy the request to wpcom
52            'callback'            => ( ( new Host() )->is_wpcom_simple() ) ? array(
53                $this,
54                'email_preview',
55            ) : array( $this, 'proxy_request_to_wpcom_as_user' ),
56            'permission_callback' => array( $this, 'permissions_check' ),
57            'args'                => array(
58                'id'     => array(
59                    'description' => __( 'Unique identifier for the post.', 'jetpack' ),
60                    'type'        => 'integer',
61                ),
62                'access' => array(
63                    'description'       => __( 'Access level.', 'jetpack' ),
64                    'enum'              => array( Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY, Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS, Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS ),
65                    'default'           => Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY,
66                    'validate_callback' => function ( $param ) {
67                        return in_array(
68                            $param,
69                            array(
70                                Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_EVERYBODY,
71                                Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_SUBSCRIBERS,
72                                Abstract_Token_Subscription_Service::POST_ACCESS_LEVEL_PAID_SUBSCRIBERS,
73                            ),
74                            true
75                        );
76                    },
77                ),
78            ),
79        );
80
81        register_rest_route(
82            $this->namespace,
83            $this->rest_base,
84            $options
85        );
86    }
87
88    /**
89     * Checks if the user is connected and has access to edit the post
90     *
91     * @param WP_REST_Request $request Full data about the request.
92     *
93     * @return true|WP_Error True if the request has edit access, WP_Error object otherwise.
94     */
95    public function permissions_check( $request ) {
96        if ( ! ( new Host() )->is_wpcom_simple() ) {
97            if ( ! ( new Manager() )->is_user_connected() ) {
98                return new WP_Error(
99                    'rest_cannot_send_email_preview',
100                    __( 'Please connect your user account to WordPress.com', 'jetpack' ),
101                    array( 'status' => rest_authorization_required_code() )
102                );
103            }
104        }
105
106        $post = get_post( $request->get_param( 'post_id' ) );
107
108        if ( ! $post ) {
109            return new \WP_Error(
110                'post_not_found',
111                __( 'Post not found.', 'jetpack' ),
112                array( 'status' => 404 )
113            );
114        }
115
116        if ( ! current_user_can( 'edit_post', $post->ID ) ) {
117            return new WP_Error(
118                'rest_forbidden_context',
119                __( 'Please connect your user account to WordPress.com', 'jetpack' ),
120                array( 'status' => rest_authorization_required_code() )
121            );
122        }
123
124        return true;
125    }
126
127    /**
128     * Returns an email preview of a post.
129     *
130     * @param WP_REST_Request $request Full data about the request.
131     *
132     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
133     */
134    public function email_preview( $request ) {
135        $post_id = $request['post_id'];
136        $access  = $request['access'];
137        $post    = get_post( $post_id );
138        return rest_ensure_response(
139            array(
140                /**
141                * Filters the generated email preview HTML.
142                *
143                * @since 13.8
144                *
145                * @param string $html   The generated HTML for the email preview.
146                * @param WP_Post $post  The post object.
147                * @param string $access The access level.
148                */
149                'html' => apply_filters( 'jetpack_generate_email_preview_html', '', $post, $access ),
150            )
151        );
152    }
153}
154
155wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Email_Preview' );