Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 180 |
|
0.00% |
0 / 13 |
CRAP | |
0.00% |
0 / 1 |
| VideoPress_Edit_Attachment | |
0.00% |
0 / 177 |
|
0.00% |
0 / 13 |
2550 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| configure_meta_boxes | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| save_fields | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
182 | |||
| normalize_checkbox_value | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| make_video_api_path | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| fields_to_edit | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
72 | |||
| videopress_information_box | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
20 | |||
| create_checkbox_for_option | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| display_embed_choice | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| display_download_choice | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| display_privacy_setting | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| display_rating | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | |
| 3 | use Automattic\Jetpack\Connection\Client; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit( 0 ); |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * VideoPress edit attachment screen |
| 11 | * |
| 12 | * @since 4.1 |
| 13 | */ |
| 14 | class VideoPress_Edit_Attachment { |
| 15 | |
| 16 | /** |
| 17 | * Singleton method to initialize the object only once. |
| 18 | * |
| 19 | * @return VideoPress_Edit_Attachment |
| 20 | */ |
| 21 | public static function init() { |
| 22 | static $instance = null; |
| 23 | |
| 24 | if ( ! $instance ) { |
| 25 | $instance = new VideoPress_Edit_Attachment(); |
| 26 | } |
| 27 | |
| 28 | return $instance; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * VideoPress_Edit_Attachment constructor. |
| 33 | * |
| 34 | * Adds in appropriate actions for attachment fields editor, meta boxes and saving. |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 ); |
| 38 | add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 ); |
| 39 | add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 ); |
| 40 | add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 ); |
| 41 | |
| 42 | add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Add VideoPress meta box. |
| 47 | * |
| 48 | * @param string $post_type Post type. |
| 49 | * @param object $post Post object. |
| 50 | */ |
| 51 | public function configure_meta_boxes( $post_type = 'unknown', $post = null ) { |
| 52 | if ( null === $post ) { |
| 53 | $post = (object) array( 'ID' => 0 ); |
| 54 | } |
| 55 | |
| 56 | if ( 'attachment' !== $post_type ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | // If this has not been processed by videopress, we can skip the rest. |
| 61 | if ( ! is_videopress_attachment( $post->ID ) ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Filter attachment fields data to save. |
| 70 | * |
| 71 | * @param array $post Post data. |
| 72 | * @param array|null $attachment Attachment metadata. |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | public function save_fields( $post, $attachment = null ) { |
| 77 | // phpcs:disable WordPress.Security.NonceVerification.Missing -- Nonce verification already done by core. |
| 78 | if ( null === $attachment && isset( $_POST['attachment'] ) ) { |
| 79 | $attachment = filter_var( wp_unslash( $_POST['attachment'] ) ); |
| 80 | } |
| 81 | |
| 82 | if ( ! isset( $attachment['is_videopress_attachment'] ) || 'yes' !== $attachment['is_videopress_attachment'] ) { |
| 83 | return $post; |
| 84 | } |
| 85 | |
| 86 | // If this has not been processed by videopress, we can skip the rest. |
| 87 | if ( ! is_videopress_attachment( $post['ID'] ) ) { |
| 88 | $post['errors']['videopress']['errors'][] = __( 'The media you are trying to update is not processed by VideoPress.', 'jetpack' ); |
| 89 | return $post; |
| 90 | } |
| 91 | |
| 92 | $post_title = isset( $_POST['post_title'] ) ? sanitize_text_field( wp_unslash( $_POST['post_title'] ) ) : null; |
| 93 | $post_excerpt = isset( $_POST['post_excerpt'] ) ? sanitize_textarea_field( wp_unslash( $_POST['post_excerpt'] ) ) : null; |
| 94 | $rating = isset( $attachment['rating'] ) ? $attachment['rating'] : null; |
| 95 | $display_embed = isset( $attachment['display_embed'] ) ? $attachment['display_embed'] : 0; |
| 96 | $allow_download = isset( $attachment['allow_download'] ) ? $attachment['allow_download'] : 0; |
| 97 | $privacy_setting = isset( $attachment['privacy_setting'] ) ? $attachment['privacy_setting'] : VIDEOPRESS_PRIVACY::SITE_DEFAULT; |
| 98 | |
| 99 | $result = Videopress_Attachment_Metadata::persist_metadata( |
| 100 | $post['ID'], |
| 101 | get_post_meta( $post['ID'], 'videopress_guid', true ), |
| 102 | $post_title, |
| 103 | null, // @todo: Check why we haven't sent the caption in the first place. |
| 104 | $post_excerpt, |
| 105 | $rating, |
| 106 | $this->normalize_checkbox_value( $display_embed ), |
| 107 | $this->normalize_checkbox_value( $allow_download ), |
| 108 | $privacy_setting |
| 109 | ); |
| 110 | |
| 111 | if ( is_wp_error( $result ) ) { |
| 112 | $post['errors']['videopress']['errors'][] = $result->get_error_message(); |
| 113 | return $post; |
| 114 | } |
| 115 | |
| 116 | return $post; |
| 117 | // phpcs:enable WordPress.Security.NonceVerification.Missing |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Convert the string values of a checkbox option to the format that they will be stored in db. |
| 122 | * |
| 123 | * @param string $value The denormalized version. |
| 124 | * |
| 125 | * @return int |
| 126 | */ |
| 127 | private function normalize_checkbox_value( $value ) { |
| 128 | return 'on' === $value ? 1 : 0; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the upload api path. |
| 133 | * |
| 134 | * @param string $guid The guid of the video. |
| 135 | * @return string |
| 136 | */ |
| 137 | public function make_video_api_path( $guid ) { |
| 138 | return sprintf( |
| 139 | '%s/rest/v%s/videos/%s', |
| 140 | JETPACK__WPCOM_JSON_API_BASE, |
| 141 | Client::WPCOM_JSON_API_VERSION, |
| 142 | $guid |
| 143 | ); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Creates an array of video fields to edit based on transcoded videos. |
| 148 | * |
| 149 | * @param array $fields video fields of interest. |
| 150 | * @param stdClass $post Post object. |
| 151 | * @return array modified version of video fields for administrative interface display |
| 152 | */ |
| 153 | public function fields_to_edit( $fields, $post ) { |
| 154 | $post_id = absint( $post->ID ); |
| 155 | |
| 156 | $meta = wp_get_attachment_metadata( $post_id ); |
| 157 | |
| 158 | // If this has not been processed by videopress, we can skip the rest. |
| 159 | if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) { |
| 160 | return $fields; |
| 161 | } |
| 162 | |
| 163 | $info = (object) $meta['videopress']; |
| 164 | $file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array(); |
| 165 | |
| 166 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
| 167 | |
| 168 | unset( $fields['url'] ); |
| 169 | unset( $fields['post_content'] ); |
| 170 | |
| 171 | // If a video isn't attached to any specific post, manually add a post ID. |
| 172 | if ( ! isset( $info->post_id ) ) { |
| 173 | $info->post_id = 0; |
| 174 | } |
| 175 | |
| 176 | if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) { |
| 177 | $v_name = preg_replace( '/\.\w+/', '', basename( $info->path ) ); |
| 178 | $video_name = $v_name . '_fmt1.ogv'; |
| 179 | $ogg_url = videopress_cdn_file_url( $guid, $video_name ); |
| 180 | |
| 181 | $fields['video-ogg'] = array( |
| 182 | 'label' => __( 'Ogg File URL', 'jetpack' ), |
| 183 | 'input' => 'html', |
| 184 | 'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />", |
| 185 | 'helps' => __( 'Location of the Ogg video file.', 'jetpack' ), |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | $fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' ); |
| 190 | |
| 191 | $fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' ); |
| 192 | $fields['post_excerpt']['input'] = 'textarea'; |
| 193 | $fields['post_excerpt']['value'] = ! empty( $info->description ) ? $info->description : ''; |
| 194 | |
| 195 | $fields['is_videopress_attachment'] = array( |
| 196 | 'input' => 'hidden', |
| 197 | 'value' => 'yes', |
| 198 | ); |
| 199 | |
| 200 | $fields['videopress_shortcode'] = array( |
| 201 | 'label' => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ), |
| 202 | 'input' => 'html', |
| 203 | 'html' => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>", |
| 204 | 'show_in_modal' => true, |
| 205 | 'show_in_edit' => false, |
| 206 | ); |
| 207 | |
| 208 | $fields['display_embed'] = array( |
| 209 | 'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ), |
| 210 | 'input' => 'html', |
| 211 | 'html' => $this->display_embed_choice( $info ), |
| 212 | ); |
| 213 | |
| 214 | $fields['allow_download'] = array( |
| 215 | 'label' => _x( 'Download', 'A header for the video allow download option area', 'jetpack' ), |
| 216 | 'input' => 'html', |
| 217 | 'html' => $this->display_download_choice( $info ), |
| 218 | ); |
| 219 | |
| 220 | $fields['video-rating'] = array( |
| 221 | 'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ), |
| 222 | 'input' => 'html', |
| 223 | 'html' => $this->display_rating( $info ), |
| 224 | ); |
| 225 | |
| 226 | $fields['privacy_setting'] = array( |
| 227 | 'label' => _x( 'Privacy Setting', 'A header for the video privacy setting area.', 'jetpack' ), |
| 228 | 'input' => 'html', |
| 229 | 'html' => $this->display_privacy_setting( $info ), |
| 230 | ); |
| 231 | |
| 232 | return $fields; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Meta box output. |
| 237 | * |
| 238 | * @param stdClass $post Post object. |
| 239 | */ |
| 240 | public function videopress_information_box( $post ) { |
| 241 | $post_id = absint( $post->ID ); |
| 242 | |
| 243 | $meta = wp_get_attachment_metadata( $post_id ); |
| 244 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
| 245 | |
| 246 | // If this has not been processed by videopress, we can skip the rest. |
| 247 | if ( ! is_videopress_attachment( $post_id ) ) { |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | $info = (object) $meta['videopress']; |
| 252 | |
| 253 | $embed = "[videopress {$guid}]"; |
| 254 | |
| 255 | $shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />'; |
| 256 | |
| 257 | $url = 'empty'; |
| 258 | if ( ! empty( $guid ) ) { |
| 259 | $url = videopress_build_url( $guid ); |
| 260 | $url = "<a href=\"{$url}\">{$url}</a>"; |
| 261 | } |
| 262 | |
| 263 | $poster = '<em>Still Processing</em>'; |
| 264 | if ( ! empty( $info->poster ) ) { |
| 265 | $poster = "<br><img src=\"{$info->poster}\" width=\"175px\">"; |
| 266 | } |
| 267 | |
| 268 | $html = <<<HTML |
| 269 | |
| 270 | <div class="misc-pub-section misc-pub-shortcode"> |
| 271 | <strong>Shortcode</strong><br> |
| 272 | {$shortcode} |
| 273 | </div> |
| 274 | <div class="misc-pub-section misc-pub-url"> |
| 275 | <strong>Url</strong> |
| 276 | {$url} |
| 277 | </div> |
| 278 | <div class="misc-pub-section misc-pub-poster"> |
| 279 | <strong>Poster</strong> |
| 280 | {$poster} |
| 281 | </div> |
| 282 | HTML; |
| 283 | |
| 284 | echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Variables built above. |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Creates a checkbox and a label for a video option. |
| 289 | * |
| 290 | * @param string $id the checkbox id. |
| 291 | * @param string $name the checkbox name. |
| 292 | * @param string $label the label text. |
| 293 | * @param bool $is_checked if the checkbox should be checked. |
| 294 | * |
| 295 | * @return string the generated HTML |
| 296 | */ |
| 297 | protected function create_checkbox_for_option( $id, $name, $label, $is_checked ) { |
| 298 | $html = "<label for='$id'><input type='checkbox' name='$name' id='$id'"; |
| 299 | if ( $is_checked ) { |
| 300 | $html .= ' checked="checked"'; |
| 301 | } |
| 302 | $html .= " />$label</label>"; |
| 303 | return $html; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Build HTML to display a form checkbox for embedcode display preference |
| 308 | * |
| 309 | * @param object $info Database row from the videos table. |
| 310 | * @return string Input element of type checkbox set to checked state based on stored embed preference. |
| 311 | */ |
| 312 | protected function display_embed_choice( $info ) { |
| 313 | return $this->create_checkbox_for_option( |
| 314 | "attachments-{$info->post_id}-displayembed", |
| 315 | "attachments[{$info->post_id}][display_embed]", |
| 316 | __( 'Display share menu and allow viewers to copy a link or embed this video', 'jetpack' ), |
| 317 | isset( $info->display_embed ) ? $info->display_embed : 0 |
| 318 | ); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Build HTML to display a form checkbox for the "allow download" video option |
| 323 | * |
| 324 | * @param object $info database row from the videos table. |
| 325 | * @return string input element of type checkbox with checked state matching the download preference |
| 326 | */ |
| 327 | protected function display_download_choice( $info ) { |
| 328 | return $this->create_checkbox_for_option( |
| 329 | "attachments-{$info->post_id}-allowdownload", |
| 330 | "attachments[{$info->post_id}][allow_download]", |
| 331 | __( 'Display download option and allow viewers to download this video', 'jetpack' ), |
| 332 | isset( $info->allow_download ) && $info->allow_download |
| 333 | ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Build HTML to display a form input radio button for video ratings |
| 338 | * |
| 339 | * @param object $info Database row from the videos table. |
| 340 | * |
| 341 | * @return string Input Elements of type radio with existing stored value selected. |
| 342 | */ |
| 343 | protected function display_privacy_setting( $info ) { |
| 344 | $privacy_settings = array( |
| 345 | VIDEOPRESS_PRIVACY::SITE_DEFAULT => __( 'Site Default', 'jetpack' ), |
| 346 | VIDEOPRESS_PRIVACY::IS_PUBLIC => __( 'Public', 'jetpack' ), |
| 347 | VIDEOPRESS_PRIVACY::IS_PRIVATE => __( 'Private', 'jetpack' ), |
| 348 | ); |
| 349 | |
| 350 | $displayed_privacy_setting = intval( isset( $info->privacy_setting ) ? $info->privacy_setting : VIDEOPRESS_PRIVACY::SITE_DEFAULT ); |
| 351 | |
| 352 | $out = "<select name='attachments[{$info->post_id}][privacy_setting]'>"; |
| 353 | foreach ( $privacy_settings as $r => $label ) { |
| 354 | $out .= "<option value=\"$r\""; |
| 355 | if ( $r === $displayed_privacy_setting ) { |
| 356 | $out .= ' selected'; |
| 357 | } |
| 358 | |
| 359 | $out .= ">$label</option>"; |
| 360 | } |
| 361 | |
| 362 | $out .= '</select>'; |
| 363 | |
| 364 | return $out; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Build HTML to display a form input radio button for video ratings |
| 369 | * |
| 370 | * @param object $info Database row from the videos table. |
| 371 | * @return string Input elements of type radio with existing stored value selected. |
| 372 | */ |
| 373 | protected function display_rating( $info ) { |
| 374 | $out = ''; |
| 375 | |
| 376 | $ratings = array( |
| 377 | 'G' => 'G', |
| 378 | 'PG-13' => 'PG-13', |
| 379 | 'R-17' => 'R', |
| 380 | ); |
| 381 | |
| 382 | $displayed_rating = isset( $info->rating ) ? $info->rating : null; |
| 383 | |
| 384 | // X-18 was previously supported but is now removed to better comply with our TOS. |
| 385 | if ( 'X-18' === $displayed_rating ) { |
| 386 | $displayed_rating = 'R-17'; |
| 387 | } |
| 388 | |
| 389 | foreach ( $ratings as $r => $label ) { |
| 390 | $id = "attachments-{$info->post_id}-rating-$r"; |
| 391 | $out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\""; |
| 392 | if ( $displayed_rating === $r ) { |
| 393 | $out .= ' checked="checked"'; |
| 394 | } |
| 395 | |
| 396 | $out .= " />$label</label>"; |
| 397 | unset( $id ); |
| 398 | } |
| 399 | |
| 400 | return $out; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Let's start this thing up. |
| 405 | VideoPress_Edit_Attachment::init(); |