Last active
August 29, 2015 14:05
-
-
Save marushu/6b363a3b808e2351da9f to your computer and use it in GitHub Desktop.
イベントカレンダーをget_calendarコピってきてやってみたんだけど、テーブル書き出す所で今ひとつわかんないところ…。。何度も書き出してしまう所を何とかしたいんだけどなぁ…(^0^;)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* from /wp-include/general-template : get_calendar | |
* | |
* @since 1.5.0 | |
* | |
* @param int $num Number of day. | |
* @return int Days since the start of the week. | |
*/ | |
function monthly_calendar_cosme_week_mod($num) { | |
$base = 7; | |
return ($num - $base*floor($num/$base)); | |
} | |
/** | |
* Display calendar with days that have posts as links. | |
* | |
* The calendar is cached, which will be retrieved, if it exists. If there are | |
* no posts for the month, then it will not be displayed. | |
* | |
* @since 1.0.0 | |
* @uses calendar_week_mod() | |
* | |
* @param bool $initial Optional, default is true. Use initial calendar names. | |
* @param bool $echo Optional, default is true. Set to false for return. | |
* @return string|null String when retrieving, null when displaying. | |
*/ | |
function get_monthly_cosme_calendar($initial = true, $echo = true) { | |
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts; | |
$cache_cosme = array(); | |
$key_cosme = md5( $m . $monthnum . $year ); | |
if ( $cache_cosme = wp_cache_get( 'get_monthly_cosme_calendar', 'calendar' ) ) { | |
if ( is_array($cache_cosme) && isset( $cache_cosme[ $key_cosme ] ) ) { | |
if ( $echo ) { | |
/** This filter is documented in wp-includes/general-template.php */ | |
echo apply_filters( 'get_calendar', $cache_cosme[$key_cosme] ); | |
return; | |
} else { | |
/** This filter is documented in wp-includes/general-template.php */ | |
return apply_filters( 'get_calendar', $cache_cosme[$key_cosme] ); | |
} | |
} | |
} | |
if ( !is_array($cache_cosme) ) | |
$cache_cosme = array(); | |
// Quick check. If we have no posts at all, abort! | |
if ( !$posts ) { | |
$gotsome_cosme = $wpdb->get_var("SELECT 1 as test FROM $wpdb->posts WHERE post_type = 'eventmonthly' AND post_status = 'publish' LIMIT 1"); | |
if ( !$gotsome_cosme ) { | |
$cache_cosme[ $key_cosme ] = ''; | |
wp_cache_set( 'get_calendar', $cache_cosme, 'calendar' ); | |
return; | |
} | |
} | |
if ( isset($_GET['w']) ) | |
$w = ''.intval($_GET['w']); | |
// week_begins = 0 stands for Sunday | |
$week_begins_cosme = intval(get_option('start_of_week')); // 管理画面 > 設定 > 一般 > 週の始まり が反映 | |
// Let's figure out when we are | |
if ( !empty($monthnum) && !empty($year) ) { | |
$thismonth = ''.zeroise(intval($monthnum), 2); | |
$thisyear = ''.intval($year); | |
} elseif ( !empty($w) ) { | |
// We need to get the month from MySQL | |
$thisyear = ''.intval(substr($m, 0, 4)); | |
$d = (($w - 1) * 7) + 6; //it seems MySQL's weeks disagree with PHP's | |
$thismonth = $wpdb->get_var("SELECT DATE_FORMAT((DATE_ADD('{$thisyear}0101', INTERVAL $d DAY) ), '%m')"); | |
} elseif ( !empty($m) ) { | |
$thisyear = ''.intval(substr($m, 0, 4)); | |
if ( strlen($m) < 6 ) | |
$thismonth = '01'; | |
else | |
$thismonth = ''.zeroise(intval(substr($m, 4, 2)), 2); | |
} else { | |
$thisyear = gmdate('Y', current_time('timestamp')); | |
$thismonth = gmdate('m', current_time('timestamp')); | |
} | |
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); | |
// 月3ケタのalphabet | |
$alpha_month = date( 'M', $unixmonth ); | |
$today = strtotime('today'); | |
// 月曜スタート | |
$w = date( "w", current_time('timestamp'))-1; // 曜日。0 (日曜)から 6 (土曜) | |
// 週の始まり | |
if( is_front_page() ) { | |
$beginning_week_date = date('j', strtotime("-{$w} day", current_time('timestamp'))); | |
} else { | |
$beginning_week_date = 1; | |
} | |
// 週の終わり | |
if( is_front_page() ) { | |
$last_week_day = date('j', strtotime('saturday', strtotime("-{$w} day", current_time('timestamp')))); | |
$last_week_day = $last_week_day + 1; // 週の終わりを日曜日に | |
} else { | |
$unixmonth = mktime(0, 0 , 0, $thismonth, 1, $thisyear); | |
$last_day = date('t', $unixmonth); | |
$last_week_day = $last_day; | |
// 週の終わりを日曜日に | |
//$last_week_day = $last_week_day + 1; // 週の終わりを日曜日に | |
// 末日 | |
$last_day = date('t', $unixmonth); | |
} | |
// Get the next and previous month and year with at least one post | |
$previous = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year | |
FROM $wpdb->posts | |
WHERE post_date < '$thisyear-$thismonth-01' | |
AND post_type = 'eventmonthly' AND post_status = 'publish' | |
ORDER BY post_date DESC | |
LIMIT 1"); | |
$next = $wpdb->get_row("SELECT MONTH(post_date) AS month, YEAR(post_date) AS year | |
FROM $wpdb->posts | |
WHERE post_date > '$thisyear-$thismonth-{$last_week_day} 23:59:59' | |
AND post_type = 'eventmonthly' AND post_status = 'publish' | |
ORDER BY post_date ASC | |
LIMIT 1"); | |
$check_start_day = sprintf( "%02d", $beginning_week_date ); | |
$previous_check = ''; | |
$next_check = ''; | |
// Check this article >> http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query | |
$querystr = " | |
SELECT $wpdb->posts.* | |
FROM $wpdb->posts, $wpdb->postmeta | |
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id | |
AND $wpdb->postmeta.meta_key = 'event_date' | |
AND $wpdb->postmeta.meta_value < '{$thisyear}{$thismonth}{$check_start_day}' | |
AND $wpdb->posts.post_status = 'publish' | |
AND $wpdb->posts.post_type = 'eventmonthly' | |
ORDER BY $wpdb->postmeta.meta_value DESC | |
LIMIT 1 | |
"; | |
$previous_obj = $wpdb->get_results( $querystr, ARRAY_N ); | |
if( ! empty( $previous_obj ) ) { $previous_check = get_post_meta( $previous_obj[0][0], 'event_date', true ); } | |
//echo "<pre>"; | |
//print_r( $previous_check ); | |
//echo "</pre>"; | |
//echo "<pre>"; | |
//print_r( get_post_status( $previous_check->post_id ) ); | |
//echo "</pre>"; | |
//$next_check = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key = 'event_date' AND meta_value > '{$thisyear}{$thismonth}{$last_week_day} 23:59:59' LIMIT 1"); | |
$querystr = " | |
SELECT $wpdb->posts.* | |
FROM $wpdb->posts, $wpdb->postmeta | |
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id | |
AND $wpdb->postmeta.meta_key = 'event_date' | |
AND $wpdb->postmeta.meta_value > '{$thisyear}{$thismonth}{$last_week_day} 23:59:59' | |
AND $wpdb->posts.post_status = 'publish' | |
AND $wpdb->posts.post_type = 'eventmonthly' | |
ORDER BY $wpdb->postmeta.meta_value ASC | |
LIMIT 1 | |
"; | |
$next_obj = $wpdb->get_results( $querystr, ARRAY_N ); | |
if( ! empty( $next_obj ) ) { $next_check = get_post_meta( $next_obj[0][0], 'event_date', true ); } | |
//echo "<pre>"; | |
//print_r( $next_check ); | |
//echo "</pre>"; | |
// calendar content wrap | |
$calendar_output_cosme = '<div id="event_calendar_wrap">'; | |
// previous event year, month | |
if( ! empty( $previous_check ) ) { $prev_year = substr( $previous_check, 0, 4 ); } | |
if( ! empty( $previous_check ) ) { $prev_month = substr( $previous_check, 4, 2 ); } | |
// next event year, month | |
if( ! empty ( $next_check ) ) { $next_year = substr( $next_check, 0, 4 ); } | |
if( ! empty( $next_check ) ) { $next_month = substr( $next_check, 4, 2 ); } | |
if( ! is_front_page() ) { | |
$calendar_output_cosme .= '<h1 class="calendar_month"><img src="' . get_template_directory_uri() . '/images/calendar/cal_' . esc_attr( intval( $thismonth ) ) . '.png" width="auto" height="auto" alt="' . esc_attr( intval( $thismonth ) ) . '" /><p><span class="mon_alpha">' . $alpha_month . '</span><span class="year_num">' . esc_attr( $thisyear ) . '</span></p></h1>'; | |
} | |
// 前月, 次月のリンク出力 | |
if( ! is_front_page() ) { | |
if ( $previous_check ) { | |
$calendar_output_cosme .= sprintf( | |
'<p id="prev"><a href="%1$s/date/%2$s/%3$s" title="%2$s年%3$s月へ">%5$s 前の月へ</a></p>', | |
home_url( '/eventmonthly' ), | |
$prev_year, | |
$prev_month, | |
esc_attr( intval( $prev_month ) ), | |
sprintf( | |
'<img src="%1$s/images/calendar/cal_arrow_prev.png" alt="cal_arrow_prev" width="10" height="13" />', | |
get_template_directory_uri() | |
) | |
); | |
} else { | |
$calendar_output_cosme .= ''; | |
} | |
if ( $next_check ) { | |
$calendar_output_cosme .= sprintf( | |
'<p id="next"><a href="%1$s/date/%2$s/%3$s" title="%2$s年%3$s月へ">次の月へ %5$s</a></p>', | |
home_url( '/eventmonthly' ), | |
$next_year, | |
$next_month, | |
esc_attr( intval( $next_month ) ), | |
sprintf( | |
'<img src="%1$s/images/calendar/cal_arrow_next.png" alt="cal_arrow_next" width="10" height="13" />', | |
get_template_directory_uri() | |
) | |
); | |
} else { | |
$calendar_output_cosme .= ''; | |
} | |
} | |
/* translators: Calendar caption: 1: month name, 2: 4-digit year */ | |
$calendar_caption = _x('%1$s %2$s', 'calendar caption'); | |
$calendar_output_cosme .= '<table id="cosme_event_calendar"> | |
<caption>' . sprintf($calendar_caption, $wp_locale->get_month($thismonth), date('Y', $unixmonth)) . '</caption>'; | |
$myweek = array(); | |
// カスタムフィールド event_date から紐付けられた投稿オブジェクトを抽出 | |
//$ak_post_titles = $wpdb->get_results("SELECT* FROM $wpdb->postmeta WHERE meta_key = 'event_date'", OBJECT); | |
//echo "<pre>"; | |
//print_r( $ak_post_titles ); | |
//echo "</pre>"; | |
$querystr = " | |
SELECT $wpdb->posts.* | |
FROM $wpdb->posts, $wpdb->postmeta | |
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id | |
AND $wpdb->postmeta.meta_key = 'event_date' | |
AND $wpdb->posts.post_status = 'publish' | |
AND $wpdb->posts.post_type = 'eventmonthly' | |
"; | |
$ak_post_titles = $wpdb->get_results( $querystr, OBJECT ); | |
//echo "<pre>"; | |
//print_r( $ak_post_titles_obj ); | |
//echo "</pre>"; | |
$daysinmonth = intval(date('W', $unixmonth)); | |
// 月間テーブル用配列を準備 | |
$i = 0; | |
$databox = array(); | |
for ( $day = $beginning_week_date; $day <= $last_week_day; ++$day ) { | |
$databox[$i] = $day; | |
$i++; | |
} | |
$i = 0; | |
for ( $day = $beginning_week_date; $day <= $last_week_day; ++$day, $i++ ) { | |
// 日付から曜日を割り出す | |
$year = $thisyear; | |
$month = $thismonth; | |
$day = $day; | |
$datetime = new DateTime(); | |
$datetime->setDate( $year, $month, $day ); | |
$week = array( "日", "月", "火", "水", "木", "金", "土" ); | |
$wee = ( int )$datetime->format( 'w' ); | |
// 日曜(w=0)ならば | |
if( $wee == 0 ) { | |
$calendar_output_cosme .= "<tr class='cal_sunday'>"; | |
// 土曜(w=6)ならば | |
} elseif( $wee == 6 ) { | |
$calendar_output_cosme .= "<tr class='cal_saturday'>"; | |
} else { | |
$calendar_output_cosme .= "<tr>"; | |
} | |
$newrow = false; | |
// 日付欄を準備 | |
if ( $day == gmdate('j', current_time('timestamp')) && $thismonth == gmdate('m', current_time('timestamp')) && $thisyear == gmdate('Y', current_time('timestamp')) ) | |
$calendar_output_cosme .= '<td id="today">'; | |
else | |
$calendar_output_cosme .= '<td>'; | |
$calendar_output_cosme .= $day; | |
$calendar_output_cosme .= '</td>'; // 日付終わり | |
// 日付から曜日を割り出す | |
//$year = $thisyear; | |
//$month = $thismonth; | |
//$day = $day; | |
//$datetime = new DateTime(); | |
//$datetime->setDate( $year, $month, $day ); | |
//$week = array( "日", "月", "火", "水", "木", "金", "土" ); | |
//$wee = ( int )$datetime->format( 'w' ); | |
// 日曜(w=0)ならば | |
if( $wee == 0 ) { | |
$tdtag = "<td class='cal_sunday'>"; | |
// 土曜(w=6)ならば | |
} elseif( $wee == 6 ) { | |
$tdtag = "<td class='cal_saturday'>"; | |
// その他平日 | |
} else { | |
$tdtag = "<td>"; | |
} | |
// 曜日欄を準備 | |
$calendar_output_cosme .= "$tdtag"; | |
$calendar_output_cosme .= $week[ $wee ]; | |
$calendar_output_cosme .= "</td>"; | |
//echo "<pre><h1>databox</h1>"; | |
//print_r( $databox ); | |
//echo "</pre>"; | |
if ( in_array($day, $databox) ) { | |
$calendar_output_cosme .= "<td>"; | |
foreach( $ak_post_titles as $ak_post_title ) { | |
//echo "<pre>"; | |
//print_r( $ak_post_title ); | |
//echo "</pre>"; | |
// prepare acf field datas | |
$event_date = get_field( 'event_date', $ak_post_title->ID ); | |
//echo "<pre>event_date<br />"; | |
//print_r( $event_date ); | |
//echo "</pre>"; | |
// カスタムフィールドの日付の2桁を抽出 | |
$for_check_date_2 = substr( $event_date, -2 ); | |
// prepare acf url field | |
$event_url = esc_url( get_field( 'event_url', $ak_post_title->ID ) ); | |
//echo "<pre>event_url<br />"; | |
//print_r( $event_url ); | |
//echo "</pre>"; | |
// カスタムフィールドの日付の6桁(年、月)を抽出 | |
$for_check_date = substr( $event_date, 0, 6 ); | |
//echo "<pre>for_check_date<br />"; | |
//print_r( $for_check_date ); | |
//echo "</pre>"; | |
// | |
//echo "<pre>"; | |
//print_r( $thisyear ); | |
//echo "</pre>"; | |
//echo "<pre>"; | |
//print_r( $thismonth ); | |
//echo "</pre>"; | |
// | |
//echo "<pre>"; | |
//print_r( $thisyear . $thismonth ); | |
//echo "</pre>"; | |
//echo "<pre>"; | |
//print_r( $for_check_date ); | |
//echo "</pre>"; | |
// domとカスタムフィールドの日付を合わせる | |
$ak_post_title->dom = $for_check_date; | |
// 日付とマッチした欄へイベントを出力 | |
$ymd_string = $thisyear . $thismonth . $day; | |
$postmeta_date = get_post_meta( $ak_post_title->ID , 'event_date', true); | |
//$published_check = get_post_status( $ak_post_title->ID ); | |
if( ( ( intval( $for_check_date_2 ) == $databox[$i] ) && ( $thisyear . $thismonth ) == $for_check_date ) ) { | |
$terms = wp_get_object_terms( $ak_post_title->ID, 'event_cat' ); | |
$term_class = array(); | |
foreach( $terms as $term ) { | |
$term_class[] = $term->slug; | |
$term_class[] = 'cal_term'; | |
} | |
// リンクを準備 | |
$link_with_title = sprintf( | |
'<p><a class="%3$s" href="%1$s" target="_blank">%2$s</a></p>', | |
esc_url( $event_url ), | |
esc_attr( get_the_title( $ak_post_title->ID ) ), | |
implode( ' ', $term_class ) | |
); | |
// カレンダーのhtmlへ | |
$calendar_output_cosme .= $link_with_title; | |
} | |
} | |
$calendar_output_cosme .= "</td>"; | |
} | |
if ( 6 == calendar_week_mod(date('w', mktime(0, 0 , 0, $thismonth, $day, $thisyear))-$week_begins_cosme) ) | |
$newrow = true; | |
$calendar_output_cosme .= '</tr>'; | |
} | |
$calendar_output_cosme .= "\n\t\n\t\n\t</table>"; | |
// end div | |
$calendar_output_cosme .= '</div>'; | |
$cache_cosme[ $key_cosme ] = $calendar_output_cosme; | |
wp_cache_set( 'get_calendar', $cache_cosme, 'calendar' ); | |
if ( $echo ) { | |
/** | |
* Filter the HTML calendar output. | |
* | |
* @since 3.0.0 | |
* | |
* @param string $calendar_output_cosme HTML output of the calendar. | |
*/ | |
echo apply_filters( 'get_calendar', $calendar_output_cosme ); | |
} else { | |
/** This filter is documented in wp-includes/general-template.php */ | |
return apply_filters( 'get_calendar', $calendar_output_cosme ); | |
} | |
} | |
/** | |
* Purge the cached results of get_calendar. | |
* | |
* @see get_calendar | |
* @since 2.1.0 | |
*/ | |
function delete_get_monthly_calendar_cosme_cache() { | |
wp_cache_delete( 'get_monthly_cosme_calendar', 'calendar' ); | |
} | |
add_action( 'save_post', 'delete_get_calendar_cache' ); | |
add_action( 'delete_post', 'delete_get_calendar_cache' ); | |
add_action( 'update_option_start_of_week', 'delete_get_calendar_cache' ); | |
add_action( 'update_option_gmt_offset', 'delete_get_calendar_cache' ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment