Ticket #16173: 16173.patch

File 16173.patch, 10.9 KB (added by sushkov, 21 months ago)

3rd take

  • 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) { 
    10701070 * @since 1.0.0 
    10711071 * 
    10721072 * @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 
    10741074 */ 
    1075 function get_calendar($initial = true, $echo = true) { 
     1075function get_calendar( $initial = true, $args = true ) { 
    10761076        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         
    10781095        $cache = array(); 
    10791096        $key = md5( $m . $monthnum . $year ); 
    1080         if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) { 
     1097        if ( $cache = wp_cache_get( $cal_name, 'calendar' ) ) { 
    10811098                if ( is_array($cache) && isset( $cache[ $key ] ) ) { 
    10821099                        if ( $echo ) { 
    1083                                 echo apply_filters( 'get_calendar',  $cache[$key] ); 
     1100                                echo apply_filters( $cal_name,  $cache[$key] ); 
    10841101                                return; 
    10851102                        } else { 
    1086                                 return apply_filters( 'get_calendar',  $cache[$key] ); 
     1103                                return apply_filters( $cal_name,  $cache[$key] ); 
    10871104                        } 
    10881105                } 
    10891106        } 
    1090  
     1107         
    10911108        if ( !is_array($cache) ) 
    10921109                $cache = array(); 
    1093  
     1110         
    10941111        // Quick check. If we have no posts at all, abort! 
    10951112        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                ); 
    10971119                if ( !$gotsome ) { 
    10981120                        $cache[ $key ] = ''; 
    1099                         wp_cache_set( 'get_calendar', $cache, 'calendar' ); 
     1121                        wp_cache_set( $cal_name, $cache, 'calendar' ); 
    11001122                        return; 
    11011123                } 
    11021124        } 
    1103  
     1125         
    11041126        if ( isset($_GET['w']) ) 
    11051127                $w = ''.intval($_GET['w']); 
    1106  
     1128         
    11071129        // week_begins = 0 stands for Sunday 
    11081130        $week_begins = intval(get_option('start_of_week')); 
    1109  
     1131         
    11101132        // Let's figure out when we are 
    11111133        if ( !empty($monthnum) && !empty($year) ) { 
    11121134                $thismonth = ''.zeroise(intval($monthnum), 2); 
    function get_calendar($initial = true, $echo = true) { 
    11151137                // We need to get the month from MySQL 
    11161138                $thisyear = ''.intval(substr($m, 0, 4)); 
    11171139                $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                ); 
    11191146        } elseif ( !empty($m) ) { 
    11201147                $thisyear = ''.intval(substr($m, 0, 4)); 
    11211148                if ( strlen($m) < 6 ) 
    function get_calendar($initial = true, $echo = true) { 
    11261153                $thisyear = gmdate('Y', current_time('timestamp')); 
    11271154                $thismonth = gmdate('m', current_time('timestamp')); 
    11281155        } 
    1129  
     1156         
    11301157        $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); 
    11311158        $last_day = date('t', $unixmonth); 
    1132  
     1159         
    11331160        // 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         
    11471179        /* translators: Calendar caption: 1: month name, 2: 4-digit year */ 
    11481180        $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 . '"> 
    11501182        <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption> 
    11511183        <thead> 
    11521184        <tr>'; 
    1153  
     1185         
    11541186        $myweek = array(); 
    1155  
     1187         
    11561188        for ( $wdcount=0; $wdcount<=6; $wdcount++ ) { 
    11571189                $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7); 
    11581190        } 
    1159  
     1191         
    11601192        foreach ( $myweek as $wd ) { 
    11611193                $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd); 
    11621194                $wd = esc_attr($wd); 
    11631195                $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; 
    11641196        } 
    1165  
     1197         
    11661198        $calendar_output .= ' 
    11671199        </tr> 
    11681200        </thead> 
    1169  
     1201         
    11701202        <tfoot> 
    11711203        <tr>'; 
    1172  
     1204         
    11731205        if ( $previous ) { 
    11741206                $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)))) . '">&laquo; ' . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . '</a></td>'; 
    11751207        } else { 
    11761208                $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>'; 
    11771209        } 
    1178  
     1210         
    11791211        $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>'; 
    1180  
     1212         
    11811213        if ( $next ) { 
    11821214                $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)) . ' &raquo;</a></td>'; 
    11831215        } else { 
    11841216                $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>'; 
    11851217        } 
    1186  
     1218         
    11871219        $calendar_output .= ' 
    11881220        </tr> 
    11891221        </tfoot> 
    1190  
     1222         
    11911223        <tbody> 
    11921224        <tr>'; 
    1193  
     1225         
    11941226        // 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        ); 
    11991238        if ( $dayswithposts ) { 
    12001239                foreach ( (array) $dayswithposts as $daywith ) { 
    12011240                        $daywithpost[] = $daywith[0]; 
    function get_calendar($initial = true, $echo = true) { 
    12031242        } else { 
    12041243                $daywithpost = array(); 
    12051244        } 
    1206  
     1245         
    12071246        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false) 
    12081247                $ak_title_separator = "\n"; 
    12091248        else 
    12101249                $ak_title_separator = ', '; 
    1211  
     1250         
    12121251        $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                ) 
    12181263        ); 
    12191264        if ( $ak_post_titles ) { 
    12201265                foreach ( (array) $ak_post_titles as $ak_post_title ) { 
    1221  
    12221266                                $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); 
    1223  
     1267                                 
    12241268                                if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) ) 
    12251269                                        $ak_titles_for_day['day_'.$ak_post_title->dom] = ''; 
    12261270                                if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one 
    function get_calendar($initial = true, $echo = true) { 
    12291273                                        $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title; 
    12301274                } 
    12311275        } 
    1232  
    1233  
     1276         
     1277         
    12341278        // See how much we should pad in the beginning 
    12351279        $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins); 
    12361280        if ( 0 != $pad ) 
    12371281                $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>'; 
    1238  
     1282         
    12391283        $daysinmonth = intval(date('t', $unixmonth)); 
    12401284        for ( $day = 1; $day <= $daysinmonth; ++$day ) { 
    12411285                if ( isset($newrow) && $newrow ) 
    12421286                        $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t"; 
    12431287                $newrow = false; 
    1244  
     1288                 
    12451289                if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) 
    12461290                        $calendar_output .= '<td id="today">'; 
    12471291                else 
    12481292                        $calendar_output .= '<td>'; 
    1249  
     1293                 
    12501294                if ( in_array($day, $daywithpost) ) // any posts today? 
    12511295                                $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; 
    12521296                else 
    12531297                        $calendar_output .= $day; 
    12541298                $calendar_output .= '</td>'; 
    1255  
     1299                 
    12561300                if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) ) 
    12571301                        $newrow = true; 
    12581302        } 
    1259  
     1303         
    12601304        $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins); 
    12611305        if ( $pad != 0 && $pad != 7 ) 
    12621306                $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>'; 
    1263  
     1307         
    12641308        $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>"; 
    1265  
     1309         
    12661310        $cache[ $key ] = $calendar_output; 
    1267         wp_cache_set( 'get_calendar', $cache, 'calendar' ); 
    1268  
     1311        wp_cache_set( $cal_name, $cache, 'calendar' ); 
     1312         
    12691313        if ( $echo ) 
    1270                 echo apply_filters( 'get_calendar',  $calendar_output ); 
     1314                echo apply_filters( $cal_name,  $calendar_output ); 
    12711315        else 
    1272                 return apply_filters( 'get_calendar',  $calendar_output ); 
    1273  
     1316                return apply_filters( $cal_name,  $calendar_output ); 
    12741317} 
    12751318 
    12761319/**