Completed
Push — fix/instant-search-non-ascii ( b82854...f54339 )
by
unknown
18:41 queued 10:23
created

WPCOM_JSON_API_Render_Embed_Endpoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B callback() 0 45 8
1
<?php
2
3
new WPCOM_JSON_API_Render_Embed_Endpoint( array(
4
	'description' => "Get a rendered embed for a site. Note: The current user must have publishing access.",
5
	'group'       => 'sites',
6
	'stat'        => 'embeds:render',
7
	'method'      => 'GET',
8
	'path'        => '/sites/%s/embeds/render',
9
	'path_labels' => array(
10
		'$site'    => '(int|string) Site ID or domain',
11
	),
12
	'query_parameters' => array(
13
		'embed_url'     => '(string) The query-string encoded embed URL to render. Required. Only accepts one at a time.',
14
	),
15
	'response_format' => array(
16
		'embed_url' => '(string) The embed_url that was passed in for rendering.',
17
		'result'    => '(html) The rendered HTML result of the embed.',
18
	),
19
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/apiexamples.wordpress.com/embeds/render?embed_url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DSQEQr7c0-dw',
20
	'example_request_data' => array(
21
		'headers' => array(
22
			'authorization' => 'Bearer YOUR_API_TOKEN'
23
		),
24
	)
25
) );
26
27
class WPCOM_JSON_API_Render_Embed_Endpoint extends WPCOM_JSON_API_Render_Endpoint {
28
	// /sites/%s/embeds/render -> $blog_id
29
	function callback( $path = '', $blog_id = 0 ) {
30
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
31
		if ( is_wp_error( $blog_id ) ) {
32
			return $blog_id;
33
		}
34
35
		if ( ! current_user_can( 'edit_posts' ) ) {
36
			return new WP_Error( 'unauthorized', __( 'Your token must have permission to post on this blog.', 'jetpack' ), 403 );
0 ignored issues
show
Unused Code introduced by
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...
37
		}
38
39
		$args = $this->query_args();
40
		$embed_url = trim( $args['embed_url'] );
41
42
		// quick validation
43
		if ( ! preg_match_all( '|^\s*(https?://[^\s"]+)\s*$|im', $embed_url, $matches ) ) {
44
			return new WP_Error( 'invalid_embed_url', __( 'The embed_url parameter must be a valid URL.', 'jetpack' ), 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_embed_url'.

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...
45
		}
46
47
		if ( count( $matches[1] ) > 1 ) {
48
			return new WP_Error( 'invalid_embed',  __( 'Only one embed can be rendered at a time.', 'jetpack' ), 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_embed'.

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...
49
		}
50
51
		$embed_url = array_shift( $matches[1] );
52
		$parts = wp_parse_url( $embed_url );
53
		if ( ! $parts ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parts of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54
			return new WP_Error( 'invalid_embed_url', __( 'The embed_url parameter must be a valid URL.', 'jetpack' ), 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_embed_url'.

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...
55
		}
56
57
		global $wp_embed;
58
		$render = $this->process_render( array( $this, 'do_embed' ), $embed_url );
59
60
		// if nothing happened, then the shortcode does not exist.
61
		$is_an_embed = ( $embed_url != $render['result'] && $wp_embed->maybe_make_link( $embed_url ) != $render['result'] );
62
		if ( ! $is_an_embed ) {
63
			return new WP_Error( 'invalid_embed',  __( 'The requested URL is not an embed.', 'jetpack' ), 400 );
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'invalid_embed'.

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...
64
		}
65
66
		// our output for this endpoint..
67
		$return['embed_url'] = $embed_url;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$return was never initialized. Although not strictly required by PHP, it is generally a good practice to add $return = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
68
		$return['result'] = $render['result'];
69
70
		$return = $this->add_assets( $return, $render['loaded_scripts'], $render['loaded_styles'] );
71
72
		return $return;
73
	}
74
75
}
76