返回介绍

wp_ajax_send_attachment_to_editor()

发布于 2017-09-11 11:31:59 字数 3887 浏览 946 评论 0 收藏 0

wp_ajax_send_attachment_to_editor()

Ajax handler for sending an attachment to the editor.


description

Generates the HTML to send an attachment to the editor. Backward compatible with the ‘media_send_to_editor’ filter and the chain of filters that follow.


源代码

File: wp-admin/includes/ajax-actions.php

function wp_ajax_send_attachment_to_editor() {
	check_ajax_referer( 'media-send-to-editor', 'nonce' );

	$attachment = wp_unslash( $_POST['attachment'] );

	$id = intval( $attachment['id'] );

	if ( ! $post = get_post( $id ) )
		wp_send_json_error();

	if ( 'attachment' != $post->post_type )
		wp_send_json_error();

	if ( current_user_can( 'edit_post', $id ) ) {
		// If this attachment is unattached, attach it. Primarily a back compat thing.
		if ( 0 == $post->post_parent && $insert_into_post_id = intval( $_POST['post_id'] ) ) {
			wp_update_post( array( 'ID' => $id, 'post_parent' => $insert_into_post_id ) );
		}
	}

	$url = empty( $attachment['url'] ) ? '' : $attachment['url'];
	$rel = ( strpos( $url, 'attachment_id') || get_attachment_link( $id ) == $url );

	remove_filter( 'media_send_to_editor', 'image_media_send_to_editor' );

	if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) {
		$align = isset( $attachment['align'] ) ? $attachment['align'] : 'none';
		$size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium';
		$alt = isset( $attachment['image_alt'] ) ? $attachment['image_alt'] : '';

		// No whitespace-only captions.
		$caption = isset( $attachment['post_excerpt'] ) ? $attachment['post_excerpt'] : '';
		if ( '' === trim( $caption ) ) {
			$caption = '';
		}

		$title = ''; // We no longer insert title tags into <img> tags, as they are redundant.
		$html = get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt );
	} elseif ( wp_attachment_is( 'video', $post ) || wp_attachment_is( 'audio', $post )  ) {
		$html = stripslashes_deep( $_POST['html'] );
	} else {
		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
		$rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized

		if ( ! empty( $url ) ) {
			$html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
		}
	}

	/** This filter is documented in wp-admin/includes/media.php */
	$html = apply_filters( 'media_send_to_editor', $html, $id, $attachment );

	wp_send_json_success( $html );
}

更新日志

Versiondescription
3.5.0Introduced.

相关函数

Uses

  • wp-includes/post.php: wp_attachment_is()
  • wp-admin/includes/media.php: media_send_to_editor
  • wp-admin/includes/media.php: get_image_send_to_editor()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/formatting.php: stripslashes_deep()
  • wp-includes/pluggable.php: check_ajax_referer()
  • wp-includes/functions.php: wp_send_json_error()
  • wp-includes/functions.php: wp_send_json_success()
  • wp-includes/link-template.php: get_attachment_link()
  • wp-includes/plugin.php: remove_filter()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/post.php: get_post()
  • Show 10 more uses Hide more uses

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文