Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

VideoPress_Edit_Attachment::save_fields()   F

Complexity

Conditions 17
Paths 652

Size

Total Lines 75
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 38
nc 652
nop 2
dl 0
loc 75
rs 2.6666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * VideoPress edit attachment screen
4
 *
5
 * @since 4.1
6
 */
7
class VideoPress_Edit_Attachment {
8
9
	/**
10
	 * Singleton method to initialize the object only once.
11
	 *
12
	 * @return VideoPress_Edit_Attachment
13
	 */
14
	public static function init() {
15
		static $instance = null;
16
17
		if ( ! $instance ) {
18
			$instance = new VideoPress_Edit_Attachment();
19
		}
20
21
		return $instance;
22
	}
23
24
	/**
25
	 * VideoPress_Edit_Attachment constructor.
26
	 *
27
	 * Adds in appropriate actions for attachment fields editor, meta boxes and saving.
28
	 */
29
	public function __construct() {
30
		add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 );
31
		add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
32
		add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 );
33
		add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 );
34
35
		add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 );
36
	}
37
38
	/**
39
	 * @param string  $post_type
0 ignored issues
show
Documentation introduced by
Should the type for parameter $post not be object|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
	 * @param object  $post
41
	 */
42
	public function configure_meta_boxes( $post_type = 'unknown', $post = NULL ) {
43
		if ( NULL == $post ) {
44
			$post = (object) array ( 'ID' => 0 );
45
		}
46
47
		if ( 'attachment' != $post_type ) {
48
			return;
49
		}
50
51
		$meta = wp_get_attachment_metadata( $post->ID );
52
53
		// If this has not been processed by videopress, we can skip the rest.
54
		if ( ! isset( $meta['videopress'] ) ) {
55
			return;
56
		}
57
58
		add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' );
59
	}
60
61
	/**
62
	 * @param array $post
63
	 * @param array|null $attachment
64
	 *
65
	 * @return array
66
	 */
67
	public function save_fields( $post, $attachment = null ) {
68
		if ( $attachment === null && isset( $_POST['attachment'] ) ) {
69
			$attachment = $_POST['attachment'];
70
		}
71
72
		if ( ! isset( $attachment['is_videopress_attachment'] ) || $attachment['is_videopress_attachment'] !== 'yes' ) {
73
			return $post;
74
		}
75
76
		$post_id = absint( $post['ID'] );
77
78
		$meta = wp_get_attachment_metadata( $post_id );
79
80
		// If this has not been processed by videopress, we can skip the rest.
81
		if ( ! isset( $meta['videopress'] ) ) {
82
			return $post;
83
		}
84
85
		$values = array();
86
87
		// Add the video title & description in, so that we save it properly.
88
		if ( isset( $_POST['post_title'] ) ) {
89
			$values['title'] = trim( strip_tags( $_POST['post_title'] ) );
90
		}
91
92
		if ( isset( $_POST['post_excerpt'] ) ) {
93
			$values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) );
94
		}
95
96
		if ( isset( $attachment['rating'] ) ) {
97
			$rating = $attachment['rating'];
98
99
			if ( ! empty( $rating ) && in_array( $rating, array( 'G', 'PG-13', 'R-17', 'X-18' ) ) ) {
100
				$values['rating'] = $rating;
101
			}
102
		}
103
104
		// We set a default here, as if it isn't selected, then we'll turn it off.
105
		$values['display_embed'] = 0;
106
		if ( isset( $attachment['display_embed'] ) ) {
107
			$display_embed = $attachment['display_embed'];
108
109
			$values['display_embed'] = 'on' === $display_embed  ? 1 : 0;
110
		}
111
112
		$args = array(
113
			'method'  => 'POST',
114
		);
115
116
		$endpoint = "videos/{$meta['videopress']['guid']}";
117
		$result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_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...
118
119
		if ( is_wp_error( $result ) ) {
120
			$post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack' );
121
			return $post;
122
		}
123
124
		if ( isset( $values['display_embed'] ) ) {
125
			$meta['videopress']['display_embed'] = $values['display_embed'];
126
		}
