Make WordPress Core

Ticket #37462: 37462.diff

File 37462.diff, 1.9 KB (added by donmhico, 5 years ago)

Added new function is_private_post() and unit test for it.

  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index ae688a3470..46ac018dbe 100644
    function get_available_post_mime_types( $type = 'attachment' ) { 
    70257025        $types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
    70267026        return $types;
    70277027}
     7028
     7029/**
     7030 * Determine if a post is private or not.
     7031 *
     7032 * If the post is not found, it will return false. If the post is an attachment,
     7033 * the parent post status will be checked.
     7034 *
     7035 * @since 5.3
     7036 *
     7037 * @see get_post_status()
     7038 * @link https://core.trac.wordpress.org/ticket/37462
     7039 *
     7040 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
     7041 * @return bool True if the post is private, false if not or if the post is not found.
     7042 */
     7043function is_private_post( $post = null ) {
     7044        if ( 'private' === get_post_status( $post ) ) {
     7045                return true;
     7046        }
     7047
     7048        return false;
     7049}
     7050 No newline at end of file
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index 6ef0c8449f..4c1b7a971f 100644
    class Tests_Post extends WP_UnitTestCase { 
    13941394        function filter_pre_wp_unique_post_slug( $default, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
    13951395                return 'override-slug-' . $post_type;
    13961396        }
     1397
     1398        /**
     1399         * @ticket 37462
     1400         */
     1401        function test_is_private_post() {
     1402                $post_id_private = $this->factory->post->create(
     1403                        array(
     1404                                'title'       => 'An example',
     1405                                'post_status' => 'private',
     1406                                'post_type'   => 'post',
     1407                        )
     1408                );
     1409
     1410                $post_id_published = $this->factory->post->create(
     1411                        array(
     1412                                'title'       => 'An example',
     1413                                'post_status' => 'publish',
     1414                                'post_type'   => 'page',
     1415                        )
     1416                );
     1417
     1418                $this->assertTrue( is_private_post( $post_id_private ) );
     1419                $this->assertFalse( is_private_post( $post_id_published ) );
     1420        }
     1421
    13971422}