PluginProbe
Polylang / 0.5
Polylang v0.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / calendar.php

calendar.php in Polylang 0.5, at include/calendar.php

234 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // obliged to rewrite the whole WordPress function as there is no filter on sql queries and only a filter on final output
3 // a request for making a filter on sql queries exists: http://core.trac.wordpress.org/ticket/15202
4 // method used in 0.4.x: use of the get_calendar filter and overwrite the output of get_calendar function -> not very efficient (add 4 to 5 sql queries)
5 // since 0.5: remove the WP widget and replace it by our own -> our language filter will not work if get_calendar is called directly by a theme
6
7 if(!class_exists('WP_Widget_Calendar')){
8 require_once( ABSPATH . '/wp-includes/default-widgets.php' );
9 }
10
11 class Polylang_Widget_Calendar extends WP_Widget_Calendar {
12
13 function widget( $args, $instance ) {
14 extract($args);
15 $title = apply_filters('widget_title', empty($instance['title']) ? '&nbsp;' : $instance['title'], $instance, $this->id_base);
16 echo $before_widget;
17 if ( $title )
18 echo $before_title . $title . $after_title;
19 echo '<div id="calendar_wrap">';
20 $this->get_calendar(); #modified#
21 echo '</div>';
22 echo $after_widget;
23 }
24
25 // modified version of WP get_calendar function to filter the query
26 function get_calendar($initial = true, $echo = true) {
27 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
28 global $polylang; #added#
29 $lang = $polylang->get_current_language()->term_taxonomy_id; #added#
30
31 $cache = array();
32 $key = md5( $lang . $m . $monthnum . $year ); #modified#
33 if ( $cache = wp_cache_get( 'get_calendar', 'calendar' ) ) {
34 if ( is_array($cache) && isset( $cache[ $key ] ) ) {
35 if ( $echo ) {
36 echo apply_filters( 'get_calendar', $cache[$key] );
37 return;
38 } else {
39 return apply_filters( 'get_calendar', $cache[$key] );
40 }
41 }
42 }
43
44 if ( !is_array($cache) )
45 $cache = array();
46
47 // Quick check. If we have no posts at all, abort!
48 if ( !$posts ) {
49 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
50 if ( !$gotsome ) {
51 $cache[ $key ] = '';
52 wp_cache_set( 'get_calendar', $cache, 'calendar' );
53 return;
54 }
55 }
56
57 if ( isset($_GET['w']) )
58 $w = ''.intval($_GET['w']);
59
60 // week_begins = 0 stands for Sunday
61 $week_begins = intval(get_option('start_of_week'));
62
63 // Let's figure out when we are
64 if ( !empty($monthnum) && !empty($year) ) {
65 $thismonth = ''.zeroise(intval($monthnum), 2);
66 $thisyear = ''.intval($year);
67 } elseif ( !empty($w) ) {
68 // We need to get the month from MySQL
69 $thisyear = ''.intval(substr($m, 0, 4));
70 $d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's
71 $thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')");
72 } elseif ( !empty($m) ) {
73 $thisyear = ''.intval(substr($m, 0, 4));
74 if ( strlen($m) < 6 )
75 $thismonth = '01';
76 else
77 $thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2);
78 } else {
79 $thisyear = gmdate('Y', current_time('timestamp'));
80 $thismonth = gmdate('m', current_time('timestamp'));
81 }
82
83 $unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
84 $last_day = date('t', $unixmonth);
85
86 // Get the next and previous month and year with at least one post
87 $previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
88 FROM $wpdb->posts
89 INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID
90 WHERE post_date < '$thisyear-$thismonth-01'
91 AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang
92 ORDER BY post_date DESC
93 LIMIT 1"); #modified#
94 $next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
95 FROM $wpdb->posts
96 INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID
97 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
98 AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang
99 ORDER BY post_date ASC
100 LIMIT 1"); #modified#
101
102 /* translators: Calendar caption: 1: month name, 2: 4-digit year */
103 $calendar_caption = _x('%1$s %2$s', 'calendar caption');
104 $calendar_output = '<table id="wp-calendar">
105 <caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>
106 <thead>
107 <tr>';
108
109 $myweek = array();
110
111 for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
112 $myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
113 }
114
115 foreach ( $myweek as $wd ) {
116 $day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
117 $wd = esc_attr($wd);
118 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
119 }
120
121 $calendar_output .= '
122 </tr>
123 </thead>
124
125 <tfoot>
126 <tr>';
127
128 if ( $previous ) {
129 $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>';
130 } else {
131 $calendar_output .= "\n\t\t".'<td colspan="3" id="prev" class="pad">&nbsp;</td>';
132 }
133
134 $calendar_output .= "\n\t\t".'<td class="pad">&nbsp;</td>';
135
136 if ( $next ) {
137 $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>';
138 } else {
139 $calendar_output .= "\n\t\t".'<td colspan="3" id="next" class="pad">&nbsp;</td>';
140 }
141
142 $calendar_output .= '
143 </tr>
144 </tfoot>
145
146 <tbody>
147 <tr>';
148
149 // Get days with posts
150 $dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
151 FROM $wpdb->posts
152 INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID
153 WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00'
154 AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang
155 AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59'", ARRAY_N); #modified#
156 if ( $dayswithposts ) {
157 foreach ( (array) $dayswithposts as $daywith ) {
158 $daywithpost[] = $daywith[0];
159 }
160 } else {
161 $daywithpost = array();
162 }
163
164 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'camino') !== false || stripos($_SERVER['HTTP_USER_AGENT'], 'safari') !== false)
165 $ak_title_separator = "\n";
166 else
167 $ak_title_separator = ', ';
168
169 $ak_titles_for_day = array();
170 $ak_post_titles = $wpdb->get_results("SELECT ID, post_title, DAYOFMONTH(post_date) as dom "
171 ."FROM $wpdb->posts "
172 ."INNER JOIN $wpdb->term_relationships AS tr ON tr.object_id = ID "
173 ."WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' "
174 ."AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' "
175 ."AND post_type = 'post' AND post_status = 'publish' AND tr.term_taxonomy_id = $lang"
176 ); #modified#
177 if ( $ak_post_titles ) {
178 foreach ( (array) $ak_post_titles as $ak_post_title ) {
179
180 $post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) );
181
182 if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
183 $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
184 if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
185 $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
186 else
187 $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
188 }
189 }
190
191 // See how much we should pad in the beginning
192 $pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
193 if ( 0 != $pad )
194 $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr($pad) .'" class="pad">&nbsp;</td>';
195
196 $daysinmonth = intval(date('t', $unixmonth));
197 for ( $day = 1; $day <= $daysinmonth; ++$day ) {
198 if ( isset($newrow) && $newrow )
199 $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
200 $newrow = false;
201
202 if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) )
203 $calendar_output .= '<td id="today">';
204 else
205 $calendar_output .= '<td>';
206
207 if ( in_array($day, $daywithpost) ) // any posts today?
208 $calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>";
209 else
210 $calendar_output .= $day;
211 $calendar_output .= '</td>';
212
213 if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
214 $newrow = true;
215 }
216
217 $pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
218 if ( $pad != 0 && $pad != 7 )
219 $calendar_output .= "\n\t\t".'<td class="pad" colspan="'. esc_attr($pad) .'">&nbsp;</td>';
220
221 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
222
223 $cache[ $key ] = $calendar_output;
224 wp_cache_set( 'get_calendar', $cache, 'calendar' );
225
226 if ( $echo )
227 echo apply_filters( 'get_calendar', $calendar_output );
228 else
229 return apply_filters( 'get_calendar', $calendar_output );
230 }
231 }
232
233 ?>
234