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