PluginProbe
Polylang / 1.8
Polylang v1.8
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 1.8, at include/widget-calendar.php

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