[wp-trac] [WordPress Trac] #58798: Fix possible PHP warning in /wp-includes/feed.php

WordPress Trac noreply at wordpress.org
Thu Jul 13 10:12:27 UTC 2023


#58798: Fix possible PHP warning in /wp-includes/feed.php
--------------------------+-----------------------------
 Reporter:  zahardoc      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Feeds         |    Version:  6.2.2
 Severity:  normal        |   Keywords:  needs-patch
  Focuses:                |
--------------------------+-----------------------------
 If some plugins or themes use "enclosure" meta field as a one-line string,
 it results in PHP warnings:

 PHP Warning: Undefined array key 2 in /wp-includes/feed.php on line 484
 PHP Warning: Undefined array key 1 in /wp-includes/feed.php on line 494

 It happens because there is no check of string content there:


 {{{
 foreach ( (array) get_post_custom() as $key => $val ) {
         if ( 'enclosure' === $key && is_array( $val ) ) {
                 foreach ( (array) $val as $enc ) {
                         $enclosure = explode( "\n", $enc );


                         / Only get the first element, e.g. 'audio/mpeg'
 from 'audio/mpeg mpga mp2 mp3'.
                         $t    = preg_split( '/[ \t]/', trim( $enclosure[2]
 ) );
                         $type = $t[0];

                                 /**
                                  * Filters the RSS enclosure HTML link tag
 for the current post.
                                  *
                                  * @since 2.2.0
                                  *
                                  * @param string $html_link_tag The HTML
 link tag with a URI and other attributes.
                                  */
                 echo apply_filters( 'rss_enclosure', '<enclosure url="' .
 esc_url( trim( $enclosure[0] ) ) . '" length="' . absint( trim(
 $enclosure[1] ) ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
                         }
                 }
         }
 }}}

 **Proposal:**

 Add checks if the enclosure is an array, and each item can be exploded
 into exactly 3 items:


 {{{
 if ( 'enclosure' === $key && is_array( $val ) ) {}
 }}}

 And this:


 {{{
 $enclosure = explode( "\n", $enc );

 if ( 3 !== count( $enclosure ) ) {
         continue;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58798>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list