Make WordPress Core

Changeset 52095


Ignore:
Timestamp:
11/10/2021 01:25:09 AM (5 years ago)
Author:
hellofromTonya
Message:

Revisions: Introduce wp_get_post_revisions_url() to get URL for editing revisions.

There's now a way to get a link to a given post's revisions. Introducing wp_get_post_revisions_url() and its unit tests.

Props adamsilverstein, audrasjb, costdev, davidbaumwald, garrett-eclipse, georgestephanis, hellofromTonya, iaaxpage.
Fixes #39062.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/revision.php

    r51327 r52095  
    518518
    519519/**
     520 * Returns the url for viewing and potentially restoring revisions of a given post.
     521 *
     522 * @since 5.9.0
     523 *
     524 * @param int|WP_Post $post_id Optional. Post ID or WP_Post object. Default is global `$post`.
     525 * @return null|string The URL for editing revisions on the given post, otherwise null.
     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 === count( $revisions ) ) {
     546                return null;
     547        }
     548
     549        $revision = reset( $revisions );
     550        return get_edit_post_link( $revision );
     551}
     552
     553/**
    520554 * Determine if revisions are enabled for a given post.
    521555 *
  • trunk/tests/phpunit/tests/post/revisions.php

    r52010 r52095  
    655655                $this->assertWPError( $revision );
    656656        }
     657
     658        /**
     659         * Tests that wp_get_post_revisions_url() returns the revisions URL.
     660         *
     661         * @ticket 39062
     662         *
     663         * @dataProvider data_wp_get_post_revisions_url
     664         *
     665         * @covers ::wp_get_post_revisions_url
     666         *
     667         * @param int $revisions The number of revisions to create.
     668         */
     669        public function test_wp_get_post_revisions_url( $revisions ) {
     670                wp_set_current_user( self::$admin_user_id );
     671
     672                $post_id            = self::factory()->post->create( array( 'post_title' => 'Some Post' ) );
     673                $latest_revision_id = null;
     674
     675                if ( 0 !== $revisions ) {
     676                        $latest_revision_id = $post_id;
     677
     678                        for ( $i = 0; $i < $revisions; ++$i ) {
     679                                wp_update_post(
     680                                        array(
     681                                                'ID'         => $post_id,
     682                                                'post_title' => 'Some Post ' . $i,
     683                                        )
     684                                );
     685
     686                                $latest_revision_id++;
     687                        }
     688                }
     689
     690                $expected = admin_url( 'revision.php?revision=' . $latest_revision_id );
     691
     692                $this->assertSame(
     693                        $expected,
     694                        wp_get_post_revisions_url( $post_id ),
     695                        'Failed when passed the Post ID'
     696                );
     697
     698                $this->assertSame(
     699                        $expected,
     700                        wp_get_post_revisions_url( $latest_revision_id ),
     701                        'Failed when passed the latest revision ID'
     702                );
     703        }
     704
     705        /**
     706         * Tests that wp_get_post_revisions_url() returns the revisions URL
     707         * when passed a WP_Post object.
     708         *
     709         * @ticket 39062
     710         *
     711         * @dataProvider data_wp_get_post_revisions_url
     712         *
     713         * @covers ::wp_get_post_revisions_url
     714         *
     715         * @param int $revisions The number of revisions to create.
     716         */
     717        public function test_wp_get_post_revisions_url_with_post_object( $revisions ) {
     718                wp_set_current_user( self::$admin_user_id );
     719
     720                $post               = self::factory()->post->create_and_get( array( 'post_title' => 'Some Post' ) );
     721                $latest_revision_id = null;
     722
     723                if ( 0 !== $revisions ) {
     724                        $latest_revision_id = $post->ID;
     725
     726                        for ( $i = 0; $i < $revisions; ++$i ) {
     727                                wp_update_post(
     728                                        array(
     729                                                'ID'         => $post->ID,
     730                                                'post_title' => 'Some Post ' . $i,
     731                                        )
     732                                );
     733
     734                                $latest_revision_id++;
     735                        }
     736                }
     737
     738                $expected = admin_url( 'revision.php?revision=' . $latest_revision_id );
     739
     740                $this->assertSame(
     741                        $expected,
     742                        wp_get_post_revisions_url( $post ),
     743                        'Failed when passed the Post Object'
     744                );
     745
     746                $this->assertSame(
     747                        $expected,
     748                        wp_get_post_revisions_url( $latest_revision_id ),
     749                        'Failed when passed the latest revision ID'
     750                );
     751        }
     752
     753        /**
     754         * Data provider.
     755         *
     756         * @return array
     757         */
     758        public function data_wp_get_post_revisions_url() {
     759                return array(
     760                        'one revision'       => array( 'revisions' => 1 ),
     761                        'multiple revisions' => array( 'revisions' => 2 ),
     762                );
     763        }
     764
     765        /**
     766         * Tests that wp_get_post_revisions_url() returns NULL when a post does not exist.
     767         *
     768         * @ticket 39062
     769         *
     770         * @covers ::wp_get_post_revisions_url
     771         */
     772        public function test_wp_get_post_revisions_url_returns_null_when_post_does_not_exist() {
     773                wp_set_current_user( self::$admin_user_id );
     774                $post_id = 99999;
     775                $this->assertNull( wp_get_post_revisions_url( $post_id ) );
     776        }
     777
     778        /**
     779         * Tests that wp_get_post_revisions_url() returns NULL when there are no revisions.
     780         *
     781         * @ticket 39062
     782         *
     783         * @covers ::wp_get_post_revisions_url
     784         */
     785        public function test_wp_get_post_revisions_url_returns_null_with_no_revisions() {
     786                wp_set_current_user( self::$admin_user_id );
     787                $post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) );
     788                $this->assertNull( wp_get_post_revisions_url( $post_id ) );
     789        }
     790
     791        /**
     792         * Tests that wp_get_post_revisions_url() returns NULL when revisions are disabled.
     793         *
     794         * @ticket 39062
     795         *
     796         * @covers ::wp_get_post_revisions_url
     797         */
     798        public function test_wp_get_post_revisions_url_returns_null_with_revisions_disabled() {
     799                wp_set_current_user( self::$admin_user_id );
     800
     801                remove_post_type_support( 'post', 'revisions' );
     802
     803                $post_id = self::factory()->post->create( array( 'post_title' => 'Some Post' ) );
     804
     805                wp_update_post(
     806                        array(
     807                                'ID'         => $post_id,
     808                                'post_title' => 'Some Post 2',
     809                        )
     810                );
     811
     812                $this->assertNull( wp_get_post_revisions_url( $post_id ) );
     813
     814                add_post_type_support( 'post', 'revisions' );
     815        }
    657816}
Note: See TracChangeset for help on using the changeset viewer.