Make WordPress Core

Ticket #51819: 51819.diff

File 51819.diff, 7.6 KB (added by ethitter, 2 years ago)
  • src/wp-admin/includes/post.php

     
    21422142}
    21432143
    21442144/**
    2145  * Returns whether the post can be edited in the block editor.
    2146  *
    2147  * @since 5.0.0
    2148  *
    2149  * @param int|WP_Post $post Post ID or WP_Post object.
    2150  * @return bool Whether the post can be edited in the block editor.
    2151  */
    2152 function use_block_editor_for_post( $post ) {
    2153         $post = get_post( $post );
    2154 
    2155         if ( ! $post ) {
    2156                 return false;
    2157         }
    2158 
    2159         // We're in the meta box loader, so don't use the block editor.
    2160         if ( isset( $_GET['meta-box-loader'] ) ) {
    2161                 check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
    2162                 return false;
    2163         }
    2164 
    2165         $use_block_editor = use_block_editor_for_post_type( $post->post_type );
    2166 
    2167         /**
    2168          * Filters whether a post is able to be edited in the block editor.
    2169          *
    2170          * @since 5.0.0
    2171          *
    2172          * @param bool    $use_block_editor Whether the post can be edited or not.
    2173          * @param WP_Post $post             The post being checked.
    2174          */
    2175         return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
    2176 }
    2177 
    2178 /**
    2179  * Returns whether a post type is compatible with the block editor.
    2180  *
    2181  * The block editor depends on the REST API, and if the post type is not shown in the
    2182  * REST API, then it won't work with the block editor.
    2183  *
    2184  * @since 5.0.0
    2185  *
    2186  * @param string $post_type The post type.
    2187  * @return bool Whether the post type can be edited with the block editor.
    2188  */
    2189 function use_block_editor_for_post_type( $post_type ) {
    2190         if ( ! post_type_exists( $post_type ) ) {
    2191                 return false;
    2192         }
    2193 
    2194         if ( ! post_type_supports( $post_type, 'editor' ) ) {
    2195                 return false;
    2196         }
    2197 
    2198         $post_type_object = get_post_type_object( $post_type );
    2199         if ( $post_type_object && ! $post_type_object->show_in_rest ) {
    2200                 return false;
    2201         }
    2202 
    2203         /**
    2204          * Filters whether a post is able to be edited in the block editor.
    2205          *
    2206          * @since 5.0.0
    2207          *
    2208          * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
    2209          * @param string $post_type         The post type being checked.
    2210          */
    2211         return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
    2212 }
    2213 
    2214 /**
    22152145 * Prepares server-registered blocks for the block editor.
    22162146 *
    22172147 * Returns an associative array of registered block data keyed by block name. Data includes properties
  • src/wp-includes/post.php

     
    80718071function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) {
    80728072        return $previous_status;
    80738073}
     8074
     8075/**
     8076 * Returns whether the post can be edited in the block editor.
     8077 *
     8078 * @since 5.0.0
     8079 *
     8080 * @param int|WP_Post $post Post ID or WP_Post object.
     8081 * @return bool Whether the post can be edited in the block editor.
     8082 */
     8083function use_block_editor_for_post( $post ) {
     8084        $post = get_post( $post );
     8085
     8086        if ( ! $post ) {
     8087                return false;
     8088        }
     8089
     8090        // We're in the meta box loader, so don't use the block editor.
     8091        if ( isset( $_GET['meta-box-loader'] ) ) {
     8092                check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' );
     8093                return false;
     8094        }
     8095
     8096        $use_block_editor = use_block_editor_for_post_type( $post->post_type );
     8097
     8098        /**
     8099         * Filters whether a post is able to be edited in the block editor.
     8100         *
     8101         * @since 5.0.0
     8102         *
     8103         * @param bool    $use_block_editor Whether the post can be edited or not.
     8104         * @param WP_Post $post             The post being checked.
     8105         */
     8106        return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post );
     8107}
     8108
     8109/**
     8110 * Returns whether a post type is compatible with the block editor.
     8111 *
     8112 * The block editor depends on the REST API, and if the post type is not shown in the
     8113 * REST API, then it won't work with the block editor.
     8114 *
     8115 * @since 5.0.0
     8116 *
     8117 * @param string $post_type The post type.
     8118 * @return bool Whether the post type can be edited with the block editor.
     8119 */
     8120function use_block_editor_for_post_type( $post_type ) {
     8121        if ( ! post_type_exists( $post_type ) ) {
     8122                return false;
     8123        }
     8124
     8125        if ( ! post_type_supports( $post_type, 'editor' ) ) {
     8126                return false;
     8127        }
     8128
     8129        $post_type_object = get_post_type_object( $post_type );
     8130        if ( $post_type_object && ! $post_type_object->show_in_rest ) {
     8131                return false;
     8132        }
     8133
     8134        /**
     8135         * Filters whether a post is able to be edited in the block editor.
     8136         *
     8137         * @since 5.0.0
     8138         *
     8139         * @param bool   $use_block_editor  Whether the post type can be edited or not. Default true.
     8140         * @param string $post_type         The post type being checked.
     8141         */
     8142        return apply_filters( 'use_block_editor_for_post_type', true, $post_type );
     8143}
  • tests/phpunit/tests/admin/includesPost.php

     
    789789                $this->assertSame( $p, post_exists( $title, $content, $date ) );
    790790        }
    791791
    792         public function test_use_block_editor_for_post() {
    793                 $this->assertFalse( use_block_editor_for_post( -1 ) );
    794                 $bogus_post_id = $this->factory()->post->create(
    795                         array(
    796                                 'post_type' => 'bogus',
    797                         )
    798                 );
    799                 $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
    800 
    801                 register_post_type(
    802                         'restless',
    803                         array(
    804                                 'show_in_rest' => false,
    805                         )
    806                 );
    807                 $restless_post_id = $this->factory()->post->create(
    808                         array(
    809                                 'post_type' => 'restless',
    810                         )
    811                 );
    812                 $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
    813 
    814                 $generic_post_id = $this->factory()->post->create();
    815 
    816                 add_filter( 'use_block_editor_for_post', '__return_false' );
    817                 $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
    818                 remove_filter( 'use_block_editor_for_post', '__return_false' );
    819 
    820                 add_filter( 'use_block_editor_for_post', '__return_true' );
    821                 $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
    822                 remove_filter( 'use_block_editor_for_post', '__return_true' );
    823         }
    824 
    825792        public function test_get_block_editor_server_block_settings() {
    826793                $name     = 'core/test';
    827794                $settings = array(
  • tests/phpunit/tests/post.php

     
    18441844                unstick_post( 3 );
    18451845                $this->assertSameSets( array( 1, 2, 2 ), get_option( 'sticky_posts' ) );
    18461846        }
     1847
     1848        /**
     1849         * Check if post supports block editor.
     1850         *
     1851         * @ticket 51819
     1852         * @covers ::use_block_editor_for_post
     1853         */
     1854        public function test_use_block_editor_for_post() {
     1855                $this->assertFalse( use_block_editor_for_post( -1 ) );
     1856                $bogus_post_id = $this->factory()->post->create(
     1857                        array(
     1858                                'post_type' => 'bogus',
     1859                        )
     1860                );
     1861                $this->assertFalse( use_block_editor_for_post( $bogus_post_id ) );
     1862
     1863                register_post_type(
     1864                        'restless',
     1865                        array(
     1866                                'show_in_rest' => false,
     1867                        )
     1868                );
     1869                $restless_post_id = $this->factory()->post->create(
     1870                        array(
     1871                                'post_type' => 'restless',
     1872                        )
     1873                );
     1874                $this->assertFalse( use_block_editor_for_post( $restless_post_id ) );
     1875
     1876                $generic_post_id = $this->factory()->post->create();
     1877
     1878                add_filter( 'use_block_editor_for_post', '__return_false' );
     1879                $this->assertFalse( use_block_editor_for_post( $generic_post_id ) );
     1880                remove_filter( 'use_block_editor_for_post', '__return_false' );
     1881
     1882                add_filter( 'use_block_editor_for_post', '__return_true' );
     1883                $this->assertTrue( use_block_editor_for_post( $restless_post_id ) );
     1884                remove_filter( 'use_block_editor_for_post', '__return_true' );
     1885        }
    18471886}