PluginProbe
Polylang / 2.1
Polylang v2.1
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 2.1, at include/widget-calendar.php

266 lines 8.8 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 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 *
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 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
29 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '&nbsp;' : $instance['title'], $instance, $this->id_base );
30 echo $args['before_widget'];
31 if ( $title ) {
32 echo $args['before_title'] . $title . $args['after_title'];
33 }
34 echo '<div id="calendar_wrap">';
35 empty( PLL()->curlang ) ? get_calendar() : self::get_calendar(); #modified#
36 echo '</div>';
37 echo $args['after_widget'];
38 }
39
40 /**
41 * modified version of WP get_calendar function to filter the query
42 *
43 * @since 0.5
44 *
45 * @param bool $initial Optional, default is true. Use initial calendar names.
46 * @param bool $echo Optional, default is true. Set to false for return.
47 * @return string|null String when retrieving, null when displaying.
48 */
49 static function get_calendar( $initial = true, $echo = true ) {
50 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
51
52 $join_clause = PLL()->model->post->join_clause(); #added#
53 $where_clause = PLL()->model->post->where_clause( PLL()->curlang ); #added#
54
55 $key = md5( PLL()->curlang->slug . $m . $monthnum . $year ); #modified#
56 $cache = wp_cache_get( 'get_calendar', 'calendar' );
57
58 if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
59 /** This filter is documented in wp-includes/general-template.php */
60 $output = apply_filters( 'get_calendar', $cache[ $key ] );
61
62 if ( $echo ) {
63 echo $output;
64 return;
65 }
66
67 return $output;
68 }
69
70 if ( ! is_array( $cache ) ) {
71 $cache = array();
72 }
73
74 // Quick check. If we have no posts at all, abort!
75 if ( ! $posts ) {
76 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
77 if ( ! $gotsome ) {
78 $cache[ $key ] = '';
79 wp_cache_set( 'get_calendar', $cache, 'calendar' );
80 return;
81 }
82 }
83
84 if ( isset( $_GET['w'] ) ) {
85 $w = (int) $_GET['w'];
86 }
87 // week_begins = 0 stands for Sunday
88 $week_begins = (int) get_option( 'start_of_week' );
89 $ts = current_time( 'timestamp' );
90
91 // Let's figure out when we are
92 if ( ! empty( $monthnum ) && ! empty( $year ) ) {
93 $thismonth = zeroise( intval( $monthnum ), 2 );
94 $thisyear = (int) $year;
95 } elseif ( ! empty( $w ) ) {
96 // We need to get the month from MySQL
97 $thisyear = (int) substr( $m, 0, 4 );
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')");
101 } elseif ( ! empty( $m ) ) {
102 $thisyear = (int) substr( $m, 0, 4 );
103 if ( strlen( $m ) < 6 ) {
104 $thismonth = '01';
105 } else {
106 $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
107 }
108 } else {
109 $thisyear = gmdate( 'Y', $ts );
110 $thismonth = gmdate( 'm', $ts );
111 }
112
113 $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
114 $last_day = date( 't', $unixmonth );
115
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
118 FROM $wpdb->posts $join_clause
119 WHERE post_date < '$thisyear-$thismonth-01'
120 AND post_type = 'post' AND post_status = 'publish' $where_clause
121 ORDER BY post_date DESC
122 LIMIT 1" ); #modified#
123 $next = $wpdb->get_row( "SELECT MONTH( post_date ) AS month, YEAR( post_date ) AS year
124 FROM $wpdb->posts $join_clause
125 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
126 AND post_type = 'post' AND post_status = 'publish' $where_clause
127 ORDER BY post_date ASC
128 LIMIT 1" ); #modified#
129
130 /* translators: Calendar caption: 1: month name, 2: 4-digit year */
131 $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
132 $calendar_output = '<table id="wp-calendar">
133 <caption>' . sprintf(
134 $calendar_caption,
135 $wp_locale->get_month( $thismonth ),
136 date( 'Y', $unixmonth )
137 ) . '</caption>
138 <thead>
139 <tr>';
140
141 $myweek = array();
142
143 for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
144 $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
145 }
146
147 foreach ( $myweek as $wd ) {
148 $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
149 $wd = esc_attr( $wd );
150 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
151 }
152
153 $calendar_output .= '
154 </tr>
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
182 <tbody>
183 <tr>';
184
185 $daywithpost = array();
186
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#
193 if ( $dayswithposts ) {
194 foreach ( (array) $dayswithposts as $daywith ) {
195 $daywithpost[] = $daywith[0];
196 }
197 }
198
199 // See how much we should pad in the beginning
200 $pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins );
201 if ( 0 != $pad ) {
202 $calendar_output .= "\n\t\t".'<td colspan="'. esc_attr( $pad ) .'" class="pad">&nbsp;</td>';
203 }
204
205 $newrow = false;
206 $daysinmonth = (int) date( 't', $unixmonth );
207
208 for ( $day = 1; $day <= $daysinmonth; ++$day ) {
209 if ( isset($newrow) && $newrow ) {
210 $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
211 }
212 $newrow = false;
213
214 if ( $day == gmdate( 'j', $ts ) &&
215 $thismonth == gmdate( 'm', $ts ) &&
216 $thisyear == gmdate( 'Y', $ts ) ) {
217 $calendar_output .= '<td id="today">';
218 } else {
219 $calendar_output .= '<td>';
220 }
221
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 );
226 $calendar_output .= sprintf(
227 '<a href="%s" aria-label="%s">%s</a>',
228 get_day_link( $thisyear, $thismonth, $day ),
229 esc_attr( $label ),
230 $day
231 );
232 } else {
233 $calendar_output .= $day;
234 }
235 $calendar_output .= '</td>';
236
237 if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
238 $newrow = true;
239 }
240 }
241
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>';
245 }
246 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
247
248 $cache[ $key ] = $calendar_output;
249 wp_cache_set( 'get_calendar', $cache, 'calendar' );
250
251 if ( $echo ) {
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 */
259 echo apply_filters( 'get_calendar', $calendar_output );
260 return;
261 }
262 /** This filter is documented in wp-includes/general-template.php */
263 return apply_filters( 'get_calendar', $calendar_output );
264 }
265 }
266