Make WordPress Core


Ignore:
Timestamp:
11/10/2021 01:25:09 AM (3 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.

File:
1 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 *
Note: See TracChangeset for help on using the changeset viewer.