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

277 lines 9.2 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 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
13 *
14 * @since 0.5
15 */
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
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.
22 *
23 * @since 0.5
24 *
25 * @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.
27 */
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#
44 echo '</div>';
45 echo $args['after_widget'];
46
47 self::$pll_instance++; #modified#
48 }
49
50 /**
51 * Modified version of WP get_calendar function to filter the query
52 *
53 * @since 0.5
54 *
55 * @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.
58 */
59 static public function get_calendar( $initial = true, $echo = true ) {
60 global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
61
62 $join_clause = PLL()->model->post->join_clause(); #added#
63 $where_clause = PLL()->model->post->where_clause( PLL()->curlang ); #added#
64
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;
75 }
76
77 return $output;
78 }
79
80 if ( ! is_array( $cache ) ) {
81 $cache = array();
82 }
83
84 // Quick check. If we have no posts at all, abort!
85 if ( ! $posts ) {
86 $gotsome = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1");
87 if ( ! $gotsome ) {
88 $cache[ $key ] = '';
89 wp_cache_set( 'get_calendar', $cache, 'calendar' );
90 return;
91 }
92 }
93
94 if ( isset( $_GET['w'] ) ) {
95 $w = (int) $_GET['w'];
96 }
97 // week_begins = 0 stands for Sunday
98 $week_begins = (int) get_option( 'start_of_week' );
99 $ts = current_time( 'timestamp' );
100
101 // 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 ) ) {
106 // 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;
110 $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 ) {
114 $thismonth = '01';
115 } else {
116 $thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
117 }
118 } else {
119 $thisyear = gmdate( 'Y', $ts );
120 $thismonth = gmdate( 'm', $ts );
121 }
122
123 $unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
124 $last_day = date( 't', $unixmonth );
125
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
128 FROM $wpdb->posts $join_clause
129 WHERE post_date < '$thisyear-$thismonth-01'
130 AND post_type = 'post' AND post_status = 'publish' $where_clause
131 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
134 FROM $wpdb->posts $join_clause
135 WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59'
136 AND post_type = 'post' AND post_status = 'publish' $where_clause
137 ORDER BY post_date ASC
138 LIMIT 1" ); #modified#
139
140 /* translators: Calendar caption: 1: month name, 2: 4-digit year */
141 $calendar_caption = _x( '%1$s %2$s', 'calendar caption' );
142 $calendar_output = '<table id="wp-calendar">
143 <caption>' . sprintf(
144 $calendar_caption,
145 $wp_locale->get_month( $thismonth ),
146 date( 'Y', $unixmonth )
147 ) . '</caption>
148 <thead>
149 <tr>';
150
151 $myweek = array();
152
153 for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
154 $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
155 }
156
157 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 );
160 $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
161 }
162
163 $calendar_output .= '
164 </tr>
165 </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 <tbody>
193 <tr>';
194
195 $daywithpost = array();
196
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#
203 if ( $dayswithposts ) {
204 foreach ( (array) $dayswithposts as $daywith ) {
205 $daywithpost[] = $daywith[0];
206 }
207 }
208
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>';
213 }
214
215 $newrow = false;
216 $daysinmonth = (int) date( 't', $unixmonth );
217
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 }
222 $newrow = false;
223
224 if ( $day == gmdate( 'j', $ts ) &&
225 $thismonth == gmdate( 'm', $ts ) &&
226 $thisyear == gmdate( 'Y', $ts ) ) {
227 $calendar_output .= '<td id="today">';
228 } else {
229 $calendar_output .= '<td>';
230 }
231
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 {
244 $calendar_output .= $day;
245 }
246 $calendar_output .= '</td>';
247
248 if ( 6 == calendar_week_mod( date( 'w', mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) {
249 $newrow = true;
250 }
251 }
252
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 }
257 $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
258
259 $cache[ $key ] = $calendar_output;
260 wp_cache_set( 'get_calendar', $cache, 'calendar' );
261
262 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 */
270 echo apply_filters( 'get_calendar', $calendar_output );
271 return;
272 }
273 /** This filter is documented in wp-includes/general-template.php */
274 return apply_filters( 'get_calendar', $calendar_output );
275 }
276 }
277