wppaste
WordPress

wp_prepare_revisions_for_js( WP_Post|int $post, int $selected_revision_id, int $from = null ): array

Since
3.6.0
Source
wp-admin/includes/revision.php:176
Prepare revisions for JavaScript.

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.

Parameters

$postWP_Post|int
The post object or post ID.
$selected_revision_idint
The selected revision ID.
$fromintoptional
The revision ID to compare from.Default: null

Return value

array
An associative array of revision data and related settings.

Performance profile

How much work a call to wp_prepare_revisions_for_js() 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 what you pass in.

Instructions
91–202

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

Plugin surface
1 hook

Third-party callbacks on 'wp_prepare_revision_for_js' 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
  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below wp_prepare_revisions_for_js()
  • serializeserialisationmaybe_unserialize()one call below wp_prepare_revisions_for_js()

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

What one call costs · 12 distinct outcomes

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

WhenInstructionsCalls it makes
$from91–108get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
!$from102–119get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), array_keys(), array_search(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && $from108–116get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), end(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
!$from117–134get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), array_keys(), array_search(), array_slice(), array_keys(), reset(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && !$from119–127get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), end(), array_keys(), array_search(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && !$from134–142get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), end(), array_keys(), array_search(), array_slice(), array_keys(), reset(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
$from151–168get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
!$from162–179get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), array_keys(), array_search(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && $from168–176get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), end(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
!$from177–194get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), array_keys(), array_search(), array_slice(), array_keys(), reset(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && !$from179–187get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), end(), array_keys(), array_search(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()
empty($current_id) && !$revision && !$from194–202get_post(), time(), wp_get_post_revisions(), wp_revisions_enabled(), get_option(), update_post_author_caches(), current_user_can(), get_the_title(), __(), strtotime(), date_i18n(), _x(), strtotime(), date_i18n(), __(), strtotime(), human_time_diff(), sprintf(), end(), array_keys(), array_search(), array_slice(), array_keys(), reset(), absint(), wp_get_revision_ui_diff(), wp_create_nonce(), array_values(), admin_url(), parse_url(), absint(), array_keys()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev35891–202211 fewer instruction than PHP 8.5
8.535991–20221
8.435991–202215 fewer instructions than PHP 8.3
8.336493–20421
8.236493–20421
8.136493–20421
7.436493–20421

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_prepare_revisions_for_js() runs, in this order:

  1. apply_filters( wp_prepare_revision_for_js )filterline 282 (+106 into the body)

    Filters the array of revisions used on the revisions screen.

Uses · 21

Show all 21
  • get_the_title()Retrieves the post title.
  • date_i18n()Retrieves the date in localized format, based on a sum of Unix timestamp and timezone offset in seconds.
  • __()Retrieves the translation of $text.
  • _x()Retrieves translated string with gettext context.
  • human_time_diff()Determines the difference between two timestamps.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • absint()Converts a value to non-negative integer.
  • wp_get_revision_ui_diff()Get the revision UI diff.
  • wp_create_nonce()Creates a cryptographic token tied to a specific action, user, user session, and window of time.

Source code

function wp_prepare_revisions_for_js( $post, $selected_revision_id, $from = null ) {	$post    = get_post( $post );	$authors = array();	$now_gmt = time(); 	$revisions = wp_get_post_revisions(		$post->ID,		array(			'order'         => 'ASC',			'check_enabled' => false,		)	);	// If revisions are disabled, we only want autosaves and the current post.	if ( ! wp_revisions_enabled( $post ) ) {		foreach ( $revisions as $revision_id => $revision ) {			if ( ! wp_is_post_autosave( $revision ) ) {				unset( $revisions[ $revision_id ] );			}		}		$revisions = array( $post->ID => $post ) + $revisions;	} 	$show_avatars = get_option( 'show_avatars' ); 	update_post_author_caches( $revisions ); 	$can_restore = current_user_can( 'edit_post', $post->ID );	$current_id  = false; 	foreach ( $revisions as $revision ) {		$modified     = strtotime( $revision->post_modified );		$modified_gmt = strtotime( $revision->post_modified_gmt . ' +0000' );		if ( $can_restore ) {			$restore_link = str_replace(				'&amp;',				'&',				wp_nonce_url(					add_query_arg(						array(							'revision' => $revision->ID,							'action'   => 'restore',						),						admin_url( 'revision.php' )					),					"restore-post_{$revision->ID}"				)			);		} 		if ( ! isset( $authors[ $revision->post_author ] ) ) {			$authors[ $revision->post_author ] = array(				'id'     => (int) $revision->post_author,				'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',				'name'   => get_the_author_meta( 'display_name', $revision->post_author ),			);		} 		$autosave = (bool) wp_is_post_autosave( $revision );		$current  = ! $autosave && $revision->post_modified_gmt === $post->post_modified_gmt;		if ( $current && ! empty( $current_id ) ) {			// If multiple revisions have the same post_modified_gmt, highest ID is current.			if ( $current_id < $revision->ID ) {				$revisions[ $current_id ]['current'] = false;				$current_id                          = $revision->ID;			} else {				$current = false;			}		} elseif ( $current ) {			$current_id = $revision->ID;		} 		$revisions_data = array(			'id'         => $revision->ID,			'title'      => get_the_title( $post->ID ),			'author'     => $authors[ $revision->post_author ],			'date'       => date_i18n( __( 'M j, Y @ H:i' ), $modified ),			'dateShort'  => date_i18n( _x( 'j M @ H:i', 'revision date short format' ), $modified ),			/* translators: %s: Human-readable time difference. */			'timeAgo'    => sprintf( __( '%s ago' ), human_time_diff( $modified_gmt, $now_gmt ) ),			'autosave'   => $autosave,

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.7.7 tag, from src/wp-admin/includes/revision.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.