wppaste
WordPress

wp_ajax_heartbeat()

Since
3.6.0
Source
wp-admin/includes/ajax-actions.php:3454
Handles the Heartbeat API via AJAX.

Description

Runs when the user is logged in.

Compatibility

WordPress
since 3.6.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_heartbeat() 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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
43–80

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

Plugin surface
4 hooks

Third-party callbacks on 'wp_refresh_nonces', 'heartbeat_received', 'heartbeat_send' 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

  • hookthird-party callbacksapply_filters()called directly
  • optionoption read or writeget_option()one call below wp_ajax_heartbeat()

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

What one call costs · 40 distinct outcomes

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

WhenInstructionsCalls it makes
$nonce_state === 1 && empty($data)43wp_verify_nonce(), apply_filters(), do_action(), time(), wp_send_json()
empty($value) && $nonce_state === 1 && empty($data)45wp_send_json_error(), wp_verify_nonce(), apply_filters(), do_action(), time(), wp_send_json()
always50–52wp_verify_nonce(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state === 1 && empty($data)50wp_verify_nonce(), wp_unslash(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state === 1 && empty($data)50wp_verify_nonce(), sanitize_key(), apply_filters(), do_action(), time(), wp_send_json()
empty($value)52–54wp_send_json_error(), wp_verify_nonce(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state === 1 && empty($data)52wp_send_json_error(), wp_verify_nonce(), wp_unslash(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state === 1 && empty($data)52wp_send_json_error(), wp_verify_nonce(), sanitize_key(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)57wp_verify_nonce(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
always57–59wp_verify_nonce(), wp_unslash(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
always57–59wp_verify_nonce(), sanitize_key(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
!empty($value) && $nonce_state === 1 && empty($data)57wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), do_action(), time(), wp_send_json()
28 further outcomes, up to 80 instructions
$nonce_state !== 1 && $nonce_state !== false && !empty($data)59wp_verify_nonce(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
empty($value) && $nonce_state !== 1 && $nonce_state === false && empty($data)59wp_send_json_error(), wp_verify_nonce(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
always59–61wp_send_json_error(), wp_verify_nonce(), wp_unslash(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
always59–61wp_send_json_error(), wp_verify_nonce(), sanitize_key(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state === 1 && empty($data)59wp_send_json_error(), wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), do_action(), time(), wp_send_json()
empty($value) && $nonce_state !== 1 && $nonce_state !== false && !empty($data)61wp_send_json_error(), wp_verify_nonce(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)64wp_verify_nonce(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)64wp_verify_nonce(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)64wp_verify_nonce(), sanitize_key(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
!empty($value)64–66wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state !== false && !empty($data)66wp_verify_nonce(), wp_unslash(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state !== false && !empty($data)66wp_verify_nonce(), sanitize_key(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
empty($value) && $nonce_state !== 1 && $nonce_state === false && !empty($data)66wp_send_json_error(), wp_verify_nonce(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)66wp_send_json_error(), wp_verify_nonce(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)66wp_send_json_error(), wp_verify_nonce(), sanitize_key(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
always66–68wp_send_json_error(), wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state !== false && !empty($data)68wp_send_json_error(), wp_verify_nonce(), wp_unslash(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state !== false && !empty($data)68wp_send_json_error(), wp_verify_nonce(), sanitize_key(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)71wp_verify_nonce(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)71wp_verify_nonce(), sanitize_key(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
!empty($value) && $nonce_state !== 1 && $nonce_state === false && empty($data)71wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
!empty($value) && $nonce_state !== 1 && $nonce_state !== false && !empty($data)73wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)73wp_send_json_error(), wp_verify_nonce(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)73wp_send_json_error(), wp_verify_nonce(), sanitize_key(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && empty($data)73wp_send_json_error(), wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state !== false && !empty($data)75wp_send_json_error(), wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
!empty($value) && $nonce_state !== 1 && $nonce_state === false && !empty($data)78wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()
$nonce_state !== 1 && $nonce_state === false && !empty($data)80wp_send_json_error(), wp_verify_nonce(), sanitize_key(), wp_unslash(), apply_filters(), wp_send_json(), apply_filters(), apply_filters(), do_action(), time(), wp_send_json()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 81 instructions, 43–80 executed per call, 6 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 · 4

4 hooks fire while wp_ajax_heartbeat() runs, in this order:

  1. apply_filters( wp_refresh_nonces )filterline 3484 (+30 into the body)

    Filters the nonces to send to the New/Edit Post screen.

  2. apply_filters( heartbeat_received )filterline 3503 (+49 into the body)

    Filters the Heartbeat response received.

  3. apply_filters( heartbeat_send )filterline 3514 (+60 into the body)

    Filters the Heartbeat response sent.

  4. do_action( heartbeat_tick )actionline 3526 (+72 into the body)

    Fires when Heartbeat ticks in logged-in environments.

Uses · 7

  • wp_send_json_error()Sends a JSON response back to an Ajax request, indicating failure.
  • wp_verify_nonce()Verifies that a correct security nonce was used with time limit.
  • sanitize_key()Sanitizes a string key.
  • wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • wp_send_json()Sends a JSON response back to an Ajax request.
  • do_action()Calls the callback functions that have been added to an action hook.

Source code

function wp_ajax_heartbeat() {	if ( empty( $_POST['_nonce'] ) ) {		wp_send_json_error();	} 	$response    = array();	$data        = array();	$nonce_state = wp_verify_nonce( $_POST['_nonce'], 'heartbeat-nonce' ); 	// 'screen_id' is the same as $current_screen->id and the JS global 'pagenow'.	if ( ! empty( $_POST['screen_id'] ) ) {		$screen_id = sanitize_key( $_POST['screen_id'] );	} else {		$screen_id = 'front';	} 	if ( ! empty( $_POST['data'] ) ) {		$data = wp_unslash( (array) $_POST['data'] );	} 	if ( 1 !== $nonce_state ) {		/**		 * Filters the nonces to send to the New/Edit Post screen.		 *		 * @since 4.3.0		 *		 * @param array  $response  The Heartbeat response.		 * @param array  $data      The $_POST data sent.		 * @param string $screen_id The screen ID.		 */		$response = apply_filters( 'wp_refresh_nonces', $response, $data, $screen_id ); 		if ( false === $nonce_state ) {			// User is logged in but nonces have expired.			$response['nonces_expired'] = true;			wp_send_json( $response );		}	} 	if ( ! empty( $data ) ) {		/**		 * Filters the Heartbeat response received.		 *		 * @since 3.6.0		 *		 * @param array  $response  The Heartbeat response.		 * @param array  $data      The $_POST data sent.		 * @param string $screen_id The screen ID.		 */		$response = apply_filters( 'heartbeat_received', $response, $data, $screen_id );	} 	/**	 * Filters the Heartbeat response sent.	 *	 * @since 3.6.0	 *	 * @param array  $response  The Heartbeat response.	 * @param string $screen_id The screen ID.	 */	$response = apply_filters( 'heartbeat_send', $response, $screen_id ); 	/**	 * Fires when Heartbeat ticks in logged-in environments.	 *	 * Allows the transport to be easily replaced with long-polling.	 *	 * @since 3.6.0	 *	 * @param array  $response  The Heartbeat response.	 * @param string $screen_id The screen ID.	 */	do_action( 'heartbeat_tick', $response, $screen_id ); 	// Send the current time according to the server.	$response['server_time'] = time(); 	wp_send_json( $response );}

Changelog

Introduced in 3.6.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.8.8 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.