Completed
Push — feature/videopress-uploader ( bb1f5c...31b66d )
by
unknown
55:38 queued 44:50
created

VideoPress_Edit_Attachment::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 4
c 2
b 0
f 2
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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
		return $post;
89
	}
90
91
92
	/**
93
	 * Creates an array of video fields to edit based on transcoded videos.
94
	 *
95
	 * @param array $fields video fields of interest
96
	 * @param array $post post object
97
	 * @return array modified version of video fields for administrative interface display
98
	 */
99
	public function fields_to_edit( $fields, $post ) {
100
		$post_id = absint( $post->ID );
101
102
		$meta = wp_get_attachment_metadata( $post_id );
103
104
		// If this has not been processed by videopress, we can skip the rest.
105
		if ( ! isset( $meta['videopress'] ) ) {
106
			return $fields;
107
		}
108
109
		$info = (object) $meta['videopress'];
110
111
		unset( $fields['url'] );
112
		unset( $fields['post_content'] );
113
114
		if ( 'done' === $info->fmts_ogg ) {
115
			$v_name  = preg_replace( '/\.\w+/', '', basename( $info->path ) );
116
			$video_name = $v_name . '_fmt1.ogv';
117
			$ogg_url  = video_cdn_file_url( $info->guid, $video_name );
118
119
			$fields['video-ogg'] = array(
120
				'label' => __('Ogg File URL'),
121
				'input' => 'html',
122
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . clean_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
123
				'helps' => __('Location of the Ogg video file.'),
124
			);
125
		}
126
127
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video' );
128
129
		$fields['post_excerpt']['label'] = __( 'Description' );
130
		$fields['post_excerpt']['input'] = 'textarea';
131
		$fields['post_excerpt']['value'] = $info->description;
132
133
		$fields['display_embed'] = array(
134
			'label' => __( 'Share' ),
135
			'input' => 'html',
136
			'html'  => $this->display_embed_choice( $info )
137
		);
138
139
		$fields['video-rating'] = array(
140
			'label' => __( 'Rating' ),
141
			'input' => 'html',
142
			'html'  => $this->display_rating( $info )
143
		);
144
145
		return $fields;
146
	}
147
148
	/**
149
	 * @param object $post
150
	 */
151
	public function videopress_information_box( $post ) {
152
		$post_id = absint( $post->ID );
153
154
		$meta = wp_get_attachment_metadata( $post_id );
155
156
		// If this has not been processed by videopress, we can skip the rest.
157
		if ( ! isset( $meta['videopress'] ) ) {
158
			return;
159
		}
160
161
		$info = (object) $meta['videopress'];
162
163
		$formats = array(
164
			'fmt_std'  => 'Standard',
165
			'fmt_dvd'  => 'DVD',
166
			'fmts_ogg' => 'Ogg Vorbis',
167
			'fmt_hd'   => 'High Definition',
168
		);
169
170
		$embed = "[wpvideo {$info->guid}]";
171
172
		$shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr($embed) . '" onclick="this.focus();this.select();" />';
173
174
		$trans_status = '';
175
		foreach ( $formats as $format => $name ) {
176
			$trans_status .= '<strong>' . $name . ':</strong> ' . ( 'done' === $info->$format  ? 'Done' : 'Processing' ) . '<br>';
177
		}
178
179
		$html = <<< HTML
180
<dl>
181
<dt>Shortcode:</dt>
182
<dd>{$shortcode}</dd>
183
<dt>Url:</dt>
184
<dd><a href="{$info->url}">{$info->url}</a></dd>
185
<dt>Poster:</dt>
186
<dd><img src="{$info->poster}" width="175px"></dd>
187
<dt>Transcoding Status:</dt>
188
<dd>{$trans_status}</dd>
189
</dl>
190
HTML;
191
192
		echo $html;
193
	}
194
195
	/**
196
	 * Build HTML to display a form checkbox for embedcode display preference
197
	 *
198
	 * @param object $info database row from the videos table
199
	 * @return string input element of type checkbox set to checked state based on stored embed preference
200
	 */
201
	protected function display_embed_choice( $info ) {
202
		$id = "attachments-{$info->post_id}-displayembed";
203
		$out  = "<input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
204
		if ( $info->display_embed )
205
			$out .= ' checked="checked"';
206
		$out .= " /><label for='$id'>" . __( 'Display share menu and allow viewers to embed or download this video' ) . '</label>';
207
		return $out;
208
	}
209
210
	/**
211
	 * Build HTML to display a form input radio button for video ratings
212
	 *
213
	 * @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...
214
	 * @param object $info database row from the videos table
215
	 * @return string input elements of type radio with existing stored value selected
216
	 */
217
	protected function display_rating( $info ) {
218
		$out = '';
219
220
		$ratings = array(
221
			'G'     => 'G',
222
			'PG-13' => 'PG-13',
223
			'R-17'  => 'R',
224
			'X-18'  => 'X',
225
		);
226
227
		foreach( $ratings as $r => $label ) {
228
			$id = "attachments-{$info->post_id}-rating-$r";
229
			$out .= "<input type='radio' name='attachments[{$info->post_id}][rating]' id='$id' value='$r'";
230
			if ( $info->rating == $r )
231
				$out .= ' checked="checked"';
232
			$out .= " /><label for='$id'>$label</label>";
233
			unset( $id );
234
		}
235
		return $out;
236
	}
237
}
238
239
VideoPress_Edit_Attachment::init();
240