diff -r c7c34916027a -r 177826044cd9 wp/wp-admin/includes/media.php --- a/wp/wp-admin/includes/media.php Mon Oct 14 18:06:33 2019 +0200 +++ b/wp/wp-admin/includes/media.php Mon Oct 14 18:28:13 2019 +0200 @@ -15,10 +15,10 @@ */ function media_upload_tabs() { $_default_tabs = array( - 'type' => __('From Computer'), // handler action suffix => tab text - 'type_url' => __('From URL'), - 'gallery' => __('Gallery'), - 'library' => __('Media Library') + 'type' => __( 'From Computer' ), // handler action suffix => tab text + 'type_url' => __( 'From URL' ), + 'gallery' => __( 'Gallery' ), + 'library' => __( 'Media Library' ), ); /** @@ -41,25 +41,26 @@ * @param array $tabs * @return array $tabs with gallery if post has image attachment */ -function update_gallery_tab($tabs) { +function update_gallery_tab( $tabs ) { global $wpdb; - if ( !isset($_REQUEST['post_id']) ) { - unset($tabs['gallery']); + if ( ! isset( $_REQUEST['post_id'] ) ) { + unset( $tabs['gallery'] ); return $tabs; } - $post_id = intval($_REQUEST['post_id']); - - if ( $post_id ) + $post_id = intval( $_REQUEST['post_id'] ); + + if ( $post_id ) { $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) ); - - if ( empty($attachments) ) { - unset($tabs['gallery']); + } + + if ( empty( $attachments ) ) { + unset( $tabs['gallery'] ); return $tabs; } - $tabs['gallery'] = sprintf(__('Gallery (%s)'), "$attachments"); + $tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "$attachments" ); return $tabs; } @@ -73,14 +74,14 @@ */ function the_media_upload_tabs() { global $redir_tab; - $tabs = media_upload_tabs(); + $tabs = media_upload_tabs(); $default = 'type'; - if ( !empty($tabs) ) { + if ( ! empty( $tabs ) ) { echo "\n"; } @@ -131,8 +141,9 @@ $rel = ''; } - if ( $url ) + if ( $url ) { $html = '' . $html . ''; + } /** * Filters the image HTML markup to send to the editor when inserting an image. @@ -159,15 +170,15 @@ * * @since 2.6.0 * - * @param string $html - * @param integer $id - * @param string $caption image caption - * @param string $title image title attribute - * @param string $align image css alignment property - * @param string $url image src url - * @param string $size image size (thumbnail, medium, large, full or added with add_image_size() ) - * @param string $alt image alt attribute - * @return string + * @param string $html The image HTML markup to send. + * @param integer $id Image attachment ID. + * @param string $caption Image caption. + * @param string $title Image title attribute (not used). + * @param string $align Image CSS alignment property. + * @param string $url Image source URL (not used). + * @param string $size Image size (`thumbnail`, `medium`, `large`, `full`, or added with `add_image_size()`) (not used). + * @param string $alt Image `alt` attribute (not used). + * @return string The image HTML markup with caption shortcode. */ function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) { @@ -197,27 +208,30 @@ * @param bool $bool Whether to disable appending captions. Returning true to the filter * will disable captions. Default empty string. */ - if ( empty($caption) || apply_filters( 'disable_captions', '' ) ) + if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) { return $html; + } $id = ( 0 < (int) $id ) ? 'attachment_' . $id : ''; - if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) ) + if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) ) { return $html; + } $width = $matches[1]; - $caption = str_replace( array("\r\n", "\r"), "\n", $caption); + $caption = str_replace( array( "\r\n", "\r" ), "\n", $caption ); $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption ); // Convert any remaining line breaks to
. $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '
', $caption ); $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html ); - if ( empty($align) ) + if ( empty( $align ) ) { $align = 'none'; - - $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]'; + } + + $shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]'; /** * Filters the image HTML markup including the caption shortcode. @@ -248,13 +262,13 @@ * * @param string $html */ -function media_send_to_editor($html) { -?> +function media_send_to_editor( $html ) { + ?> - false )) { - - $time = current_time('mysql'); - if ( $post = get_post($post_id) ) { +function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) { + + $time = current_time( 'mysql' ); + if ( $post = get_post( $post_id ) ) { // The post date doesn't usually matter for pages, so don't backdate this upload. - if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) + if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) { $time = $post->post_date; + } } - $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); - - if ( isset($file['error']) ) + $file = wp_handle_upload( $_FILES[ $file_id ], $overrides, $time ); + + if ( isset( $file['error'] ) ) { return new WP_Error( 'upload_error', $file['error'] ); - - $name = $_FILES[$file_id]['name']; + } + + $name = $_FILES[ $file_id ]['name']; $ext = pathinfo( $name, PATHINFO_EXTENSION ); $name = wp_basename( $name, ".$ext" ); - $url = $file['url']; - $type = $file['type']; - $file = $file['file']; - $title = sanitize_text_field( $name ); + $url = $file['url']; + $type = $file['type']; + $file = $file['file']; + $title = sanitize_text_field( $name ); $content = ''; $excerpt = ''; @@ -317,7 +333,6 @@ /* translators: 1: audio track title */ $content .= sprintf( __( '"%s".' ), $title ); } - } elseif ( ! empty( $meta['album'] ) ) { if ( ! empty( $meta['artist'] ) ) { @@ -326,7 +341,6 @@ } else { $content .= $meta['album'] . '.'; } - } elseif ( ! empty( $meta['artist'] ) ) { $content .= $meta['artist'] . '.'; @@ -334,7 +348,7 @@ } if ( ! empty( $meta['year'] ) ) { - /* translators: Audio file track information. 1: Year of audio track release */ + /* translators: Audio file track information. %d: Year of audio track release */ $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] ); } @@ -344,17 +358,17 @@ /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */ $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) ); } else { - /* translators: Audio file track information. 1: Audio track number */ - $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) ); + /* translators: Audio file track information. %s: Audio track number */ + $content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) ); } } if ( ! empty( $meta['genre'] ) ) { - /* translators: Audio file genre information. 1: Audio genre name */ + /* translators: Audio file genre information. %s: Audio genre name */ $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] ); } - // Use image exif/iptc data for title and caption defaults if possible. + // Use image exif/iptc data for title and caption defaults if possible. } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = wp_read_image_metadata( $file ) ) { if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { $title = $image_meta['title']; @@ -366,21 +380,24 @@ } // Construct the attachment array - $attachment = array_merge( array( - 'post_mime_type' => $type, - 'guid' => $url, - 'post_parent' => $post_id, - 'post_title' => $title, - 'post_content' => $content, - 'post_excerpt' => $excerpt, - ), $post_data ); + $attachment = array_merge( + array( + 'post_mime_type' => $type, + 'guid' => $url, + 'post_parent' => $post_id, + 'post_title' => $title, + 'post_content' => $content, + 'post_excerpt' => $excerpt, + ), + $post_data + ); // This should never be set as it would then overwrite an existing attachment. unset( $attachment['ID'] ); // Save the data $id = wp_insert_attachment( $attachment, $file, $post_id, true ); - if ( !is_wp_error($id) ) { + if ( ! is_wp_error( $id ) ) { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); } @@ -400,51 +417,60 @@ * @return int|object The ID of the attachment or a WP_Error on failure. */ function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array() ) { - $overrides = array('test_form'=>false); + $overrides = array( 'test_form' => false ); $time = current_time( 'mysql' ); if ( $post = get_post( $post_id ) ) { - if ( substr( $post->post_date, 0, 4 ) > 0 ) + if ( substr( $post->post_date, 0, 4 ) > 0 ) { $time = $post->post_date; + } } $file = wp_handle_sideload( $file_array, $overrides, $time ); - if ( isset($file['error']) ) + if ( isset( $file['error'] ) ) { return new WP_Error( 'upload_error', $file['error'] ); - - $url = $file['url']; - $type = $file['type']; - $file = $file['file']; - $title = preg_replace('/\.[^.]+$/', '', basename($file)); + } + + $url = $file['url']; + $type = $file['type']; + $file = $file['file']; + $title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); $content = ''; // Use image exif/iptc data for title and caption defaults if possible. if ( $image_meta = wp_read_image_metadata( $file ) ) { - if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) + if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { $title = $image_meta['title']; - if ( trim( $image_meta['caption'] ) ) + } + if ( trim( $image_meta['caption'] ) ) { $content = $image_meta['caption']; + } } - if ( isset( $desc ) ) + if ( isset( $desc ) ) { $title = $desc; + } // Construct the attachment array. - $attachment = array_merge( array( - 'post_mime_type' => $type, - 'guid' => $url, - 'post_parent' => $post_id, - 'post_title' => $title, - 'post_content' => $content, - ), $post_data ); + $attachment = array_merge( + array( + 'post_mime_type' => $type, + 'guid' => $url, + 'post_parent' => $post_id, + 'post_title' => $title, + 'post_content' => $content, + ), + $post_data + ); // This should never be set as it would then overwrite an existing attachment. unset( $attachment['ID'] ); // Save the attachment metadata - $id = wp_insert_attachment($attachment, $file, $post_id); - if ( !is_wp_error($id) ) + $id = wp_insert_attachment( $attachment, $file, $post_id, true ); + if ( ! is_wp_error( $id ) ) { wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) ); + } return $id; } @@ -458,25 +484,26 @@ * * @param string|callable $content_func */ -function wp_iframe($content_func /* ... */) { +function wp_iframe( $content_func /* ... */ ) { _wp_admin_html_begin(); -?> -<?php bloginfo('name') ?> › <?php _e('Uploads'); ?> — <?php _e('WordPress'); ?> - + ?> +<?php bloginfo( 'name' ); ?> › <?php _e( 'Uploads' ); ?> — <?php _e( 'WordPress' ); ?> + - + if ( is_string( $content_func ) ) { + /** + * Fires in the admin header for each specific form tab in the legacy + * (pre-3.5.0) media upload popup. + * + * The dynamic portion of the hook, `$content_func`, refers to the form + * callback for the media upload type. Possible values include + * 'media_upload_type_form', 'media_upload_type_url_form', and + * 'media_upload_library_form'. + * + * @since 2.5.0 + */ + do_action( "admin_head_{$content_func}" ); + } + + $body_id_attr = ''; + if ( isset( $GLOBALS['body_id'] ) ) { + $body_id_attr = ' id="' . $GLOBALS['body_id'] . '"'; + } + ?> - class="wp-core-ui no-js"> + class="wp-core-ui no-js"> - + ?> - $post - ) ); + } + + wp_enqueue_media( + array( + 'post' => $post, + ) + ); $img = ' '; $id_attribute = $instance === 1 ? ' id="insert-media-button"' : ''; - printf( '', + printf( + '', $id_attribute, esc_attr( $editor_id ), $img . __( 'Add Media' ) @@ -590,14 +626,14 @@ if ( $legacy_filter ) { // #WP22559. Close if a plugin started by closing to open their own tag. - if ( 0 === stripos( trim( $legacy_filter ), '' ) ) + if ( 0 === stripos( trim( $legacy_filter ), '' ) ) { $legacy_filter .= ''; + } echo $legacy_filter; } } /** - * * @global int $post_ID * @param string $type * @param int $post_id @@ -607,16 +643,19 @@ function get_upload_iframe_src( $type = null, $post_id = null, $tab = null ) { global $post_ID; - if ( empty( $post_id ) ) + if ( empty( $post_id ) ) { $post_id = $post_ID; - - $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url('media-upload.php') ); - - if ( $type && 'media' != $type ) - $upload_iframe_src = add_query_arg('type', $type, $upload_iframe_src); - - if ( ! empty( $tab ) ) - $upload_iframe_src = add_query_arg('tab', $tab, $upload_iframe_src); + } + + $upload_iframe_src = add_query_arg( 'post_id', (int) $post_id, admin_url( 'media-upload.php' ) ); + + if ( $type && 'media' != $type ) { + $upload_iframe_src = add_query_arg( 'type', $type, $upload_iframe_src ); + } + + if ( ! empty( $tab ) ) { + $upload_iframe_src = add_query_arg( 'tab', $tab, $upload_iframe_src ); + } /** * Filters the upload iframe source URL for a specific media type. @@ -630,7 +669,7 @@ */ $upload_iframe_src = apply_filters( "{$type}_upload_iframe_src", $upload_iframe_src ); - return add_query_arg('TB_iframe', true, $upload_iframe_src); + return add_query_arg( 'TB_iframe', true, $upload_iframe_src ); } /** @@ -641,72 +680,83 @@ * @return mixed void|object WP_Error on failure */ function media_upload_form_handler() { - check_admin_referer('media-form'); + check_admin_referer( 'media-form' ); $errors = null; - if ( isset($_POST['send']) ) { - $keys = array_keys( $_POST['send'] ); + if ( isset( $_POST['send'] ) ) { + $keys = array_keys( $_POST['send'] ); $send_id = (int) reset( $keys ); } - if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { - $post = $_post = get_post($attachment_id, ARRAY_A); - - if ( !current_user_can( 'edit_post', $attachment_id ) ) - continue; - - if ( isset($attachment['post_content']) ) - $post['post_content'] = $attachment['post_content']; - if ( isset($attachment['post_title']) ) - $post['post_title'] = $attachment['post_title']; - if ( isset($attachment['post_excerpt']) ) - $post['post_excerpt'] = $attachment['post_excerpt']; - if ( isset($attachment['menu_order']) ) - $post['menu_order'] = $attachment['menu_order']; - - if ( isset($send_id) && $attachment_id == $send_id ) { - if ( isset($attachment['post_parent']) ) - $post['post_parent'] = $attachment['post_parent']; - } - - /** - * Filters the attachment fields to be saved. - * - * @since 2.5.0 - * - * @see wp_get_attachment_metadata() - * - * @param array $post An array of post data. - * @param array $attachment An array of attachment metadata. - */ - $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); - - if ( isset($attachment['image_alt']) ) { - $image_alt = wp_unslash( $attachment['image_alt'] ); - if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) { - $image_alt = wp_strip_all_tags( $image_alt, true ); - - // Update_meta expects slashed. - update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); + if ( ! empty( $_POST['attachments'] ) ) { + foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { + $post = $_post = get_post( $attachment_id, ARRAY_A ); + + if ( ! current_user_can( 'edit_post', $attachment_id ) ) { + continue; + } + + if ( isset( $attachment['post_content'] ) ) { + $post['post_content'] = $attachment['post_content']; + } + if ( isset( $attachment['post_title'] ) ) { + $post['post_title'] = $attachment['post_title']; + } + if ( isset( $attachment['post_excerpt'] ) ) { + $post['post_excerpt'] = $attachment['post_excerpt']; + } + if ( isset( $attachment['menu_order'] ) ) { + $post['menu_order'] = $attachment['menu_order']; + } + + if ( isset( $send_id ) && $attachment_id == $send_id ) { + if ( isset( $attachment['post_parent'] ) ) { + $post['post_parent'] = $attachment['post_parent']; + } + } + + /** + * Filters the attachment fields to be saved. + * + * @since 2.5.0 + * + * @see wp_get_attachment_metadata() + * + * @param array $post An array of post data. + * @param array $attachment An array of attachment metadata. + */ + $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); + + if ( isset( $attachment['image_alt'] ) ) { + $image_alt = wp_unslash( $attachment['image_alt'] ); + if ( $image_alt != get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) { + $image_alt = wp_strip_all_tags( $image_alt, true ); + + // Update_meta expects slashed. + update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); + } + } + + if ( isset( $post['errors'] ) ) { + $errors[ $attachment_id ] = $post['errors']; + unset( $post['errors'] ); + } + + if ( $post != $_post ) { + wp_update_post( $post ); + } + + foreach ( get_attachment_taxonomies( $post ) as $t ) { + if ( isset( $attachment[ $t ] ) ) { + wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false ); + } } } - - if ( isset($post['errors']) ) { - $errors[$attachment_id] = $post['errors']; - unset($post['errors']); - } - - if ( $post != $_post ) - wp_update_post($post); - - foreach ( get_attachment_taxonomies($post) as $t ) { - if ( isset($attachment[$t]) ) - wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false); - } } - if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?> + if ( isset( $_POST['insert-gallery'] ) || isset( $_POST['update-gallery'] ) ) { + ?>
- +
-

-

-

+

+

+

- +
@@ -1954,24 +2097,24 @@ do_action( 'pre-html-upload-ui' ); ?>

- + - +

- +

- + } + ?>
- + - - -

- - + + +

+ + -
'.esc_html($id->get_error_message()).'
'; - exit; +
+ ' . esc_html( $id->get_error_message() ) . '
'; + exit; + } } -} -?> + ?> +

- +

- + } + ?>
- - -

+ + +

- - - + + + - - | - | - + + | + | +
- - + + - - - + + +
- - + +

- 'save-all', 'style' => 'display: none;' ) ); ?> + 'save-all', + 'style' => 'display: none;', + ) + ); + ?>