Ticket #16173: 16173.patch
File 16173.patch, 10.9 KB (added by , 13 years ago) |
---|
-
wp-includes/general-template.php
diff --git wp-includes/general-template.php wp-includes/general-template.php index 21e54c3..457b9d6 100644
function calendar_week_mod($num) { 1070 1070 * @since 1.0.0 1071 1071 * 1072 1072 * @param bool $initial Optional, default is true. Use initial calendar names. 1073 * @param bool $echo Optional, default is true. Set to false for return.1073 * @param Mixed $args Optional, check the $defaults inside function body 1074 1074 */ 1075 function get_calendar( $initial = true, $echo = true) {1075 function get_calendar( $initial = true, $args = true ) { 1076 1076 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; 1077 1077 1078 $defaults = array( 1079 'echo' => true, 1080 'post_type' => 'post', 1081 'post_status' => 'publish' 1082 ); 1083 1084 // Backcompat support 1085 if ( is_bool( $args ) ) 1086 $echo = $args; 1087 elseif ( is_array( $args ) ) { 1088 $cal_args = wp_parse_args( $args, $defaults ); 1089 extract( $cal_args, EXTR_SKIP ); 1090 } 1091 1092 // Generate a cache/hooks name like `get_calendar-post_type` preserving backcompat 1093 $cal_name = 'get_calendar' . ( $post_type != 'post' ) ? '_' . $post_type : ''; 1094 1078 1095 $cache = array(); 1079 1096 $key = md5( $m . $monthnum . $year ); 1080 if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {1097 if ( $cache = wp_cache_get( $cal_name, 'calendar' ) ) { 1081 1098 if ( is_array($cache) && isset( $cache[ $key ] ) ) { 1082 1099 if ( $echo ) { 1083 echo apply_filters( 'get_calendar', $cache[$key] );1100 echo apply_filters( $cal_name, $cache[$key] ); 1084 1101 return; 1085 1102 } else { 1086 return apply_filters( 'get_calendar', $cache[$key] );1103 return apply_filters( $cal_name, $cache[$key] ); 1087 1104 } 1088 1105 } 1089 1106 } 1090 1107 1091 1108 if ( !is_array($cache) ) 1092 1109 $cache = array(); 1093 1110 1094 1111 // Quick check. If we have no posts at all, abort! 1095 1112 if ( !$posts ) { 1096 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1"); 1113 $gotsome = $wpdb->get_var( 1114 $wpdb->prepare( 1115 "SELECT 1 as test FROM `$wpdb->posts` WHERE `post_type` = %s AND `post_status` = %s LIMIT 1", 1116 $post_type, $post_status 1117 ) 1118 ); 1097 1119 if ( !$gotsome ) { 1098 1120 $cache[ $key ] = ''; 1099 wp_cache_set( 'get_calendar', $cache, 'calendar' );1121 wp_cache_set( $cal_name, $cache, 'calendar' ); 1100 1122 return; 1101 1123 } 1102 1124 } 1103 1125 1104 1126 if ( isset($_GET['w']) ) 1105 1127 $w = ''.intval($_GET['w']); 1106 1128 1107 1129 // week_begins = 0 stands for Sunday 1108 1130 $week_begins = intval(get_option('start_of_week')); 1109 1131 1110 1132 // Let's figure out when we are 1111 1133 if ( !empty($monthnum) && !empty($year) ) { 1112 1134 $thismonth = ''.zeroise(intval($monthnum), 2); … … function get_calendar($initial = true, $echo = true) { 1115 1137 // We need to get the month from MySQL 1116 1138 $thisyear = ''.intval(substr($m, 0, 4)); 1117 1139 $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's 1118 $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); 1140 $thismonth = $wpdb->get_var( 1141 $wpdb->prepare( 1142 "SELECT DATE_FORMAT((DATE_ADD(%s, INTERVAL %d DAY) ), '%m')", 1143 $thisyear . '0101', $d 1144 ) 1145 ); 1119 1146 } elseif ( !empty($m) ) { 1120 1147 $thisyear = ''.intval(substr($m, 0, 4)); 1121 1148 if ( strlen($m) < 6 ) … … function get_calendar($initial = true, $echo = true) { 1126 1153 $thisyear = gmdate('Y', current_time('timestamp')); 1127 1154 $thismonth = gmdate('m', current_time('timestamp')); 1128 1155 } 1129 1156 1130 1157 $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); 1131 1158 $last_day = date('t', $unixmonth); 1132 1159 1133 1160 // Get the next and previous month and year with at least one post 1134 $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1135 FROM $wpdb->posts 1136 WHERE post_date < '$thisyear-$thismonth-01' 1137 AND post_type = 'post' AND post_status = 'publish' 1138 ORDER BY post_date DESC 1139 LIMIT 1"); 1140 $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year 1141 FROM $wpdb->posts 1142 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' 1143 AND post_type = 'post' AND post_status = 'publish' 1144 ORDER BY post_date ASC 1145 LIMIT 1"); 1146 1161 $previous = $wpdb->get_row( 1162 $wpdb->prepare( 1163 "SELECT MONTH(post_date) AS `month`, YEAR(post_date) AS `year` 1164 FROM $wpdb->posts WHERE `post_date` < %s 1165 AND `post_type` = %s AND `post_status` = %s 1166 ORDER BY `post_date` DESC LIMIT 1", 1167 $thisyear . '-' . $thismonth . '-01', $post_type, $post_status 1168 ) 1169 ); 1170 $next = $wpdb->get_row( 1171 $wpdb->prepare( 1172 "SELECT MONTH(post_date) AS `month`, YEAR(post_date) AS `year` 1173 FROM $wpdb->posts WHERE `post_date` > %s 1174 AND `post_type` = %s AND `post_status` = %s ORDER BY `post_date` ASC LIMIT 1", 1175 $thisyear . '-' . $thismonth . '-' . $last_day . '23:59:59', $post_type, $post_status 1176 ) 1177 ); 1178 1147 1179 /* translators: Calendar caption: 1: month name, 2: 4-digit year */ 1148 1180 $calendar_caption = _x('%1$s %2$s', 'calendar caption'); 1149 $calendar_output = '<table id="wp-calendar" >1181 $calendar_output = '<table id="wp-calendar" class="' . $post_type . '"> 1150 1182 <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> 1151 1183 <thead> 1152 1184 <tr>'; 1153 1185 1154 1186 $myweek = array(); 1155 1187 1156 1188 for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { 1157 1189 $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); 1158 1190 } 1159 1191 1160 1192 foreach ( $myweek as $wd ) { 1161 1193 $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); 1162 1194 $wd = esc_attr($wd); 1163 1195 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 1164 1196 } 1165 1197 1166 1198 $calendar_output .= ' 1167 1199 </tr> 1168 1200 </thead> 1169 1201 1170 1202 <tfoot> 1171 1203 <tr>'; 1172 1204 1173 1205 if ( $previous ) { 1174 1206 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev"><a href="' . get_month_link($previous->year, $previous->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($previous->month), date('Y', mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . '">« ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; 1175 1207 } else { 1176 1208 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad"> </td>'; 1177 1209 } 1178 1210 1179 1211 $calendar_output .= "\n\t\t".'<td class="pad"> </td>'; 1180 1212 1181 1213 if ( $next ) { 1182 1214 $calendar_output .= "\n\t\t".'<td colspan="3" id="next"><a href="' . get_month_link($next->year, $next->month) . '" title="' . esc_attr( sprintf(__('View posts for %1$s %2$s'), $wp_locale->get_month($next->month), date('Y', mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . '">' . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ' »</a></td>'; 1183 1215 } else { 1184 1216 $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad"> </td>'; 1185 1217 } 1186 1218 1187 1219 $calendar_output .= ' 1188 1220 </tr> 1189 1221 </tfoot> 1190 1222 1191 1223 <tbody> 1192 1224 <tr>'; 1193 1225 1194 1226 // Get days with posts 1195 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date) 1196 FROM $wpdb->posts WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' 1197 AND post_type = 'post' AND post_status = 'publish' 1198 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); 1227 $dayswithposts = $wpdb->get_results( 1228 $wpdb->prepare( 1229 "SELECT DISTINCT DAYOFMONTH(post_date) 1230 FROM $wpdb->posts WHERE `post_date` >= %s 1231 AND `post_type` = %s AND post_status = %s 1232 AND `post_date` <= %s", 1233 $thisyear . '-' . $thismonth . '-01 00:00:00', 1234 $post_type, $post_status, 1235 $thisyear . '-' . $thismonth . '-' . $last_day . ' 23:59:59' 1236 ), ARRAY_N 1237 ); 1199 1238 if ( $dayswithposts ) { 1200 1239 foreach ( (array) $dayswithposts as $daywith ) { 1201 1240 $daywithpost[] = $daywith[0]; … … function get_calendar($initial = true, $echo = true) { 1203 1242 } else { 1204 1243 $daywithpost = array(); 1205 1244 } 1206 1245 1207 1246 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) 1208 1247 $ak_title_separator = "\n"; 1209 1248 else 1210 1249 $ak_title_separator = ', '; 1211 1250 1212 1251 $ak_titles_for_day = array(); 1213 $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom " 1214 ."FROM $wpdb->posts " 1215 ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' " 1216 ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' " 1217 ."AND post_type = 'post' AND post_status = 'publish'" 1252 $ak_post_titles = $wpdb->get_results( 1253 $wpdb->prepare( 1254 "SELECT `ID`, `post_title`, DAYOFMONTH(post_date) as `dom` 1255 FROM $wpdb->posts 1256 WHERE `post_date` >= %s 1257 AND `post_date` <= %s 1258 AND `post_type` = %s AND `post_status` = %s", 1259 $thisyear . '-' . $thismonth . '-01 00:00:00', 1260 $thisyear . '-' . $thismonth . '-' . $last_day . ' 23:59:59', 1261 $post_type, $post_status 1262 ) 1218 1263 ); 1219 1264 if ( $ak_post_titles ) { 1220 1265 foreach ( (array) $ak_post_titles as $ak_post_title ) { 1221 1222 1266 $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); 1223 1267 1224 1268 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) 1225 1269 $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; 1226 1270 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one … … function get_calendar($initial = true, $echo = true) { 1229 1273 $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; 1230 1274 } 1231 1275 } 1232 1233 1276 1277 1234 1278 // See how much we should pad in the beginning 1235 1279 $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); 1236 1280 if ( 0 != $pad ) 1237 1281 $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad"> </td>'; 1238 1282 1239 1283 $daysinmonth = intval(date('t', $unixmonth)); 1240 1284 for ( $day = 1; $day <= $daysinmonth; ++$day ) { 1241 1285 if ( isset($newrow) && $newrow ) 1242 1286 $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; 1243 1287 $newrow = false; 1244 1288 1245 1289 if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) 1246 1290 $calendar_output .= '<td id="today">'; 1247 1291 else 1248 1292 $calendar_output .= '<td>'; 1249 1293 1250 1294 if ( in_array($day, $daywithpost) ) // any posts today? 1251 1295 $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; 1252 1296 else 1253 1297 $calendar_output .= $day; 1254 1298 $calendar_output .= '</td>'; 1255 1299 1256 1300 if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) 1257 1301 $newrow = true; 1258 1302 } 1259 1303 1260 1304 $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); 1261 1305 if ( $pad != 0 && $pad != 7 ) 1262 1306 $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'"> </td>'; 1263 1307 1264 1308 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; 1265 1309 1266 1310 $cache[ $key ] = $calendar_output; 1267 wp_cache_set( 'get_calendar', $cache, 'calendar' );1268 1311 wp_cache_set( $cal_name, $cache, 'calendar' ); 1312 1269 1313 if ( $echo ) 1270 echo apply_filters( 'get_calendar', $calendar_output );1314 echo apply_filters( $cal_name, $calendar_output ); 1271 1315 else 1272 return apply_filters( 'get_calendar', $calendar_output ); 1273 1316 return apply_filters( $cal_name, $calendar_output ); 1274 1317 } 1275 1318 1276 1319 /**