Make WordPress Core

Ticket #33591: 33591.6.patch

File 33591.6.patch, 6.7 KB (added by birgire, 7 years ago)
  • src/wp-includes/feed.php

    diff --git src/wp-includes/feed.php src/wp-includes/feed.php
    index dbeb3a0..59eb0db 100644
    function atom_enclosure() { 
    503503                if ($key == 'enclosure') {
    504504                        foreach ( (array) $val as $enc ) {
    505505                                $enclosure = explode("\n", $enc);
     506
     507                                $url    = '';
     508                                $length = 0;
     509                                $type   = '';
     510
     511                                $mimes = get_allowed_mime_types();
     512
     513                                // Parse url
     514                                if ( isset( $enclosure[0] ) && is_string( $enclosure[0] ) ) {
     515                                        $url = trim( $enclosure[0] );
     516                                }
     517
     518                                // Parse length and type
     519                                foreach( range( 1, 2 ) as $i ) {
     520                                        if ( isset( $enclosure[$i] ) ) {
     521                                                if( is_numeric( $enclosure[$i] ) ) {
     522                                                        $length = trim( $enclosure[$i] );
     523                                                } elseif ( in_array( $enclosure[$i], $mimes ) ) {
     524                                                        $type = trim( $enclosure[$i] );
     525                                                }
     526                                        }
     527                                }
     528                       
     529                                $html_link_tag = sprintf(
     530                                        "<link href=\"%s\" rel=\"enclosure\" length=\"%d\" type=\"%s\" />\n",
     531                                        esc_url ( $url ),
     532                                        esc_attr( $length ),
     533                                        esc_attr( $type )
     534                                );
     535       
    506536                                /**
    507537                                 * Filters the atom enclosure HTML link tag for the current post.
    508538                                 *
    function atom_enclosure() { 
    510540                                 *
    511541                                 * @param string $html_link_tag The HTML link tag with a URI and other attributes.
    512542                                 */
    513                                 echo apply_filters( 'atom_enclosure', '<link href="' . trim( htmlspecialchars( $enclosure[0] ) ) . '" rel="enclosure" length="' . trim( $enclosure[1] ) . '" type="' . trim( $enclosure[2] ) . '" />' . "\n" );
     543                                 echo apply_filters( 'atom_enclosure', $html_link_tag );
    514544                        }
    515545                }
    516546        }
  • tests/phpunit/tests/feed/atom.php

    diff --git tests/phpunit/tests/feed/atom.php tests/phpunit/tests/feed/atom.php
    index fbe064b..7298b23 100644
    class Tests_Feeds_Atom extends WP_UnitTestCase { 
    197197                        }
    198198                }
    199199        }
     200
     201        /**
     202         * @test 33591
     203         */
     204        function test_atom_enclosure_with_extended_url_length_type_parsing()
     205        {
     206                $enclosures = array(
     207                        array(
     208                                'actual'        => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4\n318465\nvideo/mp4",     // url length type
     209                                'expected'      => array(
     210                                        'href'          => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4",
     211                                        'length'        => 318465,
     212                                        'type'          => "video/mp4"
     213                                )
     214                        ),
     215                        array(
     216                                'actual'        => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4\nvideo/mp4\n318465",     // url type length
     217                                'expected'      => array(
     218                                        'href'          => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4",
     219                                        'length'        => 318465,
     220                                        'type'          => "video/mp4"
     221                                )
     222                        ),
     223                        array(
     224                                'actual'        => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4\n318465",                // url length
     225                                'expected'      => array(
     226                                        'href'          => "https://wordpress.dev/wp-content/uploads/2017/09/movie.mp4",
     227                                        'length'        => 318465,
     228                                        'type'          => ""
     229                                )
     230                        ),
     231                        array(
     232                                'actual'        => "https://wordpress.dev/wp-content/uploads/2017/01/audio.mp3\n\naudio/mpeg",          // url type
     233                                'expected'      => array(
     234                                        'href'          => "https://wordpress.dev/wp-content/uploads/2017/01/audio.mp3",
     235                                        'length'        => 0,
     236                                        'type'          => "audio/mpeg"
     237                                )
     238                        ),
     239                        array(
     240                                'actual'        => "https://wordpress.dev/wp-content/uploads/2016/01/test.mp4",                         // url
     241                                'expected'      => array(
     242                                        'href'          => "https://wordpress.dev/wp-content/uploads/2016/01/test.mp4",
     243                                        'length'        => 0,
     244                                        'type'          => ""
     245                                )
     246                        ),
     247                );
     248
     249                $post_id = end( self::$posts );
     250                foreach( $enclosures as $enclosure ) {
     251                        add_post_meta( $post_id, 'enclosure', $enclosure['actual'] );
     252                }
     253                $this->go_to( '/?feed=atom' );
     254                $feed = $this->do_atom();
     255                $xml = xml_to_array( $feed );
     256                $entries = xml_find( $xml, 'feed', 'entry' );
     257                $entries = array_slice( $entries, 0, 1 );
     258
     259                foreach ( $entries as $key => $entry ) {
     260                        $links = xml_find( $entries[$key]['child'], 'link' );
     261                        $i = 0;
     262                        foreach ( (array) $links as $link ) {
     263                                if ( 'enclosure' == $link['attributes']['rel'] ) {
     264                                        $this->assertEquals( $enclosures[$i]['expected']['href'] , $link['attributes']['href'] );
     265                                        $this->assertEquals( $enclosures[$i]['expected']['length'] , $link['attributes']['length'] );
     266                                        $this->assertEquals( $enclosures[$i]['expected']['type'] , $link['attributes']['type'] );
     267                                        $i++;
     268                                }
     269                        }
     270                }
     271        }
     272
     273
    200274}