Edit File by line
/home/barbar84/www/wp-admin/includes
File: media.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress Administration Media API.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Administration
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
/**
[8] Fix | Delete
* Defines the default media upload tabs
[9] Fix | Delete
*
[10] Fix | Delete
* @since 2.5.0
[11] Fix | Delete
*
[12] Fix | Delete
* @return string[] Default tabs.
[13] Fix | Delete
*/
[14] Fix | Delete
function media_upload_tabs() {
[15] Fix | Delete
$_default_tabs = array(
[16] Fix | Delete
'type' => __( 'From Computer' ), // Handler action suffix => tab text.
[17] Fix | Delete
'type_url' => __( 'From URL' ),
[18] Fix | Delete
'gallery' => __( 'Gallery' ),
[19] Fix | Delete
'library' => __( 'Media Library' ),
[20] Fix | Delete
);
[21] Fix | Delete
[22] Fix | Delete
/**
[23] Fix | Delete
* Filters the available tabs in the legacy (pre-3.5.0) media popup.
[24] Fix | Delete
*
[25] Fix | Delete
* @since 2.5.0
[26] Fix | Delete
*
[27] Fix | Delete
* @param string[] $_default_tabs An array of media tabs.
[28] Fix | Delete
*/
[29] Fix | Delete
return apply_filters( 'media_upload_tabs', $_default_tabs );
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Adds the gallery tab back to the tabs array if post has image attachments
[34] Fix | Delete
*
[35] Fix | Delete
* @since 2.5.0
[36] Fix | Delete
*
[37] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[38] Fix | Delete
*
[39] Fix | Delete
* @param array $tabs
[40] Fix | Delete
* @return array $tabs with gallery if post has image attachment
[41] Fix | Delete
*/
[42] Fix | Delete
function update_gallery_tab( $tabs ) {
[43] Fix | Delete
global $wpdb;
[44] Fix | Delete
[45] Fix | Delete
if ( ! isset( $_REQUEST['post_id'] ) ) {
[46] Fix | Delete
unset( $tabs['gallery'] );
[47] Fix | Delete
return $tabs;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
$post_id = (int) $_REQUEST['post_id'];
[51] Fix | Delete
[52] Fix | Delete
if ( $post_id ) {
[53] Fix | Delete
$attachments = (int) $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) );
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
if ( empty( $attachments ) ) {
[57] Fix | Delete
unset( $tabs['gallery'] );
[58] Fix | Delete
return $tabs;
[59] Fix | Delete
}
[60] Fix | Delete
[61] Fix | Delete
/* translators: %s: Number of attachments. */
[62] Fix | Delete
$tabs['gallery'] = sprintf( __( 'Gallery (%s)' ), "<span id='attachments-count'>$attachments</span>" );
[63] Fix | Delete
[64] Fix | Delete
return $tabs;
[65] Fix | Delete
}
[66] Fix | Delete
[67] Fix | Delete
/**
[68] Fix | Delete
* Outputs the legacy media upload tabs UI.
[69] Fix | Delete
*
[70] Fix | Delete
* @since 2.5.0
[71] Fix | Delete
*
[72] Fix | Delete
* @global string $redir_tab
[73] Fix | Delete
*/
[74] Fix | Delete
function the_media_upload_tabs() {
[75] Fix | Delete
global $redir_tab;
[76] Fix | Delete
$tabs = media_upload_tabs();
[77] Fix | Delete
$default = 'type';
[78] Fix | Delete
[79] Fix | Delete
if ( ! empty( $tabs ) ) {
[80] Fix | Delete
echo "<ul id='sidemenu'>\n";
[81] Fix | Delete
[82] Fix | Delete
if ( isset( $redir_tab ) && array_key_exists( $redir_tab, $tabs ) ) {
[83] Fix | Delete
$current = $redir_tab;
[84] Fix | Delete
} elseif ( isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $tabs ) ) {
[85] Fix | Delete
$current = $_GET['tab'];
[86] Fix | Delete
} else {
[87] Fix | Delete
/** This filter is documented in wp-admin/media-upload.php */
[88] Fix | Delete
$current = apply_filters( 'media_upload_default_tab', $default );
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
foreach ( $tabs as $callback => $text ) {
[92] Fix | Delete
$class = '';
[93] Fix | Delete
[94] Fix | Delete
if ( $current == $callback ) {
[95] Fix | Delete
$class = " class='current'";
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
$href = add_query_arg(
[99] Fix | Delete
array(
[100] Fix | Delete
'tab' => $callback,
[101] Fix | Delete
's' => false,
[102] Fix | Delete
'paged' => false,
[103] Fix | Delete
'post_mime_type' => false,
[104] Fix | Delete
'm' => false,
[105] Fix | Delete
)
[106] Fix | Delete
);
[107] Fix | Delete
$link = "<a href='" . esc_url( $href ) . "'$class>$text</a>";
[108] Fix | Delete
echo "\t<li id='" . esc_attr( "tab-$callback" ) . "'>$link</li>\n";
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
echo "</ul>\n";
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Retrieves the image HTML to send to the editor.
[117] Fix | Delete
*
[118] Fix | Delete
* @since 2.5.0
[119] Fix | Delete
*
[120] Fix | Delete
* @param int $id Image attachment ID.
[121] Fix | Delete
* @param string $caption Image caption.
[122] Fix | Delete
* @param string $title Image title attribute.
[123] Fix | Delete
* @param string $align Image CSS alignment property.
[124] Fix | Delete
* @param string $url Optional. Image src URL. Default empty.
[125] Fix | Delete
* @param bool|string $rel Optional. Value for rel attribute or whether to add a default value. Default false.
[126] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[127] Fix | Delete
* width and height values in pixels (in that order). Default 'medium'.
[128] Fix | Delete
* @param string $alt Optional. Image alt attribute. Default empty.
[129] Fix | Delete
* @return string The HTML output to insert into the editor.
[130] Fix | Delete
*/
[131] Fix | Delete
function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
[132] Fix | Delete
[133] Fix | Delete
$html = get_image_tag( $id, $alt, '', $align, $size );
[134] Fix | Delete
[135] Fix | Delete
if ( $rel ) {
[136] Fix | Delete
if ( is_string( $rel ) ) {
[137] Fix | Delete
$rel = ' rel="' . esc_attr( $rel ) . '"';
[138] Fix | Delete
} else {
[139] Fix | Delete
$rel = ' rel="attachment wp-att-' . (int) $id . '"';
[140] Fix | Delete
}
[141] Fix | Delete
} else {
[142] Fix | Delete
$rel = '';
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
if ( $url ) {
[146] Fix | Delete
$html = '<a href="' . esc_attr( $url ) . '"' . $rel . '>' . $html . '</a>';
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
/**
[150] Fix | Delete
* Filters the image HTML markup to send to the editor when inserting an image.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 2.5.0
[153] Fix | Delete
* @since 5.6.0 The `$rel` parameter was added.
[154] Fix | Delete
*
[155] Fix | Delete
* @param string $html The image HTML markup to send.
[156] Fix | Delete
* @param int $id The attachment ID.
[157] Fix | Delete
* @param string $caption The image caption.
[158] Fix | Delete
* @param string $title The image title.
[159] Fix | Delete
* @param string $align The image alignment.
[160] Fix | Delete
* @param string $url The image source URL.
[161] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[162] Fix | Delete
* an array of width and height values in pixels (in that order).
[163] Fix | Delete
* @param string $alt The image alternative, or alt, text.
[164] Fix | Delete
* @param string $rel The image rel attribute.
[165] Fix | Delete
*/
[166] Fix | Delete
$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt, $rel );
[167] Fix | Delete
[168] Fix | Delete
return $html;
[169] Fix | Delete
}
[170] Fix | Delete
[171] Fix | Delete
/**
[172] Fix | Delete
* Adds image shortcode with caption to editor.
[173] Fix | Delete
*
[174] Fix | Delete
* @since 2.6.0
[175] Fix | Delete
*
[176] Fix | Delete
* @param string $html The image HTML markup to send.
[177] Fix | Delete
* @param int $id Image attachment ID.
[178] Fix | Delete
* @param string $caption Image caption.
[179] Fix | Delete
* @param string $title Image title attribute (not used).
[180] Fix | Delete
* @param string $align Image CSS alignment property.
[181] Fix | Delete
* @param string $url Image source URL (not used).
[182] Fix | Delete
* @param string $size Image size (not used).
[183] Fix | Delete
* @param string $alt Image `alt` attribute (not used).
[184] Fix | Delete
* @return string The image HTML markup with caption shortcode.
[185] Fix | Delete
*/
[186] Fix | Delete
function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
[187] Fix | Delete
[188] Fix | Delete
/**
[189] Fix | Delete
* Filters the caption text.
[190] Fix | Delete
*
[191] Fix | Delete
* Note: If the caption text is empty, the caption shortcode will not be appended
[192] Fix | Delete
* to the image HTML when inserted into the editor.
[193] Fix | Delete
*
[194] Fix | Delete
* Passing an empty value also prevents the {@see 'image_add_caption_shortcode'}
[195] Fix | Delete
* Filters from being evaluated at the end of image_add_caption().
[196] Fix | Delete
*
[197] Fix | Delete
* @since 4.1.0
[198] Fix | Delete
*
[199] Fix | Delete
* @param string $caption The original caption text.
[200] Fix | Delete
* @param int $id The attachment ID.
[201] Fix | Delete
*/
[202] Fix | Delete
$caption = apply_filters( 'image_add_caption_text', $caption, $id );
[203] Fix | Delete
[204] Fix | Delete
/**
[205] Fix | Delete
* Filters whether to disable captions.
[206] Fix | Delete
*
[207] Fix | Delete
* Prevents image captions from being appended to image HTML when inserted into the editor.
[208] Fix | Delete
*
[209] Fix | Delete
* @since 2.6.0
[210] Fix | Delete
*
[211] Fix | Delete
* @param bool $bool Whether to disable appending captions. Returning true from the filter
[212] Fix | Delete
* will disable captions. Default empty string.
[213] Fix | Delete
*/
[214] Fix | Delete
if ( empty( $caption ) || apply_filters( 'disable_captions', '' ) ) {
[215] Fix | Delete
return $html;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
$id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
[219] Fix | Delete
[220] Fix | Delete
if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) ) {
[221] Fix | Delete
return $html;
[222] Fix | Delete
}
[223] Fix | Delete
[224] Fix | Delete
$width = $matches[1];
[225] Fix | Delete
[226] Fix | Delete
$caption = str_replace( array( "\r\n", "\r" ), "\n", $caption );
[227] Fix | Delete
$caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
[228] Fix | Delete
[229] Fix | Delete
// Convert any remaining line breaks to <br />.
[230] Fix | Delete
$caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
[231] Fix | Delete
[232] Fix | Delete
$html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
[233] Fix | Delete
if ( empty( $align ) ) {
[234] Fix | Delete
$align = 'none';
[235] Fix | Delete
}
[236] Fix | Delete
[237] Fix | Delete
$shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
[238] Fix | Delete
[239] Fix | Delete
/**
[240] Fix | Delete
* Filters the image HTML markup including the caption shortcode.
[241] Fix | Delete
*
[242] Fix | Delete
* @since 2.6.0
[243] Fix | Delete
*
[244] Fix | Delete
* @param string $shcode The image HTML markup with caption shortcode.
[245] Fix | Delete
* @param string $html The image HTML markup.
[246] Fix | Delete
*/
[247] Fix | Delete
return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
/**
[251] Fix | Delete
* Private preg_replace callback used in image_add_caption().
[252] Fix | Delete
*
[253] Fix | Delete
* @access private
[254] Fix | Delete
* @since 3.4.0
[255] Fix | Delete
*/
[256] Fix | Delete
function _cleanup_image_add_caption( $matches ) {
[257] Fix | Delete
// Remove any line breaks from inside the tags.
[258] Fix | Delete
return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
/**
[262] Fix | Delete
* Adds image HTML to editor.
[263] Fix | Delete
*
[264] Fix | Delete
* @since 2.5.0
[265] Fix | Delete
*
[266] Fix | Delete
* @param string $html
[267] Fix | Delete
*/
[268] Fix | Delete
function media_send_to_editor( $html ) {
[269] Fix | Delete
?>
[270] Fix | Delete
<script type="text/javascript">
[271] Fix | Delete
var win = window.dialogArguments || opener || parent || top;
[272] Fix | Delete
win.send_to_editor( <?php echo wp_json_encode( $html ); ?> );
[273] Fix | Delete
</script>
[274] Fix | Delete
<?php
[275] Fix | Delete
exit;
[276] Fix | Delete
}
[277] Fix | Delete
[278] Fix | Delete
/**
[279] Fix | Delete
* Saves a file submitted from a POST request and create an attachment post for it.
[280] Fix | Delete
*
[281] Fix | Delete
* @since 2.5.0
[282] Fix | Delete
*
[283] Fix | Delete
* @param string $file_id Index of the `$_FILES` array that the file was sent. Required.
[284] Fix | Delete
* @param int $post_id The post ID of a post to attach the media item to. Required, but can
[285] Fix | Delete
* be set to 0, creating a media item that has no relationship to a post.
[286] Fix | Delete
* @param array $post_data Optional. Overwrite some of the attachment.
[287] Fix | Delete
* @param array $overrides Optional. Override the wp_handle_upload() behavior.
[288] Fix | Delete
* @return int|WP_Error ID of the attachment or a WP_Error object on failure.
[289] Fix | Delete
*/
[290] Fix | Delete
function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
[291] Fix | Delete
$time = current_time( 'mysql' );
[292] Fix | Delete
$post = get_post( $post_id );
[293] Fix | Delete
[294] Fix | Delete
if ( $post ) {
[295] Fix | Delete
// The post date doesn't usually matter for pages, so don't backdate this upload.
[296] Fix | Delete
if ( 'page' !== $post->post_type && substr( $post->post_date, 0, 4 ) > 0 ) {
[297] Fix | Delete
$time = $post->post_date;
[298] Fix | Delete
}
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
$file = wp_handle_upload( $_FILES[ $file_id ], $overrides, $time );
[302] Fix | Delete
[303] Fix | Delete
if ( isset( $file['error'] ) ) {
[304] Fix | Delete
return new WP_Error( 'upload_error', $file['error'] );
[305] Fix | Delete
}
[306] Fix | Delete
[307] Fix | Delete
$name = $_FILES[ $file_id ]['name'];
[308] Fix | Delete
$ext = pathinfo( $name, PATHINFO_EXTENSION );
[309] Fix | Delete
$name = wp_basename( $name, ".$ext" );
[310] Fix | Delete
[311] Fix | Delete
$url = $file['url'];
[312] Fix | Delete
$type = $file['type'];
[313] Fix | Delete
$file = $file['file'];
[314] Fix | Delete
$title = sanitize_text_field( $name );
[315] Fix | Delete
$content = '';
[316] Fix | Delete
$excerpt = '';
[317] Fix | Delete
[318] Fix | Delete
if ( preg_match( '#^audio#', $type ) ) {
[319] Fix | Delete
$meta = wp_read_audio_metadata( $file );
[320] Fix | Delete
[321] Fix | Delete
if ( ! empty( $meta['title'] ) ) {
[322] Fix | Delete
$title = $meta['title'];
[323] Fix | Delete
}
[324] Fix | Delete
[325] Fix | Delete
if ( ! empty( $title ) ) {
[326] Fix | Delete
[327] Fix | Delete
if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
[328] Fix | Delete
/* translators: 1: Audio track title, 2: Album title, 3: Artist name. */
[329] Fix | Delete
$content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
[330] Fix | Delete
} elseif ( ! empty( $meta['album'] ) ) {
[331] Fix | Delete
/* translators: 1: Audio track title, 2: Album title. */
[332] Fix | Delete
$content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
[333] Fix | Delete
} elseif ( ! empty( $meta['artist'] ) ) {
[334] Fix | Delete
/* translators: 1: Audio track title, 2: Artist name. */
[335] Fix | Delete
$content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
[336] Fix | Delete
} else {
[337] Fix | Delete
/* translators: %s: Audio track title. */
[338] Fix | Delete
$content .= sprintf( __( '"%s".' ), $title );
[339] Fix | Delete
}
[340] Fix | Delete
} elseif ( ! empty( $meta['album'] ) ) {
[341] Fix | Delete
[342] Fix | Delete
if ( ! empty( $meta['artist'] ) ) {
[343] Fix | Delete
/* translators: 1: Audio album title, 2: Artist name. */
[344] Fix | Delete
$content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
[345] Fix | Delete
} else {
[346] Fix | Delete
$content .= $meta['album'] . '.';
[347] Fix | Delete
}
[348] Fix | Delete
} elseif ( ! empty( $meta['artist'] ) ) {
[349] Fix | Delete
[350] Fix | Delete
$content .= $meta['artist'] . '.';
[351] Fix | Delete
[352] Fix | Delete
}
[353] Fix | Delete
[354] Fix | Delete
if ( ! empty( $meta['year'] ) ) {
[355] Fix | Delete
/* translators: Audio file track information. %d: Year of audio track release. */
[356] Fix | Delete
$content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
[357] Fix | Delete
}
[358] Fix | Delete
[359] Fix | Delete
if ( ! empty( $meta['track_number'] ) ) {
[360] Fix | Delete
$track_number = explode( '/', $meta['track_number'] );
[361] Fix | Delete
[362] Fix | Delete
if ( isset( $track_number[1] ) ) {
[363] Fix | Delete
/* translators: Audio file track information. 1: Audio track number, 2: Total audio tracks. */
[364] Fix | Delete
$content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
[365] Fix | Delete
} else {
[366] Fix | Delete
/* translators: Audio file track information. %s: Audio track number. */
[367] Fix | Delete
$content .= ' ' . sprintf( __( 'Track %s.' ), number_format_i18n( $track_number[0] ) );
[368] Fix | Delete
}
[369] Fix | Delete
}
[370] Fix | Delete
[371] Fix | Delete
if ( ! empty( $meta['genre'] ) ) {
[372] Fix | Delete
/* translators: Audio file genre information. %s: Audio genre name. */
[373] Fix | Delete
$content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
[374] Fix | Delete
}
[375] Fix | Delete
[376] Fix | Delete
// Use image exif/iptc data for title and caption defaults if possible.
[377] Fix | Delete
} elseif ( 0 === strpos( $type, 'image/' ) ) {
[378] Fix | Delete
$image_meta = wp_read_image_metadata( $file );
[379] Fix | Delete
[380] Fix | Delete
if ( $image_meta ) {
[381] Fix | Delete
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
[382] Fix | Delete
$title = $image_meta['title'];
[383] Fix | Delete
}
[384] Fix | Delete
[385] Fix | Delete
if ( trim( $image_meta['caption'] ) ) {
[386] Fix | Delete
$excerpt = $image_meta['caption'];
[387] Fix | Delete
}
[388] Fix | Delete
}
[389] Fix | Delete
}
[390] Fix | Delete
[391] Fix | Delete
// Construct the attachment array.
[392] Fix | Delete
$attachment = array_merge(
[393] Fix | Delete
array(
[394] Fix | Delete
'post_mime_type' => $type,
[395] Fix | Delete
'guid' => $url,
[396] Fix | Delete
'post_parent' => $post_id,
[397] Fix | Delete
'post_title' => $title,
[398] Fix | Delete
'post_content' => $content,
[399] Fix | Delete
'post_excerpt' => $excerpt,
[400] Fix | Delete
),
[401] Fix | Delete
$post_data
[402] Fix | Delete
);
[403] Fix | Delete
[404] Fix | Delete
// This should never be set as it would then overwrite an existing attachment.
[405] Fix | Delete
unset( $attachment['ID'] );
[406] Fix | Delete
[407] Fix | Delete
// Save the data.
[408] Fix | Delete
$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
[409] Fix | Delete
[410] Fix | Delete
if ( ! is_wp_error( $attachment_id ) ) {
[411] Fix | Delete
// Set a custom header with the attachment_id.
[412] Fix | Delete
// Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
[413] Fix | Delete
if ( ! headers_sent() ) {
[414] Fix | Delete
header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
[415] Fix | Delete
}
[416] Fix | Delete
[417] Fix | Delete
// The image sub-sizes are created during wp_generate_attachment_metadata().
[418] Fix | Delete
// This is generally slow and may cause timeouts or out of memory errors.
[419] Fix | Delete
wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
[420] Fix | Delete
}
[421] Fix | Delete
[422] Fix | Delete
return $attachment_id;
[423] Fix | Delete
}
[424] Fix | Delete
[425] Fix | Delete
/**
[426] Fix | Delete
* Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
[427] Fix | Delete
*
[428] Fix | Delete
* @since 2.6.0
[429] Fix | Delete
* @since 5.3.0 The `$post_id` parameter was made optional.
[430] Fix | Delete
*
[431] Fix | Delete
* @param string[] $file_array Array that represents a `$_FILES` upload array.
[432] Fix | Delete
* @param int $post_id Optional. The post ID the media is associated with.
[433] Fix | Delete
* @param string $desc Optional. Description of the side-loaded file. Default null.
[434] Fix | Delete
* @param array $post_data Optional. Post data to override. Default empty array.
[435] Fix | Delete
* @return int|WP_Error The ID of the attachment or a WP_Error on failure.
[436] Fix | Delete
*/
[437] Fix | Delete
function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {
[438] Fix | Delete
$overrides = array( 'test_form' => false );
[439] Fix | Delete
[440] Fix | Delete
if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) {
[441] Fix | Delete
$time = $post_data['post_date'];
[442] Fix | Delete
} else {
[443] Fix | Delete
$post = get_post( $post_id );
[444] Fix | Delete
if ( $post && substr( $post->post_date, 0, 4 ) > 0 ) {
[445] Fix | Delete
$time = $post->post_date;
[446] Fix | Delete
} else {
[447] Fix | Delete
$time = current_time( 'mysql' );
[448] Fix | Delete
}
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
$file = wp_handle_sideload( $file_array, $overrides, $time );
[452] Fix | Delete
[453] Fix | Delete
if ( isset( $file['error'] ) ) {
[454] Fix | Delete
return new WP_Error( 'upload_error', $file['error'] );
[455] Fix | Delete
}
[456] Fix | Delete
[457] Fix | Delete
$url = $file['url'];
[458] Fix | Delete
$type = $file['type'];
[459] Fix | Delete
$file = $file['file'];
[460] Fix | Delete
$title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
[461] Fix | Delete
$content = '';
[462] Fix | Delete
[463] Fix | Delete
// Use image exif/iptc data for title and caption defaults if possible.
[464] Fix | Delete
$image_meta = wp_read_image_metadata( $file );
[465] Fix | Delete
[466] Fix | Delete
if ( $image_meta ) {
[467] Fix | Delete
if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
[468] Fix | Delete
$title = $image_meta['title'];
[469] Fix | Delete
}
[470] Fix | Delete
[471] Fix | Delete
if ( trim( $image_meta['caption'] ) ) {
[472] Fix | Delete
$content = $image_meta['caption'];
[473] Fix | Delete
}
[474] Fix | Delete
}
[475] Fix | Delete
[476] Fix | Delete
if ( isset( $desc ) ) {
[477] Fix | Delete
$title = $desc;
[478] Fix | Delete
}
[479] Fix | Delete
[480] Fix | Delete
// Construct the attachment array.
[481] Fix | Delete
$attachment = array_merge(
[482] Fix | Delete
array(
[483] Fix | Delete
'post_mime_type' => $type,
[484] Fix | Delete
'guid' => $url,
[485] Fix | Delete
'post_parent' => $post_id,
[486] Fix | Delete
'post_title' => $title,
[487] Fix | Delete
'post_content' => $content,
[488] Fix | Delete
),
[489] Fix | Delete
$post_data
[490] Fix | Delete
);
[491] Fix | Delete
[492] Fix | Delete
// This should never be set as it would then overwrite an existing attachment.
[493] Fix | Delete
unset( $attachment['ID'] );
[494] Fix | Delete
[495] Fix | Delete
// Save the attachment metadata.
[496] Fix | Delete
$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
[497] Fix | Delete
[498] Fix | Delete
if ( ! is_wp_error( $attachment_id ) ) {
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function