Completed
Push — add/safe-register-block-type-f... ( 08e7bc...86d3d3 )
by Bernhard
09:35 queued 02:05
created

update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Extend the REST API functionality for VideoPress users.
4
 *
5
 * @package Jetpack
6
 */
7
8
/**
9
 * Add per-attachment VideoPress data.
10
 *
11
 * { # Attachment Object
12
 *   ...
13
 *   jetpack_videopress_guid: (string) VideoPress identifier
14
 *   ...
15
 * }
16
 *
17
 * @since 7.1.0
18
 */
19
class WPCOM_REST_API_V2_Attachment_VideoPress_Field extends WPCOM_REST_API_V2_Field_Controller {
20
	/**
21
	 * The REST Object Type to which the jetpack_videopress_guid field will be added.
22
	 *
23
	 * @var string
24
	 */
25
	protected $object_type = 'attachment';
26
27
	/**
28
	 * The name of the REST API field to add.
29
	 *
30
	 * @var string $field_name
31
	 */
32
	protected $field_name = 'jetpack_videopress_guid';
33
34
	/**
35
	 * Registers the jetpack_videopress field and adds a filter to remove it for attachments that are not videos.
36
	 */
37
	public function register_fields() {
38
		parent::register_fields();
39
40
		add_filter( 'rest_prepare_attachment', array( $this, 'remove_field_for_non_videos' ), 10, 2 );
41
	}
42
43
	/**
44
	 * Defines data structure and what elements are visible in which contexts
45
	 */
46
	public function get_schema() {
47
		return array(
48
			'$schema'     => 'http://json-schema.org/draft-04/schema#',
49
			'title'       => $this->field_name,
50
			'type'        => 'string',
51
			'context'     => array( 'view', 'edit' ),
52
			'readonly'    => true,
53
			'description' => __( 'Unique VideoPress ID', 'jetpack' ),
54
		);
55
	}
56
57
	/**
58
	 * Getter: Retrieve current VideoPress data for a given attachment.
59
	 *
60
	 * @param array           $attachment Response from the attachment endpoint.
61
	 * @param WP_REST_Request $request Request to the attachment endpoint.
62
	 *
63
	 * @return string
64
	 */
65
	public function get( $attachment, $request ) {
66
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
67
			$blog_id = get_current_blog_id();
68
		} else {
69
			$blog_id = Jetpack_Options::get_option( 'id' );
70
		}
71
72
		$post_id = absint( $attachment['id'] );
73
74
		$videopress_guid = $this->get_videopress_guid( $post_id, $blog_id );
75
76
		if ( ! $videopress_guid ) {
77
			return '';
78
		}
79
80
		return $videopress_guid;
81
	}
82
83
	/**
84
	 * Gets the VideoPress GUID for a given attachment.
85
	 *
86
	 * This is pulled out into a separate method to support unit test mocking.
87
	 *
88
	 * @param int $attachment_id Attachment ID.
89
	 * @param int $blog_id Blog ID.
90
	 *
91
	 * @return string
92
	 */
93
	public function get_videopress_guid( $attachment_id, $blog_id ) {
94
		return video_get_info_by_blogpostid( $blog_id, $attachment_id )->guid;
95
	}
96
97
	/**
98
	 * Checks if the given attachment is a video.
99
	 *
100
	 * @param object $attachment The attachment object.
101
	 *
102
	 * @return false|int
103
	 */
104
	public function is_video( $attachment ) {
105
		return wp_startswith( $attachment->post_mime_type, 'video/' );
106
	}
107
108
	/**
109
	 * Removes the jetpack_videopress_guid field from the response if the
110
	 * given attachment is not a video.
111
	 *
112
	 * @param WP_REST_Response $response Response from the attachment endpoint.
113
	 * @param WP_Post          $attachment The original attachment object.
114
	 *
115
	 * @return mixed
116
	 */
117
	public function remove_field_for_non_videos( $response, $attachment ) {
118
		if ( ! $this->is_video( $attachment ) ) {
119
			unset( $response->data[ $this->field_name ] );
120
		}
121
122
		return $response;
123
	}
124
125
	/**
126
	 * Setter: It does nothing since `jetpack_videopress` is a read-only field.
127
	 *
128
	 * @param mixed           $value The new value for the field.
129
	 * @param WP_Post         $object The attachment object.
130
	 * @param WP_REST_Request $request The request object.
131
	 *
132
	 * @return null
133
	 */
134
	public function update( $value, $object, $request ) {
135
		return null;
136
	}
137
138
	/**
139
	 * Permission Check for the field's getter. Delegate the responsibility to the
140
	 * attachment endpoint, so it always returns true.
141
	 *
142
	 * @param mixed           $object Response from the attachment endpoint.
143
	 * @param WP_REST_Request $request Request to the attachment endpoint.
144
	 *
145
	 * @return true
146
	 */
147
	public function get_permission_check( $object, $request ) {
148
		return true;
149
	}
150
151
	/**
152
	 * Permission Check for the field's setter. Delegate the responsibility to the
153
	 * attachment endpoint, so it always returns true.
154
	 *
155
	 * @param mixed           $value The new value for the field.
156
	 * @param WP_Post         $object The attachment object.
157
	 * @param WP_REST_Request $request Request to the attachment endpoint.
158
	 *
159
	 * @return true
160
	 */
161
	public function update_permission_check( $value, $object, $request ) {
162
		return true;
163
	}
164
}
165
166
if (
167
	( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) ||
168
	( defined( 'IS_WPCOM' ) && IS_WPCOM )
169
) {
170
	wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Attachment_VideoPress_Field' );
171
}
172