diff -r 3d4e9c994f10 -r a86126ab1dd4 wp/wp-admin/includes/media.php
--- a/wp/wp-admin/includes/media.php Tue Oct 22 16:11:46 2019 +0200
+++ b/wp/wp-admin/includes/media.php Tue Dec 15 13:49:49 2020 +0100
@@ -11,11 +11,11 @@
*
* @since 2.5.0
*
- * @return array default tabs
+ * @return string[] Default tabs.
*/
function media_upload_tabs() {
$_default_tabs = array(
- 'type' => __( 'From Computer' ), // handler action suffix => tab text
+ 'type' => __( 'From Computer' ), // Handler action suffix => tab text.
'type_url' => __( 'From URL' ),
'gallery' => __( 'Gallery' ),
'library' => __( 'Media Library' ),
@@ -26,7 +26,7 @@
*
* @since 2.5.0
*
- * @param array $_default_tabs An array of media tabs.
+ * @param string[] $_default_tabs An array of media tabs.
*/
return apply_filters( 'media_upload_tabs', $_default_tabs );
}
@@ -60,6 +60,7 @@
return $tabs;
}
+ /* translators: %s: Number of attachments. */
$tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "$attachments" );
return $tabs;
@@ -79,6 +80,7 @@
if ( ! empty( $tabs ) ) {
echo "
\n";
}
}
@@ -116,7 +119,7 @@
*
* @since 2.5.0
*
- * @param int $id Image attachment id.
+ * @param int $id Image attachment ID.
* @param string $caption Image caption.
* @param string $title Image title attribute.
* @param string $align Image CSS alignment property.
@@ -151,7 +154,7 @@
* @since 2.5.0
*
* @param string $html The image HTML markup to send.
- * @param int $id The attachment id.
+ * @param int $id The attachment ID.
* @param string $caption The image caption.
* @param string $title The image title.
* @param string $align The image alignment.
@@ -166,7 +169,7 @@
}
/**
- * Adds image shortcode with caption to editor
+ * Adds image shortcode with caption to editor.
*
* @since 2.6.0
*
@@ -205,7 +208,7 @@
*
* @since 2.6.0
*
- * @param bool $bool Whether to disable appending captions. Returning true to the filter
+ * @param bool $bool Whether to disable appending captions. Returning true from the filter
* will disable captions. Default empty string.
*/
if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) {
@@ -223,7 +226,7 @@
$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
.
+ // 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 );
@@ -245,7 +248,7 @@
}
/**
- * Private preg_replace callback used in image_add_caption()
+ * Private preg_replace callback used in image_add_caption().
*
* @access private
* @since 3.4.0
@@ -256,7 +259,7 @@
}
/**
- * Adds image html to editor
+ * Adds image HTML to editor.
*
* @since 2.5.0
*
@@ -264,30 +267,31 @@
*/
function media_send_to_editor( $html ) {
?>
-
+
false ) ) {
-
$time = current_time( 'mysql' );
- if ( $post = get_post( $post_id ) ) {
+ $post = get_post( $post_id );
+
+ if ( $post ) {
// 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 ) {
$time = $post->post_date;
@@ -321,22 +325,22 @@
if ( ! empty( $title ) ) {
if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
- /* translators: 1: audio track title, 2: album title, 3: artist name */
+ /* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
$content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
} elseif ( ! empty( $meta['album'] ) ) {
- /* translators: 1: audio track title, 2: album title */
+ /* translators: 1: Audio track title, 2: Album title. */
$content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
} elseif ( ! empty( $meta['artist'] ) ) {
- /* translators: 1: audio track title, 2: artist name */
+ /* translators: 1: Audio track title, 2: Artist name. */
$content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
} else {
- /* translators: 1: audio track title */
+ /* translators: %s: Audio track title. */
$content .= sprintf( __( '"%s".' ), $title );
}
} elseif ( ! empty( $meta['album'] ) ) {
if ( ! empty( $meta['artist'] ) ) {
- /* translators: 1: audio album title, 2: artist name */
+ /* translators: 1: Audio album title, 2: Artist name. */
$content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
} else {
$content .= $meta['album'] . '.';
@@ -348,38 +352,43 @@
}
if ( ! empty( $meta['year'] ) ) {
- /* translators: Audio file track information. %d: Year of audio track release */
+ /* translators: Audio file track information. %d: Year of audio track release. */
$content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
}
if ( ! empty( $meta['track_number'] ) ) {
$track_number = explode( '/', $meta['track_number'] );
+
if ( isset( $track_number[1] ) ) {
- /* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks */
+ /* 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. %s: Audio track number */
+ /* 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. %s: 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.
- } 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'];
- }
-
- if ( trim( $image_meta['caption'] ) ) {
- $excerpt = $image_meta['caption'];
+ } elseif ( 0 === strpos( $type, 'image/' ) ) {
+ $image_meta = wp_read_image_metadata( $file );
+
+ if ( $image_meta ) {
+ if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
+ $title = $image_meta['title'];
+ }
+
+ if ( trim( $image_meta['caption'] ) ) {
+ $excerpt = $image_meta['caption'];
+ }
}
}
- // Construct the attachment array
+ // Construct the attachment array.
$attachment = array_merge(
array(
'post_mime_type' => $type,
@@ -395,38 +404,50 @@
// 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 ) ) {
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
+ // Save the data.
+ $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
+
+ if ( ! is_wp_error( $attachment_id ) ) {
+ // Set a custom header with the attachment_id.
+ // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+ if ( ! headers_sent() ) {
+ header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+ }
+
+ // The image sub-sizes are created during wp_generate_attachment_metadata().
+ // This is generally slow and may cause timeouts or out of memory errors.
+ wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
}
- return $id;
-
+ return $attachment_id;
}
/**
* Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
*
* @since 2.6.0
+ * @since 5.3.0 The `$post_id` parameter was made optional.
*
* @param array $file_array Array similar to a `$_FILES` upload array.
- * @param int $post_id The post ID the media is associated with.
+ * @param int $post_id Optional. The post ID the media is associated with.
* @param string $desc Optional. Description of the side-loaded file. Default null.
* @param array $post_data Optional. Post data to override. Default empty array.
- * @return int|object The ID of the attachment or a WP_Error on failure.
+ * @return int|WP_Error The ID of the attachment or a WP_Error on failure.
*/
-function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array() ) {
+function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
$overrides = array( 'test_form' => false );
$time = current_time( 'mysql' );
- if ( $post = get_post( $post_id ) ) {
+ $post = get_post( $post_id );
+
+ if ( $post ) {
if ( substr( $post->post_date, 0, 4 ) > 0 ) {
$time = $post->post_date;
}
}
$file = wp_handle_sideload( $file_array, $overrides, $time );
+
if ( isset( $file['error'] ) ) {
return new WP_Error( 'upload_error', $file['error'] );
}
@@ -438,10 +459,13 @@
$content = '';
// Use image exif/iptc data for title and caption defaults if possible.
- if ( $image_meta = wp_read_image_metadata( $file ) ) {
+ $image_meta = wp_read_image_metadata( $file );
+
+ if ( $image_meta ) {
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
$title = $image_meta['title'];
}
+
if ( trim( $image_meta['caption'] ) ) {
$content = $image_meta['caption'];
}
@@ -466,43 +490,49 @@
// 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, true );
- if ( ! is_wp_error( $id ) ) {
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
+ // Save the attachment metadata.
+ $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
+
+ if ( ! is_wp_error( $attachment_id ) ) {
+ wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
}
- return $id;
+ return $attachment_id;
}
/**
- * Adds the iframe to display content for the media upload page
+ * Outputs the iframe to display the media upload page.
*
* @since 2.5.0
+ * @since 5.3.0 Formalized the existing and already documented `...$args` parameter
+ * by adding it to the function signature.
*
* @global int $body_id
*
- * @param string|callable $content_func
+ * @param callable $content_func Function that outputs the content.
+ * @param mixed ...$args Optional additional parameters to pass to the callback function when it's called.
*/
-function wp_iframe( $content_func /* ... */ ) {
+function wp_iframe( $content_func, ...$args ) {
_wp_admin_html_begin();
?>
- › —
+ › —
-
+
-
- class="wp-core-ui no-js">
-
+
+ class="wp-core-ui no-js">
+
-
-
-