Make WordPress Core

Ticket #39293: 39293.diff

File 39293.diff, 2.3 KB (added by ChopinBach, 5 years ago)

39293 Adds a test to handle edge case uses of add_theme_support. When add_theme_support( 'post-formats' ) is called, it resolves to true. This caused PHP to throw warnings expecting an array. This patch resolves this problem for any themes/plugins accidentally triggering this.

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

     
    19231923
    19241924                                case 'post-formats':
    19251925                                        $supports_formats = get_theme_support( 'post-formats' );
     1926
     1927                                        // Force to an array. Supports formats can return true even if empty in some cases.
     1928                                        $supports_formats = is_array( $supports_formats ) ? array_values( $supports_formats[0] ) : array();
     1929
     1930                                        $supported_formats = array_merge( array( 'standard' ), $supports_formats );
     1931
    19261932                                        $schema['properties']['format'] = array(
    19271933                                                'description' => __( 'The format for the object.' ),
    19281934                                                'type'        => 'string',
    1929                                                 'enum'        => array_merge( array( 'standard' ), $supports_formats ? array_values( $supports_formats[0] ) : array() ),
     1935                                                'enum'        => $supported_formats,
    19301936                                                'context'     => array( 'view', 'edit' ),
    19311937                                        );
    19321938                                        break;
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

     
    858858                $this->assertEquals( $post2, $data[0]['id'] );
    859859        }
    860860
     861        /**
     862         * Ticket #39293
     863         */
     864        public function test_get_items_no_supported_post_formats() {
     865                // See Ticket #39293. This sets an empty post format which resolves to `true`.
     866                add_theme_support( 'post-formats' );
     867
     868                $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
     869                $response = $this->server->dispatch( $request );
     870                $data = $response->get_data();
     871
     872                $formats = array( 'standard' );
     873
     874                $this->assertEquals( $formats, $data['schema']['properties']['format']['enum'] );
     875
     876                // Set the expected state back for the rest of the tests.
     877                global $_wp_theme_features;
     878                unset( $_wp_theme_features['post-formats'] );
     879                add_theme_support( 'post-formats', array( 'post', 'gallery' ) );
     880        }
     881
    861882        public function test_get_item() {
    862883                $request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
    863884                $response = $this->server->dispatch( $request );