127
128
		if ( isset( $values['rating'] ) ) {
129
			$meta['videopress']['rating'] = $values['rating'];
130
		}
131
132
		wp_update_attachment_metadata( $post_id, $meta );
133
134
		$response = json_decode( $result['body'], true );
135
136
		if ( 'true' !== $response ) {
137
			return $post;
138
		}
139
140
		return $post;
141
	}
142
143
144
	/**
145
	 * Get the upload api path.
146
	 *
147
	 * @param string $guid
148
	 * @return string
149
	 */
150
	public function make_video_api_path( $guid ) {
151
		return sprintf(
152
			'%s://%s/rest/v%s/videos/%s',
153
			'https',
154
			'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST,
155
			Jetpack_Client::WPCOM_JSON_API_VERSION,
156
			$guid
157
		);
158
	}
159
160
161
	/**
162
	 * Creates an array of video fields to edit based on transcoded videos.
163
	 *
164
	 * @param array $fields video fields of interest
165
	 * @param stdClass $post post object
166
	 * @return array modified version of video fields for administrative interface display
167
	 */
168
	public function fields_to_edit( $fields, $post ) {
169
		$post_id = absint( $post->ID );
170
171
		$meta = wp_get_attachment_metadata( $post_id );
172
173
		// If this has not been processed by videopress, we can skip the rest.
174
		if ( ! isset( $meta['videopress'] ) ) {
175
			return $fields;
176
		}
177
178
		$info = (object) $meta['videopress'];
179
180
		unset( $fields['url'] );
181
		unset( $fields['post_content'] );
182
183
		if ( isset( $info->files_status['std']['ogg'] ) && 'done' ===  $info->files_status['std']['ogg'] ) {
184
			$v_name  = preg_replace( '/\.\w+/', '', basename( $info->path ) );
185
			$video_name = $v_name . '_fmt1.ogv';
186
			$ogg_url  = videopress_cdn_file_url( $info->guid, $video_name );
187
188
			$fields['video-ogg'] = array(
189
				'label' => __( 'Ogg File URL', 'jetpack' ),
190
				'input' => 'html',
191
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
192
				'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
193
			);
194
		}
195
196
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
197
198
		$fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
199
		$fields['post_excerpt']['input'] = 'textarea';
200
		$fields['post_excerpt']['value'] = $info->description;
201
202
		$fields['is_videopress_attachment'] = array(
203
			'input' => 'hidden',
204
			'value' => 'yes',
205
		);
206
207
		$fields['videopress_shortcode'] = array(
208
			'label'         => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
209
			'input'         => 'html',
210
			'html'          => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$info->guid}]\" readonly=\"readonly\"/>",
211
			'show_in_modal' => true,
212
			'show_in_edit'  => false,
213
		);
214
215
		$fields['display_embed'] = array(
216
			'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
217
			'input' => 'html',
218
			'html'  => $this->display_embed_choice( $info )
219
		);
220
221
		$fields['video-rating'] = array(
222
			'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
223
			'input' => 'html',
224
			'html'  => $this->display_rating( $info )
225
		);
226
227
		return $fields;
228
	}
229
230
	/**
231
	 * @param stdClass $post
232
	 */
