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