Completed
Push — feature/reorg ( 58a363...38f0cd )
by
unknown
104:53 queued 95:15
created

get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 17
loc 17
rs 9.7
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: (object) VideoPress data
14
 *   ...
15
 * }
16
 *
17
 * @since 7.1.0
18
 */
19
class WPCOM_REST_API_V2_Attachment_VideoPress_Data extends WPCOM_REST_API_V2_Field_Controller {
20
	/**
21
	 * The REST Object Type to which the jetpack_videopress 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';
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 View Code Duplication
	public function get_schema() {
47
		return array(
48
			'$schema'     => 'http://json-schema.org/draft-04/schema#',
49
			'title'       => $this->field_name,
50
			'type'        => 'object',
51
			'context'     => array( 'view', 'edit' ),
52
			'readonly'    => true,
53
			'description' => __( 'VideoPress Data', '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 View Code Duplication
	public function get( $attachment, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
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 = $this->get_videopress_data( $post_id, $blog_id );
75
76
		if ( ! $videopress ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $videopress of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
77
			return array();
78
		}
79
80
		return $videopress;
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_data( $attachment_id, $blog_id ) {
94
		$info = video_get_info_by_blogpostid( $blog_id, $attachment_id );
95
		return array(
96
			'guid'   => $info->guid,
97
			'rating' => $info->rating,
98
		);
99
	}
100
101
	/**
102
	 * Checks if the given attachment is a video.
103
	 *
104
	 * @param object $attachment The attachment object.
105
	 *
106
	 * @return false|int
107
	 */
108
	public function is_video( $attachment ) {
109
		return isset( $attachment->post_mime_type ) && wp_startswith( $attachment->post_mime_type, 'video/' );
110
	}
111
112
	/**
113
	 * Removes the jetpack_videopress field from the response if the
114
	 * given attachment is not a video.
115
	 *
116
	 * @param WP_REST_Response $response Response from the attachment endpoint.
117
	 * @param WP_Post          $attachment The original attachment object.
118
	 *
119
	 * @return mixed
120
	 */
121
	public function remove_field_for_non_videos( $response, $attachment ) {
122
		if ( ! $this->is_video( $attachment ) ) {
123
			unset( $response->data[ $this->field_name ] );
124
		}
125
126
		return $response;
127
	}
128
129
	/**
130
	 * Setter: It does nothing since `jetpack_videopress` is a read-only field.
131
	 *
132
	 * @param mixed           $value The new value for the field.
133
	 * @param WP_Post         $object The attachment object.
134
	 * @param WP_REST_Request $request The request object.
135
	 *
136
	 * @return null
137
	 */
138
	public function update( $value, $object, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
139
		return null;
140
	}
141
142
	/**
143
	 * Permission Check for the field's getter. Delegate the responsibility to the
144
	 * attachment endpoint, so it always returns true.
145
	 *
146
	 * @param mixed           $object Response from the attachment endpoint.
147
	 * @param WP_REST_Request $request Request to the attachment endpoint.
148
	 *
149
	 * @return true
150
	 */
151
	public function get_permission_check( $object, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
152
		return true;
153
	}
154
155
	/**
156
	 * Permission Check for the field's setter. Delegate the responsibility to the
157
	 * attachment endpoint, so it always returns true.
158
	 *
159
	 * @param mixed           $value The new value for the field.
160
	 * @param WP_Post         $object The attachment object.
161
	 * @param WP_REST_Request $request Request to the attachment endpoint.
162
	 *
163
	 * @return true
164
	 */
165
	public function update_permission_check( $value, $object, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
166
		return true;
167
	}
168
}
169
170 View Code Duplication
if (
171
	( method_exists( 'Jetpack', 'is_active' ) && Jetpack::is_active() ) ||
172
	( defined( 'IS_WPCOM' ) && IS_WPCOM )
173
) {
174
	wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Attachment_VideoPress_Data' );
175
}
176