[wp-trac] [WordPress Trac] #62824: single_month_title() can cause a warning.

WordPress Trac noreply at wordpress.org
Mon Jan 20 09:09:30 UTC 2025


#62824: single_month_title() can cause a warning.
--------------------------+------------------------
 Reporter:  apermo        |       Owner:  audrasjb
     Type:  defect (bug)  |      Status:  reviewing
 Priority:  normal        |   Milestone:  6.8
Component:  Date/Time     |     Version:  2.1
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |     Focuses:
--------------------------+------------------------

Comment (by apermo):

 Hey @audrasjb.

 I should have copied a little bit more.

 Here is the bigger context.


 {{{#!php
 <?php
 /**
  * Displays or retrieves page title for post archive based on date.
  *
  * Useful for when the template only needs to display the month and year,
  * if either are available. The prefix does not automatically place a
 space
  * between the prefix, so if there should be a space, the parameter value
  * will need to have it at the end.
  *
  * @since 0.71
  *
  * @global WP_Locale $wp_locale WordPress date and time locale object.
  *
  * @param string $prefix  Optional. What to display before the title.
  * @param bool   $display Optional. Whether to display or retrieve title.
 Default true.
  * @return string|false|void False if there's no valid title for the
 month. Title when retrieving.
  */
 function single_month_title( $prefix = '', $display = true ) {
         global $wp_locale;

         $m        = get_query_var( 'm' );
         $year     = get_query_var( 'year' );
         $monthnum = get_query_var( 'monthnum' );

         if ( ! empty( $monthnum ) && ! empty( $year ) ) {
                 $my_year  = $year;
                 $my_month = $wp_locale->get_month( $monthnum );
         } elseif ( ! empty( $m ) ) {
                 $my_year  = substr( $m, 0, 4 );
                 $my_month = $wp_locale->get_month( substr( $m, 4, 2 ) );
         }

         if ( empty( $my_month ) ) {
                 return false;
         }

         $result = $prefix . $my_month . $prefix . $my_year;

         if ( ! $display ) {
                 return $result;
         }
         echo $result;
 }
 }}}


 In that case that was caught by sentry, `$m` was just a 4 digit
 representation of a year.

 So I ended up in the `elseif()` but sent an empty string to
 `$wp_locale->get_month( substr( $m, 4, 2 ) )` resulting the warning and
 `$my_month` to be empty, and thus the whole function to return false.

 Returning January by default would be wrong in my eyes, and as I wanted to
 point out, not necessary, as `single_month_title()` already accounts for
 it. I understand your point, and I also think this function as a whole
 could use an overhaul, but the one backwards compatible way me and my
 colleague found was to add the sanitization as we did in the PR.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/62824#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list