Completed
Push — master ( 4679e7...74d050 )
by
unknown
10:55
created

normalize_display_embed_value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	 * Disable phpcs rule for nonce verification since it's already done by Core.
67
	 * @phpcs:disable WordPress.Security.NonceVerification
68
	 *
69
	 * @return array
70
	 */
71
	public function save_fields( $post, $attachment = null ) {
72
		if ( null === $attachment && isset( $_POST['attachment'] ) ) {
73
			$attachment = $_POST['attachment'];
74
		}
75
76
		if ( ! isset( $attachment['is_videopress_attachment'] ) || 'yes' !== $attachment['is_videopress_attachment'] ) {
77
			return $post;
78
		}
79
80
		// If this has not been processed by videopress, we can skip the rest.
81
		if ( ! is_videopress_attachment( $post['ID'] ) ) {
82
			$post['errors']['videopress']['errors'][] = __( 'The media you are trying to update is not processed by VideoPress.', 'jetpack' );
83
			return $post;
84
		}
85
86
		$post_title    = isset( $_POST['post_title'] ) ? $_POST['post_title'] : null;
87
		$post_excerpt  = isset( $_POST['post_excerpt'] ) ? $_POST['post_excerpt'] : null;
88
		$rating        = isset( $attachment['rating'] ) ? $attachment['rating'] : null;
89
		$display_embed = isset( $attachment['display_embed'] ) ? $attachment['display_embed'] : 0;
90
91
		$result = Videopress_Attachment_Metadata::persist_metadata(
92
			$post['ID'],
93
			get_post_meta( $post['ID'], 'videopress_guid', true ),
94
			$post_title,
95
			null, // @todo: Check why we haven't sent the caption in the first place.
96
			$post_excerpt,
97
			$rating,
98
			$this->normalize_display_embed_value( $display_embed )
99
		);
100
101
		if ( is_wp_error( $result ) ) {
102
			$post['errors']['videopress']['errors'][] = $result->get_error_message();
0 ignored issues
show
Bug introduced by
The method get_error_message() does not seem to exist on object<WP_Error>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
103
			return $post;
104
		}
105
106
		return $post;
107
	}
108
109
	/**
110
	 * Convert the string values of display_embed option to the format that they will be stored in db.
111
	 *
112
	 * @param string $display_embed The denormalized version.
113
	 *
114
	 * @return int
115
	 */
116
	private function normalize_display_embed_value( $display_embed ) {
117
		return 'on' === $display_embed ? 1 : 0;
118
	}
119
120
	/**
121
	 * Get the upload api path.
122
	 *
123
	 * @param string $guid
124
	 * @return string
125
	 */
126
	public function make_video_api_path( $guid ) {
127
		return sprintf(
128
			'%s/rest/v%s/videos/%s',
129
			JETPACK__WPCOM_JSON_API_BASE,
130
			Client::WPCOM_JSON_API_VERSION,
131
			$guid
132
		);
133
	}
134
135
136
	/**
137
	 * Creates an array of video fields to edit based on transcoded videos.
138
	 *
139
	 * @param array    $fields video fields of interest
140
	 * @param stdClass $post post object
141
	 * @return array modified version of video fields for administrative interface display
142
	 */
143
	public function fields_to_edit( $fields, $post ) {
144
		$post_id = absint( $post->ID );
145
146
		$meta = wp_get_attachment_metadata( $post_id );
147
148
		// If this has not been processed by videopress, we can skip the rest.
149
		if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) {
150
			return $fields;
151
		}
152
153
		$info          = (object) $meta['videopress'];
154
		$file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array();
155
156
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
157
158
		unset( $fields['url'] );
159
		unset( $fields['post_content'] );
160
161
		if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) {
162
			$v_name     = preg_replace( '/\.\w+/', '', basename( $info->path ) );
163
			$video_name = $v_name . '_fmt1.ogv';
164
			$ogg_url    = videopress_cdn_file_url( $guid, $video_name );
165
166
			$fields['video-ogg'] = array(
167
				'label' => __( 'Ogg File URL', 'jetpack' ),
168
				'input' => 'html',
169
				'html'  => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />",
170
				'helps' => __( 'Location of the Ogg video file.', 'jetpack' ),
171
			);
172
		}
173
174
		$fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' );
175
176
		$fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' );
177
		$fields['post_excerpt']['input'] = 'textarea';
178
		$fields['post_excerpt']['value'] = $info->description;
179
180
		$fields['is_videopress_attachment'] = array(
181
			'input' => 'hidden',
182
			'value' => 'yes',
183
		);
184
185
		$fields['videopress_shortcode'] = array(
186
			'label'         => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ),
187
			'input'         => 'html',
188
			'html'          => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>",
189
			'show_in_modal' => true,
190
			'show_in_edit'  => false,
191
		);
192
193
		$fields['display_embed'] = array(
194
			'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ),
195
			'input' => 'html',
196
			'html'  => $this->display_embed_choice( $info ),
197
		);
198
199
		$fields['video-rating'] = array(
200
			'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ),
201
			'input' => 'html',
202
			'html'  => $this->display_rating( $info ),
203
		);
204
205
		return $fields;
206
	}
207
208
	/**
209
	 * @param stdClass $post
210
	 */
211
	public function videopress_information_box( $post ) {
212
		$post_id = absint( $post->ID );
213
214
		$meta = wp_get_attachment_metadata( $post_id );
215
		$guid = get_post_meta( $post_id, 'videopress_guid', true );
216
217
		// If this has not been processed by videopress, we can skip the rest.
218
		if ( ! is_videopress_attachment( $post_id ) ) {
219
			return;
220
		}
221
222
		$info = (object) $meta['videopress'];
223
224
		$embed = "[videopress {$guid}]";
225
226
		$shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />';
227
228
		$url = 'empty';
229
		if ( ! empty( $guid ) ) {
230
			$url = videopress_build_url( $guid );
231
			$url = "<a href=\"{$url}\">{$url}</a>";
232
		}
233
234
		$poster = '<em>Still Processing</em>';
235
		if ( ! empty( $info->poster ) ) {
236
			$poster = "<br><img src=\"{$info->poster}\" width=\"175px\">";
237
		}
238
239
		$html = <<< HTML
240
241
<div class="misc-pub-section misc-pub-shortcode">
242
	<strong>Shortcode</strong><br>
243
	{$shortcode}
244
</div>
245
<div class="misc-pub-section misc-pub-url">
246
	<strong>Url</strong>
247
	{$url}
248
</div>
249
<div class="misc-pub-section misc-pub-poster">
250
	<strong>Poster</strong>
251
	{$poster}
252
</div>
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 = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'";
267
		if ( $info->display_embed ) {
268
			$out .= ' checked="checked"';
269
		}
270
		$out .= ' />' . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>';
271
		return $out;
272
	}
273
274
	/**
275
	 * Build HTML to display a form input radio button for video ratings
276
	 *
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 .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\"";
293
			if ( $info->rating == $r ) {
294
				$out .= ' checked="checked"';
295
			}
296
297
			$out .= " />$label</label>";
298
			unset( $id );
299
		}
300
301
		return $out;
302
	}
303
}
304
305
// Let's start this thing up.
306
VideoPress_Edit_Attachment::init();
307