WordPress.org

Make WordPress Core

Ticket #21112: 21112.4.diff

File 21112.4.diff, 2.3 KB (added by javorszky, 4 years ago)

Rebased on latest master (50cd98012e), and changed @since tag to 5.0

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 98ad57fbd0..ac748820be 100644
    function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_p 
    39843984                return $slug;
    39853985        }
    39863986
     3987        /**
     3988         * Filter to allow setting the unique post slug before it is generated.
     3989         *
     3990         * Returning a non-empty value will preempt unique slug detection and
     3991         * generation and will result in that value being used as the post's slug.
     3992         *
     3993         * @since 5.0
     3994         *
     3995         * @param string $default_slug  The default post slug. Default empty.
     3996         * @param string $slug          The desired slug (post_name).
     3997         * @param int    $post_ID       Post ID.
     3998         * @param string $post_status   The post status.
     3999         * @param string $post_type     Post type.
     4000         * @param int    $post_parent   Post parent ID
     4001         */
     4002        $override_slug = apply_filters( 'pre_wp_unique_post_slug', '', $slug, $post_ID, $post_status, $post_type, $post_parent );
     4003        if ( $override_slug ) {
     4004                return $override_slug;
     4005        }
     4006
    39874007        global $wpdb, $wp_rewrite;
    39884008
    39894009        $original_slug = $slug;
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index c0b20bb2a7..9786f5f2fa 100644
    class Tests_Post extends WP_UnitTestCase { 
    13541354                $this->assertEquals( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    13551355        }
    13561356
     1357        /**
     1358         * Test ensuring that the post_slug can be filtered with a custom value short circuiting the built in
     1359         * function that tries to create a unique name based on the post name.
     1360         *
     1361         * @see wp_unique_post_slug()
     1362         * @ticket 21112
     1363         */
     1364        function test_pre_wp_unique_post_slug_filter() {
     1365                add_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 );
     1366
     1367                $post_id = $this->factory->post->create( array( 'title' => 'An example', 'post_status' => 'publish', 'post_type' => 'page' ) );
     1368                $post = get_post( $post_id );
     1369                $this->assertEquals( 'override-slug-' . $post->post_type, $post->post_name );
     1370
     1371                remove_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 );
     1372        }
     1373
     1374        function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
     1375                return 'override-slug-' . $post_type;
     1376        }
    13571377}