233
	public function videopress_information_box( $post ) {
234
		$post_id = absint( $post->ID );
235
236
		$meta = wp_get_attachment_metadata( $post_id );
237
238
		// If this has not been processed by videopress, we can skip the rest.
239
		if ( ! isset( $meta['videopress'] ) ) {
240
			return;
241
		}
242
243
		$info = (object) $meta['videopress'];
244
245
		$status = videopress_get_transcoding_status( $post_id );
246
247
		$formats = array(
248
			'std_mp4' => 'Standard MP4',
249
			'std_ogg' => 'OGG Vorbis',
250
			'dvd_mp4' => 'DVD',
251
			'hd_mp4'  => 'High Definition',
252
		);
253
254
		$embed = "[videopress {$info->guid}]";
255
256
		$shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />';
257
258
		$trans_status = '';
259
		$all_trans_done = true;
260
		foreach ( $formats as $status_key => $name ) {
261
			if ( 'DONE' !== $status[ $status_key ] ) {
262
				$all_trans_done = false;
263
			}
264
265
			$trans_status .= '- <strong>' . $name . ":</strong> <span id=\"status_$status_key\">" . ( 'DONE' === $status[ $status_key ]  ? 'Done' : 'Processing' ) . '</span><br>';
266
		}
267
268
		$nonce = wp_create_nonce( 'videopress-update-transcoding-status' );
269
270
		$url = 'empty';
271
		if ( ! empty( $info->guid ) ) {
272
			$url = videopress_build_url( $info->guid );
273
			$url = "<a href=\"{$url}\">{$url}</a>";
274
		}
275
276
		$poster = '<em>Still Processing</em>';
277
		if ( ! empty( $info->poster ) ) {
278
			$poster = "<br><img src=\"{$info->poster}\" width=\"175px\">";
279
		}
280
281
		$status_update = '';
282
		if ( ! $all_trans_done ) {
283
			$status_update = ' (<a href="javascript:;" id="videopress-update-transcoding-status">update</a>)';
284
		}
285
286
		$html = <<< HTML
287
288
<div class="misc-pub-section misc-pub-shortcode">
289
	<strong>Shortcode</strong><br>
290
	{$shortcode}
291
</div> 
292
<div class="misc-pub-section misc-pub-url">
293
	<strong>Url</strong>
294
	{$url}
295
</div> 
296
<div class="misc-pub-section misc-pub-poster">
297
	<strong>Poster</strong>
298
	{$poster}
299
</div>
300
<div class="misc-pub-section misc-pub-status">
301
	<strong>Transcoding Status$status_update:</strong>
302
	<div id="videopress-transcoding-status">{$trans_status}</div>
303
</div>
304
305
306
307
<script>
308
	jQuery( function($) {
309
		$( '#videopress-update-transcoding-status' ).on( "click", function() {
310
			jQuery.ajax( {
311
				type: 'post',
312
				url: 'admin-ajax.php',
313
				data: { 
314
					action: 'videopress-update-transcoding-status',
315
					post_id: '{$post_id}',
316
					_ajax_nonce: '{$nonce}' 
317
				},
318
				complete: function( response ) {
319
					if ( 200 === response.status ) {
320
						var statuses = response.responseJSON.data.status;
321
322
						for (var key in statuses) {
323
							$('#status_' + key).text( 'DONE' === statuses[key] ? 'Done' : 'Processing' );
324
						}
325
					}
326
				}
327
			});
328
		} );
329
	} );
330
</script>
331
HTML;
332
333
		echo $html;
334
	}
335
336
	/**
337
	 * Build HTML to display a form checkbox for embedcode display preference
338
	 *
339
	 * @param object $info database row from the videos table
340
	 * @return string input element of type checkbox set to checked state based on stored embed preference
341
	 */
342
	protected function display_embed_choice( $info ) {
343
		$id = "attachments-{$info->post_id}-displayembed";
344
		$out  = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
345
		if ( $info->display_embed )
346
			$out .= ' checked="checked"';
347
		$out .= " />" . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
348
		return $out;
349
	}
350
351
	/**
352
	 * Build HTML to display a form input radio button for video ratings
353
	 *
354
	 * @param object $info database row from the videos table
355
	 * @return string input elements of type radio with existing stored value selected
356
	 */
357
	protected function display_rating( $info ) {
358
		$out = '';
359
360
		$ratings = array(
361
			'G'     => 'G',
362
			'PG-13' => 'PG-13',
363
			'R-17'  => 'R',
364
			'X-18'  => 'X',
365
		);
366
367
		foreach( $ratings as $r => $label ) {
368
			$id = "attachments-{$info->post_id}-rating-$r";
369
			$out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
370
			if ( $info->rating == $r ) {
371
				$out .= ' checked="checked"';
372
			}
373
374
			$out .= " />$label</label>";
375
			unset( $id );
376
		}
377
378
		return $out;
379
	}
380
}
381
382
// Let's start this thing up.
383
VideoPress_Edit_Attachment::init();