web/wp-content/plugins/future-posts-calendar/future_calendar.php
branchwordpress
changeset 112 fb7cd02b9848
parent 111 6b96085291d7
child 123 561aa6d282f6
equal deleted inserted replaced
111:6b96085291d7 112:fb7cd02b9848
     1 <?php
       
     2 /*
       
     3 Plugin Name: Future Calendar
       
     4 Plugin URI: http://anthologyoi.com/wordpress/plugins/future-posts-calendar-plugin.html
       
     5 Description: A simple plugin that utalizes a modified get_calendar function that shows what dates have a future post scheduled in a calendar format, and makes it easy to change the current timestamp. Temperature Functionality and some tweaks by Flavio Jarabeck (www.InternetDrops.com.br)
       
     6 Author: Aaron Harun
       
     7 Version: 1.0
       
     8 Author URI: http://anthologyoi.com/
       
     9 */
       
    10 
       
    11 /*Uncomment the following line if you want to have the calendar appear on the dashboard.*/
       
    12 //add_action('activity_box_end', 'get_future_calendar_html');
       
    13 
       
    14 
       
    15 	if (strpos($_SERVER['PHP_SELF'], 'post')){	
       
    16 		if($wp_version < 2.5){
       
    17 			add_action('dbx_post_sidebar', 'get_future_calendar_html',2);
       
    18 		}else{
       
    19 			add_action('submitpost_box', 'get_future_calendar_html');
       
    20 		}
       
    21 		add_action('admin_head', 'fcal_javascript');
       
    22 	}
       
    23 
       
    24 
       
    25 // This gets called at the plugins_loaded action
       
    26 function widget_fut_posts_init() {
       
    27 
       
    28 	// Check for the required API functions
       
    29 	if ( !function_exists('register_sidebar_widget') || !function_exists('register_widget_control') )
       
    30 		return;
       
    31 
       
    32 	// This saves options and prints the widget's config form.
       
    33 	function widget_fut_posts_control() {
       
    34 		$options = $newoptions = get_option('widget_fut_posts');
       
    35 		if ( $_POST['fut_posts-submit'] ) {
       
    36 			$newoptions['title'] = strip_tags(stripslashes($_POST['fut_posts-title']));
       
    37 		}
       
    38 		if ( $options != $newoptions ) {
       
    39 			$options = $newoptions;
       
    40 			update_option('widget_fut_posts', $options);
       
    41 		}
       
    42 	?>
       
    43 				<div style="text-align:right">
       
    44 				<label for="fut_posts-title" style="line-height:35px;display:block;"><?php _e('Widget title:', 'widgets'); ?> <input type="text" id="fut_posts-title" name="fut_posts-title" value="<?php echo wp_specialchars($options['title'], true); ?>" /></label>
       
    45 				<input type="hidden" name="fut_posts-submit" id="fut_posts-submit" value="1" />
       
    46 				</div>
       
    47 	<?php
       
    48 	}
       
    49 
       
    50 	// This prints the widget
       
    51 	function widget_fut_posts($args) {
       
    52 		extract($args);
       
    53 		$defaults = array('title' => 'Future Posts');
       
    54 		$options = (array) get_option('widget_fut_posts');
       
    55 
       
    56 		foreach ( $defaults as $key => $value )
       
    57 			if ( !isset($options[$key]) )
       
    58 				$options[$key] = $defaults[$key];
       
    59 
       
    60 			echo $before_widget . $before_title . $title . $after_title;
       
    61 			fcal_get_future_posts(0);
       
    62 			echo $after_widget;
       
    63 	}
       
    64 
       
    65 
       
    66 	register_sidebar_widget('Future Posts', 'widget_fut_posts');
       
    67 	register_widget_control('Future Posts', 'widget_fut_posts_control');
       
    68 }
       
    69 
       
    70 // Delay plugin execution to ensure Dynamic Sidebar has a chance to load first
       
    71 add_action('widgets_init', 'widget_fut_posts_init');
       
    72 
       
    73 
       
    74 function future_post_sidebar(){
       
    75 	$title = __('Future Post','fcal');
       
    76 	echo '<div>' . '<h3>' . $title . '</h3>';
       
    77 	fcal_get_future_posts();
       
    78 	echo '</div>';
       
    79 }
       
    80 
       
    81 function fcal_javascript(){
       
    82 	echo '
       
    83 	<script type="text/javascript">
       
    84 		function fcal_set_date(day,month,year){
       
    85 
       
    86 				if(day > 0 && month <= 12 && month >= 0 && year > 0){
       
    87 				document.getElementById("jj").value = day;
       
    88 				document.getElementById("aa").value = year;
       
    89 				document.getElementsByName("mm")[0].selectedIndex = month;
       
    90 				}
       
    91 
       
    92 		}
       
    93 	</script>
       
    94 	';
       
    95 }
       
    96 
       
    97 
       
    98 function get_future_calendar_html(){
       
    99 global $wp_version;
       
   100 	
       
   101 	if($wp_version < 2.5){
       
   102 		echo '<fieldset id="future_cal" class="dbx-box side-info">';
       
   103 		echo '<h3 class="dbx-handle">'.__('Future Post Dates','fcal').'</h3>';
       
   104 		echo '<div class="dbx-content">';
       
   105 			fcal_get_future_posts();
       
   106 		echo '</div></fieldset>';
       
   107 	}else{
       
   108 		echo '<div class="inside">';
       
   109 		echo '<p><strong>'.__('Future Post Dates','fcal').'</strong></p>';
       
   110 			fcal_get_future_posts();
       
   111 		echo '</div>';
       
   112 		
       
   113 	}
       
   114 	
       
   115 }
       
   116 
       
   117 function fcal_get_future_posts($onclick = 1){
       
   118 global $wpdb, $wp_locale;
       
   119 
       
   120 	$thisyear = gmdate('Y', current_time('timestamp'));
       
   121 	$thismonth = gmdate('m', current_time('timestamp'));
       
   122 
       
   123 	// Quick check. If we have no posts at all, abort!
       
   124 	if ( !$posts ) {
       
   125 		$gotsome = $wpdb->get_var("SELECT ID from $wpdb->posts WHERE post_type = 'post' AND post_status = 'future' ORDER BY post_date DESC LIMIT 1");
       
   126 		if ( !$gotsome ){
       
   127 			get_future_calendar($thismonth,$thisyear,$onclick);
       
   128 			return;
       
   129 		}
       
   130 	}
       
   131 
       
   132 	get_future_calendar($thismonth,$thisyear,$onclick);
       
   133 
       
   134 	//Technically thismonth is really nextmonth, but no reason to be technical about it
       
   135 	//But if thismonth is 12 then we need to reset it, and add a year otherwise we will be checking
       
   136 	// out the 13th month of this year.
       
   137 	if($thismonth == 12){
       
   138 		$thismonth = 0;
       
   139 		$thisyear +=1;
       
   140 	}
       
   141 	// Get months this year and next with at least one post
       
   142 	$future = $wpdb->get_results("SELECT
       
   143 		DISTINCT MONTH(post_date) AS month, YEAR(post_date) AS year
       
   144 		FROM $wpdb->posts
       
   145 		WHERE post_date >'$thisyear-".($thismonth+1)."-01'
       
   146 		AND post_type = 'post' AND post_status = 'future'
       
   147 		ORDER	BY post_date ASC");
       
   148 
       
   149 	foreach($future as $now){
       
   150 		get_future_calendar($now->month,$now->year);
       
   151 	}
       
   152 }
       
   153 
       
   154 
       
   155 // Calendar Output...
       
   156 function get_future_calendar( $thismonth ='', $thisyear='', $onclick=1, $initial=true ) {
       
   157 	global $wpdb, $timedifference, $wp_locale;
       
   158 	$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear);
       
   159 
       
   160 	// week_begins = 0 stands for Sunday
       
   161 	$week_begins = intval(get_option('start_of_week'));
       
   162 	$add_hours = intval(get_option('gmt_offset'));
       
   163 	$add_minutes = intval(60 * (get_option('gmt_offset') - $add_hours));
       
   164 
       
   165 	echo '<table class="wp-calendar">
       
   166 	<caption><em>' . $wp_locale->get_month($thismonth) . ' ' . $thisyear . '</em></caption>
       
   167 	<thead>
       
   168 	<tr>';
       
   169 
       
   170 	$myweek = array();
       
   171 
       
   172 	for ( $wdcount=0; $wdcount<=6; $wdcount++ ) {
       
   173 		$myweek[] = $wp_locale->get_weekday(($wdcount+$week_begins)%7);
       
   174 	}
       
   175 
       
   176 	foreach ( $myweek as $wd ) {
       
   177 		$day_name = (true == $initial) ? $wp_locale->get_weekday_initial($wd) : $wp_locale->get_weekday_abbrev($wd);
       
   178 		echo "\n\t\t<th abbr=\"$wd\" scope=\"col\" title=\"$wd\">$day_name</th>";
       
   179 	}
       
   180 
       
   181 	echo '
       
   182 	</tr>
       
   183 	</thead>
       
   184 	<tbody>
       
   185 	<tr>';
       
   186 
       
   187 	// Get days with posts
       
   188 	$dayswithposts = $wpdb->get_results("SELECT DISTINCT DAYOFMONTH(post_date)
       
   189 		FROM $wpdb->posts WHERE MONTH(post_date) = '$thismonth'
       
   190 		AND YEAR(post_date) = '$thisyear'
       
   191 		AND post_type = 'post' AND post_status = 'future'
       
   192 		AND post_date > '" . current_time('mysql') . '\'', ARRAY_N);
       
   193 	if ( $dayswithposts ) {
       
   194 		foreach ( $dayswithposts as $daywith ) {
       
   195 			$daywithpost[] = $daywith[0];
       
   196 		}
       
   197 	} else {
       
   198 		$daywithpost = array();
       
   199 	}
       
   200 
       
   201 
       
   202 
       
   203 	if ( strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'camino') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'safari') )
       
   204 		$ak_title_separator = "\n";
       
   205 	else
       
   206 		$ak_title_separator = ', ';
       
   207 
       
   208 	$ak_titles_for_day = array();
       
   209     //sets the Density Thermometer
       
   210 	$ak_posts_for_day = array();
       
   211 
       
   212 	$ak_post_titles = $wpdb->get_results("SELECT post_title, DAYOFMONTH(post_date) as dom "
       
   213 		."FROM $wpdb->posts "
       
   214 		."WHERE YEAR(post_date) = '$thisyear' "
       
   215 		."AND MONTH(post_date) = '$thismonth' "
       
   216 		."AND post_date > '".current_time('mysql')."' "
       
   217 		."AND post_type = 'post' AND post_status = 'future'"
       
   218 	);
       
   219 	if ( $ak_post_titles ) {
       
   220 		foreach ( $ak_post_titles as $ak_post_title ) {
       
   221 				if ( empty($ak_titles_for_day['day_'.$ak_post_title->dom]) )
       
   222 					$ak_titles_for_day['day_'.$ak_post_title->dom] = '';
       
   223 				if ( empty($ak_titles_for_day["$ak_post_title->dom"]) ) // first one
       
   224 					$ak_titles_for_day["$ak_post_title->dom"] = str_replace('"', '&quot;', wptexturize($ak_post_title->post_title));
       
   225 				else
       
   226 					$ak_titles_for_day["$ak_post_title->dom"] .= $ak_title_separator . str_replace('"', '&quot;', wptexturize($ak_post_title->post_title));
       
   227 
       
   228                 $ak_posts_for_day["$ak_post_title->dom"] +=1;
       
   229 
       
   230 		}
       
   231 	}
       
   232 
       
   233 
       
   234 	// See how much we should pad in the beginning
       
   235 	$pad = calendar_week_mod(date('w', $unixmonth)-$week_begins);
       
   236 	if ( 0 != $pad ) { echo "\n\t\t".'<td colspan="'.$pad.'" class="pad">&nbsp;</td>'; }
       
   237 
       
   238 	    //Determines the Density Thermometer colors
       
   239 	    $thermo = Array( "#BDFFBE", "#7AFFDE", "#2FEEFF", "#108BFF", "#0E72FF" );
       
   240 
       
   241 
       
   242 	$daysinmonth = intval(date('t', $unixmonth));
       
   243 	for ( $day = 1; $day <= $daysinmonth; ++$day ) {
       
   244 		if ( isset($newrow) && $newrow )
       
   245 			echo "\n\t</tr>\n\t<tr>\n\t\t";
       
   246 		$newrow = false;
       
   247 
       
   248 		if ( $day == gmdate('j', (time() + (get_option('gmt_offset') * 3600))) && $thismonth == gmdate('m', time()+(get_option('gmt_offset') * 3600)) && $thisyear == gmdate('Y', time()+(get_option('gmt_offset') * 3600)) )
       
   249 			echo '<td style="font-weight:bold;">';
       
   250 		else
       
   251 			echo '<td>';
       
   252 
       
   253 		if($onclick == 1){
       
   254 			$onclick1 = 'onclick="fcal_set_date('.$day.','.($thismonth-1).','.$thisyear.')"';
       
   255 		}
       
   256 
       
   257         // any posts on that day?
       
   258 		if ( in_array($day, $daywithpost) ) {
       
   259             //Outputs the Density Thermometer along with the day...
       
   260 			echo '<span style="background-color:'.($ak_posts_for_day[$day]<=Count($thermo) ? $thermo[$ak_posts_for_day[$day]-1] : $thermo[Count($thermo)-1]).';" title="'.$ak_titles_for_day[$day].' '.$onclick1.' >'.$day.'</span>';
       
   261 
       
   262 		} else {
       
   263 			echo '<span '.$onclick1.' >'.$day.'</span>';
       
   264         }
       
   265 		echo '</td>';
       
   266 
       
   267 		if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins) )
       
   268 			$newrow = true;
       
   269 	}
       
   270 
       
   271 	$pad = 7 - calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins);
       
   272 	if ( $pad != 0 && $pad != 7 )
       
   273 		echo "\n\t\t".'<td class="pad" colspan="'.$pad.'">&nbsp;</td>';
       
   274 
       
   275 	echo "\n\t</tr>\n\t</tbody>\n\t</table>";
       
   276 }
       
   277 ?>