Learn how to describe the purpose of the image%3$s. Leave empty if the image is purely decorative.' ),
/* translators: Localized tutorial, if one exists. W3C Web Accessibility Initiative link has list of existing translations. */
esc_url( __( 'https://www.w3.org/WAI/tutorials/images/decision-tree/' ) ),
'target="_blank"',
sprintf(
' %s',
/* translators: Hidden accessibility text. */
__( '(opens in a new tab)' )
)
);
?>
post_excerpt; ?>
'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
$editor_args = array(
'textarea_name' => 'content',
'textarea_rows' => 5,
'media_buttons' => false,
/**
* Filters the TinyMCE argument for the media description field on the attachment details screen.
*
* @since 6.6.0
*
* @param bool $tinymce Whether to activate TinyMCE in media description field. Default false.
*/
'tinymce' => apply_filters( 'activate_tinymce_for_media_description', false ),
'quicktags' => $quicktags_settings,
);
?>
post_content ), 'attachment_content', $editor_args ); ?>
ID );
echo $extras['item'];
echo '' . "\n";
}
/**
* Displays non-editable attachment metadata in the publish meta box.
*
* @since 3.5.0
*/
function attachment_submitbox_metadata() {
$post = get_post();
$attachment_id = $post->ID;
$file = get_attached_file( $attachment_id );
$filename = esc_html( wp_basename( $file ) );
$media_dims = '';
$meta = wp_get_attachment_metadata( $attachment_id );
if ( isset( $meta['width'], $meta['height'] ) ) {
/* translators: 1: A number of pixels wide, 2: A number of pixels tall. */
$media_dims .= "" . sprintf( __( '%1$s by %2$s pixels' ), $meta['width'], $meta['height'] ) . '';
}
/** This filter is documented in wp-admin/includes/media.php */
$media_dims = apply_filters( 'media_meta', $media_dims, $post );
$att_url = wp_get_attachment_url( $attachment_id );
$author = new WP_User( $post->post_author );
$uploaded_by_name = __( '(no author)' );
$uploaded_by_link = '';
if ( $author->exists() ) {
$uploaded_by_name = $author->display_name ? $author->display_name : $author->nickname;
$uploaded_by_link = get_edit_user_link( $author->ID );
}
?>
post_mime_type ) ) {
$fields = array(
'length_formatted' => __( 'Length:' ),
'bitrate' => __( 'Bitrate:' ),
);
/**
* Filters the audio and video metadata fields to be shown in the publish meta box.
*
* The key for each item in the array should correspond to an attachment
* metadata key, and the value should be the desired label.
*
* @since 3.7.0
* @since 4.9.0 Added the `$post` parameter.
*
* @param array $fields An array of the attachment metadata keys and labels.
* @param WP_Post $post WP_Post object for the current attachment.
*/
$fields = apply_filters( 'media_submitbox_misc_sections', $fields, $post );
foreach ( $fields as $key => $label ) {
if ( empty( $meta[ $key ] ) ) {
continue;
}
?>
__( 'Audio Format:' ),
'codec' => __( 'Audio Codec:' ),
);
/**
* Filters the audio attachment metadata fields to be shown in the publish meta box.
*
* The key for each item in the array should correspond to an attachment
* metadata key, and the value should be the desired label.
*
* @since 3.7.0
* @since 4.9.0 Added the `$post` parameter.
*
* @param array $fields An array of the attachment metadata keys and labels.
* @param WP_Post $post WP_Post object for the current attachment.
*/
$audio_fields = apply_filters( 'audio_submitbox_misc_sections', $fields, $post );
foreach ( $audio_fields as $key => $label ) {
if ( empty( $meta['audio'][ $key ] ) ) {
continue;
}
?>
$list ) {
if ( 'length' !== $key && ! empty( $list ) ) {
$metadata[ $key ] = wp_kses_post( reset( $list ) );
// Fix bug in byte stream analysis.
if ( 'terms_of_use' === $key && str_starts_with( $metadata[ $key ], 'yright notice.' ) ) {
$metadata[ $key ] = 'Cop' . $metadata[ $key ];
}
}
}
break;
}
}
if ( ! empty( $data['id3v2']['APIC'] ) ) {
$image = reset( $data['id3v2']['APIC'] );
if ( ! empty( $image['data'] ) ) {
$metadata['image'] = array(
'data' => $image['data'],
'mime' => $image['image_mime'],
'width' => $image['image_width'],
'height' => $image['image_height'],
);
}
} elseif ( ! empty( $data['comments']['picture'] ) ) {
$image = reset( $data['comments']['picture'] );
if ( ! empty( $image['data'] ) ) {
$metadata['image'] = array(
'data' => $image['data'],
'mime' => $image['image_mime'],
);
}
}
}
/**
* Retrieves metadata from a video file's ID3 tags.
*
* @since 3.6.0
*
* @param string $file Path to file.
* @return array|false Returns array of metadata, if found.
*/
function wp_read_video_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
$metadata = array();
if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
define( 'GETID3_TEMP_DIR', get_temp_dir() );
}
if ( ! class_exists( 'getID3', false ) ) {
require ABSPATH . WPINC . '/ID3/getid3.php';
}
$id3 = new getID3();
// Required to get the `created_timestamp` value.
$id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $id3->analyze( $file );
if ( isset( $data['video']['lossless'] ) ) {
$metadata['lossless'] = $data['video']['lossless'];
}
if ( ! empty( $data['video']['bitrate'] ) ) {
$metadata['bitrate'] = (int) $data['video']['bitrate'];
}
if ( ! empty( $data['video']['bitrate_mode'] ) ) {
$metadata['bitrate_mode'] = $data['video']['bitrate_mode'];
}
if ( ! empty( $data['filesize'] ) ) {
$metadata['filesize'] = (int) $data['filesize'];
}
if ( ! empty( $data['mime_type'] ) ) {
$metadata['mime_type'] = $data['mime_type'];
}
if ( ! empty( $data['playtime_seconds'] ) ) {
$metadata['length'] = (int) round( $data['playtime_seconds'] );
}
if ( ! empty( $data['playtime_string'] ) ) {
$metadata['length_formatted'] = $data['playtime_string'];
}
if ( ! empty( $data['video']['resolution_x'] ) ) {
$metadata['width'] = (int) $data['video']['resolution_x'];
}
if ( ! empty( $data['video']['resolution_y'] ) ) {
$metadata['height'] = (int) $data['video']['resolution_y'];
}
if ( ! empty( $data['fileformat'] ) ) {
$metadata['fileformat'] = $data['fileformat'];
}
if ( ! empty( $data['video']['dataformat'] ) ) {
$metadata['dataformat'] = $data['video']['dataformat'];
}
if ( ! empty( $data['video']['encoder'] ) ) {
$metadata['encoder'] = $data['video']['encoder'];
}
if ( ! empty( $data['video']['codec'] ) ) {
$metadata['codec'] = $data['video']['codec'];
}
if ( ! empty( $data['audio'] ) ) {
unset( $data['audio']['streams'] );
$metadata['audio'] = $data['audio'];
}
if ( empty( $metadata['created_timestamp'] ) ) {
$created_timestamp = wp_get_media_creation_timestamp( $data );
if ( false !== $created_timestamp ) {
$metadata['created_timestamp'] = $created_timestamp;
}
}
wp_add_id3_tag_data( $metadata, $data );
$file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
/**
* Filters the array of metadata retrieved from a video.
*
* In core, usually this selection is what is stored.
* More complete data can be parsed from the `$data` parameter.
*
* @since 4.9.0
*
* @param array $metadata Filtered video metadata.
* @param string $file Path to video file.
* @param string|null $file_format File format of video, as analyzed by getID3.
* Null if unknown.
* @param array $data Raw metadata from getID3.
*/
return apply_filters( 'wp_read_video_metadata', $metadata, $file, $file_format, $data );
}
/**
* Retrieves metadata from an audio file's ID3 tags.
*
* @since 3.6.0
*
* @param string $file Path to file.
* @return array|false Returns array of metadata, if found.
*/
function wp_read_audio_metadata( $file ) {
if ( ! file_exists( $file ) ) {
return false;
}
$metadata = array();
if ( ! defined( 'GETID3_TEMP_DIR' ) ) {
define( 'GETID3_TEMP_DIR', get_temp_dir() );
}
if ( ! class_exists( 'getID3', false ) ) {
require ABSPATH . WPINC . '/ID3/getid3.php';
}
$id3 = new getID3();
// Required to get the `created_timestamp` value.
$id3->options_audiovideo_quicktime_ReturnAtomData = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName
$data = $id3->analyze( $file );
if ( ! empty( $data['audio'] ) ) {
unset( $data['audio']['streams'] );
$metadata = $data['audio'];
}
if ( ! empty( $data['fileformat'] ) ) {
$metadata['fileformat'] = $data['fileformat'];
}
if ( ! empty( $data['filesize'] ) ) {
$metadata['filesize'] = (int) $data['filesize'];
}
if ( ! empty( $data['mime_type'] ) ) {
$metadata['mime_type'] = $data['mime_type'];
}
if ( ! empty( $data['playtime_seconds'] ) ) {
$metadata['length'] = (int) round( $data['playtime_seconds'] );
}
if ( ! empty( $data['playtime_string'] ) ) {
$metadata['length_formatted'] = $data['playtime_string'];
}
if ( empty( $metadata['created_timestamp'] ) ) {
$created_timestamp = wp_get_media_creation_timestamp( $data );
if ( false !== $created_timestamp ) {
$metadata['created_timestamp'] = $created_timestamp;
}
}
wp_add_id3_tag_data( $metadata, $data );
$file_format = isset( $metadata['fileformat'] ) ? $metadata['fileformat'] : null;
/**
* Filters the array of metadata retrieved from an audio file.
*
* In core, usually this selection is what is stored.
* More complete data can be parsed from the `$data` parameter.
*
* @since 6.1.0
*
* @param array $metadata Filtered audio metadata.
* @param string $file Path to audio file.
* @param string|null $file_format File format of audio, as analyzed by getID3.
* Null if unknown.
* @param array $data Raw metadata from getID3.
*/
return apply_filters( 'wp_read_audio_metadata', $metadata, $file, $file_format, $data );
}
/**
* Parses creation date from media metadata.
*
* The getID3 library doesn't have a standard method for getting creation dates,
* so the location of this data can vary based on the MIME type.
*
* @since 4.9.0
*
* @link https://github.com/JamesHeinrich/getID3/blob/master/structure.txt
*
* @param array $metadata The metadata returned by getID3::analyze().
* @return int|false A UNIX timestamp for the media's creation date if available
* or a boolean FALSE if a timestamp could not be determined.
*/
function wp_get_media_creation_timestamp( $metadata ) {
$creation_date = false;
if ( empty( $metadata['fileformat'] ) ) {
return $creation_date;
}
switch ( $metadata['fileformat'] ) {
case 'asf':
if ( isset( $metadata['asf']['file_properties_object']['creation_date_unix'] ) ) {
$creation_date = (int) $metadata['asf']['file_properties_object']['creation_date_unix'];
}
break;
case 'matroska':
case 'webm':
if ( isset( $metadata['matroska']['comments']['creation_time'][0] ) ) {
$creation_date = strtotime( $metadata['matroska']['comments']['creation_time'][0] );
} elseif ( isset( $metadata['matroska']['info'][0]['DateUTC_unix'] ) ) {
$creation_date = (int) $metadata['matroska']['info'][0]['DateUTC_unix'];
}
break;
case 'quicktime':
case 'mp4':
if ( isset( $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix'] ) ) {
$creation_date = (int) $metadata['quicktime']['moov']['subatoms'][0]['creation_time_unix'];
}
break;
}
return $creation_date;
}
/**
* Encapsulates the logic for Attach/Detach actions.
*
* @since 4.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $parent_id Attachment parent ID.
* @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'.
* Default 'attach'.
*/
function wp_media_attach_action( $parent_id, $action = 'attach' ) {
global $wpdb;
if ( ! $parent_id ) {
return;
}
if ( ! current_user_can( 'edit_post', $parent_id ) ) {
wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
}
$ids = array();
foreach ( (array) $_REQUEST['media'] as $attachment_id ) {
$attachment_id = (int) $attachment_id;
if ( ! current_user_can( 'edit_post', $attachment_id ) ) {
continue;
}
$ids[] = $attachment_id;
}
if ( ! empty( $ids ) ) {
$ids_string = implode( ',', $ids );
if ( 'attach' === $action ) {
$result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( $ids_string )", $parent_id ) );
} else {
$result = $wpdb->query( "UPDATE $wpdb->posts SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( $ids_string )" );
}
}
if ( isset( $result ) ) {
foreach ( $ids as $attachment_id ) {
/**
* Fires when media is attached or detached from a post.
*
* @since 5.5.0
*
* @param string $action Attach/detach action. Accepts 'attach' or 'detach'.
* @param int $attachment_id The attachment ID.
* @param int $parent_id Attachment parent ID.
*/
do_action( 'wp_media_attach_action', $action, $attachment_id, $parent_id );
clean_attachment_cache( $attachment_id );
}
$location = 'upload.php';
$referer = wp_get_referer();
if ( $referer ) {
if ( str_contains( $referer, 'upload.php' ) ) {
$location = remove_query_arg( array( 'attached', 'detach' ), $referer );
}
}
$key = 'attach' === $action ? 'attached' : 'detach';
$location = add_query_arg( array( $key => $result ), $location );
wp_redirect( $location );
exit;
}
}