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