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