wp_ajax_save_attachment_compat()
云策文档标注
概述
wp_ajax_save_attachment_compat() 是一个 WordPress AJAX 处理函数,用于保存向后兼容的附件属性。它通过验证请求、权限和数据类型,更新附件文章并处理相关分类法。
关键要点
- 函数通过 AJAX 处理附件属性的保存,确保向后兼容性。
- 验证包括检查 ID、附件数据、nonce 和用户权限(edit_post)。
- 使用 wp_update_post() 更新附件文章,并通过 wp_set_object_terms() 处理分类法关系。
- 最终返回 JSON 响应,成功时包含通过 wp_prepare_attachment_for_js() 准备的附件数据。
- 关键 Hook:apply_filters('attachment_fields_to_save', $post, $attachment_data) 用于过滤要保存的附件字段。
代码示例
if ( ! isset( $_REQUEST['id'] ) ) {
wp_send_json_error();
}
$id = absint( $_REQUEST['id'] );
if ( ! $id ) {
wp_send_json_error();
}
if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) ) {
wp_send_json_error();
}
$attachment_data = $_REQUEST['attachments'][ $id ];
check_ajax_referer( 'update-post_' . $id, 'nonce' );
if ( ! current_user_can( 'edit_post', $id ) ) {
wp_send_json_error();
}
$post = get_post( $id, ARRAY_A );
if ( 'attachment' !== $post['post_type'] ) {
wp_send_json_error();
}
$post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );
if ( isset( $post['errors'] ) ) {
$errors = $post['errors'];
unset( $post['errors'] );
}
wp_update_post( $post );
foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
if ( isset( $attachment_data[ $taxonomy ] ) ) {
wp_set_object_terms( $id, array_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false );
}
}
$attachment = wp_prepare_attachment_for_js( $id );
if ( ! $attachment ) {
wp_send_json_error();
}
wp_send_json_success( $attachment );注意事项
- 函数自 WordPress 3.5.0 版本引入,用于处理旧版附件保存逻辑。
- 依赖多个核心函数如 check_ajax_referer() 和 current_user_can() 以确保安全性和权限控制。
- 错误处理通过 wp_send_json_error() 返回,开发者需在前端处理这些响应。
原文内容
Handles saving backward compatible attachment attributes via AJAX.
Source
function wp_ajax_save_attachment_compat() {
if ( ! isset( $_REQUEST['id'] ) ) {
wp_send_json_error();
}
$id = absint( $_REQUEST['id'] );
if ( ! $id ) {
wp_send_json_error();
}
if ( empty( $_REQUEST['attachments'] ) || empty( $_REQUEST['attachments'][ $id ] ) ) {
wp_send_json_error();
}
$attachment_data = $_REQUEST['attachments'][ $id ];
check_ajax_referer( 'update-post_' . $id, 'nonce' );
if ( ! current_user_can( 'edit_post', $id ) ) {
wp_send_json_error();
}
$post = get_post( $id, ARRAY_A );
if ( 'attachment' !== $post['post_type'] ) {
wp_send_json_error();
}
/** This filter is documented in wp-admin/includes/media.php */
$post = apply_filters( 'attachment_fields_to_save', $post, $attachment_data );
if ( isset( $post['errors'] ) ) {
$errors = $post['errors']; // @todo return me and display me!
unset( $post['errors'] );
}
wp_update_post( $post );
foreach ( get_attachment_taxonomies( $post ) as $taxonomy ) {
if ( isset( $attachment_data[ $taxonomy ] ) ) {
wp_set_object_terms( $id, array_map( 'trim', preg_split( '/,+/', $attachment_data[ $taxonomy ] ) ), $taxonomy, false );
}
}
$attachment = wp_prepare_attachment_for_js( $id );
if ( ! $attachment ) {
wp_send_json_error();
}
wp_send_json_success( $attachment );
}
Hooks
- apply_filters( ‘attachment_fields_to_save’, array $post, array $attachment )
-
Filters the attachment fields to be saved.
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Introduced. |