Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * VideoPress edit attachment screen |
||
| 4 | * |
||
| 5 | * @since 4.1 |
||
| 6 | */ |
||
| 7 | class VideoPress_Edit_Attachment { |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Singleton method to initialize the object only once. |
||
| 11 | * |
||
| 12 | * @return VideoPress_Edit_Attachment |
||
| 13 | */ |
||
| 14 | public static function init() { |
||
| 15 | static $instance = null; |
||
| 16 | |||
| 17 | if ( ! $instance ) { |
||
| 18 | $instance = new VideoPress_Edit_Attachment(); |
||
| 19 | } |
||
| 20 | |||
| 21 | return $instance; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * VideoPress_Edit_Attachment constructor. |
||
| 26 | * |
||
| 27 | * Adds in appropriate actions for attachment fields editor, meta boxes and saving. |
||
| 28 | */ |
||
| 29 | public function __construct() { |
||
| 30 | add_filter( 'attachment_fields_to_edit', array( $this, 'fields_to_edit' ), 10, 2 ); |
||
| 31 | add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 10, 2 ); |
||
| 32 | add_filter( 'wp_ajax_save-attachment', array( $this, 'save_fields' ), -1 ); |
||
| 33 | add_filter( 'wp_ajax_save-attachment-compat', array( $this, 'save_fields' ), -1 ); |
||
| 34 | |||
| 35 | add_action( 'add_meta_boxes', array( $this, 'configure_meta_boxes' ), 10, 2 ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @param string $post_type |
||
| 40 | * @param object $post |
||
| 41 | */ |
||
| 42 | public function configure_meta_boxes( $post_type = 'unknown', $post = NULL ) { |
||
| 43 | if ( NULL == $post ) { |
||
| 44 | $post = (object) array ( 'ID' => 0 ); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ( 'attachment' != $post_type ) { |
||
| 48 | return; |
||
| 49 | } |
||
| 50 | |||
| 51 | // If this has not been processed by videopress, we can skip the rest. |
||
| 52 | if ( ! is_videopress_attachment( $post->ID ) ) { |
||
| 53 | return; |
||
| 54 | } |
||
| 55 | |||
| 56 | add_meta_box( 'videopress-media-info', __( 'VideoPress Information', 'jetpack' ), array( $this, 'videopress_information_box' ), 'attachment', 'side', 'core' ); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param array $post |
||
| 61 | * @param array|null $attachment |
||
| 62 | * |
||
| 63 | * @return array |
||
| 64 | */ |
||
| 65 | public function save_fields( $post, $attachment = null ) { |
||
| 66 | if ( $attachment === null && isset( $_POST['attachment'] ) ) { |
||
| 67 | $attachment = $_POST['attachment']; |
||
| 68 | } |
||
| 69 | |||
| 70 | if ( ! isset( $attachment['is_videopress_attachment'] ) || $attachment['is_videopress_attachment'] !== 'yes' ) { |
||
| 71 | return $post; |
||
| 72 | } |
||
| 73 | |||
| 74 | $post_id = absint( $post['ID'] ); |
||
| 75 | |||
| 76 | $meta = wp_get_attachment_metadata( $post_id ); |
||
| 77 | |||
| 78 | // If this has not been processed by videopress, we can skip the rest. |
||
| 79 | if ( ! is_videopress_attachment( $post['ID'] ) ) { |
||
| 80 | return $post; |
||
| 81 | } |
||
| 82 | |||
| 83 | $values = array(); |
||
| 84 | |||
| 85 | // Add the video title & description in, so that we save it properly. |
||
| 86 | if ( isset( $_POST['post_title'] ) ) { |
||
| 87 | $values['title'] = trim( strip_tags( $_POST['post_title'] ) ); |
||
| 88 | } |
||
| 89 | |||
| 90 | if ( isset( $_POST['post_excerpt'] ) ) { |
||
| 91 | $values['description'] = trim( strip_tags( $_POST['post_excerpt'] ) ); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ( isset( $attachment['rating'] ) ) { |
||
| 95 | $rating = $attachment['rating']; |
||
| 96 | |||
| 97 | if ( ! empty( $rating ) && in_array( $rating, array( 'G', 'PG-13', 'R-17', 'X-18' ) ) ) { |
||
| 98 | $values['rating'] = $rating; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // We set a default here, as if it isn't selected, then we'll turn it off. |
||
| 103 | $values['display_embed'] = 0; |
||
| 104 | if ( isset( $attachment['display_embed'] ) ) { |
||
| 105 | $display_embed = $attachment['display_embed']; |
||
| 106 | |||
| 107 | $values['display_embed'] = 'on' === $display_embed ? 1 : 0; |
||
| 108 | } |
||
| 109 | |||
| 110 | $args = array( |
||
| 111 | 'method' => 'POST', |
||
| 112 | ); |
||
| 113 | |||
| 114 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
||
| 115 | |||
| 116 | $endpoint = "videos/{$guid}"; |
||
| 117 | $result = Jetpack_Client::wpcom_json_api_request_as_blog( $endpoint, Jetpack_Client::WPCOM_JSON_API_VERSION, $args, $values ); |
||
|
0 ignored issues
–
show
|
|||
| 118 | |||
| 119 | if ( is_wp_error( $result ) ) { |
||
| 120 | $post['errors']['videopress']['errors'][] = __( 'There was an issue saving your updates to the VideoPress service. Please try again later.', 'jetpack' ); |
||
| 121 | return $post; |
||
| 122 | } |
||
| 123 | |||
| 124 | if ( isset( $values['display_embed'] ) ) { |
||
| 125 | $meta['videopress']['display_embed'] = $values['display_embed']; |
||
| 126 | } |
||
| 127 | |||
| 128 | if ( isset( $values['rating'] ) ) { |
||
| 129 | $meta['videopress']['rating'] = $values['rating']; |
||
| 130 | } |
||
| 131 | |||
| 132 | wp_update_attachment_metadata( $post_id, $meta ); |
||
| 133 | |||
| 134 | $response = json_decode( $result['body'], true ); |
||
| 135 | |||
| 136 | if ( 'true' !== $response ) { |
||
| 137 | return $post; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $post; |
||
| 141 | } |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * Get the upload api path. |
||
| 146 | * |
||
| 147 | * @param string $guid |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function make_video_api_path( $guid ) { |
||
| 151 | return sprintf( |
||
| 152 | '%s://%s/rest/v%s/videos/%s', |
||
| 153 | 'https', |
||
| 154 | 'public-api.wordpress.com', //JETPACK__WPCOM_JSON_API_HOST, |
||
| 155 | Jetpack_Client::WPCOM_JSON_API_VERSION, |
||
| 156 | $guid |
||
| 157 | ); |
||
| 158 | } |
||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Creates an array of video fields to edit based on transcoded videos. |
||
| 163 | * |
||
| 164 | * @param array $fields video fields of interest |
||
| 165 | * @param stdClass $post post object |
||
| 166 | * @return array modified version of video fields for administrative interface display |
||
| 167 | */ |
||
| 168 | public function fields_to_edit( $fields, $post ) { |
||
| 169 | $post_id = absint( $post->ID ); |
||
| 170 | |||
| 171 | $meta = wp_get_attachment_metadata( $post_id ); |
||
| 172 | |||
| 173 | // If this has not been processed by videopress, we can skip the rest. |
||
| 174 | if ( ! is_videopress_attachment( $post_id ) || ! isset( $meta['videopress'] ) ) { |
||
| 175 | return $fields; |
||
| 176 | } |
||
| 177 | |||
| 178 | $info = (object) $meta['videopress']; |
||
| 179 | $file_statuses = isset( $meta['file_statuses'] ) ? $meta['file_statuses'] : array(); |
||
| 180 | |||
| 181 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
||
| 182 | |||
| 183 | unset( $fields['url'] ); |
||
| 184 | unset( $fields['post_content'] ); |
||
| 185 | |||
| 186 | if ( isset( $file_statuses['ogg'] ) && 'done' === $file_statuses['ogg'] ) { |
||
| 187 | $v_name = preg_replace( '/\.\w+/', '', basename( $info->path ) ); |
||
| 188 | $video_name = $v_name . '_fmt1.ogv'; |
||
| 189 | $ogg_url = videopress_cdn_file_url( $guid, $video_name ); |
||
| 190 | |||
| 191 | $fields['video-ogg'] = array( |
||
| 192 | 'label' => __( 'Ogg File URL', 'jetpack' ), |
||
| 193 | 'input' => 'html', |
||
| 194 | 'html' => "<input type='text' class='urlfield' readonly='readonly' name='attachments[$post_id][oggurl]' value='" . esc_url( $ogg_url, array( 'http', 'https' ) ) . "' />", |
||
| 195 | 'helps' => __( 'Location of the Ogg video file.', 'jetpack' ), |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | $fields['post_title']['helps'] = __( 'Title will appear on the first frame of your video', 'jetpack' ); |
||
| 200 | |||
| 201 | $fields['post_excerpt']['label'] = _x( 'Description', 'A header for the short description display', 'jetpack' ); |
||
| 202 | $fields['post_excerpt']['input'] = 'textarea'; |
||
| 203 | $fields['post_excerpt']['value'] = $info->description; |
||
| 204 | |||
| 205 | $fields['is_videopress_attachment'] = array( |
||
| 206 | 'input' => 'hidden', |
||
| 207 | 'value' => 'yes', |
||
| 208 | ); |
||
| 209 | |||
| 210 | $fields['videopress_shortcode'] = array( |
||
| 211 | 'label' => _x( 'Shortcode', 'A header for the shortcode display', 'jetpack' ), |
||
| 212 | 'input' => 'html', |
||
| 213 | 'html' => "<input type=\"text\" name=\"videopress_shortcode\" value=\"[videopress {$guid}]\" readonly=\"readonly\"/>", |
||
| 214 | 'show_in_modal' => true, |
||
| 215 | 'show_in_edit' => false, |
||
| 216 | ); |
||
| 217 | |||
| 218 | $fields['display_embed'] = array( |
||
| 219 | 'label' => _x( 'Share', 'A header for the video sharing options area', 'jetpack' ), |
||
| 220 | 'input' => 'html', |
||
| 221 | 'html' => $this->display_embed_choice( $info ) |
||
| 222 | ); |
||
| 223 | |||
| 224 | $fields['video-rating'] = array( |
||
| 225 | 'label' => _x( 'Rating', 'A header for the video rating area', 'jetpack' ), |
||
| 226 | 'input' => 'html', |
||
| 227 | 'html' => $this->display_rating( $info ) |
||
| 228 | ); |
||
| 229 | |||
| 230 | return $fields; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param stdClass $post |
||
| 235 | */ |
||
| 236 | public function videopress_information_box( $post ) { |
||
| 237 | $post_id = absint( $post->ID ); |
||
| 238 | |||
| 239 | $meta = wp_get_attachment_metadata( $post_id ); |
||
| 240 | $guid = get_post_meta( $post_id, 'videopress_guid', true ); |
||
| 241 | |||
| 242 | // If this has not been processed by videopress, we can skip the rest. |
||
| 243 | if ( ! is_videopress_attachment( $post_id ) ) { |
||
| 244 | return; |
||
| 245 | } |
||
| 246 | |||
| 247 | $info = (object) $meta['videopress']; |
||
| 248 | |||
| 249 | $status = videopress_get_transcoding_status( $post_id ); |
||
| 250 | |||
| 251 | $formats = array( |
||
| 252 | 'std_mp4' => 'Standard MP4', |
||
| 253 | 'std_ogg' => 'OGG Vorbis', |
||
| 254 | 'dvd_mp4' => 'DVD', |
||
| 255 | 'hd_mp4' => 'High Definition', |
||
| 256 | ); |
||
| 257 | |||
| 258 | $embed = "[videopress {$guid}]"; |
||
| 259 | |||
| 260 | $shortcode = '<input type="text" id="plugin-embed" readonly="readonly" style="width:180px;" value="' . esc_attr( $embed ) . '" onclick="this.focus();this.select();" />'; |
||
| 261 | |||
| 262 | $trans_status = ''; |
||
| 263 | $all_trans_done = true; |
||
| 264 | foreach ( $formats as $status_key => $name ) { |
||
| 265 | if ( 'DONE' !== $status[ $status_key ] ) { |
||
| 266 | $all_trans_done = false; |
||
| 267 | } |
||
| 268 | |||
| 269 | $trans_status .= '- <strong>' . $name . ":</strong> <span id=\"status_$status_key\">" . ( 'DONE' === $status[ $status_key ] ? 'Done' : 'Processing' ) . '</span><br>'; |
||
| 270 | } |
||
| 271 | |||
| 272 | $nonce = wp_create_nonce( 'videopress-update-transcoding-status' ); |
||
| 273 | |||
| 274 | $url = 'empty'; |
||
| 275 | if ( ! empty( $guid ) ) { |
||
| 276 | $url = videopress_build_url( $guid ); |
||
| 277 | $url = "<a href=\"{$url}\">{$url}</a>"; |
||
| 278 | } |
||
| 279 | |||
| 280 | $poster = '<em>Still Processing</em>'; |
||
| 281 | if ( ! empty( $info->poster ) ) { |
||
| 282 | $poster = "<br><img src=\"{$info->poster}\" width=\"175px\">"; |
||
| 283 | } |
||
| 284 | |||
| 285 | $status_update = ''; |
||
| 286 | if ( ! $all_trans_done ) { |
||
| 287 | $status_update = ' (<a href="javascript:;" id="videopress-update-transcoding-status">update</a>)'; |
||
| 288 | } |
||
| 289 | |||
| 290 | $html = <<< HTML |
||
| 291 | |||
| 292 | <div class="misc-pub-section misc-pub-shortcode"> |
||
| 293 | <strong>Shortcode</strong><br> |
||
| 294 | {$shortcode} |
||
| 295 | </div> |
||
| 296 | <div class="misc-pub-section misc-pub-url"> |
||
| 297 | <strong>Url</strong> |
||
| 298 | {$url} |
||
| 299 | </div> |
||
| 300 | <div class="misc-pub-section misc-pub-poster"> |
||
| 301 | <strong>Poster</strong> |
||
| 302 | {$poster} |
||
| 303 | </div> |
||
| 304 | <div class="misc-pub-section misc-pub-status"> |
||
| 305 | <strong>Transcoding Status$status_update:</strong> |
||
| 306 | <div id="videopress-transcoding-status">{$trans_status}</div> |
||
| 307 | </div> |
||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | <script> |
||
| 312 | jQuery( function($) { |
||
| 313 | $( '#videopress-update-transcoding-status' ).on( "click", function() { |
||
| 314 | jQuery.ajax( { |
||
| 315 | type: 'post', |
||
| 316 | url: 'admin-ajax.php', |
||
| 317 | data: { |
||
| 318 | action: 'videopress-update-transcoding-status', |
||
| 319 | post_id: '{$post_id}', |
||
| 320 | _ajax_nonce: '{$nonce}' |
||
| 321 | }, |
||
| 322 | complete: function( response ) { |
||
| 323 | if ( 200 === response.status ) { |
||
| 324 | var statuses = response.responseJSON.data.status; |
||
| 325 | |||
| 326 | for (var key in statuses) { |
||
| 327 | $('#status_' + key).text( 'DONE' === statuses[key] ? 'Done' : 'Processing' ); |
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | }); |
||
| 332 | } ); |
||
| 333 | } ); |
||
| 334 | </script> |
||
| 335 | HTML; |
||
| 336 | |||
| 337 | echo $html; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Build HTML to display a form checkbox for embedcode display preference |
||
| 342 | * |
||
| 343 | * @param object $info database row from the videos table |
||
| 344 | * @return string input element of type checkbox set to checked state based on stored embed preference |
||
| 345 | */ |
||
| 346 | protected function display_embed_choice( $info ) { |
||
| 347 | $id = "attachments-{$info->post_id}-displayembed"; |
||
| 348 | $out = "<label for='$id'><input type='checkbox' name='attachments[{$info->post_id}][display_embed]' id='$id'"; |
||
| 349 | if ( $info->display_embed ) |
||
| 350 | $out .= ' checked="checked"'; |
||
| 351 | $out .= " />" . __( 'Display share menu and allow viewers to embed or download this video', 'jetpack' ) . '</label>'; |
||
| 352 | return $out; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Build HTML to display a form input radio button for video ratings |
||
| 357 | * |
||
| 358 | * @param object $info database row from the videos table |
||
| 359 | * @return string input elements of type radio with existing stored value selected |
||
| 360 | */ |
||
| 361 | protected function display_rating( $info ) { |
||
| 362 | $out = ''; |
||
| 363 | |||
| 364 | $ratings = array( |
||
| 365 | 'G' => 'G', |
||
| 366 | 'PG-13' => 'PG-13', |
||
| 367 | 'R-17' => 'R', |
||
| 368 | 'X-18' => 'X', |
||
| 369 | ); |
||
| 370 | |||
| 371 | foreach( $ratings as $r => $label ) { |
||
| 372 | $id = "attachments-{$info->post_id}-rating-$r"; |
||
| 373 | $out .= "<label for=\"$id\"><input type=\"radio\" name=\"attachments[{$info->post_id}][rating]\" id=\"$id\" value=\"$r\""; |
||
| 374 | if ( $info->rating == $r ) { |
||
| 375 | $out .= ' checked="checked"'; |
||
| 376 | } |
||
| 377 | |||
| 378 | $out .= " />$label</label>"; |
||
| 379 | unset( $id ); |
||
| 380 | } |
||
| 381 | |||
| 382 | return $out; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | // Let's start this thing up. |
||
| 387 | VideoPress_Edit_Attachment::init(); |
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: