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

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

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