Completed
Push — dependabot/npm_and_yarn/ini-1.... ( bb632f...6a445d )
by Yaroslav
100:51 queued 92:02
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * REST API endpoint for managing VideoPress metadata.
4
 *
5
 * @package Jetpack
6
 * @since 9.3.0
7
 */
8
9
use Automattic\Jetpack\Connection\Client;
10
11
/**
12
 * VideoPress wpcom api v2 endpoint
13
 */
14
class WPCOM_REST_API_V2_Endpoint_VideoPress extends WP_REST_Controller {
15
	/**
16
	 * Constructor.
17
	 */
18
	public function __construct() {
19
		$this->namespace = 'wpcom/v2';
20
		$this->rest_base = 'videopress';
21
22
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
23
	}
24
25
	/**
26
	 * Register the route.
27
	 */
28
	public function register_routes() {
29
		register_rest_route(
30
			$this->namespace,
31
			$this->rest_base . '/meta',
32
			array(
33
				'args'                => array(
34
					'guid'          => array(
35
						'description'       => __( 'The VideoPress video guid.', 'jetpack' ),
36
						'type'              => 'string',
37
						'required'          => true,
38
						'sanitize_callback' => 'sanitize_text_field',
39
					),
40
					'title'         => array(
41
						'description'       => __( 'The title of the video.', 'jetpack' ),
42
						'type'              => 'string',
43
						'required'          => false,
44
						'sanitize_callback' => 'sanitize_text_field',
45
					),
46
					'description'   => array(
47
						'description'       => __( 'The description of the video.', 'jetpack' ),
48
						'type'              => 'string',
49
						'required'          => false,
50
						'sanitize_callback' => 'sanitize_text_field',
51
					),
52
					'rating'        => array(
53
						'description'       => __( 'The video content rating. One of G, PG-13, R-17 or X-18', 'jetpack' ),
54
						'type'              => 'string',
55
						'required'          => false,
56
						'sanitize_callback' => 'sanitize_text_field',
57
					),
58
					'display_embed' => array(
59
						'description'       => __( 'Display the share menu in the player.', 'jetpack' ),
60
						'type'              => 'boolean',
61
						'required'          => false,
62
						'sanitize_callback' => 'rest_sanitize_boolean',
63
					),
64
				),
65
				'methods'             => WP_REST_Server::EDITABLE,
66
				'callback'            => array( $this, 'videopress_block_update_meta' ),
67
				'permission_callback' => function () {
68
					return current_user_can( 'edit_posts' );
69
				},
70
			)
71
		);
72
	}
73
74
	/**
75
	 * Updates video metadata via the WPCOM REST API.
76
	 *
77
	 * @param WP_REST_Request $request the request object.
78
	 * @return object|WP_Error Success object or WP_Error with error details.
79
	 */
80
	public function videopress_block_update_meta( $request ) {
81
		$json_params = $request->get_json_params();
82
83
		$endpoint = 'videos';
84
		$args     = array(
85
			'method'  => 'POST',
86
			'headers' => array( 'content-type' => 'application/json' ),
87
		);
88
89
		$result = Client::wpcom_json_api_request_as_blog(
90
			$endpoint,
91
			'2',
92
			$args,
93
			wp_json_encode( $json_params ),
0 ignored issues
show
Security Bug introduced by
It seems like wp_json_encode($json_params) targeting wp_json_encode() can also be of type false; however, Automattic\Jetpack\Conne...n_api_request_as_blog() does only seem to accept string|null, did you maybe forget to handle an error condition?
Loading history...
94
			'wpcom'
95
		);
96
97
		if ( is_wp_error( $result ) ) {
98
			return rest_ensure_response( $result );
99
		}
100
101
		$response_body = json_decode( wp_remote_retrieve_body( $result ) );
102 View Code Duplication
		if ( is_bool( $response_body ) && $response_body ) {
103
			return rest_ensure_response(
104
				array(
105
					'code'    => 'success',
106
					'message' => __( 'Video meta updated successfully.', 'jetpack' ),
107
					'data'    => 200,
108
				)
109
			);
110
		} else {
111
			return rest_ensure_response(
112
				new WP_Error(
113
					$response_body->code,
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $response_body->code.

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...
114
					$response_body->message,
115
					$response_body->data
116
				)
117
			);
118
		}
119
	}
120
}
121
122
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_VideoPress' );
123