Make WordPress Core

Ticket #43638: 43638.2.patch

File 43638.2.patch, 2.9 KB (added by enrico.sorcinelli, 7 years ago)
  • src/wp-includes/post.php

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 98ad57f..dfeb89c 100644
    a b function is_sticky( $post_id = 0 ) { 
    20982098 *
    20992099 * @see sanitize_post_field()
    21002100 *
    2101  * @param object|WP_Post|array $post    The Post Object or Array
     2101 * @param object|WP_Post|array $post    The Post Object or Array.
    21022102 * @param string               $context Optional. How to sanitize post fields.
    21032103 *                                      Accepts 'raw', 'edit', 'db', or 'display'.
    21042104 *                                      Default 'display'.
    function is_sticky( $post_id = 0 ) { 
    21062106 *                              same type as $post).
    21072107 */
    21082108function sanitize_post( $post, $context = 'display' ) {
     2109
     2110        $original_post = $post;
     2111
    21092112        if ( is_object( $post ) ) {
    21102113                // Check if post already filtered for this context.
    21112114                if ( isset( $post->filter ) && $context == $post->filter ) {
    function sanitize_post( $post, $context = 'display' ) { 
    21312134                }
    21322135                $post['filter'] = $context;
    21332136        }
    2134         return $post;
     2137
     2138        /**
     2139         * Filters the sanitized post fields.
     2140         *
     2141         * @since 5.0.0
     2142         *
     2143         * @param object|WP_Post|array $post          The sanitized Post Object or Array.
     2144         * @param object|WP_Post|array $original_post The Post Object or Array.
     2145         * @param string               $context       Optional. How to sanitize post fields.
     2146         *                                            Accepts 'raw', 'edit', 'db', or 'display'.
     2147         *                                            Default 'display'.
     2148         */
     2149        return apply_filters( 'sanitize_post', $post, $original_post, $context );
    21352150}
    21362151
    21372152/**
  • tests/phpunit/tests/post.php

    diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
    index c0b20bb..4fa81a9 100644
    a b 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 'sanitize_post' filter.
     1359         *
     1360         * @ticket 43638
     1361         */
     1362        public function test_sanitize_post_filter() {
     1363                add_filter( 'sanitize_post', array( $this, 'filter_sanitize_post' ), 10, 3 );
     1364                $post_id = wp_insert_post(
     1365                        array(
     1366                                'post_type' => 'post',
     1367                                'post_title' => 'Stuff and Things',
     1368                                'post_status' => 'draft'
     1369                        )
     1370                );
     1371                $post = get_post( $post_id );
     1372                $this->assertEquals( $post->post_title, 'Stuff and Things - draft' );
     1373                remove_filter( 'sanitize_post', array( $this, 'filter_sanitize_post' ), 10, 3 );
     1374        }
     1375
     1376        public function filter_sanitize_post( $post, $original_post, $context ) {
     1377                if ( 'raw' === $context ) {
     1378                        return $post;
     1379                }
     1380                if ( $context === 'db' ) {
     1381                        if ( is_object( $post ) ) {
     1382                                $post->post_title .= ' - ' . $post->post_status;
     1383                        }
     1384                        else {
     1385                                $post['post_title'] .= ' - ' . $post['post_status'];
     1386                        }
     1387                }
     1388                return $post;
     1389        }
     1390
    13571391}