| 1 |
<?php |
| 2 |
/** |
| 3 |
* Welcart Blog Calendar Widget |
| 4 |
* |
| 5 |
* @package Welcart |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Blog Calendar |
| 10 |
* |
| 11 |
* @param bool $initial Initial or not. |
| 12 |
* @param bool $echo Return value or echo. |
| 13 |
* @return string|void |
| 14 |
*/ |
| 15 |
function get_wcblog_calendar( $initial = true, $echo = true ) { |
| 16 |
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; |
| 17 |
|
| 18 |
$cache = array(); |
| 19 |
$key = md5( $m . $monthnum . $year ); |
| 20 |
|
| 21 |
if ( ! is_array( $cache ) ) { |
| 22 |
$cache = array(); |
| 23 |
} |
| 24 |
|
| 25 |
// Quick check. If we have no posts at all, abort!. |
| 26 |
if ( ! $posts ) { |
| 27 |
$gotsome = $wpdb->get_var( |
| 28 |
$wpdb->prepare( |
| 29 |
"SELECT 1 as test |
| 30 |
FROM $wpdb->posts |
| 31 |
WHERE post_type = %s |
| 32 |
AND post_mime_type <> %s |
| 33 |
AND post_status = %s |
| 34 |
LIMIT 1", |
| 35 |
'post', // post_type. |
| 36 |
'item', // post_mime_type. |
| 37 |
'publish' // post_status. |
| 38 |
) |
| 39 |
); |
| 40 |
if ( ! $gotsome ) { |
| 41 |
$cache[ $key ] = ''; |
| 42 |
wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 43 |
return; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if ( isset( $_GET['w'] ) ) { |
| 48 |
$w = '' . intval( $_GET['w'] ); |
| 49 |
} |
| 50 |
|
| 51 |
// week_begins = 0 stands for Sunday. |
| 52 |
$week_begins = intval( get_option( 'start_of_week' ) ); |
| 53 |
|
| 54 |
// Let's figure out when we are. |
| 55 |
if ( ! empty( $monthnum ) && ! empty( $year ) ) { |
| 56 |
$thismonth = '' . zeroise( intval( $monthnum ), 2 ); |
| 57 |
$thisyear = '' . intval( $year ); |
| 58 |
} elseif ( ! empty( $w ) ) { |
| 59 |
// We need to get the month from MySQL. |
| 60 |
$thisyear = '' . intval( substr( $m, 0, 4 ) ); |
| 61 |
$d = ( ( (int) $w - 1 ) * 7 ) + 6; // it seems MySQL's weeks disagree with PHP's. |
| 62 |
$thismonth = $wpdb->get_var( |
| 63 |
$wpdb->prepare( |
| 64 |
"SELECT DATE_FORMAT((DATE_ADD(%s, INTERVAL %d DAY) ), '%m')", |
| 65 |
$thisyear . '0101', |
| 66 |
$d |
| 67 |
) |
| 68 |
); |
| 69 |
} elseif ( ! empty( $m ) ) { |
| 70 |
$thisyear = '' . intval( substr( $m, 0, 4 ) ); |
| 71 |
if ( strlen( $m ) < 6 ) { |
| 72 |
$thismonth = '01'; |
| 73 |
} else { |
| 74 |
$thismonth = '' . zeroise( intval( substr( $m, 4, 2 ) ), 2 ); |
| 75 |
} |
| 76 |
} else { |
| 77 |
$thisyear = wp_date( 'Y' ); |
| 78 |
$thismonth = wp_date( 'm' ); |
| 79 |
} |
| 80 |
|
| 81 |
$unixmonth = mktime( 0, 0, 0, $thismonth, 1, $thisyear ); |
| 82 |
$date_string = sprintf( '%04d-%02d-01', $thisyear, $thismonth ); |
| 83 |
|
| 84 |
// Get the next and previous month and year with at least one post. |
| 85 |
$previous = $wpdb->get_row( |
| 86 |
$wpdb->prepare( |
| 87 |
"SELECT DISTINCT MONTH(post_date) AS `month`, YEAR(post_date) AS `year` |
| 88 |
FROM $wpdb->posts |
| 89 |
WHERE post_date < %s |
| 90 |
AND post_mime_type <> %s |
| 91 |
AND post_type = %s |
| 92 |
AND post_status = %s |
| 93 |
ORDER BY post_date DESC |
| 94 |
LIMIT 1", |
| 95 |
$date_string, // This year-month. |
| 96 |
'item', // post_mime_type. |
| 97 |
'post', // post_type. |
| 98 |
'publish' // post_status. |
| 99 |
) |
| 100 |
); |
| 101 |
$next = $wpdb->get_row( |
| 102 |
$wpdb->prepare( |
| 103 |
"SELECT DISTINCT MONTH(post_date) AS `month`, YEAR(post_date) AS `year` |
| 104 |
FROM $wpdb->posts |
| 105 |
WHERE post_date > %s |
| 106 |
AND post_mime_type <> %s |
| 107 |
AND MONTH(post_date) != MONTH(%s) |
| 108 |
AND post_type = %s |
| 109 |
AND post_status = %s |
| 110 |
ORDER BY post_date ASC |
| 111 |
LIMIT 1", |
| 112 |
$date_string, // This year-month. |
| 113 |
'item', // post_mime_type. |
| 114 |
$date_string, // This year-month. |
| 115 |
'post', // post_type. |
| 116 |
'publish' // post_status. |
| 117 |
) |
| 118 |
); |
| 119 |
|
| 120 |
/* translators: Calendar caption: 1: month name, 2: 4-digit year */ |
| 121 |
$calendar_caption = _x( '%1$s %2$s', 'calendar caption' ); |
| 122 |
$calendar_output = '<table id="wp-calendar" summary="' . esc_attr__( 'Calendar', 'usces' ) . '"> |
| 123 |
<caption>' . sprintf( $calendar_caption, $wp_locale->get_month( $thismonth ), date( 'Y', $unixmonth ) ) . '</caption> |
| 124 |
<thead> |
| 125 |
<tr>'; |
| 126 |
|
| 127 |
$myweek = array(); |
| 128 |
|
| 129 |
for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) { |
| 130 |
$myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 ); |
| 131 |
} |
| 132 |
|
| 133 |
foreach ( $myweek as $wd ) { |
| 134 |
$day_name = ( $initial ) ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd ); |
| 135 |
$wd = esc_attr( $wd ); |
| 136 |
$calendar_output .= "<th scope=\"col\" title=\"$wd\">$day_name</th>"; |
| 137 |
} |
| 138 |
|
| 139 |
$calendar_output .= ' |
| 140 |
</tr> |
| 141 |
</thead> |
| 142 |
|
| 143 |
<tfoot> |
| 144 |
<tr>'; |
| 145 |
|
| 146 |
if ( $previous ) { |
| 147 |
$calendar_output .= '<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year, $previous->month ) . '" title="' . sprintf( __( 'View posts for %1$s %2$s', 'usces' ), $wp_locale->get_month( $previous->month ), date( 'Y', mktime( 0, 0, 0, $previous->month, 1, $previous->year ) ) ) . '">« ' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) . '</a></td>'; |
| 148 |
} else { |
| 149 |
$calendar_output .= '<td colspan="3" id="prev" class="pad"> </td>'; |
| 150 |
} |
| 151 |
|
| 152 |
$calendar_output .= '<td class="pad"> </td>'; |
| 153 |
|
| 154 |
if ( $next ) { |
| 155 |
$calendar_output .= '<td colspan="3" id="next"><a href="' . get_month_link( $next->year, $next->month ) . '" title="' . esc_attr( sprintf( __( 'View posts for %1$s %2$s', 'usces' ), $wp_locale->get_month( $next->month ), date( 'Y', mktime( 0, 0, 0, $next->month, 1, $next->year ) ) ) ) . '">' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) . ' »</a></td>'; |
| 156 |
} else { |
| 157 |
$calendar_output .= '<td colspan="3" id="next" class="pad"> </td>'; |
| 158 |
} |
| 159 |
|
| 160 |
$calendar_output .= ' |
| 161 |
</tr> |
| 162 |
</tfoot> |
| 163 |
|
| 164 |
<tbody> |
| 165 |
<tr>'; |
| 166 |
|
| 167 |
// Get days with posts. |
| 168 |
$dayswithposts = $wpdb->get_results( |
| 169 |
$wpdb->prepare( |
| 170 |
"SELECT DISTINCT DAYOFMONTH(post_date) |
| 171 |
FROM $wpdb->posts |
| 172 |
WHERE MONTH(post_date) = %d |
| 173 |
AND post_mime_type <> %s |
| 174 |
AND YEAR(post_date) = %d |
| 175 |
AND post_type = %s |
| 176 |
AND post_status = %s |
| 177 |
AND post_date < %s", |
| 178 |
$thismonth, // This month. |
| 179 |
'item', // post_mime_type. |
| 180 |
$thisyear, // This year. |
| 181 |
'post', // post_type. |
| 182 |
'publish', // post_status. |
| 183 |
current_time( 'mysql' ) // Current time. |
| 184 |
), |
| 185 |
ARRAY_N |
| 186 |
); |
| 187 |
|
| 188 |
if ( $dayswithposts ) { |
| 189 |
foreach ( (array) $dayswithposts as $daywith ) { |
| 190 |
$daywithpost[] = $daywith[0]; |
| 191 |
} |
| 192 |
} else { |
| 193 |
$daywithpost = array(); |
| 194 |
} |
| 195 |
|
| 196 |
if ( false !== strpos( $_SERVER['HTTP_USER_AGENT'], 'MSIE' ) || false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'camino' ) || false !== stripos( $_SERVER['HTTP_USER_AGENT'], 'safari' ) ) { |
| 197 |
$ak_title_separator = "\n"; |
| 198 |
} else { |
| 199 |
$ak_title_separator = ', '; |
| 200 |
} |
| 201 |
|
| 202 |
$ak_titles_for_day = array(); |
| 203 |
$ak_post_titles = $wpdb->get_results( |
| 204 |
$wpdb->prepare( |
| 205 |
"SELECT ID, post_title, DAYOFMONTH(post_date) as dom |
| 206 |
FROM $wpdb->posts |
| 207 |
WHERE YEAR(post_date) = %d |
| 208 |
AND MONTH(post_date) = %d |
| 209 |
AND post_mime_type <> %s |
| 210 |
AND post_date < %s |
| 211 |
AND post_type = %s |
| 212 |
AND post_status = %s", |
| 213 |
$thisyear, // This year. |
| 214 |
$thismonth, // This month. |
| 215 |
'item', // post_mime_type. |
| 216 |
current_time( 'mysql' ), // Current time. |
| 217 |
'post', // post_type. |
| 218 |
'publish' // post_status. |
| 219 |
) |
| 220 |
); |
| 221 |
if ( $ak_post_titles ) { |
| 222 |
foreach ( (array) $ak_post_titles as $ak_post_title ) { |
| 223 |
|
| 224 |
$post_title = esc_attr( apply_filters( 'the_title', $ak_post_title->post_title, $ak_post_title->ID ) ); |
| 225 |
|
| 226 |
if ( empty( $ak_titles_for_day[ 'day_' . $ak_post_title->dom ] ) ) { |
| 227 |
$ak_titles_for_day[ 'day_' . $ak_post_title->dom ] = ''; |
| 228 |
} |
| 229 |
if ( empty( $ak_titles_for_day[ "$ak_post_title->dom" ] ) ) { // first one. |
| 230 |
$ak_titles_for_day[ "$ak_post_title->dom" ] = $post_title; |
| 231 |
} else { |
| 232 |
$ak_titles_for_day[ "$ak_post_title->dom" ] .= $ak_title_separator . $post_title; |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// See how much we should pad in the beginning. |
| 238 |
$pad = calendar_week_mod( date( 'w', $unixmonth ) - $week_begins ); |
| 239 |
if ( 0 !== (int) $pad ) { |
| 240 |
$calendar_output .= '<td colspan="' . esc_attr( $pad ) . '" class="pad"> </td>'; |
| 241 |
} |
| 242 |
$daysinmonth = intval( date( 't', $unixmonth ) ); |
| 243 |
$todayd = wp_date( 'j' ); |
| 244 |
$todaym = wp_date( 'm' ); |
| 245 |
$todayy = wp_date( 'Y' ); |
| 246 |
for ( $day = 1; $day <= $daysinmonth; ++$day ) { |
| 247 |
if ( isset( $newrow ) && $newrow ) { |
| 248 |
$calendar_output .= '</tr><tr>'; |
| 249 |
} |
| 250 |
$newrow = false; |
| 251 |
|
| 252 |
if ( $day === (int) $todayd && (int) $thismonth === (int) $todaym && (int) $thisyear === (int) $todayy ) { |
| 253 |
$calendar_output .= '<td id="today">'; |
| 254 |
} else { |
| 255 |
$calendar_output .= '<td>'; |
| 256 |
} |
| 257 |
|
| 258 |
if ( in_array( $day, $daywithpost ) ) { // any posts today? |
| 259 |
$calendar_output .= '<a href="' . get_day_link( $thisyear, $thismonth, $day ) . '" title="' . esc_attr( $ak_titles_for_day[ $day ] ) . "\">$day</a>"; |
| 260 |
} else { |
| 261 |
$calendar_output .= $day; |
| 262 |
} |
| 263 |
$calendar_output .= '</td>'; |
| 264 |
|
| 265 |
if ( 6 == calendar_week_mod( date( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ) ) { |
| 266 |
$newrow = true; |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
$pad = 7 - calendar_week_mod( date( 'w', mktime( 0, 0, 0, $thismonth, $day, $thisyear ) ) - $week_begins ); |
| 271 |
if ( 0 !== (int) $pad && 7 !== (int) $pad ) { |
| 272 |
$calendar_output .= '<td class="pad" colspan="' . esc_attr( $pad ) . '"> </td>'; |
| 273 |
} |
| 274 |
$calendar_output .= '</tr></tbody></table>'; |
| 275 |
|
| 276 |
$cache[ $key ] = $calendar_output; |
| 277 |
wp_cache_set( 'get_calendar', $cache, 'calendar' ); |
| 278 |
|
| 279 |
if ( $echo ) { |
| 280 |
echo apply_filters( 'get_calendar', $calendar_output ); |
| 281 |
} else { |
| 282 |
return apply_filters( 'get_calendar', $calendar_output ); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Welcart_Blog_Calendar Class |
| 288 |
* |
| 289 |
* @see WP_Widget |
| 290 |
*/ |
| 291 |
class Welcart_Blog_Calendar extends WP_Widget { |
| 292 |
|
| 293 |
/** |
| 294 |
* Constructor. |
| 295 |
*/ |
| 296 |
public function __construct() { |
| 297 |
$widget_ops = array( |
| 298 |
'classname' => 'welcart_blog_calendar', |
| 299 |
'description' => __( 'A calendar of your site’s posts.', 'usces' ) . __( 'Non-item', 'usces' ), |
| 300 |
); |
| 301 |
parent::__construct( 'welcart-blog-calendar', 'Welcart ' . __( 'Blog Calendar', 'usces' ), $widget_ops ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Echoes the widget content. |
| 306 |
* |
| 307 |
* @see WP_Widget::widget |
| 308 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 309 |
* 'before_widget', and 'after_widget'. |
| 310 |
* @param array $instance The settings for the particular instance of the widget. |
| 311 |
*/ |
| 312 |
public function widget( $args, $instance ) { |
| 313 |
extract( $args ); |
| 314 |
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); |
| 315 |
wel_esc_script_e( $before_widget ); |
| 316 |
if ( trim( $title ) ) { |
| 317 |
wel_esc_script_e( $before_title . $title . $after_title ); |
| 318 |
} |
| 319 |
echo '<div id="calendar_wrap">'; |
| 320 |
get_wcblog_calendar(); |
| 321 |
echo '</div>'; |
| 322 |
wel_esc_script_e( $after_widget ); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Updates a particular instance of a widget. |
| 327 |
* |
| 328 |
* @see WP_Widget::update |
| 329 |
* @param array $new_instance New settings for this instance as input by the user via |
| 330 |
* WP_Widget::form(). |
| 331 |
* @param array $old_instance Old settings for this instance. |
| 332 |
* @return array Settings to save or bool false to cancel saving. |
| 333 |
*/ |
| 334 |
public function update( $new_instance, $old_instance ) { |
| 335 |
$instance = $old_instance; |
| 336 |
$instance['title'] = strip_tags( $new_instance['title'] ); |
| 337 |
return $instance; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Outputs the settings update form. |
| 342 |
* |
| 343 |
* @see WP_Widget::form |
| 344 |
* @param array $instance Current settings. |
| 345 |
*/ |
| 346 |
public function form( $instance ) { |
| 347 |
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
| 348 |
$title = strip_tags( $instance['title'] ); |
| 349 |
?> |
| 350 |
<p><label for="<?php wel_esc_script_e( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'usces' ); ?></label> |
| 351 |
<input class="widefat" id="<?php wel_esc_script_e( $this->get_field_id( 'title' ) ); ?>" name="<?php wel_esc_script_e( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> |
| 352 |
<?php |
| 353 |
} |
| 354 |
} |
| 355 |
|