Make WordPress Core

Changeset 59947


Ignore:
Timestamp:
03/06/2025 09:19:10 PM (18 months ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Fix WPCS issues in get_calendar().

Includes:

  • Using strict comparison and $wpdb::prepare().
  • Removing one-time variables so that $wpdb::prepare() calls are picked up correctly by PHPCS.
  • Bringing consistency to the type of internal variables, i.e. $thismonth and $thisyear are both an integer now.

Follow-up to [508], [509], [510], [716], [933], [12590], [34463], [44809], [47223], [59908].

See #62279.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r59939 r59947  
    23482348        // Quick check. If we have no posts at all, abort!
    23492349        if ( ! $posts ) {
    2350                 $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type );
    2351                 $gotsome        = $wpdb->get_var( $prepared_query );
     2350                $gotsome = $wpdb->get_var(
     2351                        $wpdb->prepare(
     2352                                "SELECT 1 as test
     2353                                FROM $wpdb->posts
     2354                                WHERE post_type = %s
     2355                                AND post_status = 'publish'
     2356                                LIMIT 1",
     2357                                $post_type
     2358                        )
     2359                );
     2360
    23522361                if ( ! $gotsome ) {
    23532362                        $cache[ $key ] = '';
     
    23622371        // Let's figure out when we are.
    23632372        if ( ! empty( $monthnum ) && ! empty( $year ) ) {
    2364                 $thismonth = zeroise( (int) $monthnum, 2 );
     2373                $thismonth = (int) $monthnum;
    23652374                $thisyear  = (int) $year;
    23662375        } elseif ( ! empty( $w ) ) {
     
    23692378                // It seems MySQL's weeks disagree with PHP's.
    23702379                $d         = ( ( $w - 1 ) * 7 ) + 6;
    2371                 $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" );
     2380                $thismonth = (int) $wpdb->get_var(
     2381                        $wpdb->prepare(
     2382                                "SELECT DATE_FORMAT((DATE_ADD('%d0101', INTERVAL %d DAY) ), '%%m')",
     2383                                $thisyear,
     2384                                $d
     2385                        )
     2386                );
    23722387        } elseif ( ! empty( $m ) ) {
    23732388                $thisyear = (int) substr( $m, 0, 4 );
    23742389                if ( strlen( $m ) < 6 ) {
    2375                         $thismonth = '01';
     2390                        $thismonth = 1;
    23762391                } else {
    2377                         $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
     2392                        $thismonth = (int) substr( $m, 4, 2 );
    23782393                }
    23792394        } else {
    2380                 $thisyear  = current_time( 'Y' );
    2381                 $thismonth = current_time( 'm' );
     2395                $thisyear  = (int) current_time( 'Y' );
     2396                $thismonth = (int) current_time( 'm' );
    23822397        }
    23832398
     
    23862401
    23872402        // Get the next and previous month and year with at least one post.
    2388         $previous_prepared_query = $wpdb->prepare(
    2389                 "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    2390                 FROM $wpdb->posts
    2391                 WHERE post_date < '$thisyear-$thismonth-01'
    2392                 AND post_type = %s AND post_status = 'publish'
    2393                 ORDER BY post_date DESC
    2394                 LIMIT 1",
    2395                 $post_type
     2403        $previous = $wpdb->get_row(
     2404                $wpdb->prepare(
     2405                        "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
     2406                        FROM $wpdb->posts
     2407                        WHERE post_date < '%d-%d-01'
     2408                        AND post_type = %s AND post_status = 'publish'
     2409                        ORDER BY post_date DESC
     2410                        LIMIT 1",
     2411                        $thisyear,
     2412                        zeroise( $thismonth, 2 ),
     2413                        $post_type
     2414                )
    23962415        );
    2397         $previous                = $wpdb->get_row( $previous_prepared_query );
    2398 
    2399         $next_prepared_query = $wpdb->prepare(
    2400                 "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
    2401                 FROM $wpdb->posts
    2402                 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
    2403                 AND post_type = %s AND post_status = 'publish'
    2404                 ORDER BY post_date ASC
    2405                 LIMIT 1",
    2406                 $post_type
     2416
     2417        $next = $wpdb->get_row(
     2418                $wpdb->prepare(
     2419                        "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
     2420                        FROM $wpdb->posts
     2421                        WHERE post_date > '%d-%d-%d 23:59:59'
     2422                        AND post_type = %s AND post_status = 'publish'
     2423                        ORDER BY post_date ASC
     2424                        LIMIT 1",
     2425                        $thisyear,
     2426                        zeroise( $thismonth, 2 ),
     2427                        $last_day,
     2428                        $post_type
     2429                )
    24072430        );
    2408         $next                = $wpdb->get_row( $next_prepared_query );
    24092431
    24102432        /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */
     
    24402462
    24412463        // Get days with posts.
    2442         $dayswithposts_prepared_query = $wpdb->prepare(
    2443                 "SELECT DISTINCT DAYOFMONTH(post_date)
    2444                 FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
    2445                 AND post_type = %s AND post_status = 'publish'
    2446                 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'",
    2447                 $post_type
     2464        $dayswithposts = $wpdb->get_results(
     2465                $wpdb->prepare(
     2466                        "SELECT DISTINCT DAYOFMONTH(post_date)
     2467                        FROM $wpdb->posts WHERE post_date >= '%d-%d-01 00:00:00'
     2468                        AND post_type = %s AND post_status = 'publish'
     2469                        AND post_date <= '%d-%d-%d 23:59:59'",
     2470                        $thisyear,
     2471                        zeroise( $thismonth, 2 ),
     2472                        $post_type,
     2473                        $thisyear,
     2474                        zeroise( $thismonth, 2 ),
     2475                        $last_day
     2476                ),
     2477                ARRAY_N
    24482478        );
    2449         $dayswithposts                = $wpdb->get_results( $dayswithposts_prepared_query, ARRAY_N );
    24502479
    24512480        if ( $dayswithposts ) {
     
    24572486        // See how much we should pad in the beginning.
    24582487        $pad = calendar_week_mod( (int) gmdate( 'w', $unixmonth ) - $week_begins );
    2459         if ( 0 != $pad ) {
     2488        if ( $pad > 0 ) {
    24602489                $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad">&nbsp;</td>';
    24612490        }
     
    24702499                $newrow = false;
    24712500
    2472                 if ( current_time( 'j' ) == $day &&
    2473                         current_time( 'm' ) == $thismonth &&
    2474                         current_time( 'Y' ) == $thisyear ) {
     2501                if ( (int) current_time( 'j' ) === $day &&
     2502                        (int) current_time( 'm' ) === $thismonth &&
     2503                        (int) current_time( 'Y' ) === $thisyear ) {
    24752504                        $calendar_output .= '<td id="today">';
    24762505                } else {
     
    24952524                $calendar_output .= '</td>';
    24962525
    2497                 if ( 6 == calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
     2526                if ( 6 === (int) calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
    24982527                        $newrow = true;
    24992528                }
     
    25012530
    25022531        $pad = 7 - calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins );
    2503         if ( 0 != $pad && 7 != $pad ) {
     2532        if ( 0 < $pad && $pad < 7 ) {
    25042533                $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '">&nbsp;</td>';
    25052534        }
     
    25122541
    25132542        if ( $previous ) {
    2514                 $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">&laquo; ' .
    2515                         $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
    2516                 '</a></span>';
     2543                $calendar_output .= "\n\t\t" . sprintf(
     2544                        '<span class="wp-calendar-nav-prev"><a href="%1$s">&laquo; %2$s</a></span>',
     2545                        get_month_link( $previous->year, $previous->month ),
     2546                        $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) )
     2547                );
    25172548        } else {
    25182549                $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev">&nbsp;</span>';
     
    25222553
    25232554        if ( $next ) {
    2524                 $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
    2525                         $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
    2526                 ' &raquo;</a></span>';
     2555                $calendar_output .= "\n\t\t" . sprintf(
     2556                        '<span class="wp-calendar-nav-next"><a href="%1$s">%2$s &raquo;</a></span>',
     2557                        get_month_link( $next->year, $next->month ),
     2558                        $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) )
     2559                );
    25272560        } else {
    25282561                $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next">&nbsp;</span>';
Note: See TracChangeset for help on using the changeset viewer.