WordPress.org

Make WordPress Core

Ticket #21112: 21112.2.diff

File 21112.2.diff, 2.1 KB (added by coffee2code, 7 years ago)

Refreshed the 21112.diff patch. See associated comment for details.

  • src/wp-includes/post.php

     
    36653665        if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) )
    36663666                return $slug;
    36673667
     3668        /**
     3669         * Filter to allow setting the unique post slug before it is generated.
     3670         *
     3671         * Returning a non-empty value will preempt unique slug detection and
     3672         * generation and will result in that value being used as the post's slug.
     3673         *
     3674         * @since 4.1
     3675         *
     3676         * @param string $default_slug  The default post slug. Default empty.
     3677         * @param string $slug          The desired slug (post_name).
     3678         * @param int    $post_ID       Post ID.
     3679         * @param string $post_status   The post status.
     3680         * @param string $post_type     Post type.
     3681         * @param int    $post_parent   Post parent ID
     3682         */
     3683        $override_slug = apply_filters( 'pre_wp_unique_post_slug', '', $slug, $post_ID, $post_status, $post_type, $post_parent );
     3684        if ( $override_slug ) {
     3685                return $override_slug;
     3686        }
     3687
    36683688        global $wpdb, $wp_rewrite;
    36693689
    36703690        $original_slug = $slug;
  • tests/phpunit/tests/post.php

     
    10161016                _unregister_post_type( 'post-type-1' );
    10171017                _unregister_post_type( 'post-type-2' );
    10181018        }
     1019
     1020        function test_pre_wp_unique_post_slug_filter() {
     1021                add_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 );
     1022
     1023                $post_id = $this->factory->post->create( array( 'title' => 'An example', 'post_status' => 'publish', 'post_type' => 'page' ) );
     1024                $post = get_post( $post_id );
     1025
     1026                $this->assertEquals( 'override-slug-' . $post->post_type, $post->post_name );
     1027
     1028                remove_filter( 'pre_wp_unique_post_slug', array( $this, 'filter_pre_wp_unique_post_slug' ), 10, 6 );
     1029        }
     1030
     1031        function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
     1032                return 'override-slug-' . $post_type;
     1033        }
     1034
    10191035}