Make WordPress Core

Ticket #48401: 48401.3.diff

File 48401.3.diff, 2.5 KB (added by TimothyBlynJacobs, 5 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 25b7fa2d8c..f97a2e898a 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    22422242
    22432243                $taxonomies = wp_list_filter( get_object_taxonomies( $this->post_type, 'objects' ), array( 'show_in_rest' => true ) );
    22442244                foreach ( $taxonomies as $taxonomy ) {
    2245                         $base                          = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
     2245                        $base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
     2246
     2247                        if ( array_key_exists( $base, $schema['properties'] ) ) {
     2248                                $taxonomy_field_name_with_conflict = ! empty( $taxonomy->rest_base ) ? 'rest_base' : 'name';
     2249                                _doing_it_wrong(
     2250                                        'register_taxonomy',
     2251                                        sprintf(
     2252                                                /* translators: 1. The taxonomy name, 2. The property name, either rest_base or name. */
     2253                                                __( 'The "%1$s" taxonomy "%2$s" conflicts with an existing property on the REST API Posts Controller. Specify a custom "rest_base" when registering the taxonomy to avoid this error.' ),
     2254                                                $base,
     2255                                                $taxonomy_field_name_with_conflict
     2256                                        ),
     2257                                        '5.4.0'
     2258                                );
     2259                        }
     2260
    22462261                        $schema['properties'][ $base ] = array(
    22472262                                /* translators: %s: Taxonomy name. */
    22482263                                'description' => sprintf( __( 'The terms assigned to the object in the %s taxonomy.' ), $taxonomy->name ),
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index 2aca849347..da6c0ada98 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    39183918                $this->assertArrayHasKey( 'categories', $properties );
    39193919        }
    39203920
     3921        /**
     3922         * @ticket 48401
     3923         */
     3924        public function test_get_item_schema_issues_doing_it_wrong_when_taxonomy_name_is_already_set_in_properties() {
     3925                $this->setExpectedIncorrectUsage( 'register_taxonomy' );
     3926
     3927                // Register a taxonomy with 'status' as name.
     3928                register_taxonomy( 'status', 'post', array( 'show_in_rest' => true ) );
     3929
     3930                // Re-initialize the controller
     3931                $controller = new WP_REST_Posts_Controller( 'post' );
     3932                $controller->register_routes();
     3933        }
     3934
    39213935        /**
    39223936         * @ticket 39805
    39233937         */