Completed
Push — update/dialogue-focus-on-conte... ( 9f1745...fa862f )
by
unknown
80:03 queued 71:18
created

class.wpcom-json-api-render-shortcode-endpoint.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_Render_Shortcode_Endpoint( array(
4
	'description' => "Get a rendered shortcode for a site. Note: The current user must have publishing access.",
5
	'group'       => 'sites',
6
	'stat'        => 'shortcodes:render',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/shortcodes/render',
9
	'path_labels' => array(
10
		'$site'    => '(int|string) Site ID or domain',
11
	),
12
	'query_parameters' => array(
13
		'shortcode'     => '(string) The query-string encoded shortcode string to render. Required. Only accepts one at a time.',
14
	),
15
	'response_format' => array(
16
		'shortcode' => '(string) The shortcode that was passed in for rendering.',
17
		'result'    => '(html) The rendered HTML result of the shortcode.',
18
		'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.',
19
		'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.',
20
	),
21
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/82974409/shortcodes/render?shortcode=%5Bgallery%20ids%3D%22729%2C732%2C731%2C720%22%5D',
22
	'example_request_data' => array(
23
		'headers' => array(
24
			'authorization' => 'Bearer YOUR_API_TOKEN'
25
		),
26
	)
27
) );
28
29
class WPCOM_JSON_API_Render_Shortcode_Endpoint extends WPCOM_JSON_API_Render_Endpoint {
30
	// /sites/%s/shortcodes/render -> $blog_id
31
	function callback( $path = '', $blog_id = 0 ) {
32
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
33
		if ( is_wp_error( $blog_id ) ) {
34
			return $blog_id;
35
		}
36
37
		if ( ! current_user_can( 'edit_posts' ) ) {
38
			return new WP_Error( 'unauthorized', 'Your token must have permission to post on this blog.', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
		}
40
41
		$args = $this->query_args();
42
		$shortcode = trim( $args['shortcode'] );
43
44
		// Quick validation - shortcodes should always be enclosed in brackets []
45
		if ( ! wp_startswith( $shortcode, '[' ) || ! wp_endswith( $shortcode, ']' ) ) {
46
			return new WP_Error( 'invalid_shortcode',  'The shortcode parameter must begin and end with square brackets.', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_shortcode'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47
		}
48
49
		// Make sure only one shortcode is being rendered at a time
50
		$pattern = get_shortcode_regex();
51
		preg_match_all( "/$pattern/s", $shortcode, $matches );
52
		if ( count( $matches[0] ) > 1 ) {
53
			return new WP_Error( 'invalid_shortcode',  'Only one shortcode can be rendered at a time.', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_shortcode'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
54
		}
55
56
		$render = $this->process_render( array( $this, 'do_shortcode' ), $shortcode );
57
58
		// if nothing happened, then the shortcode does not exist.
59
		if ( $shortcode == $render['result'] ) {
60
			return new WP_Error( 'invalid_shortcode',  'The requested shortcode does not exist.', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_shortcode'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
		}
62
63
		// our output for this endpoint..
64
		$return['shortcode'] = $shortcode;
65
		$return['result'] = $render['result'];
66
67
		$return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] );
68
69
		return $return;
70
	}
71
}