Completed
Push — feature/videopress-uploader ( 022324...7bd9dc )
by
unknown
09:39
created

VideoPress_Edit_Attachment::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 9
rs 9.6666
1
<?php
2
3
class VideoPress_Edit_Attachment {
4
	public static function init() {
5
		static $instance = false;
6
7
		if ( !$instance ) {
8
			$instance = new VideoPress_Edit_Attachment();
9
		}
10
11
		return $instance;
12
	}
13
14
	public function __construct() {
15
		add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 );
16
		add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 );
17
18
		add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 );
19
	}
20
21
	/**
22
	 * @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...
23
	 * @param object  $post
24
	 */
25
	public function configure_meta_boxes( $post_type = 'unknown', $post = NULL ) {
26
		if ( NULL == $post ) {
27
			$post = (object) array ( 'ID' => 0 );
28
		}
29
30
		if ( 'attachment' != $post_type ) {
31
			return;
32
		}
33
34
		$meta = wp_get_attachment_metadata( $post->ID );
35
36
		// If this has not been processed by videopress, we can skip the rest.
37
		if ( ! isset( $meta['videopress'] ) ) {
38
			return;
39
		}
40
41
		add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' );
42
	}
43
44
	/**
45
	 * @param array $post
46
	 * @param array $attachment
47
	 *
48
	 * @return array
49
	 */
50
	public function save_fields( $post, $attachment ) {
51
52
		$post_id = absint( $post['ID'] );
53
54
		$meta = wp_get_attachment_metadata( $post_id );
55
56
		// If this has not been processed by videopress, we can skip the rest.
57
		if ( ! isset( $meta['videopress'] ) ) {
58
			return $post;
59
		}
60
61
		$values = array();
62
63
		// Add the video title & description in, so that we save it properly.
64
		if ( isset( $_POST['post_title'] ) ) {
65
			$values['title'] = trim( strip_tags( $_POST['post_title'] ) );
66
		}
67
68
		if ( isset( $_POST['post_excerpt'] ) ) {
69
			$values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) );
70
		}
71
72
		if ( isset( $attachment['rating'] ) ) {
73
			$rating = $attachment['rating'];
74
75
			if ( ! empty( $value ) && in_array( $rating, array( 'G', 'PG-13', 'R-17', 'X-18' ) ) ) {
0 ignored issues
show
Bug introduced by
The variable $value does not exist. Did you mean $values?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
76
				$values['rating'] = $rating;
77
			}
78
		}
79
80
		// We set a default here, as if it isn't selected, then we'll turn it off.
81
		$attachment['display_embed'] = 0;
82
		if ( isset( $attachment['display_embed'] ) ) {
83
			$display_embed = $attachment['display_embed'];
84
85
			$values['display_embed'] = 'on' === $display_embed  ? 1 : 0;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of 'on' (string) and $display_embed (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
86
		}
87
88
		$args = array(
89
			'method'  => 'POST',
90
		);
91
92
		$endpoint = "videos/{$meta['videopress']['guid']}";
93
		$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, 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...
94
95
		if ( is_wp_error( $result ) ) {
96
			$post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.' );
97
			return $post;
98
		}
99
100
		$response = json_decode( $result['body'], true );
101
102
		if ( 'true' !== $response ) {
103
			return $post;
104
		}
105
106
		return $post;
107
	}
108
109
110
	/**
111
	 * Get the upload api path.
112
	 *
113
	 * @param string $guid
114
	 * @return string
115
	 */
116
	public function make_video_api_path( $guid ) {
117
		return sprintf(
118
			'%s://%s/rest/v%s/videos/%s',
119
			'https',
120
			'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST,
121
			Jetpack_Client::WPCOM_JSON_API_VERSION,
122
			$guid
123
		);
124
	}
125
126
127
	/**
128
	 * Creates an array of video fields to edit based on transcoded videos.
129
	 *
130
	 * @param array $fields video fields of interest
131
	 * @param array $post post object
132
	 * @return array modified version of video fields for administrative interface display
133
	 */
134
	public function fields_to_edit( $fields, $post ) {
135
		$post_id = absint( $post->ID );
136
137
		$meta = wp_get_attachment_metadata( $post_id );
138
139
		// If this has not been processed by videopress, we can skip the rest.
140
		if ( ! isset( $meta['videopress'] ) ) {
141
			return $fields;
142
		}
143
144
		$info = (object) $meta['videopress'];
145
146
		unset( $fields['url'] );
147
		unset( $fields['post_content'] );
148
149
		if ( isset( $info->files_status['std']['ogg'] ) && 'done' ===  $info->files_status['std']['ogg'] ) {
150
			$v_name  = preg_replace( '/\.\w+/', '', basename( $info->path ) );
151
			$video_name = $v_name . '_fmt1.ogv';
152
			$ogg_url  = video_cdn_file_url( $info->guid, $video_name );
153
154
			$fields['video-ogg'] = array(
155
				'label' => __('Ogg File URL'),
156
				'input' => 'html',
157
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . clean_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
158
				'helps' => __('Location of the Ogg video file.'),
159
			);
160
		}
161
162
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video' );
163
164
		$fields['post_excerpt']['label'] = __( 'Description' );
165
		$fields['post_excerpt']['input'] = 'textarea';
166
		$fields['post_excerpt']['value'] = $info->description;
167
168
		$fields['display_embed'] = array(
169
			'label' => __( 'Share' ),
170
			'input' => 'html',
171
			'html'  => $this->display_embed_choice( $info )
172
		);
173
174
		$fields['video-rating'] = array(
175
			'label' => __( 'Rating' ),
176
			'input' => 'html',
177
			'html'  => $this->display_rating( $info )
178
		);
179
180
		return $fields;
181
	}
