__('From Computer'), // handler action suffix => tab text
'type_url' => __('From URL'),
'gallery' => __('Gallery'),
'library' => __('Media Library')
);
return apply_filters('media_upload_tabs', $_default_tabs);
}
/**
* Adds the gallery tab back to the tabs array if post has image attachments
*
* @since 2.5.0
*
* @param array $tabs
* @return array $tabs with gallery if post has image attachment
*/
function update_gallery_tab($tabs) {
global $wpdb;
if ( !isset($_REQUEST['post_id']) ) {
unset($tabs['gallery']);
return $tabs;
}
$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']);
return $tabs;
}
$tabs['gallery'] = sprintf(__('Gallery (%s)'), "$attachments");
return $tabs;
}
add_filter('media_upload_tabs', 'update_gallery_tab');
/**
* {@internal Missing Short Description}}
*
* @since 2.5.0
*/
function the_media_upload_tabs() {
global $redir_tab;
$tabs = media_upload_tabs();
$default = 'type';
if ( !empty($tabs) ) {
echo "
\n";
}
}
/**
* {@internal Missing Short Description}}
*
* @since 2.5.0
*
* @param integer $id image attachment id
* @param string $caption image caption
* @param string $alt image alt attribute
* @param string $title image title attribute
* @param string $align image css alignment property
* @param string $url image src url
* @param string|bool $rel image rel attribute
* @param string $size image size (thumbnail, medium, large, full or added with add_image_size() )
* @return string the html to insert into editor
*/
function get_image_send_to_editor($id, $caption, $title, $align, $url='', $rel = false, $size='medium', $alt = '') {
$html = get_image_tag($id, $alt, '', $align, $size);
$rel = $rel ? ' rel="attachment wp-att-' . esc_attr($id).'"' : '';
if ( $url )
$html = '$html";
$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
return $html;
}
/**
* Adds image shortcode with caption to editor
*
* @since 2.6.0
*
* @param string $html
* @param integer $id
* @param string $caption image caption
* @param string $alt image alt attribute
* @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() )
* @return string
*/
function image_add_caption( $html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
if ( empty($caption) || apply_filters( 'disable_captions', '' ) )
return $html;
$id = ( 0 < (int) $id ) ? 'attachment_' . $id : '';
if ( ! preg_match( '/width=["\']([0-9]+)/', $html, $matches ) )
return $html;
$width = $matches[1];
$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) )
$align = 'none';
$shcode = '[caption id="' . $id . '" align="align' . $align . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
}
add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 );
/**
* Private preg_replace callback used in image_add_caption()
*
* @access private
* @since 3.4.0
*/
function _cleanup_image_add_caption( $matches ) {
// remove any line breaks from inside the tags
return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
}
/**
* Adds image html to editor
*
* @since 2.5.0
*
* @param string $html
*/
function media_send_to_editor($html) {
?>
false )) {
$time = current_time('mysql');
if ( $post = get_post($post_id) ) {
if ( substr( $post->post_date, 0, 4 ) > 0 )
$time = $post->post_date;
}
$name = $_FILES[$file_id]['name'];
$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);
if ( isset($file['error']) )
return new WP_Error( 'upload_error', $file['error'] );
$name_parts = pathinfo($name);
$name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = sanitize_text_field( $name );
$content = '';
if ( preg_match( '#^audio#', $type ) ) {
$meta = wp_read_audio_metadata( $file );
if ( ! empty( $meta['title'] ) )
$title = $meta['title'];
$content = '';
if ( ! empty( $title ) ) {
if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
/* 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'] );
} else if ( ! empty( $meta['album'] ) ) {
/* translators: 1: audio track title, 2: album title */
$content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
} else if ( ! empty( $meta['artist'] ) ) {
/* translators: 1: audio track title, 2: artist name */
$content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
} else {
$content .= sprintf( __( '"%s".' ), $title );
}
} else if ( ! empty( $meta['album'] ) ) {
if ( ! empty( $meta['artist'] ) ) {
/* translators: 1: audio album title, 2: artist name */
$content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
} else {
$content .= $meta['album'] . '.';
}
} else if ( ! empty( $meta['artist'] ) ) {
$content .= $meta['artist'] . '.';
}
if ( ! empty( $meta['year'] ) )
$content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
if ( ! empty( $meta['track_number'] ) ) {
$track_number = explode( '/', $meta['track_number'] );
if ( isset( $track_number[1] ) )
$content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
else
$content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
}
if ( ! empty( $meta['genre'] ) )
$content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
// use image exif/iptc data for title and caption defaults if possible
} elseif ( $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'] ) )
$content = $image_meta['caption'];
}
// 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 );
// This should never be set as it would then overwrite an existing attachment.
if ( isset( $attachment['ID'] ) )
unset( $attachment['ID'] );
// Save the data
$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) ) {
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
}
return $id;
}
/**
* This handles a sideloaded file in the same way as an uploaded file is handled by {@link media_handle_upload()}
*
* @since 2.6.0
*
* @param array $file_array Array similar to a {@link $_FILES} upload array
* @param int $post_id The post ID the media is associated with
* @param string $desc Description of the sideloaded file
* @param array $post_data allows you to overwrite some of the attachment
* @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);
$time = current_time( 'mysql' );
if ( $post = get_post( $post_id ) ) {
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'] );
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace('/\.[^.]+$/', '', 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'] ) ) )
$title = $image_meta['title'];
if ( trim( $image_meta['caption'] ) )
$content = $image_meta['caption'];
}
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 );
// This should never be set as it would then overwrite an existing attachment.
if ( isset( $attachment['ID'] ) )
unset( $attachment['ID'] );
// Save the attachment metadata
$id = wp_insert_attachment($attachment, $file, $post_id);
if ( !is_wp_error($id) )
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
return $id;
}
/**
* Adds the iframe to display content for the media upload page
*
* @since 2.5.0
*
* @param array $content_func
*/
function wp_iframe($content_func /* ... */) {
_wp_admin_html_begin();
?>
› —
class="wp-core-ui no-js">