<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[59947] trunk/src/wp-includes/general-template.php: Coding Standards: Fix WPCS issues in `get_calendar()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/59947">59947</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/59947","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2025-03-06 21:19:10 +0000 (Thu, 06 Mar 2025)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/changeset/508">[508]</a>, <a href="https://core.trac.wordpress.org/changeset/509">[509]</a>, <a href="https://core.trac.wordpress.org/changeset/510">[510]</a>, <a href="https://core.trac.wordpress.org/changeset/716">[716]</a>, <a href="https://core.trac.wordpress.org/changeset/933">[933]</a>, <a href="https://core.trac.wordpress.org/changeset/12590">[12590]</a>, <a href="https://core.trac.wordpress.org/changeset/34463">[34463]</a>, <a href="https://core.trac.wordpress.org/changeset/44809">[44809]</a>, <a href="https://core.trac.wordpress.org/changeset/47223">[47223]</a>, <a href="https://core.trac.wordpress.org/changeset/59908">[59908]</a>.
See <a href="https://core.trac.wordpress.org/ticket/62279">#62279</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesgeneraltemplatephp">trunk/src/wp-includes/general-template.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesgeneraltemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/general-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/general-template.php 2025-03-06 15:34:28 UTC (rev 59946)
+++ trunk/src/wp-includes/general-template.php 2025-03-06 21:19:10 UTC (rev 59947)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2347,8 +2347,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Quick check. If we have no posts at all, abort!
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! $posts ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type );
- $gotsome = $wpdb->get_var( $prepared_query );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $gotsome = $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT 1 as test
+ FROM $wpdb->posts
+ WHERE post_type = %s
+ AND post_status = 'publish'
+ LIMIT 1",
+ $post_type
+ )
+ );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $gotsome ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $cache[ $key ] = '';
</span><span class="cx" style="display: block; padding: 0 10px"> wp_cache_set( 'get_calendar', $cache, 'calendar' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2361,7 +2370,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Let's figure out when we are.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! empty( $monthnum ) && ! empty( $year ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $thismonth = zeroise( (int) $monthnum, 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thismonth = (int) $monthnum;
</ins><span class="cx" style="display: block; padding: 0 10px"> $thisyear = (int) $year;
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( ! empty( $w ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // We need to get the month from MySQL.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2368,17 +2377,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $thisyear = (int) substr( $m, 0, 4 );
</span><span class="cx" style="display: block; padding: 0 10px"> // It seems MySQL's weeks disagree with PHP's.
</span><span class="cx" style="display: block; padding: 0 10px"> $d = ( ( $w - 1 ) * 7 ) + 6;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thismonth = (int) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT DATE_FORMAT((DATE_ADD('%d0101', INTERVAL %d DAY) ), '%%m')",
+ $thisyear,
+ $d
+ )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> } elseif ( ! empty( $m ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $thisyear = (int) substr( $m, 0, 4 );
</span><span class="cx" style="display: block; padding: 0 10px"> if ( strlen( $m ) < 6 ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $thismonth = '01';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thismonth = 1;
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thismonth = (int) substr( $m, 4, 2 );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $thisyear = current_time( 'Y' );
- $thismonth = current_time( 'm' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $thisyear = (int) current_time( 'Y' );
+ $thismonth = (int) current_time( 'm' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2385,27 +2400,34 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $last_day = gmdate( 't', $unixmonth );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Get the next and previous month and year with at least one post.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $previous_prepared_query = $wpdb->prepare(
- "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
- FROM $wpdb->posts
- WHERE post_date < '$thisyear-$thismonth-01'
- AND post_type = %s AND post_status = 'publish'
- ORDER BY post_date DESC
- LIMIT 1",
- $post_type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $previous = $wpdb->get_row(
+ $wpdb->prepare(
+ "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
+ FROM $wpdb->posts
+ WHERE post_date < '%d-%d-01'
+ AND post_type = %s AND post_status = 'publish'
+ ORDER BY post_date DESC
+ LIMIT 1",
+ $thisyear,
+ zeroise( $thismonth, 2 ),
+ $post_type
+ )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $previous = $wpdb->get_row( $previous_prepared_query );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $next_prepared_query = $wpdb->prepare(
- "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
- FROM $wpdb->posts
- WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
- AND post_type = %s AND post_status = 'publish'
- ORDER BY post_date ASC
- LIMIT 1",
- $post_type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $next = $wpdb->get_row(
+ $wpdb->prepare(
+ "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
+ FROM $wpdb->posts
+ WHERE post_date > '%d-%d-%d 23:59:59'
+ AND post_type = %s AND post_status = 'publish'
+ ORDER BY post_date ASC
+ LIMIT 1",
+ $thisyear,
+ zeroise( $thismonth, 2 ),
+ $last_day,
+ $post_type
+ )
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $next = $wpdb->get_row( $next_prepared_query );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2439,14 +2461,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $daywithpost = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Get days with posts.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $dayswithposts_prepared_query = $wpdb->prepare(
- "SELECT DISTINCT DAYOFMONTH(post_date)
- FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
- AND post_type = %s AND post_status = 'publish'
- AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'",
- $post_type
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $dayswithposts = $wpdb->get_results(
+ $wpdb->prepare(
+ "SELECT DISTINCT DAYOFMONTH(post_date)
+ FROM $wpdb->posts WHERE post_date >= '%d-%d-01 00:00:00'
+ AND post_type = %s AND post_status = 'publish'
+ AND post_date <= '%d-%d-%d 23:59:59'",
+ $thisyear,
+ zeroise( $thismonth, 2 ),
+ $post_type,
+ $thisyear,
+ zeroise( $thismonth, 2 ),
+ $last_day
+ ),
+ ARRAY_N
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query, ARRAY_N );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $dayswithposts ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $dayswithposts as $daywith ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2456,7 +2485,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // See how much we should pad in the beginning.
</span><span class="cx" style="display: block; padding: 0 10px"> $pad = calendar_week_mod( (int) gmdate( 'w', $unixmonth ) - $week_begins );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 0 != $pad ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $pad > 0 ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad"> </td>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2469,9 +2498,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> $newrow = false;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( current_time( 'j' ) == $day &&
- current_time( 'm' ) == $thismonth &&
- current_time( 'Y' ) == $thisyear ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( (int) current_time( 'j' ) === $day &&
+ (int) current_time( 'm' ) === $thismonth &&
+ (int) current_time( 'Y' ) === $thisyear ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= '<td id="today">';
</span><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= '<td>';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2494,13 +2523,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= '</td>';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 6 == calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 6 === (int) calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $newrow = true;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $pad = 7 - calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 0 != $pad && 7 != $pad ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( 0 < $pad && $pad < 7 ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '"> </td>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2511,9 +2540,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= '<nav aria-label="' . __( 'Previous and next months' ) . '" class="wp-calendar-nav">';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $previous ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '">« ' .
- $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) .
- '</a></span>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $calendar_output .= "\n\t\t" . sprintf(
+ '<span class="wp-calendar-nav-prev"><a href="%1$s">« %2$s</a></span>',
+ get_month_link( $previous->year, $previous->month ),
+ $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-prev"> </span>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2521,9 +2552,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= "\n\t\t" . '<span class="pad"> </span>';
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $next ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="' . get_month_link( $next->year, $next->month ) . '">' .
- $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) .
- ' »</a></span>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $calendar_output .= "\n\t\t" . sprintf(
+ '<span class="wp-calendar-nav-next"><a href="%1$s">%2$s »</a></span>',
+ get_month_link( $next->year, $next->month ),
+ $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) )
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> } else {
</span><span class="cx" style="display: block; padding: 0 10px"> $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"> </span>';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>