Make WordPress Core

Changeset 50949


Ignore:
Timestamp:
05/21/2021 03:28:39 PM (4 years ago)
Author:
adamsilverstein
Message:

Revisions: add a new filter for revisions to keep by post type.

Add a new wp_{$post_type}_revisions_to_keep filter that makes it convenient to filter the number of revisions created for a specific post type.

Overrides both the value of WP_POST_REVISIONS and the wp_revisions_to_keep filter.

Props dlh.
Fixes #51550.

Location:
trunk
Files:
2 edited

Legend:

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

    r50558 r50949  
    565565     * @param WP_Post $post Post object.
    566566     */
    567     return (int) apply_filters( 'wp_revisions_to_keep', $num, $post );
     567    $num = apply_filters( 'wp_revisions_to_keep', $num, $post );
     568
     569    /**
     570     * Filters the number of revisions to save for the given post by its post type.
     571     *
     572     * Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter.
     573     *
     574     * The dynamic portion of the hook name, `$post->post_type`, refers to
     575     * the post type slug.
     576     *
     577     * @since 5.8.0
     578     *
     579     * @param int     $num  Number of revisions to store.
     580     * @param WP_Post $post Post object.
     581     */
     582    $num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post );
     583
     584    return (int) $num;
    568585}
    569586
  • trunk/tests/phpunit/tests/post/revisions.php

    r50463 r50949  
    577577        $this->assertSame( $revision_ids, array_values( wp_list_pluck( $revisions, 'ID' ) ) );
    578578    }
     579
     580    /*
     581     * @ticket 51550
     582     */
     583    public function test_wp_revisions_to_keep_filter() {
     584        $post = self::factory()->post->create_and_get(
     585            array(
     586                'post_title'   => 'some-post',
     587                'post_type'    => 'post',
     588                'post_content' => 'some_content',
     589            )
     590        );
     591
     592        $default  = wp_revisions_to_keep( $post );
     593        $expected = $default + 1;
     594
     595        add_filter(
     596            'wp_revisions_to_keep',
     597            function () use ( $expected ) {
     598                return $expected;
     599            }
     600        );
     601
     602        $this->assertSame( $expected, wp_revisions_to_keep( $post ) );
     603    }
     604
     605    /*
     606     * @ticket 51550
     607     */
     608    public function test_wp_post_type_revisions_to_keep_filter() {
     609        $post = self::factory()->post->create_and_get(
     610            array(
     611                'post_title'   => 'some-post',
     612                'post_type'    => 'post',
     613                'post_content' => 'some_content',
     614            )
     615        );
     616
     617        $default = wp_revisions_to_keep( $post );
     618        $generic = $default + 1;
     619
     620        add_filter(
     621            'wp_revisions_to_keep',
     622            function () use ( $generic ) {
     623                return $generic;
     624            }
     625        );
     626        $this->assertSame( $generic, wp_revisions_to_keep( $post ) );
     627
     628        $expected = $generic + 1;
     629
     630        add_filter(
     631            "wp_{$post->post_type}_revisions_to_keep",
     632            function () use ( $expected ) {
     633                return $expected;
     634            }
     635        );
     636        $this->assertSame( $expected, wp_revisions_to_keep( $post ) );
     637    }
    579638}
Note: See TracChangeset for help on using the changeset viewer.