wppaste
WordPress

wp_ajax_save_attachment_compat()

Since
3.5.0
Source
wp-admin/includes/ajax-actions.php:3198
Handles saving backward compatible attachment attributes via AJAX.

Compatibility

WordPress
since 3.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to wp_ajax_save_attachment_compat() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
63–80

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 98.

Plugin surface
1 hook

Third-party callbacks on 'attachment_fields_to_save' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_delete()one call below wp_ajax_save_attachment_compat()

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 48 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_ajax_save_attachment_compat() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
isset($value) && current_user_can()63–70absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && current_user_can()65–68absint(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && current_user_can()65–72absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && current_user_can()65–72absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !current_user_can()65–72absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && current_user_can()65–72absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && current_user_can()65–72wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && current_user_can()67–70absint(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && current_user_can()67–70absint(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && !current_user_can()67–70absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && current_user_can()67–70wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && current_user_can()67–74absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
36 further outcomes, up to 80 instructions
isset($value) && !current_user_can()67–74absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !current_user_can()67–74absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && current_user_can()67–74absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && current_user_can()67–74absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !current_user_can()67–74absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && current_user_can()67–74wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && current_user_can()67–74wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !current_user_can()67–74wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && current_user_can()67–74wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && current_user_can()69–72absint(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && !current_user_can()69–72absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && !current_user_can()69–72absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && current_user_can()69–72wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && current_user_can()69–72wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && !current_user_can()69–72wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !current_user_can()69–76absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && current_user_can()69–76absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !current_user_can()69–76absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
isset($value) && !current_user_can()69–76absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !current_user_can()69–76wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !empty($value) && !empty($id) && !current_user_can()71–74absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && current_user_can()71–74wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && !current_user_can()71–74wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && !current_user_can()71–74wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
isset($value) && !current_user_can()71–78absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()71–78wp_send_json_error(), absint(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && current_user_can()71–78wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()71–78wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()71–78wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_success()
!isset($value) && !empty($value) && !empty($id) && !current_user_can()73–76wp_send_json_error(), absint(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()
!isset($value) && !current_user_can()73–80wp_send_json_error(), absint(), wp_send_json_error(), wp_send_json_error(), check_ajax_referer(), current_user_can(), wp_send_json_error(), get_post(), wp_send_json_error(), apply_filters(), wp_update_post(), get_attachment_taxonomies(), wp_prepare_attachment_for_js(), wp_send_json_error(), wp_send_json_success()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 98 instructions, 63–80 executed per call, 11 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_ajax_save_attachment_compat() runs, in this order:

  1. apply_filters( attachment_fields_to_save )filterline 3227 (+29 into the body)

    Filters the attachment fields to be saved.

Uses · 11

Source code

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 );}

Changelog

Introduced in 3.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-admin/includes/ajax-actions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.