Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Render_Shortcode_Endpoint
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
72
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
72
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7new WPCOM_JSON_API_Render_Shortcode_Endpoint(
8    array(
9        'description'          => 'Get a rendered shortcode for a site. Note: The current user must have publishing access.',
10        'group'                => 'sites',
11        'stat'                 => 'shortcodes:render',
12        'method'               => 'GET',
13        'path'                 => '/sites/%s/shortcodes/render',
14        'path_labels'          => array(
15            '$site' => '(int|string) Site ID or domain',
16        ),
17        'query_parameters'     => array(
18            'shortcode' => '(string) The query-string encoded shortcode string to render. Required. Only accepts one at a time.',
19        ),
20        'response_format'      => array(
21            'shortcode' => '(string) The shortcode that was passed in for rendering.',
22            'result'    => '(html) The rendered HTML result of the shortcode.',
23            'scripts'   => '(array) An array of JavaScript files needed to render the shortcode. Returned in the format of <code>{ "script-slug" : { "src": "http://example.com/file.js", "extra" : "" } }</code> where extra contains any neccessary extra JS for initializing the source file and src contains the script to load. Omitted if no scripts are neccessary.',
24            'styles'    => '(array) An array of CSS files needed to render the shortcode. Returned in the format of <code>{ "style-slug" : { "src": "http://example.com/file.css", "media" : "all" } }</code>. Omitted if no styles are neccessary.',
25        ),
26        'example_request'      => 'https://public-api.wordpress.com/rest/v1/sites/82974409/shortcodes/render?shortcode=%5Bgallery%20ids%3D%22729%2C732%2C731%2C720%22%5D',
27        'example_request_data' => array(
28            'headers' => array(
29                'authorization' => 'Bearer YOUR_API_TOKEN',
30            ),
31        ),
32    )
33);
34
35/**
36 * Render shortcode endpoint class.
37 *
38 * /sites/%s/shortcodes/render -> $blog_id
39 *
40 * @phan-constructor-used-for-side-effects
41 */
42class WPCOM_JSON_API_Render_Shortcode_Endpoint extends WPCOM_JSON_API_Render_Endpoint {
43    /**
44     * The API callback.
45     *
46     * @param string $path - the path.
47     * @param int    $blog_id - the blog ID.
48     *
49     * @return array|WP_Error
50     */
51    public function callback( $path = '', $blog_id = 0 ) {
52        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
53        if ( is_wp_error( $blog_id ) ) {
54            return $blog_id;
55        }
56
57        if ( ! current_user_can( 'edit_posts' ) ) {
58            return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
59        }
60
61        $args      = $this->query_args();
62        $shortcode = trim( $args['shortcode'] );
63
64        // Quick validation - shortcodes should always be enclosed in brackets []
65        if ( ! wp_startswith( $shortcode, '[' ) || ! wp_endswith( $shortcode, ']' ) ) {
66            return new WP_Error( 'invalid_shortcode', 'The shortcode parameter must begin and end with square brackets.', 400 );
67        }
68
69        // Make sure only one shortcode is being rendered at a time
70        $pattern = get_shortcode_regex();
71        preg_match_all( "/$pattern/s", $shortcode, $matches );
72        if ( is_countable( $matches[0] ) && count( $matches[0] ) > 1 ) {
73            return new WP_Error( 'invalid_shortcode', 'Only one shortcode can be rendered at a time.', 400 );
74        }
75
76        $render = $this->process_render( array( $this, 'do_shortcode' ), $shortcode );
77
78        // if nothing happened, then the shortcode does not exist.
79        if ( $shortcode === $render['result'] ) {
80            return new WP_Error( 'invalid_shortcode', 'The requested shortcode does not exist.', 400 );
81        }
82
83        // our output for this endpoint..
84        $return              = array();
85        $return['shortcode'] = $shortcode;
86        $return['result']    = $render['result'];
87
88        $return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] );
89
90        return $return;
91    }
92}