Completed
Push — feature/videopress-uploader ( 31b66d...022324 )
by
unknown
09:37
created

VideoPress_Edit_Attachment::fields_to_edit()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 3
Metric Value
cc 3
eloc 30
c 3
b 1
f 3
nc 3
nop 2
dl 0
loc 48
rs 9.125
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 ( 'done' === $info->fmts_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
			'fmt_std'  => 'Standard',
200
			'fmt_dvd'  => 'DVD',
201
			'fmts_ogg' => 'Ogg Vorbis',
202
			'fmt_hd'   => 'High Definition',
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 $format => $name ) {
211
			$trans_status .= '<strong>' . $name . ':</strong> ' . ( 'done' === $info->$format  ? 'Done' : 'Processing' ) . '<br>';
212
		}
213
214
		$nonce = wp_create_nonce( 'videopress-update-transcoding-status' );
215
216
		$url = 'empty';
217
		if ( isset( $info->url ) ) {
218
			$url = "<a href=\"{$info->url}\">{$info->url}</a>";
219
		}
220
221
		$poster = 'empty';
222
		if ( isset( $info->poster ) ) {
223
			$poster = "<img src=\"{$info->poster}\" width=\"175px\">";
224
		}
225
226
		$html = <<< HTML
227
228
<div class="misc-pub-section misc-pub-shortcode">Shortcode:</div>
229
<strong>{$shortcode}</strong>
230
<div class="misc-pub-section misc-pub-url">Url:</div>
231
<strong>{$url}</strong>
232
<div class="misc-pub-section misc-pub-poster">Poster:</div>
233
<strong>{$poster}</strong>
234
<div class="misc-pub-section misc-pub-status">Status (<a href="javascript:;" id="videopress-update-transcoding-status">update</a>):</div>
235
<strong id="videopress-transcoding-status">{$trans_status}</strong>
236
237
238
<script>
239
	jQuery( function($) {
240
		$( '#videopress-update-transcoding-status' ).on( "click", function() {
241
			jQuery.ajax( {
242
				type: 'post',
243
				url: 'admin-ajax.php',
244
				data: { 
245
					action: 'videopress-update-transcoding-status',
246
					post_id: '{$post_id}',
247
					_ajax_nonce: '{$nonce}' 
248
				}
249
			});
250
		} );
251
	} );
252
</script>
253
HTML;
254
255
		echo $html;
256
	}
257
258
	/**
259
	 * Build HTML to display a form checkbox for embedcode display preference
260
	 *
261
	 * @param object $info database row from the videos table
262
	 * @return string input element of type checkbox set to checked state based on stored embed preference
263
	 */
264
	protected function display_embed_choice( $info ) {
265
		$id = "attachments-{$info->post_id}-displayembed";
266
		$out  = "<input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
267
		if ( $info->display_embed )
268
			$out .= ' checked="checked"';
269
		$out .= " /><label for='$id'>" . __( 'Display share menu and allow viewers to embed or download this video' ) . '</label>';
270
		return $out;
271
	}
272
273
	/**
274
	 * Build HTML to display a form input radio button for video ratings
275
	 *
276
	 * @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...
277
	 * @param object $info database row from the videos table
278
	 * @return string input elements of type radio with existing stored value selected
279
	 */
280
	protected function display_rating( $info ) {
281
		$out = '';
282
283
		$ratings = array(
284
			'G'     => 'G',
285
			'PG-13' => 'PG-13',
286
			'R-17'  => 'R',
287
			'X-18'  => 'X',
288
		);
289
290
		foreach( $ratings as $r => $label ) {
291
			$id = "attachments-{$info->post_id}-rating-$r";
292
			$out .= "<input type='radio' name='attachments[{$info->post_id}][rating]' id='$id' value='$r'";
293
			if ( $info->rating == $r )
294
				$out .= ' checked="checked"';
295
			$out .= " /><label for='$id'>$label</label>";
296
			unset( $id );
297
		}
298
		return $out;
299
	}
300
}
301
302
VideoPress_Edit_Attachment::init();
303