Completed
Push — update/phpcs-comment-likes ( acaac7...fc086f )
by
unknown
465:39 queued 455:18
created

VideoPress_Edit_Attachment::fields_to_edit()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 2
dl 0
loc 64
rs 8.1632
c 0
b 0
f 0

How to fix   Long Method   

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 ) && videopress_is_valid_video_rating( $rating ) ) {
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/rest/v%s/videos/%s',
156
			JETPACK__WPCOM_JSON_API_BASE,
157
			Client::WPCOM_JSON_API_VERSION,
158
			$guid
159
		);
160
	}
161
162
163
	/**
164
	 * Creates an array of video fields to edit based on transcoded videos.
165
	 *
166
	 * @param array    $fields video fields of interest
167
	 * @param stdClass $post post object
168
	 * @return array modified version of video fields for administrative interface display
169
	 */
170
	public function fields_to_edit( $fields, $post ) {
171
		$post_id = absint( $post->ID );
172
173
		$meta = wp_get_attachment_metadata( $post_id );
174
175
		// If this has not been processed by videopress, we can skip the rest.
176
		if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) {
177
			return $fields;
178
		}
179
180
		$info          = (object) $meta['videopress'];
181
		$file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array();
182
183
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
184
185
		unset( $fields['url'] );
186
		unset( $fields['post_content'] );
187
188
		if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) {
189
			$v_name     = preg_replace( '/\.\w+/', '', basename( $info->path ) );
190
			$video_name = $v_name . '_fmt1.ogv';
191
			$ogg_url    = videopress_cdn_file_url( $guid, $video_name );
192
193
			$fields['video-ogg'] = array(
194
				'label' => __( 'Ogg File URL', 'jetpack' ),
195
				'input' => 'html',
196
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
197
				'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
198
			);
199
		}
200
201
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
202
203
		$fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
204
		$fields['post_excerpt']['input'] = 'textarea';
205
		$fields['post_excerpt']['value'] = $info->description;
206
207
		$fields['is_videopress_attachment'] = array(
208
			'input' => 'hidden',
209
			'value' => 'yes',
210
		);
211
212
		$fields['videopress_shortcode'] = array(
213
			'label'         => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
214
			'input'         => 'html',
215
			'html'          => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>",
216
			'show_in_modal' => true,
217
			'show_in_edit'  => false,
218
		);
219
220
		$fields['display_embed'] = array(
221
			'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
222
			'input' => 'html',
223
			'html'  => $this->display_embed_choice( $info ),
224
		);
225
226
		$fields['video-rating'] = array(
227
			'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
228
			'input' => 'html',
229
			'html'  => $this->display_rating( $info ),
230
		);
231
232
		return $fields;
233
	}
234
235
	/**
236
	 * @param stdClass $post
237
	 */
238
	public function videopress_information_box( $post ) {
239
		$post_id = absint( $post->ID );
240
241
		$meta = wp_get_attachment_metadata( $post_id );
242
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
243
244
		// If this has not been processed by videopress, we can skip the rest.
245
		if ( ! is_videopress_attachment( $post_id ) ) {
246
			return;
247
		}
248
249
		$info = (object) $meta['videopress'];
250
251
		$embed = "[videopress {$guid}]";
252
253
		$shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />';
254
255
		$url = 'empty';
256
		if ( ! empty( $guid ) ) {
257
			$url = videopress_build_url( $guid );
258
			$url = "<a href=\"{$url}\">{$url}</a>";
259
		}
260
261
		$poster = '<em>Still Processing</em>';
262
		if ( ! empty( $info->poster ) ) {
263
			$poster = "<br><img src=\"{$info->poster}\" width=\"175px\">";
264
		}
265
266
		$html = <<< HTML
267
268
<div class="misc-pub-section misc-pub-shortcode">
269
	<strong>Shortcode</strong><br>
270
	{$shortcode}
271
</div>
272
<div class="misc-pub-section misc-pub-url">
273
	<strong>Url</strong>
274
	{$url}
275
</div>
276
<div class="misc-pub-section misc-pub-poster">
277
	<strong>Poster</strong>
278
	{$poster}
279
</div>
280
HTML;
281
282
		echo $html;
283
	}
284
285
	/**
286
	 * Build HTML to display a form checkbox for embedcode display preference
287
	 *
288
	 * @param object $info database row from the videos table
289
	 * @return string input element of type checkbox set to checked state based on stored embed preference
290
	 */
291
	protected function display_embed_choice( $info ) {
292
		$id  = "attachments-{$info->post_id}-displayembed";
293
		$out = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
294
		if ( $info->display_embed ) {
295
			$out .= ' checked="checked"';
296
		}
297
		$out .= ' />' . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
298
		return $out;
299
	}
300
301
	/**
302
	 * Build HTML to display a form input radio button for video ratings
303
	 *
304
	 * @param object $info database row from the videos table
305
	 * @return string input elements of type radio with existing stored value selected
306
	 */
307
	protected function display_rating( $info ) {
308
		$out = '';
309
310
		$ratings = array(
311
			'G'     => 'G',
312
			'PG-13' => 'PG-13',
313
			'R-17'  => 'R',
314
			'X-18'  => 'X',
315
		);
316
317
		foreach ( $ratings as $r => $label ) {
318
			$id   = "attachments-{$info->post_id}-rating-$r";
319
			$out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
320
			if ( $info->rating == $r ) {
321
				$out .= ' checked="checked"';
322
			}
323
324
			$out .= " />$label</label>";
325
			unset( $id );
326
		}
327
328
		return $out;
329
	}
330
}
331
332
// Let's start this thing up.
333
VideoPress_Edit_Attachment::init();
334