wppaste
WordPress

wp_get_revision_ui_diff( WP_Post|int $post, int $compare_from, int $compare_to ): array|false

Since
3.6.0
Source
wp-admin/includes/revision.php:21
Get the revision UI diff.

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.
$compare_fromint
The revision ID to compare from.
$compare_toint
The revision ID to come to.

Return value

array|false
Associative array of a post's revisioned fields and their diffs.
Or, false on failure.

Performance profile

How much work a call to wp_get_revision_ui_diff() 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
9–79

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

Plugin surface
3 hooks

Third-party callbacks on '_wp_post_revision_field_{$field}', 'revision_text_diff_options', 'wp_get_revision_ui_diff' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach option, cache, 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 · 13 distinct outcomes

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

WhenInstructionsCalls it makes
always9get_post()
always15–33get_post(), get_post()
always20–37get_post(), get_post(), get_post()
!empty($compare_to)36–49get_post(), get_post(), _wp_post_revision_fields(), apply_filters()
!empty($compare_to)40–53get_post(), get_post(), get_post(), _wp_post_revision_fields(), apply_filters()
empty($compare_to)41–54get_post(), get_post(), __(), _wp_post_revision_fields(), apply_filters()
empty($compare_to)45–58get_post(), get_post(), get_post(), __(), _wp_post_revision_fields(), apply_filters()
!empty($compare_to)47–65get_post(), get_post(), strtotime(), strtotime(), _wp_post_revision_fields(), apply_filters()
!empty($compare_to)51–69get_post(), get_post(), get_post(), strtotime(), strtotime(), _wp_post_revision_fields(), apply_filters()
always52–70get_post(), get_post(), strtotime(), strtotime(), __(), _wp_post_revision_fields(), apply_filters()
always56–74get_post(), get_post(), get_post(), strtotime(), strtotime(), __(), _wp_post_revision_fields(), apply_filters()
empty($compare_from) && empty($compare_to)59–75get_post(), get_post(), strtotime(), strtotime(), __(), __(), _wp_post_revision_fields(), apply_filters()
1 further outcome, up to 79 instructions
empty($compare_from) && empty($compare_to)63–79get_post(), get_post(), get_post(), strtotime(), strtotime(), __(), __(), _wp_post_revision_fields(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1879–79221 fewer instruction than PHP 8.5
8.51889–7922
8.41889–7922
8.31889–7922
8.21889–7922
8.11889–7922
7.41889–7922

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

  1. apply_filters( _wp_post_revision_field_{$field} )filterline 90 (+69 into the body)

    Contextually filter a post revision field.

  2. apply_filters( _wp_post_revision_field_{$field} )filterline 93 (+72 into the body)

    Contextually filter a post revision field.

  3. apply_filters( revision_text_diff_options )filterline 118 (+97 into the body)

    Filters revisions text diff options.

  4. apply_filters( wp_get_revision_ui_diff )filterline 163 (+142 into the body)

    Filters the fields displayed in the post revision diff UI.

Uses · 6

  • get_post()Retrieves post data given a post ID or post object.
  • __()Retrieves the translation of $text.
  • _wp_post_revision_fields()Determines which fields of posts are to be saved in revisions.
  • apply_filters()Calls the callback functions that have been added to a filter hook.
  • wp_text_diff()Displays a human readable HTML representation of the difference between two strings.
  • esc_html()Escaping for HTML blocks.

Used by · 2

Source code

function wp_get_revision_ui_diff( $post, $compare_from, $compare_to ) {	$post = get_post( $post );	if ( ! $post ) {		return false;	} 	if ( $compare_from ) {		$compare_from = get_post( $compare_from );		if ( ! $compare_from ) {			return false;		}	} else {		// If we're dealing with the first revision...		$compare_from = false;	} 	$compare_to = get_post( $compare_to );	if ( ! $compare_to ) {		return false;	} 	/*	 * If comparing revisions, make sure we are dealing with the right post parent.	 * The parent post may be a 'revision' when revisions are disabled and we're looking at autosaves.	 */	if ( $compare_from && $compare_from->post_parent !== $post->ID && $compare_from->ID !== $post->ID ) {		return false;	}	if ( $compare_to->post_parent !== $post->ID && $compare_to->ID !== $post->ID ) {		return false;	} 	if ( $compare_from && strtotime( $compare_from->post_date_gmt ) > strtotime( $compare_to->post_date_gmt ) ) {		$temp         = $compare_from;		$compare_from = $compare_to;		$compare_to   = $temp;	} 	// Add default title if title field is empty.	if ( $compare_from && empty( $compare_from->post_title ) ) {		$compare_from->post_title = __( '(no title)' );	}	if ( empty( $compare_to->post_title ) ) {		$compare_to->post_title = __( '(no title)' );	} 	$return = array(); 	foreach ( _wp_post_revision_fields( $post ) as $field => $name ) {		/**		 * Contextually filter a post revision field.		 *		 * The dynamic portion of the hook name, `$field`, corresponds to a name of a		 * field of the revision object.		 *		 * Possible hook names include:		 *		 *  - `_wp_post_revision_field_post_title`		 *  - `_wp_post_revision_field_post_content`		 *  - `_wp_post_revision_field_post_excerpt`		 *		 * @since 3.6.0		 *		 * @param string  $revision_field The current revision field to compare to or from.		 * @param string  $field          The current revision field.		 * @param WP_Post $compare_from   The revision post object to compare to or from.		 * @param string  $context        The context of whether the current revision is the old		 *                                or the new one. Either 'to' or 'from'.		 */		$content_from = $compare_from ? apply_filters( "_wp_post_revision_field_{$field}", $compare_from->$field, $field, $compare_from, 'from' ) : ''; 		/** This filter is documented in wp-admin/includes/revision.php */		$content_to = apply_filters( "_wp_post_revision_field_{$field}", $compare_to->$field, $field, $compare_to, 'to' ); 		$args = array(			'show_split_view' => true,			'title_left'      => __( 'Removed' ),			'title_right'     => __( 'Added' ),		);

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 7.1.0 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.