Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.22 KB | None | 0 0
  1. class hmt_cpt_calender extends WP_Widget {
  2.  
  3.  
  4. /** constructor */
  5. function hmt_cpt_calender() {
  6. parent::WP_Widget(false, $name = 'CPT Calendar');
  7. }
  8.  
  9. /** @see WP_Widget::widget */
  10. function widget($args, $instance) {
  11. extract( $args );
  12. $title = apply_filters('widget_title', $instance['title']);
  13. $posttype_enabled = $instance['posttype_enabled'];
  14. $posttype = $instance['posttype'];
  15. ?>
  16. <?php echo $before_widget; ?>
  17. <?php if ( $title )
  18. echo $before_title . $title . $after_title; ?>
  19. <div class="widget_calendar">
  20. <div id="calendar_wrap">
  21. <?php if($posttype_enabled == true) {
  22. ucc_get_calendar(array($posttype));
  23. } else {
  24. ucc_get_calendar();
  25. } ?>
  26. </div>
  27. </div>
  28. <?php echo $after_widget; ?>
  29. <?php
  30. }
  31.  
  32. /** @see WP_Widget::update */
  33. function update($new_instance, $old_instance) {
  34. $instance = $old_instance;
  35. $instance['title'] = strip_tags($new_instance['title']);
  36. $instance['posttype_enabled'] = $new_instance['posttype_enabled'];
  37. $instance['posttype'] = $new_instance['posttype'];
  38. return $instance;
  39. }
  40.  
  41. /** @see WP_Widget::form */
  42. function form($instance) {
  43.  
  44. $posttypes = get_post_types('', 'objects');
  45.  
  46. $title = esc_attr($instance['title']);
  47. $posttype_enabled = esc_attr($instance['posttype_enabled']);
  48. $posttype = esc_attr($instance['posttype']);
  49. ?>
  50. <p>
  51. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  52. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
  53. </p>
  54. <p>
  55. <input id="<?php echo $this->get_field_id('posttype_enabled'); ?>" name="<?php echo $this->get_field_name('posttype_enabled'); ?>" type="checkbox" value="1" <?php checked( '1', $posttype_enabled ); ?>/>
  56. <label for="<?php echo $this->get_field_id('posttype_enabled'); ?>"><?php _e('Show only one post type?'); ?></label>
  57. </p>
  58. <p>
  59. <label for="<?php echo $this->get_field_id('posttype'); ?>"><?php _e('Choose the Post Type to display'); ?></label>
  60. <select name="<?php echo $this->get_field_name('posttype'); ?>" id="<?php echo $this->get_field_id('posttype'); ?>" class="widefat">
  61. <?php
  62. foreach ($posttypes as $option) {
  63. echo '<option value="' . $option->name . '" id="' . $option->name . '"', $posttype == $option->name ? ' selected="selected"' : '', '>', $option->name, '</option>';
  64. }
  65. ?>
  66. </select>
  67. </p>
  68. <?php
  69. }
  70.  
  71.  
  72. } //
  73. // register CPT Calendar widget
  74. add_action('widgets_init', create_function('', 'return register_widget("hmt_cpt_calender");'));
  75.  
  76.  
  77.  
  78. /* ucc_get_calendar() :: Extends get_calendar() by including custom post types.*/
  79.  
  80. function ucc_get_calendar( $post_types = '' , $initial = true , $echo = true ) {
  81. global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;
  82.  
  83. if ( empty( $post_types ) || !is_array( $post_types ) ) {
  84. $args = array(
  85. 'public' => true ,
  86. '_builtin' => false
  87. );
  88. $output = 'names';
  89. $operator = 'and';
  90.  
  91. $post_types = get_post_types( $args , $output , $operator );
  92. $post_types = array_merge( $post_types , array( 'post' ) );
  93. } else {
  94. /* Trust but verify. */
  95. $my_post_types = array();
  96. foreach ( $post_types as $post_type ) {
  97. if ( post_type_exists( $post_type ) )
  98. $my_post_types[] = $post_type;
  99. }
  100. $post_types = $my_post_types;
  101. }
  102. $post_types_key = implode( '' , $post_types );
  103. $post_types = "'" . implode( "' , '" , $post_types ) . "'";
  104.  
  105. $cache = array();
  106. $key = md5( $m . $monthnum . $year . $post_types_key );
  107. if ( $cache = wp_cache_get( 'get_calendar' , 'calendar' ) ) {
  108. if ( is_array( $cache ) && isset( $cache[$key] ) ) {
  109. remove_filter( 'get_calendar' , 'ucc_get_calendar_filter' );
  110. $output = apply_filters( 'get_calendar', $cache[$key] );
  111. add_filter( 'get_calendar' , 'ucc_get_calendar_filter' );
  112. if ( $echo ) {
  113. echo $output;
  114. return;
  115. } else {
  116. return $output;
  117. }
  118. }
  119. }
  120.  
  121. if ( !is_array( $cache ) )
  122. $cache = array();
  123.  
  124. // Quick check. If we have no posts at all, abort!
  125. if ( !$posts ) {
  126. $sql = "SELECT 1 as test FROM $wpdb->posts WHERE post_type IN ( $post_types ) AND post_status = 'publish' LIMIT 1";
  127. $gotsome = $wpdb->get_var( $sql );
  128. if ( !$gotsome ) {
  129. $cache[$key] = '';
  130. wp_cache_set( 'get_calendar' , $cache , 'calendar' );
  131. return;
  132. }
  133. }
  134.  
  135. if ( isset( $_GET['w'] ) )
  136. $w = '' . intval( $_GET['w'] );
  137.  
  138. // week_begins = 0 stands for Sunday
  139. $week_begins = intval( get_option( 'start_of_week' ) );
  140.  
  141. // Let's figure out when we are
  142. if ( !empty( $monthnum ) && !empty( $year ) ) {
  143. $thismonth = '' . zeroise( intval( $monthnum ) , 2 );
  144. $thisyear = ''.intval($year);
  145. } elseif ( !empty( $w ) ) {
  146. // We need to get the month from MySQL
  147. $thisyear = '' . intval( substr( $m , 0 , 4 ) );
  148. $d = ( ( $w - 1 ) * 7 ) + 6; //it seems MySQL's weeks disagree with PHP's
  149. $thismonth = $wpdb->get_var( "SELECT DATE_FORMAT( ( DATE_ADD( '${thisyear}0101' , INTERVAL $d DAY ) ) , '%m' ) " );
  150. } elseif ( !empty( $m ) ) {
  151. $thisyear = '' . intval( substr( $m , 0 , 4 ) );
  152. if ( strlen( $m ) < 6 )
  153. $thismonth = '01';
  154. else
  155. $thismonth = '' . zeroise( intval( substr( $m , 4 , 2 ) ) , 2 );
  156. } else {
  157. $thisyear = gmdate( 'Y' , current_time( 'timestamp' ) );
  158. $thismonth = gmdate( 'm' , current_time( 'timestamp' ) );
  159. }
  160.  
  161. $unixmonth = mktime( 0 , 0 , 0 , $thismonth , 1 , $thisyear);
  162.  
  163. // Get the next and previous month and year with at least one post
  164. $previous = $wpdb->get_row( "SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year
  165. FROM $wpdb->posts
  166. WHERE post_date < '$thisyear-$thismonth-01'
  167. AND post_type IN ( $post_types ) AND post_status = 'publish'
  168. ORDER BY post_date DESC
  169. LIMIT 1" );
  170. $next = $wpdb->get_row( "SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year
  171. FROM $wpdb->posts
  172. WHERE post_date > '$thisyear-$thismonth-01'
  173. AND MONTH( post_date ) != MONTH( '$thisyear-$thismonth-01' )
  174. AND post_type IN ( $post_types ) AND post_status = 'publish'
  175. ORDER BY post_date ASC
  176. LIMIT 1" );
  177.  
  178. /* translators: Calendar caption: 1: month name, 2: 4-digit year */
  179. $calendar_caption = _x( '%1$s %2$s' , 'calendar caption' );
  180. $calendar_output = '<table id="wp-calendar" summary="' . esc_attr__( 'Calendar' ) . '">
  181. <caption>' . sprintf( $calendar_caption , $wp_locale->get_month( $thismonth ) , date( 'Y' , $unixmonth ) ) . '</caption>
  182. <thead>
  183. <tr>';
  184.  
  185. $myweek = array();
  186.  
  187. for ( $wdcount = 0 ; $wdcount <= 6 ; $wdcount++ ) {
  188. $myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
  189. }
  190.  
  191. foreach ( $myweek as $wd ) {
  192. $day_name = ( true == $initial ) ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
  193. $wd = esc_attr( $wd );
  194. $calendar_output .= "\n\t\t<th scope=\"col\" title=\"$wd\">$day_name</th>";
  195. }
  196.  
  197. $calendar_output .= '
  198. </tr>
  199. </thead>
  200.  
  201. <tfoot>
  202. <tr>';
  203.  
  204. if ( $previous ) { $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev"><a href="' . get_month_link( $previous->year , $previous->month ) . '" title="' . sprintf( __( 'View posts for %1$s %2$s' ) , $wp_locale->get_month( $previous->month ) , date( 'Y' , mktime( 0 , 0 , 0 , $previous->month , 1 , $previous->year ) ) ) . '">&laquo; ' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $previous->month ) ) . '</a></td>';
  205. } else {
  206. $calendar_output .= "\n\t\t" . '<td colspan="3" id="prev" class="pad">&nbsp;</td>';
  207. }
  208.  
  209. $calendar_output .= "\n\t\t" . '<td class="pad">&nbsp;</td>';
  210.  
  211. if ( $next ) { $calendar_output .= "\n\t\t" . '<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' ) , $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 ) ) . ' &raquo;</a></td>';
  212. } else {
  213. $calendar_output .= "\n\t\t" . '<td colspan="3" id="next" class="pad">&nbsp;</td>';
  214. }
  215.  
  216. $calendar_output .= '
  217. </tr>
  218. </tfoot>
  219.  
  220. <tbody>
  221. <tr>';
  222.  
  223. // Get days with posts
  224. $dayswithposts = $wpdb->get_results( "SELECT DISTINCT DAYOFMONTH( post_date )
  225. FROM $wpdb->posts WHERE MONTH( post_date ) = '$thismonth'
  226. AND YEAR( post_date ) = '$thisyear'
  227. AND post_type IN ( $post_types ) AND post_status = 'publish'
  228. AND post_date < '" . current_time( 'mysql' ) . '\'', ARRAY_N );
  229. if ( $dayswithposts ) {
  230. foreach ( (array) $dayswithposts as $daywith ) {
  231. $daywithpost[] = $daywith[0];
  232. }
  233. } else {
  234. $daywithpost = array();
  235. }
  236.  
  237. if ( strpos( $_SERVER['HTTP_USER_AGENT'] , 'MSIE' ) !== false || stripos( $_SERVER['HTTP_USER_AGENT'] , 'camino' ) !== false || stripos( $_SERVER['HTTP_USER_AGENT'] , 'safari' ) !== false )
  238. $ak_title_separator = "\n";
  239. else
  240. $ak_title_separator = ', ';
  241.  
  242. $ak_titles_for_day = array();
  243. $ak_post_titles = $wpdb->get_results( "SELECT ID, post_title, DAYOFMONTH( post_date ) as dom "
  244. . "FROM $wpdb->posts "
  245. . "WHERE YEAR( post_date ) = '$thisyear' "
  246. . "AND MONTH( post_date ) = '$thismonth' "
  247. . "AND post_date < '" . current_time( 'mysql' ) . "' "
  248. . "AND post_type IN ( $post_types ) AND post_status = 'publish'"
  249. );
  250. if ( $ak_post_titles ) {
  251. foreach ( (array) $ak_post_titles as $ak_post_title ) {
  252.  
  253. $post_title = esc_attr( apply_filters( 'the_title' , $ak_post_title->post_title , $ak_post_title->ID ) );
  254.  
  255. if ( empty( $ak_titles_for_day['day_' . $ak_post_title->dom] ) )
  256. $ak_titles_for_day['day_'.$ak_post_title->dom] = '';
  257. if ( empty( $ak_titles_for_day["$ak_post_title->dom"] ) ) // first one
  258. $ak_titles_for_day["$ak_post_title->dom"] = $post_title;
  259. else
  260. $ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . $post_title;
  261. }
  262. }
  263.  
  264. // See how much we should pad in the beginning
  265. $pad = calendar_week_mod( date( 'w' , $unixmonth ) - $week_begins );
  266. if ( 0 != $pad )
  267. $calendar_output .= "\n\t\t" . '<td colspan="' . esc_attr( $pad ) . '" class="pad">&nbsp;</td>';
  268.  
  269. $daysinmonth = intval( date( 't' , $unixmonth ) );
  270. for ( $day = 1 ; $day <= $daysinmonth ; ++$day ) {
  271. if ( isset( $newrow ) && $newrow )
  272. $calendar_output .= "\n\t</tr>\n\t<tr>\n\t\t";
  273. $newrow = false;
  274.  
  275. if ( $day == gmdate( 'j' , current_time( 'timestamp' ) ) && $thismonth == gmdate( 'm' , current_time( 'timestamp' ) ) && $thisyear == gmdate( 'Y' , current_time( 'timestamp' ) ) )
  276. $calendar_output .= '<td id="today">';
  277. else
  278. $calendar_output .= '<td>';
  279.  
  280. if ( in_array( $day , $daywithpost ) ) // any posts today?
  281. $calendar_output .= '<a href="' . get_day_link( $thisyear , $thismonth , $day ) . "\" title=\"" . esc_attr( $ak_titles_for_day[$day] ) . "\">$day</a>";
  282. else
  283. $calendar_output .= $day;
  284. $calendar_output .= '</td>';
  285.  
  286. if ( 6 == calendar_week_mod( date( 'w' , mktime( 0 , 0 , 0 , $thismonth , $day , $thisyear ) ) - $week_begins ) )
  287. $newrow = true;
  288. }
  289.  
  290. $pad = 7 - calendar_week_mod( date( 'w' , mktime( 0 , 0 , 0 , $thismonth , $day , $thisyear ) ) - $week_begins );
  291. if ( $pad != 0 && $pad != 7 )
  292. $calendar_output .= "\n\t\t" . '<td class="pad" colspan="' . esc_attr( $pad ) . '">&nbsp;</td>';
  293.  
  294. $calendar_output .= "\n\t</tr>\n\t</tbody>\n\t</table>";
  295.  
  296. $cache[$key] = $calendar_output;
  297. wp_cache_set( 'get_calendar' , $cache, 'calendar' );
  298.  
  299. remove_filter( 'get_calendar' , 'ucc_get_calendar_filter' );
  300. $output = apply_filters( 'get_calendar', $calendar_output );
  301. add_filter( 'get_calendar' , 'ucc_get_calendar_filter' );
  302.  
  303. if ( $echo )
  304. echo $output;
  305. else
  306. return $output;
  307. }
  308.  
  309. function ucc_get_calendar_filter( $content ) {
  310. $output = ucc_get_calendar( '' , '' , false );
  311. return $output;
  312. }
  313. add_filter( 'get_calendar' , 'ucc_get_calendar_filter' , 10 , 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement