Completed
Push — fix/videopress-media-detail-vi... ( 1fcb41 )
by
unknown
77:39 queued 67:55
created

VideoPress_Edit_Attachment   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 248
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 248
rs 9.84
c 0
b 0
f 0
wmc 32
lcom 0
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 9 2
A __construct() 0 6 1
F save_fields() 0 77 17
A make_video_api_path() 0 8 1
B fields_to_edit() 0 64 6
A display_embed_choice() 0 9 2
A display_rating() 0 23 3
1
<?php
2
3
use Automattic\Jetpack\Connection\Client;
4
5
/**
6
 * VideoPress edit attachment screen
7
 *
8
 * @since 4.1
9
 */
10
class VideoPress_Edit_Attachment {
11
12
	/**
13
	 * Singleton method to initialize the object only once.
14
	 *
15
	 * @return VideoPress_Edit_Attachment
16
	 */
17
	public static function init() {
18
		static $instance = null;
19
20
		if ( ! $instance ) {
21
			$instance = new VideoPress_Edit_Attachment();
22
		}
23
24
		return $instance;
25
	}
26
27
	/**
28
	 * VideoPress_Edit_Attachment constructor.
29
	 *
30
	 * Adds in appropriate actions for attachment fields editor, meta boxes and saving.
31
	 */
32
	public function __construct() {
33
		add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 );
34
		add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
35
		add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 );
36
		add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 );
37
	}
38
39
	/**
40
	 * @param array      $post
41
	 * @param array|null $attachment
42
	 *
43
	 * @return array
44
	 */
45
	public function save_fields( $post, $attachment = null ) {
46
		if ( $attachment === null && isset( $_POST['attachment'] ) ) {
47
			$attachment = $_POST['attachment'];
48
		}
49
50
		if ( ! isset( $attachment['is_videopress_attachment'] ) || $attachment['is_videopress_attachment'] !== 'yes' ) {
51
			return $post;
52
		}
53
54
		$post_id = absint( $post['ID'] );
55
56
		$meta = wp_get_attachment_metadata( $post_id );
57
58
		// If this has not been processed by videopress, we can skip the rest.
59
		if ( ! is_videopress_attachment( $post['ID'] ) ) {
60
			return $post;
61
		}
62
63
		$values = array();
64
65
		// Add the video title & description in, so that we save it properly.
66
		if ( isset( $_POST['post_title'] ) ) {
67
			$values['title'] = trim( strip_tags( $_POST['post_title'] ) );
68
		}
69
70
		if ( isset( $_POST['post_excerpt'] ) ) {
71
			$values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) );
72
		}
73
74
		if ( isset( $attachment['rating'] ) ) {
75
			$rating = $attachment['rating'];
76
77
			if ( ! empty( $rating ) && videopress_is_valid_video_rating( $rating ) ) {
78
				$values['rating'] = $rating;
79
			}
80
		}
81
82
		// We set a default here, as if it isn't selected, then we'll turn it off.
83
		$values['display_embed'] = 0;
84
		if ( isset( $attachment['display_embed'] ) ) {
85
			$display_embed = $attachment['display_embed'];
86
87
			$values['display_embed'] = 'on' === $display_embed ? 1 : 0;
88
		}
89
90
		$args = array(
91
			'method' => 'POST',
92
		);
93
94
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
95
96
		$endpoint = "videos/{$guid}";
97
		$result   = Client::wpcom_json_api_request_as_blog( $endpoint, Client::WPCOM_JSON_API_VERSION, $args, $values );
0 ignored issues
show
Documentation introduced by
$values is of type array<string,integer,{"display_embed":"integer"}>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
99
		if ( is_wp_error( $result ) ) {
100
			$post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack' );
101
			return $post;
102
		}
103
104
		if ( isset( $values['display_embed'] ) ) {
105
			$meta['videopress']['display_embed'] = $values['display_embed'];
106
		}
107
108
		if ( isset( $values['rating'] ) ) {
109
			$meta['videopress']['rating'] = $values['rating'];
110
		}
111
112
		wp_update_attachment_metadata( $post_id, $meta );
113
114
		$response = json_decode( $result['body'], true );
