| @@ -48,34 +48,96 @@ | ||
| 48 | 48 | empty( PLL()->curlang ) ? get_calendar() : self::get_calendar(); #modified# |
| 49 | 49 | echo '</div>'; |
| 50 | 50 | echo $args['after_widget']; |
| 51 | 51 | |
| 52 | - self::$pll_instance++; #modified# | |
| 52 | + ++self::$pll_instance; #modified# | |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | - * Modified version of the WP get_calendar() function to filter the queries. | |
| 56 | + * Modified version of the WP `get_calendar()` function to filter the queries. | |
| 57 | 57 | * |
| 58 | 58 | * @since 0.5 |
| 59 | + * @since 3.8 New argument $args added, with backward compatibility (WP 6.8). | |
| 59 | 60 | * |
| 60 | - * @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. | |
| 63 | - */ | |
| 64 | - static public function get_calendar( $initial = true, $echo = true ) { | |
| 61 | + * @param array $args { | |
| 62 | + * Optional. Arguments for the `get_calendar` function. | |
| 63 | + * | |
| 64 | + * @type bool $initial Whether to use initial calendar names. Default true. | |
| 65 | + * @type bool $display Whether to display the calendar output. Default true. | |
| 66 | + * @type string $post_type Optional. Post type. Default 'post'. | |
| 67 | + * } | |
| 68 | + * @return void|string Void if `$display` argument is true, calendar HTML if `$display` is false. | |
| 69 | + */ | |
| 70 | + static function get_calendar( $args = array() ) { | |
| 65 | 71 | global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; |
| 66 | 72 | |
| 67 | - $join_clause = PLL()->model->post->join_clause(); #added# | |
| 68 | - $where_clause = PLL()->model->post->where_clause( PLL()->curlang ); #added# | |
| 73 | + $defaults = array( | |
| 74 | + 'initial' => true, | |
| 75 | + 'display' => true, | |
| 76 | + 'post_type' => 'post', | |
| 77 | + ); | |
| 69 | 78 | |
| 70 | - $key = md5( PLL()->curlang->slug . $m . $monthnum . $year ); #modified# | |
| 79 | + $original_args = func_get_args(); | |
| 80 | + $args = array(); | |
| 81 | + | |
| 82 | + if ( ! empty( $original_args ) ) { | |
| 83 | + if ( ! is_array( $original_args[0] ) ) { | |
| 84 | + if ( isset( $original_args[0] ) && is_bool( $original_args[0] ) ) { | |
| 85 | + $defaults['initial'] = $original_args[0]; | |
| 86 | + } | |
| 87 | + if ( isset( $original_args[1] ) && is_bool( $original_args[1] ) ) { | |
| 88 | + $defaults['display'] = $original_args[1]; | |
| 89 | + } | |
| 90 | + } else { | |
| 91 | + $args = $original_args[0]; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** This filter is documented in wp-includes/general-template.php */ | |
| 96 | + $args = apply_filters( 'get_calendar_args', wp_parse_args( $args, $defaults ) ); | |
| 97 | + | |
| 98 | + $args['lang'] = PLL()->curlang->slug; #added# | |
| 99 | + | |
| 100 | + if ( ! post_type_exists( $args['post_type'] ) ) { | |
| 101 | + $args['post_type'] = 'post'; | |
| 102 | + } | |
| 103 | + | |
| 104 | + $w = 0; | |
| 105 | + if ( isset( $_GET['w'] ) ) { | |
| 106 | + $w = (int) $_GET['w']; | |
| 107 | + } | |
| 108 | + | |
| 109 | + /* | |
| 110 | + * Normalize the cache key. | |
| 111 | + * | |
| 112 | + * The following ensures the same cache key is used for the same parameter | |
| 113 | + * and parameter equivalents. This prevents `post_type > post, initial > true` | |
| 114 | + * from generating a different key from the same values in the reverse order. | |
| 115 | + * | |
| 116 | + * `display` is excluded from the cache key as the cache contains the same | |
| 117 | + * HTML regardless of this function's need to echo or return the output. | |
| 118 | + * | |
| 119 | + * The global values contain data generated by the URL query string variables. | |
| 120 | + */ | |
| 121 | + $cache_args = $args; | |
| 122 | + unset( $cache_args['display'] ); | |
| 123 | + | |
| 124 | + $cache_args['globals'] = array( | |
| 125 | + 'm' => $m, | |
| 126 | + 'monthnum' => $monthnum, | |
| 127 | + 'year' => $year, | |
| 128 | + 'week' => $w, | |
| 129 | + ); | |
| 130 | + | |
| 131 | + wp_recursive_ksort( $cache_args ); | |
| 132 | + $key = md5( serialize( $cache_args ) ); | |
| 71 | 133 | $cache = wp_cache_get( 'get_calendar', 'calendar' ); |
| 72 | 134 | |
| 73 | 135 | if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) { |
| 74 | 136 | /** This filter is documented in wp-includes/general-template.php */ |
| 75 | - $output = apply_filters( 'get_calendar', $cache[ $key ] ); | |
| 137 | + $output = apply_filters( 'get_calendar', $cache[ $key ], $args ); | |
| 76 | 138 | |
| 77 | - if ( $echo ) { | |
| 139 | + if ( $args['display'] ) { | |
| 78 | 140 | echo $output; |
| 79 | 141 | return; |
| 80 | 142 | } |
| 81 | 143 | |
| @@ -85,11 +147,14 @@ | ||
| 85 | 147 | if ( ! is_array( $cache ) ) { |
| 86 | 148 | $cache = array(); |
| 87 | 149 | } |
| 88 | 150 | |
| 151 | + $post_type = $args['post_type']; | |
| 152 | + | |
| 89 | 153 | // Quick check. If we have no posts at all, abort! |
| 90 | 154 | if ( ! $posts ) { |
| 91 | - $gotsome = $wpdb->get_var( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' LIMIT 1" ); | |
| 155 | + $prepared_query = $wpdb->prepare( "SELECT 1 as test FROM $wpdb->posts WHERE post_type = %s AND post_status = 'publish' LIMIT 1", $post_type ); | |
| 156 | + $gotsome = $wpdb->get_var( $prepared_query ); | |
| 92 | 157 | if ( ! $gotsome ) { |
| 93 | 158 | $cache[ $key ] = ''; |
| 94 | 159 | wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 95 | 160 | return; |
| @@ -95,11 +160,8 @@ | ||
| 95 | 160 | return; |
| 96 | 161 | } |
| 97 | 162 | } |
| 98 | 163 | |
| 99 | - if ( isset( $_GET['w'] ) ) { | |
| 100 | - $w = (int) $_GET['w']; | |
| 101 | - } | |
| 102 | 164 | // week_begins = 0 stands for Sunday. |
| 103 | 165 | $week_begins = (int) get_option( 'start_of_week' ); |
| 104 | 166 | |
| 105 | 167 | // Let's figure out when we are. |
| @@ -126,25 +188,33 @@ | ||
| 126 | 188 | |
| 127 | 189 | $unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear ); |
| 128 | 190 | $last_day = gmdate( 't', $unixmonth ); |
| 129 | 191 | |
| 192 | + $join_clause = PLL()->model->post->join_clause(); #added# | |
| 193 | + $where_clause = PLL()->model->post->where_clause( PLL()->curlang ); #added# | |
| 194 | + | |
| 130 | 195 | // Get the next and previous month and year with at least one post. |
| 131 | - $previous = $wpdb->get_row( | |
| 196 | + $previous_prepared_query = $wpdb->prepare( | |
| 132 | 197 | "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 133 | 198 | FROM $wpdb->posts $join_clause |
| 134 | 199 | WHERE post_date < '$thisyear-$thismonth-01' |
| 135 | - AND post_type = 'post' AND post_status = 'publish' $where_clause | |
| 136 | - ORDER BY post_date DESC | |
| 137 | - LIMIT 1" | |
| 200 | + AND post_type = %s AND post_status = 'publish' $where_clause | |
| 201 | + ORDER BY post_date DESC | |
| 202 | + LIMIT 1", | |
| 203 | + $post_type | |
| 138 | 204 | ); #modified# |
| 139 | - $next = $wpdb->get_row( | |
| 205 | + $previous = $wpdb->get_row( $previous_prepared_query ); | |
| 206 | + | |
| 207 | + $next_prepared_query = $wpdb->prepare( | |
| 140 | 208 | "SELECT MONTH(post_date) AS month, YEAR(post_date) AS year |
| 141 | 209 | FROM $wpdb->posts $join_clause |
| 142 | 210 | WHERE post_date > '$thisyear-$thismonth-{$last_day} 23:59:59' |
| 143 | - AND post_type = 'post' AND post_status = 'publish' $where_clause | |
| 144 | - ORDER BY post_date ASC | |
| 145 | - LIMIT 1" | |
| 211 | + AND post_type = %s AND post_status = 'publish' $where_clause | |
| 212 | + ORDER BY post_date ASC | |
| 213 | + LIMIT 1", | |
| 214 | + $post_type | |
| 146 | 215 | ); #modified# |
| 216 | + $next = $wpdb->get_row( $next_prepared_query ); | |
| 147 | 217 | |
| 148 | 218 | /* translators: Calendar caption: 1: Month name, 2: 4-digit year. */ |
| 149 | 219 | $calendar_caption = _x( '%1$s %2$s', 'calendar caption' ); |
| 150 | 220 | $calendar_output = '<table id="wp-calendar" class="wp-calendar-table"> |
| @@ -162,11 +232,11 @@ | ||
| 162 | 232 | $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 ); |
| 163 | 233 | } |
| 164 | 234 | |
| 165 | 235 | foreach ( $myweek as $wd ) { |
| 166 | - $day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); | |
| 236 | + $day_name = $args['initial'] ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); | |
| 167 | 237 | $wd = esc_attr( $wd ); |
| 168 | - $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>"; | |
| 238 | + $calendar_output .= "\n\t\t<th scope=\"col\" aria-label=\"$wd\">$day_name</th>"; | |
| 169 | 239 | } |
| 170 | 240 | |
| 171 | 241 | $calendar_output .= ' |
| 172 | 242 | </tr> |
| @@ -176,15 +246,16 @@ | ||
| 176 | 246 | |
| 177 | 247 | $daywithpost = array(); |
| 178 | 248 | |
| 179 | 249 | // Get days with posts. |
| 180 | - $dayswithposts = $wpdb->get_results( | |
| 250 | + $dayswithposts_prepared_query = $wpdb->prepare( | |
| 181 | 251 | "SELECT DISTINCT DAYOFMONTH(post_date) |
| 182 | 252 | FROM $wpdb->posts $join_clause WHERE post_date >= '{$thisyear}-{$thismonth}-01 00:00:00' |
| 183 | - AND post_type = 'post' AND post_status = 'publish' | |
| 253 | + AND post_type = %s AND post_status = 'publish' | |
| 184 | 254 | AND post_date <= '{$thisyear}-{$thismonth}-{$last_day} 23:59:59' $where_clause", |
| 185 | - ARRAY_N | |
| 255 | + $post_type | |
| 186 | 256 | ); #modified# |
| 257 | + $dayswithposts = $wpdb->get_results( $dayswithposts_prepared_query, ARRAY_N ); | |
| 187 | 258 | |
| 188 | 259 | if ( $dayswithposts ) { |
| 189 | 260 | foreach ( (array) $dayswithposts as $daywith ) { |
| 190 | 261 | $daywithpost[] = (int) $daywith[0]; |
| @@ -191,9 +262,9 @@ | ||
| 191 | 262 | } |
| 192 | 263 | } |
| 193 | 264 | |
| 194 | 265 | // See how much we should pad in the beginning. |
| 195 | - $pad = calendar_week_mod( gmdate( 'w', $unixmonth ) - $week_begins ); | |
| 266 | + $pad = calendar_week_mod( (int) gmdate( 'w', $unixmonth ) - $week_begins ); | |
| 196 | 267 | if ( 0 != $pad ) { |
| 197 | 268 | $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad"> </td>'; |
| 198 | 269 | } |
| 199 | 270 | |
| @@ -230,14 +301,14 @@ | ||
| 230 | 301 | } |
| 231 | 302 | |
| 232 | 303 | $calendar_output .= '</td>'; |
| 233 | 304 | |
| 234 | - if ( 6 == calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) { | |
| 305 | + if ( 6 == calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) { | |
| 235 | 306 | $newrow = true; |
| 236 | 307 | } |
| 237 | 308 | } |
| 238 | 309 | |
| 239 | - $pad = 7 - calendar_week_mod( gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ); | |
| 310 | + $pad = 7 - calendar_week_mod( (int) gmdate( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ); | |
| 240 | 311 | if ( 0 != $pad && 7 != $pad ) { |
| 241 | 312 | $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '"> </td>'; |
| 242 | 313 | } |
| 243 | 314 | |
| @@ -270,13 +341,15 @@ | ||
| 270 | 341 | |
| 271 | 342 | $cache[ $key ] = $calendar_output; |
| 272 | 343 | wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 273 | 344 | |
| 274 | - if ( $echo ) { | |
| 275 | - /** This filter is documented in wp-includes/general-template.php */ | |
| 276 | - echo apply_filters( 'get_calendar', $calendar_output ); | |
| 345 | + /** This filter is documented in wp-includes/general-template.php */ | |
| 346 | + $calendar_output = apply_filters( 'get_calendar', $calendar_output, $args ); | |
| 347 | + | |
| 348 | + if ( $args['display'] ) { | |
| 349 | + echo $calendar_output; | |
| 277 | 350 | return; |
| 278 | 351 | } |
| 279 | - /** This filter is documented in wp-includes/general-template.php */ | |
| 280 | - return apply_filters( 'get_calendar', $calendar_output ); | |
| 352 | + | |
| 353 | + return $calendar_output; | |
| 281 | 354 | } |
| 282 | 355 | } |