diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index 3b0875f876..cf6eac7c81 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -2347,8 +2347,17 @@ function get_calendar( $args = array() ) { // Quick check. If we have no posts at all, abort! if ( ! $posts ) { - $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 ); + $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 + ) + ); + if ( ! $gotsome ) { $cache[ $key ] = ''; wp_cache_set( 'get_calendar', $cache, 'calendar' ); @@ -2361,51 +2370,64 @@ function get_calendar( $args = array() ) { // Let's figure out when we are. if ( ! empty( $monthnum ) && ! empty( $year ) ) { - $thismonth = zeroise( (int) $monthnum, 2 ); + $thismonth = (int) $monthnum; $thisyear = (int) $year; } elseif ( ! empty( $w ) ) { // We need to get the month from MySQL. $thisyear = (int) substr( $m, 0, 4 ); // It seems MySQL's weeks disagree with PHP's. $d = ( ( $w - 1 ) * 7 ) + 6; - $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')" ); + $thismonth = (int) $wpdb->get_var( + $wpdb->prepare( + "SELECT DATE_FORMAT((DATE_ADD('%d0101', INTERVAL %d DAY) ), '%%m')", + $thisyear, + $d + ) + ); } elseif ( ! empty( $m ) ) { $thisyear = (int) substr( $m, 0, 4 ); if ( strlen( $m ) < 6 ) { - $thismonth = '01'; + $thismonth = 1; } else { - $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 ); + $thismonth = (int) substr( $m, 4, 2 ); } } else { - $thisyear = current_time( 'Y' ); - $thismonth = current_time( 'm' ); + $thisyear = (int) current_time( 'Y' ); + $thismonth = (int) current_time( 'm' ); } $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear ); $last_day = gmdate( 't', $unixmonth ); // Get the next and previous month and year with at least one post. - $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 + $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 + ) ); - $previous = $wpdb->get_row( $previous_prepared_query ); - $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 + $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 + ) ); - $next = $wpdb->get_row( $next_prepared_query ); /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */ $calendar_caption = _x( '%1$s %2$s', 'calendar caption' ); @@ -2439,14 +2461,21 @@ function get_calendar( $args = array() ) { $daywithpost = array(); // Get days with posts. - $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 + $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 ); - $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query, ARRAY_N ); if ( $dayswithposts ) { foreach ( (array) $dayswithposts as $daywith ) { @@ -2456,7 +2485,7 @@ function get_calendar( $args = array() ) { // See how much we should pad in the beginning. $pad = calendar_week_mod( (int) gmdate( 'w', $unixmonth ) - $week_begins ); - if ( 0 != $pad ) { + if ( $pad > 0 ) { $calendar_output .= "\n\t\t" . '