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