Completed
Push — update/video-block-json-endpoi... ( 66777e )
by
unknown
08:28
created

videopress.php ➔ videopress_block_update_meta()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
namespace Automattic\Jetpack\Extensions\VideoPress;
3
4
use Automattic\Jetpack\Connection\Client;
5
6
const FEATURE_NAME = 'videopress';
7
const BLOCK_NAME   = 'jetpack/' . FEATURE_NAME;
8
9
/**
10
 * Updates video metadata via the WPCOM REST API.
11
 * @param $request WP_REST_Request object
12
 *
13
 * @return bool If the request was successful
14
 */
15
function videopress_block_update_meta( $request ) {
16
	$json_params = $request->get_json_params();
17
	if ( ! isset( $json_params ) || ! isset( $json_params['guid'] ) ) {
18
		return false;
19
	}
20
21
	$endpoint = "videos/{$json_params['guid']}";
22
	$args = array(
23
		'method' => 'POST',
24
		'headers' => array( 'Content-Type' => 'application/json' ),
25
	);
26
27
	$result = Client::wpcom_json_api_request_as_blog(
28
		$endpoint,
29
		Client::WPCOM_JSON_API_VERSION,
30
		$args,
31
		json_encode( $json_params )
32
	);
33
34
	if ( is_wp_error( $result ) ) {
35
		return false;
36
	}
37
38
	return true;
39
}
40
41
add_action( 'rest_api_init', function () {
42
	register_rest_route( 'wpcom/v2', '/videopress/meta', array(
43
		'methods' => 'POST',
44
		'callback' => __NAMESPACE__ . '\videopress_block_update_meta',
45
		'permission_callback' => function () {
46
			return current_user_can( 'edit_posts' );
47
		},
48
	) );
49
} );
50