Make WordPress Core

Ticket #39062: 39062.1.diff

File 39062.1.diff, 2.7 KB (added by audrasjb, 2 years ago)

Revisions: Introduce wp_get_post_revisions_url() to get the URL of the revisions screen for a given post.

  • src/wp-includes/revision.php

    diff --git a/src/wp-includes/revision.php b/src/wp-includes/revision.php
    index 70051a07da..4abf7803d0 100644
    a b function wp_get_post_revisions( $post_id = 0, $args = null ) { 
    516516        return $revisions;
    517517}
    518518
     519/**
     520 * Returns the url for viewing and potentially restoring revisions of a given post.
     521 *
     522 * @since 5.9.0
     523 *
     524 * @param int $post_id The Post ID that you'd like to get a revision for. If omitted, will use the global post.
     525 * @return null|string The URL for editing revisions on the given post. Returns null if none found.
     526 */
     527function wp_get_post_revisions_url( $post_id = 0 ) {
     528        $post = get_post( $post_id );
     529
     530        if ( ! $post instanceof WP_Post ) {
     531                return null;
     532        }
     533
     534        // If the post is a revision, return early.
     535        if ( 'revision' === $post->post_type ) {
     536                return get_edit_post_link( $post );
     537        }
     538
     539        if ( ! wp_revisions_enabled( $post ) ) {
     540                return null;
     541        }
     542
     543        $revisions = wp_get_post_revisions( $post->ID, array( 'posts_per_page' => 1 ) );
     544
     545        if ( 0 === sizeof( $revisions ) ) {
     546                return null;
     547        }
     548
     549        $revision = reset( $revisions );
     550        return get_edit_post_link( $revision );
     551}
     552
    519553/**
    520554 * Determine if revisions are enabled for a given post.
    521555 *
  • tests/phpunit/tests/post/revisions.php

    diff --git a/tests/phpunit/tests/post/revisions.php b/tests/phpunit/tests/post/revisions.php
    index 840597c4a9..f1054d998d 100644
    a b class Tests_Post_Revisions extends WP_UnitTestCase { 
    654654
    655655                $this->assertWPError( $revision );
    656656        }
     657
     658        /**
     659         * Verifies that trying to create a revision with an invalid ID returns a WP_Error.
     660         *
     661         * @ticket 39062
     662         */
     663        public function test_wp_get_post_revisions_url() {
     664                $u = self::factory()->user->create( array(
     665                        'role' => 'administrator',
     666                ) );
     667                wp_set_current_user( $u );
     668
     669                $post = self::factory()->post->create_and_get( array(
     670                        'post_title' => 'Some Post',
     671                ) );
     672
     673                wp_update_post( array(
     674                        'ID' => $post->ID,
     675                        'post_title' => 'Some Post 2',
     676                ) );
     677
     678                $latest_revision_id = key( wp_get_post_revisions( $post, array( 'posts_per_page' => 1 ) ) );
     679                $this->assertEquals( admin_url( 'revision.php?revision=' . $latest_revision_id ), wp_get_post_revisions_url( $post ) );
     680
     681                wp_update_post( array(
     682                        'ID' => $post->ID,
     683                        'post_title' => 'Some Post 3',
     684                ) );
     685
     686                $latest_revision_id = key( wp_get_post_revisions( $post, array( 'posts_per_page' => 1 ) ) );
     687                $this->assertEquals( admin_url( 'revision.php?revision=' . $latest_revision_id ), wp_get_post_revisions_url( $post ) );
     688                $this->assertEquals( admin_url( 'revision.php?revision=' . $latest_revision_id ), wp_get_post_revisions_url( $latest_revision_id ) );
     689        }
    657690}