115
116
		if ( 'true' !== $response ) {
117
			return $post;
118
		}
119
120
		return $post;
121
	}
122
123
124
	/**
125
	 * Get the upload api path.
126
	 *
127
	 * @param string $guid
128
	 * @return string
129
	 */
130
	public function make_video_api_path( $guid ) {
131
		return sprintf(
132
			'%s/rest/v%s/videos/%s',
133
			JETPACK__WPCOM_JSON_API_BASE,
134
			Client::WPCOM_JSON_API_VERSION,
135
			$guid
136
		);
137
	}
138
139
140
	/**
141
	 * Creates an array of video fields to edit based on transcoded videos.
142
	 *
143
	 * @param array    $fields video fields of interest
144
	 * @param stdClass $post post object
145
	 * @return array modified version of video fields for administrative interface display
146
	 */
147
	public function fields_to_edit( $fields, $post ) {
148
		$post_id = absint( $post->ID );
149
150
		$meta = wp_get_attachment_metadata( $post_id );
151
152
		// If this has not been processed by videopress, we can skip the rest.
153
		if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) {
154
			return $fields;
155
		}
156
157
		$info          = (object) $meta['videopress'];
158
		$file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array();
159
160
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
161
162
		unset( $fields['url'] );
163
		unset( $fields['post_content'] );
164
165
		if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) {
166
			$v_name     = preg_replace( '/\.\w+/', '', basename( $info->path ) );
167
			$video_name = $v_name . '_fmt1.ogv';
168
			$ogg_url    = videopress_cdn_file_url( $guid, $video_name );
169
170
			$fields['video-ogg'] = array(
171
				'label' => __( 'Ogg File URL', 'jetpack' ),
172
				'input' => 'html',
173
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
174
				'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
175
			);
176
		}
177
178
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
179
180
		$fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
181
		$fields['post_excerpt']['input'] = 'textarea';
182
		$fields['post_excerpt']['value'] = $info->description;
183
184
		$fields['is_videopress_attachment'] = array(
185
			'input' => 'hidden',
186
			'value' => 'yes',
187
		);
188
189
		$fields['videopress_shortcode'] = array(
190
			'label'         => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
191
			'input'         => 'html',
192
			'html'          => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>",
193
			'show_in_modal' => true,
194
			'show_in_edit'  => false,
195
		);
196
197
		$fields['display_embed'] = array(
198
			'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
199
			'input' => 'html',
200
			'html'  => $this->display_embed_choice( $info ),
201
		);
202
203
		$fields['video-rating'] = array(
204
			'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
205
			'input' => 'html',
206
			'html'  => $this->display_rating( $info ),
207
		);
208
209
		return $fields;
210
	}
211
212
	/**
213
	 * Build HTML to display a form checkbox for embedcode display preference
214
	 *
215
	 * @param object $info database row from the videos table
216
	 * @return string input element of type checkbox set to checked state based on stored embed preference
217
	 */
218
	protected function display_embed_choice( $info ) {
219
		$id  = "attachments-{$info->post_id}-displayembed";
220
		$out = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
221
		if ( $info->display_embed ) {
222
			$out .= ' checked="checked"';
223
		}
224
		$out .= ' />' . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
225
		return $out;
226
	}
227
228
	/**
229
	 * Build HTML to display a form input radio button for video ratings
230
	 *
231
	 * @param object $info database row from the videos table
232
	 * @return string input elements of type radio with existing stored value selected
233
	 */
234
	protected function display_rating( $info ) {
235
		$out = '';
236
237
		$ratings = array(
238
			'G'     => 'G',
239
			'PG-13' => 'PG-13',
240
			'R-17'  => 'R',
241
			'X-18'  => 'X',
242
		);
243
244
		foreach ( $ratings as $r => $label ) {
245
			$id   = "attachments-{$info->post_id}-rating-$r";
246
			$out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
247
			if ( $info->rating == $r ) {
248
				$out .= ' checked="checked"';
249
			}
250
251
			$out .= " />$label</label>";
252
			unset( $id );
253
		}
254
255
		return $out;
256
	}
257
}
258
259
// Let's start this thing up.
260
VideoPress_Edit_Attachment::init();
261