Completed
Push — renovate/gridicons-3.x ( c004c1...f8ccd4 )
by
unknown
284:06 queued 275:32
created

VideoPress_Edit_Attachment::save_fields()   F

Complexity

Conditions 17
Paths 652

Size

Total Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

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