返回介绍

media_upload_form_handler()

发布于 2017-09-11 00:59:12 字数 5314 浏览 936 评论 0 收藏 0

media_upload_form_handler()

Handles form submissions for the legacy media uploader.


description


返回值

(mixed) void|object WP_Error on failure


源代码

File: wp-admin/includes/media.php

function media_upload_form_handler() {
	check_admin_referer('media-form');

	$errors = null;

	if ( isset($_POST['send']) ) {
		$keys = array_keys( $_POST['send'] );
		$send_id = (int) reset( $keys );
	}

	if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) {
		$post = $_post = get_post($attachment_id, ARRAY_A);

		if ( !current_user_can( 'edit_post', $attachment_id ) )
			continue;

		if ( isset($attachment['post_content']) )
			$post['post_content'] = $attachment['post_content'];
		if ( isset($attachment['post_title']) )
			$post['post_title'] = $attachment['post_title'];
		if ( isset($attachment['post_excerpt']) )
			$post['post_excerpt'] = $attachment['post_excerpt'];
		if ( isset($attachment['menu_order']) )
			$post['menu_order'] = $attachment['menu_order'];

		if ( isset($send_id) && $attachment_id == $send_id ) {
			if ( isset($attachment['post_parent']) )
				$post['post_parent'] = $attachment['post_parent'];
		}

		/**
		 * Filters the attachment fields to be saved.
		 *
		 * @since 2.5.0
		 *
		 * @see wp_get_attachment_metadata()
		 *
		 * @param array $post       An array of post data.
		 * @param array $attachment An array of attachment metadata.
		 */
		$post = apply_filters( 'attachment_fields_to_save', $post, $attachment );

		if ( isset($attachment['image_alt']) ) {
			$image_alt = wp_unslash( $attachment['image_alt'] );
			if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) {
				$image_alt = wp_strip_all_tags( $image_alt, true );

				// Update_meta expects slashed.
				update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
			}
		}

		if ( isset($post['errors']) ) {
			$errors[$attachment_id] = $post['errors'];
			unset($post['errors']);
		}

		if ( $post != $_post )
			wp_update_post($post);

		foreach ( get_attachment_taxonomies($post) as $t ) {
			if ( isset($attachment[$t]) )
				wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $attachment[$t])), $t, false);
		}
	}

	if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?>
		<script type="text/javascript">
		var win = window.dialogArguments || opener || parent || top;
		win.tb_remove();
		</script>
		<?php
		exit;
	}

	if ( isset($send_id) ) {
		$attachment = wp_unslash( $_POST['attachments'][$send_id] );

		$html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : '';
		if ( !empty($attachment['url']) ) {
			$rel = '';
			if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] )
				$rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'";
			$html = "<a href='{$attachment['url']}'$rel>$html</a>";
		}

		/**
		 * Filters the HTML markup for a media item sent to the editor.
		 *
		 * @since 2.5.0
		 *
		 * @see wp_get_attachment_metadata()
		 *
		 * @param string $html       HTML markup for a media item sent to the editor.
		 * @param int    $send_id    The first key from the $_POST['send'] data.
		 * @param array  $attachment Array of attachment metadata.
		 */
		$html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment );
		return media_send_to_editor($html);
	}

	return $errors;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/media.php: attachment_fields_to_save
  • wp-admin/includes/media.php: media_send_to_editor
  • wp-admin/includes/media.php: media_send_to_editor()
  • wp-includes/capabilities.php: current_user_can()
  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: wp_strip_all_tags()
  • wp-includes/formatting.php: wp_slash()
  • wp-includes/formatting.php: esc_attr()
  • wp-includes/pluggable.php: check_admin_referer()
  • wp-includes/taxonomy.php: wp_set_object_terms()
  • wp-includes/link-template.php: get_attachment_link()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: get_attachment_taxonomies()
  • wp-includes/post.php: wp_update_post()
  • wp-includes/post.php: get_post_meta()
  • wp-includes/post.php: update_post_meta()
  • wp-includes/post.php: get_post()
  • Show 12 more uses Hide more uses

Used By

  • wp-admin/includes/media.php: wp_media_upload_handler()
  • wp-admin/includes/media.php: media_upload_gallery()
  • wp-admin/includes/media.php: media_upload_library()

User Contributed Notes

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

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

发布评论

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