Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
REST_Token_Controller
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 generate_preview_token
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 permissions_check
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 get_item_schema
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Defines the endpoints used for handling tokens for the Social Image Generator.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize\Social_Image_Generator;
9
10use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
11use WP_Error;
12use WP_REST_Controller;
13use WP_REST_Request;
14use WP_REST_Server;
15
16/**
17 * Class used to register token related REST API endpoints used by Social Image Generator.
18 */
19class REST_Token_Controller extends WP_REST_Controller {
20
21    /**
22     * Registers the REST routes on the `rest_api_init` hook.
23     *
24     * Instantiated here, rather than eagerly, so the controller class only loads
25     * on requests that reach `rest_api_init`. Static so the callback can be
26     * unregistered.
27     *
28     * @return void
29     */
30    public static function register() {
31        ( new self() )->register_routes();
32    }
33
34    /**
35     * Register REST API endpoints.
36     *
37     * @return void
38     */
39    public function register_routes() {
40        register_rest_route(
41            'jetpack/v4',
42            '/social-image-generator/generate-preview-token',
43            array(
44                'methods'             => WP_REST_Server::CREATABLE,
45                'callback'            => array( $this, 'generate_preview_token' ),
46                'permission_callback' => array( $this, 'permissions_check' ),
47                'args'                => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
48                'schema'              => array( $this, 'get_public_item_schema' ),
49            )
50        );
51    }
52
53    /**
54     * Passes the request parameters to the WPCOM endpoint to generate a preview image token.
55     *
56     * @param WP_REST_Request $request The request object, which includes the parameters.
57     * @return array|WP_Error The token or an error.
58     */
59    public function generate_preview_token( $request ) {
60
61        Utils::endpoint_deprecated_warning(
62            __METHOD__,
63            'jetpack-14.5, jetpack-social-6.2.3',
64            'jetpack/v4/social-image-generator/generate-preview-token',
65            'wpcom/v2/publicize/social-image-generator/generate-token'
66        );
67
68        $text      = $request->get_param( 'text' );
69        $image_url = $request->get_param( 'image_url' );
70        $template  = $request->get_param( 'template' );
71
72        return fetch_token( $text, $image_url, $template );
73    }
74
75    /**
76     * Check the current user permissions for the endpoints.
77     *
78     * @return bool|WP_Error True if user can manage options.
79     */
80    public function permissions_check() {
81        if ( ! current_user_can( 'edit_posts' ) ) {
82            return new WP_Error(
83                'rest_forbidden_context',
84                __( 'Sorry, you are not allowed to access this endpoint.', 'jetpack-publicize-pkg' ),
85                array( 'status' => rest_authorization_required_code() )
86            );
87        }
88
89        return true;
90    }
91
92    /**
93     * Retrieves the JSON schema for the token generation.
94     *
95     * @return array Schema data.
96     */
97    public function get_item_schema() {
98        $schema = array(
99            '$schema'    => 'http://json-schema.org/draft-04/schema#',
100            'title'      => 'social-image-generator-token',
101            'type'       => 'object',
102            'properties' => array(
103                'text'      => array(
104                    'description' => __( 'The text to be used to generate the image.', 'jetpack-publicize-pkg' ),
105                    'type'        => 'string',
106                    'required'    => true,
107                    'context'     => array( 'edit' ),
108                ),
109                'image_url' => array(
110                    'description' => __( 'The URL of the background image to use when generating the social image.', 'jetpack-publicize-pkg' ),
111                    'type'        => 'string',
112                    'format'      => 'uri',
113                    'required'    => false,
114                    'context'     => array( 'edit' ),
115                ),
116                'template'  => array(
117                    'description' => __( 'The template slug', 'jetpack-publicize-pkg' ),
118                    'type'        => 'string',
119                    'enum'        => Templates::TEMPLATES,
120                    'required'    => false,
121                ),
122            ),
123        );
124
125        return rest_default_additional_properties_to_false( $schema );
126    }
127}