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