[wp-trac] [WordPress Trac] #46828: Undefined Offset -1 in wp-includes/rewrite.php on lines 379 and 381

WordPress Trac noreply at wordpress.org
Mon Apr 8 03:53:26 UTC 2019


#46828: Undefined Offset -1 in wp-includes/rewrite.php on lines 379 and 381
--------------------------+-----------------------------
 Reporter:  thomstark     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 This is what's currently in the function
 wp_resolve_numeric_slug_conflicts():

 {{{#!php
 <?php
     $permastructs   = array_values( array_filter( explode( '/',
 get_option( 'permalink_structure' ) ) ) );
     $postname_index = array_search( '%postname%', $permastructs );
     if ( false === $postname_index ) {
         return $query_vars;
     }
     $compare = '';
     if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset(
 $query_vars['monthnum'] ) ) ) {
         $compare = 'year';
     } elseif ( '%year%' === $permastructs[ $postname_index - 1 ] && (
 isset( $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
         $compare = 'monthnum';
     } elseif ( '%monthnum%' === $permastructs[ $postname_index - 1 ] &&
 isset( $query_vars['day'] ) ) {
         $compare = 'day';
     }

 }}}

 Here's the problem:

 When {{{ get_option( 'permalink_structure' ) = '/%postname%/' }}}
 then {{{ $permastructs = array(0=>'%postname%'); }}}
 and {{{ $postname_index = 0 }}}

 Therefore, lines 379 and 381 are trying to access

 {{{ $permastructs[ $postname_index - 1 ] }}}

 i.e.

 {{{ $permastructs[-1] }}}

 which outputs a PHP Notice: Undefined offset: -1.

 Needs to be adjusted as follows:

 {{{#!php
 <?php
 if ( 0 === $postname_index && ( isset( $query_vars['year'] ) || isset(
 $query_vars['monthnum'] ) ) ) {
         $compare = 'year';
     } elseif ($postname_index && '%year%' === $permastructs[
 $postname_index - 1 ] && ( isset( $query_vars['monthnum'] ) || isset(
 $query_vars['day'] ) ) ) {
         $compare = 'monthnum';
     } elseif ($postname_index && '%monthnum%' === $permastructs[
 $postname_index - 1 ] && isset( $query_vars['day'] ) ) {
         $compare = 'day';
     }
 }}}

 OR

 {{{#!php
 <?php
 if ( 0 === $postname_index) {
     if( isset( $query_vars['year'] ) || isset( $query_vars['monthnum'] ) )
 ) {
         $compare = 'year';
     }
 }
 else {
     if ('%year%' === $permastructs[ $postname_index - 1 ] && ( isset(
 $query_vars['monthnum'] ) || isset( $query_vars['day'] ) ) ) {
         $compare = 'monthnum';
     } elseif ('%monthnum%' === $permastructs[ $postname_index - 1 ] &&
 isset( $query_vars['day'] ) ) {
         $compare = 'day';
     }
 }

 }}}

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


More information about the wp-trac mailing list