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

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