182
183
	/**
184
	 * @param object $post
185
	 */
186
	public function videopress_information_box( $post ) {
187
		$post_id = absint( $post->ID );
188
189
		$meta = wp_get_attachment_metadata( $post_id );
190
191
		// If this has not been processed by videopress, we can skip the rest.
192
		if ( ! isset( $meta['videopress'] ) ) {
193
			return;
194
		}
195
196
		$info = (object) $meta['videopress'];
197
198
		$formats = array(
199
			'Standard' => isset( $info->files_status ) ? $info->files_status['std']['mp4'] : null,
200
			'Ogg Vorbis' => isset( $info->files_status ) ? $info->files_status['std']['ogg'] : null,
201
			'DVD' => isset( $info->files_status ) ? $info->files_status['dvd'] : null,
202
			'HD' => isset( $info->files_status ) ? $info->files_status['hd'] : null,
203
		);
204
205
		$embed = "[wpvideo {$info->guid}]";
206
207
		$shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr($embed) . '" onclick="this.focus();this.select();" />';
208
209
		$trans_status = '';
210
		foreach ( $formats as $name => $status) {
211
			$trans_status .= '<strong>' . $name . ':</strong> ' . ( 'done' === $status  ? 'Done' : 'Processing' ) . '<br>';
212
		}
213
214
		$nonce = wp_create_nonce( 'videopress-update-transcoding-status' );
215
216
		$url = 'empty';
217
		if ( ! empty( $info->url ) ) {
218
			$url = "<a href=\"{$info->url}\">{$info->url}</a>";
219
		}
220
221
222
223
		$poster = 'empty';
224
		if ( ! empty( $info->poster ) ) {
225
			$poster = "<img src=\"{$info->poster}\" width=\"175px\">";
226
		}
227
228
		$html = <<< HTML
229
230
<div class="misc-pub-section misc-pub-shortcode">Shortcode:</div>
231
<strong>{$shortcode}</strong>
232
<div class="misc-pub-section misc-pub-url">Url:</div>
233
<strong>{$url}</strong>
234
<div class="misc-pub-section misc-pub-poster">Poster:</div>
235
<strong>{$poster}</strong>
236
<div class="misc-pub-section misc-pub-status">Status (<a href="javascript:;" id="videopress-update-transcoding-status">update</a>):</div>
237
<strong id="videopress-transcoding-status">{$trans_status}</strong>
238
239
240
<script>
241
	jQuery( function($) {
242
		$( '#videopress-update-transcoding-status' ).on( "click", function() {
243
			jQuery.ajax( {
244
				type: 'post',
245
				url: 'admin-ajax.php',
246
				data: { 
247
					action: 'videopress-update-transcoding-status',
248
					post_id: '{$post_id}',
249
					_ajax_nonce: '{$nonce}' 
250
				}
251
			});
252
		} );
253
	} );
254
</script>
255
HTML;
256
257
		echo $html;
258
	}
259
260
	/**
261
	 * Build HTML to display a form checkbox for embedcode display preference
262
	 *
263
	 * @param object $info database row from the videos table
264
	 * @return string input element of type checkbox set to checked state based on stored embed preference
265
	 */
266
	protected function display_embed_choice( $info ) {
267
		$id = "attachments-{$info->post_id}-displayembed";
268
		$out  = "<input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
269
		if ( $info->display_embed )
270
			$out .= ' checked="checked"';
271
		$out .= " /><label for='$id'>" . __( 'Display share menu and allow viewers to embed or download this video' ) . '</label>';
272
		return $out;
273
	}
274
275
	/**
276
	 * Build HTML to display a form input radio button for video ratings
277
	 *
278
	 * @param int $post_id numeric post (attachment) identifier
0 ignored issues
show
Bug introduced by
There is no parameter named $post_id. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
279
	 * @param object $info database row from the videos table
280
	 * @return string input elements of type radio with existing stored value selected
281
	 */
282
	protected function display_rating( $info ) {
283
		$out = '';
284
285
		$ratings = array(
286
			'G'     => 'G',
287
			'PG-13' => 'PG-13',
288
			'R-17'  => 'R',
289
			'X-18'  => 'X',
290
		);
291
292
		foreach( $ratings as $r => $label ) {
293
			$id = "attachments-{$info->post_id}-rating-$r";
294
			$out .= "<input type='radio' name='attachments[{$info->post_id}][rating]' id='$id' value='$r'";
295
			if ( $info->rating == $r )
296
				$out .= ' checked="checked"';
297
			$out .= " /><label for='$id'>$label</label>";
298
			unset( $id );
299
		}
300
		return $out;
301
	}
302
}
303
304
VideoPress_Edit_Attachment::init();
305