get_media_item

函数


get_media_item ( $attachment_id, $args = null )
参数
  • (int)
    $attachment_id
    Attachment ID for modification.
    Required:
  • (string|array)
    $args
    Optional. Override defaults.
    Required:
    Default: null
返回值
  • (string) HTML form for attachment.
定义位置
  • wp-admin/includes/media.php
    , line 1570
引入
2.5.0
弃用

检索用于修改图片附件的HTML表格。

function get_media_item( $attachment_id, $args = null ) {
	global $redir_tab;

	$thumb_url     = false;
	$attachment_id = (int) $attachment_id;

	if ( $attachment_id ) {
		$thumb_url = wp_get_attachment_image_src( $attachment_id, 'thumbnail', true );

		if ( $thumb_url ) {
			$thumb_url = $thumb_url[0];
		}
	}

	$post            = get_post( $attachment_id );
	$current_post_id = ! empty( $_GET['post_id'] ) ? (int) $_GET['post_id'] : 0;

	$default_args = array(
		'errors'     => null,
		'send'       => $current_post_id ? post_type_supports( get_post_type( $current_post_id ), 'editor' ) : true,
		'delete'     => true,
		'toggle'     => true,
		'show_title' => true,
	);

	$parsed_args = wp_parse_args( $args, $default_args );

	/**
	 * Filters the arguments used to retrieve an image for the edit image form.
	 *
	 * @since 3.1.0
	 *
	 * @see get_media_item
	 *
	 * @param array $parsed_args An array of arguments.
	 */
	$parsed_args = apply_filters( 'get_media_item_args', $parsed_args );

	$toggle_on  = __( 'Show' );
	$toggle_off = __( 'Hide' );

	$file     = get_attached_file( $post->ID );
	$filename = esc_html( wp_basename( $file ) );
	$title    = esc_attr( $post->post_title );

	$post_mime_types = get_post_mime_types();
	$keys            = array_keys( wp_match_mime_types( array_keys( $post_mime_types ), $post->post_mime_type ) );
	$type            = reset( $keys );
	$type_html       = "";

	$form_fields = get_attachment_fields_to_edit( $post, $parsed_args['errors'] );

	if ( $parsed_args['toggle'] ) {
		$class        = empty( $parsed_args['errors'] ) ? 'startclosed' : 'startopen';
		$toggle_links = "
		$toggle_on
		$toggle_off";
	} else {
		$class        = '';
		$toggle_links = '';
	}

	$display_title = ( ! empty( $title ) ) ? $title : $filename; // $title shouldn't ever be empty, but just in case.
	$display_title = $parsed_args['show_title'] ? "
" . wp_html_excerpt( $display_title, 60, '…' ) . '
' : ''; $gallery = ( ( isset( $_REQUEST['tab'] ) && 'gallery' === $_REQUEST['tab'] ) || ( isset( $redir_tab ) && 'gallery' === $redir_tab ) ); $order = ''; foreach ( $form_fields as $key => $val ) { if ( 'menu_order' === $key ) { if ( $gallery ) { $order = ""; } else { $order = ""; } unset( $form_fields['menu_order'] ); break; } } $media_dims = ''; $meta = wp_get_attachment_metadata( $post->ID ); if ( isset( $meta['width'], $meta['height'] ) ) { $media_dims .= "{$meta['width']} × {$meta['height']} "; } /** * Filters the media metadata. * * @since 2.5.0 * * @param string $media_dims The HTML markup containing the media dimensions. * @param WP_Post $post The WP_Post attachment object. */ $media_dims = apply_filters( 'media_meta', $media_dims, $post ); $image_edit_button = ''; if ( wp_attachment_is_image( $post->ID ) && wp_image_editor_supports( array( 'mime_type' => $post->post_mime_type ) ) ) { $nonce = wp_create_nonce( "image_editor-$post->ID" ); $image_edit_button = ""; } $attachment_url = get_permalink( $attachment_id ); $item = " $type_html $toggle_links $order $display_title

n”;

$item .= ”

n

n

n”;

$defaults = array(
‘input’ => ‘text’,
‘required’ => false,
‘value’ => ”,
‘extra_rows’ => array(),
);

if ( $parsed_args[‘send’] ) {
$parsed_args[‘send’] = get_submit_button( __( ‘Insert into Post’ ), ”, “send[$attachment_id]”, false );
}

$delete = empty( $parsed_args[‘delete’] ) ? ” : $parsed_args[‘delete’];
if ( $delete && current_user_can( ‘delete_post’, $attachment_id ) ) {
if ( ! EMPTY_TRASH_DAYS ) {
$delete = “” . __( ‘Delete Permanently’ ) . ”;
} elseif ( ! MEDIA_TRASH ) {
$delete = “” . __( ‘Delete’ ) . ”

‘;
} else {
$delete = “” . __( ‘Move to Trash’ ) . ”
” . __( ‘Undo’ ) . ”;
}
} else {
$delete = ”;
}

$thumbnail = ”;
$calling_post_id = 0;

if ( isset( $_GET[‘post_id’] ) ) {
$calling_post_id = absint( $_GET[‘post_id’] );
} elseif ( isset( $_POST ) && count( $_POST ) ) {// Like for async-upload where $_GET[‘post_id’] isn’t set.
$calling_post_id = $post->post_parent;
}

if ( ‘image’ === $type && $calling_post_id
&& current_theme_supports( ‘post-thumbnails’, get_post_type( $calling_post_id ) )
&& post_type_supports( get_post_type( $calling_post_id ), ‘thumbnail’ )
&& get_post_thumbnail_id( $calling_post_id ) != $attachment_id
) {

$calling_post = get_post( $calling_post_id );
$calling_post_type_object = get_post_type_object( $calling_post->post_type );

$ajax_nonce = wp_create_nonce( “set_post_thumbnail-$calling_post_id” );
$thumbnail = “” . esc_html( $calling_post_type_object->labels->use_featured_image ) . ”;
}

if ( ( $parsed_args[‘send’] || $thumbnail || $delete ) && ! isset( $form_fields[‘buttons’] ) ) {
$form_fields[‘buttons’] = array( ‘tr’ => “tt

n” );
}

$hidden_fields = array();

foreach ( $form_fields as $id => $field ) {
if ( ‘_’ === $id[0] ) {
continue;
}

if ( ! empty( $field[‘tr’] ) ) {
$item .= $field[‘tr’];
continue;
}

$field = array_merge( $defaults, $field );
$name = “attachments[$attachment_id][$id]”;

if ( ‘hidden’ === $field[‘input’] ) {
$hidden_fields[ $name ] = $field[‘value’];
continue;
}

$required = $field[‘required’] ? ‘ ‘ . wp_required_field_indicator() : ”;
$required_attr = $field[‘required’] ? ‘ required’ : ”;
$class = $id;
$class .= $field[‘required’] ? ‘ form-required’ : ”;

$item .= “tt

nttt

nttt

ntt

n”;

$extra_rows = array();

if ( ! empty( $field[‘errors’] ) ) {
foreach ( array_unique( (array) $field[‘errors’] ) as $error ) {
$extra_rows[‘error’][] = $error;
}
}

if ( ! empty( $field[‘extra_rows’] ) ) {
foreach ( $field[‘extra_rows’] as $class => $rows ) {
foreach ( (array) $rows as $html ) {
$extra_rows[ $class ][] = $html;
}
}
}

foreach ( $extra_rows as $class => $rows ) {
foreach ( $rows as $html ) {
$item .= “tt

n”;
}
}
}

if ( ! empty( $form_fields[‘_final’] ) ) {
$item .= “tt

n”;
}

$item .= “t

n”;
$item .= “t

get_media_item插图

$image_edit_button

” . __( ‘File name:’ ) . “ $filename

” . __( ‘File type:’ ) . “ $post->post_mime_type

” . __( ‘Upload date:’ ) . ‘ ‘ . mysql2date( __( ‘F j, Y’ ), $post->post_date ) . ‘

‘;

if ( ! empty( $media_dims ) ) {
$item .= ‘

‘ . __( ‘Dimensions:’ ) . “ $media_dims

n”;
}

$item .= “

” .
wp_required_field_message() .

” . $parsed_args[‘send’] . ” $thumbnail $delete
“;

if ( ! empty( $field[ $field[‘input’] ] ) ) {
$item .= $field[ $field[‘input’] ];
} elseif ( ‘textarea’ === $field[‘input’] ) {
if ( ‘post_content’ === $id && user_can_richedit() ) {
// Sanitize_post() skips the post_content when user_can_richedit.
$field[‘value’] = htmlspecialchars( $field[‘value’], ENT_QUOTES );
}
// Post_excerpt is already escaped by sanitize_post() in get_attachment_fields_to_edit().
$item .= “‘;
} else {
$item .= ““;
}

if ( ! empty( $field[‘helps’] ) ) {
$item .= “

” . implode( “

n

“, array_unique( (array) $field[‘helps’] ) ) . ‘

‘;
}
$item .= “

$html
{$form_fields[‘_final’]}

n”;

foreach ( $hidden_fields as $name => $value ) {
$item .= “tn”;
}

if ( $post->post_parent n”;
}

return $item;
}