Make WordPress Core

Ticket #31154: 31154.patch

File 31154.patch, 2.4 KB (added by tyxla, 9 years ago)

Call a _doing_it_wrong() when reserved post types post, page, attachment, revision and nav_menu_item are used with _built_in = false, and when disallowed post types action, 'order' and theme are used. Including unit tests.

  • src/wp-includes/post.php

     
    13421342        $post_type = sanitize_key( $post_type );
    13431343        $args->name = $post_type;
    13441344
     1345        $reserved_post_types = array( 'post', 'page', 'attachment', 'revision', 'nav_menu_item' );
     1346        if ( in_array( $post_type, $reserved_post_types ) && !$args->_builtin ) {
     1347                _doing_it_wrong( __FUNCTION__, __( 'Reserved post types cannot be registered as non-built-in ones.' ), '4.2' );
     1348                return new WP_Error( 'reserved_post_type_non_built_in', __( 'Reserved post types cannot be registered as non-built-in ones.' ) );
     1349        }
     1350
     1351        $disallowed_post_types = array( 'action', 'order', 'theme' );
     1352        if ( in_array( $post_type, $disallowed_post_types ) ) {
     1353                _doing_it_wrong( __FUNCTION__, __( 'Disallowed post types cannot be registered.' ), '4.2' );
     1354                return new WP_Error( 'post_type_disallowed', __( 'Disallowed post type cannot be registered.' ) );
     1355        }
     1356
    13451357        if ( strlen( $post_type ) > 20 ) {
    13461358                _doing_it_wrong( __FUNCTION__, __( 'Post types cannot exceed 20 characters in length' ), '4.0' );
    13471359                return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  • tests/phpunit/tests/post/types.php

     
    8181                update_option( 'permalink_structure', $old_permastruct );
    8282                _unregister_post_type( 'foo' );
    8383        }
     84
     85        /**
     86         * @ticket 31154
     87         *
     88         * @expectedIncorrectUsage register_post_type
     89         */
     90        function test_register_post_type_reserved() {
     91                $result = register_post_type( 'post' );
     92                $this->assertInstanceOf( 'WP_Error', $result );
     93
     94                $result = register_post_type( 'post', array( '_builtin' => true ) );
     95                $this->assertTrue( post_type_exists( 'post' ) );
     96                $this->assertInstanceOf( 'stdClass', $result );
     97        }
     98
     99        /**
     100         * @ticket 31154
     101         *
     102         * @expectedIncorrectUsage register_post_type
     103         */
     104        function test_register_post_type_disallowed() {
     105                $result = register_post_type( 'action' );
     106                $this->assertInstanceOf( 'WP_Error', $result );
     107
     108                $result = register_post_type( 'action', array( '_builtin' => true ) );
     109                $this->assertInstanceOf( 'WP_Error', $result );
     110        }
    84111}