PluginProbe
Polylang / 1.4.4
Polylang v1.4.4
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 / widget-calendar.php

widget-calendar.php in Polylang 1.4.4, at include/widget-calendar.php

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