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