XO Event Calendar イベントリストショートコード(テスト用)
if ( defined( 'XO_EVENT_CALENDAR_VERSION' ) ) {
/**
* Shortcode that outputs an event list for the XO Event Calendar.
*
* @version 0.5.0
*
* @param array $attr {
* Array of default event list attributes.
*
* @type string $type Type of list to display. Accepts 'short', 'standard', 'detail' or Any character string. Default 'standard'.
* @type string $class CSS Class. Default 'xo-event-list'.
* @type string $before HTML displayed before the list. Default '<dl>'.
* @type string $after HTML displayed after the list. Default '</dl>'.
* @type string $format Display Format. Default empty.
* @type int $limit Amount of list to display. Default 10.
* @type string $order Designates ascending or descending order of items in the list. Accepts 'ASC', 'DESC'. Default 'DESC'.
* @type string $orderby Any column, or columns, to sort the list. Accepts 'date', 'modified', 'title', 'rand', 'event_start_date' or 'event_end_date'. Default 'date'.
* @type string $thumbnail_size Thumbnail image size to use. Accepts 'thumbnail', 'medium', 'large', 'full' or '(width),(height)'. Default 'thumbnail'.
* @type string $category Category slug, or comma-separated list of slugs. Default empty.
* @type int $default_thumbnail_id The default attached thumbnail ID. Default null.
* }
*
* @return string Event list output. Empty string if the passed type is unsupported.
*/
function xo_event_list_shortcode( $attr ) {
global $allowedposttags, $xo_event_calendar;
$atts = shortcode_atts( array(
'type' => 'standard',
'class' => 'xo-event-list',
'before' => '<dl>',
'after' => '</dl>',
'format' => '',
'limit' => '10',
'order' => 'DESC',
'orderby' => 'date',
'thumbnail_size' => 'thumbnail',
'category' => '',
'default_thumbnail_id' => null,
'excerpt_length' => 120,
'more' => __( '…' ),
'exclude_end_events' => '',
), $attr, 'xo_event_list' );
// Structure tags
$tags = array(
'%date%', // Post date
'%event_start_date%', // Event start date
'%event_end_date%', // Event end date
'%event_date%', // Event date
'%category%', // Category
'%category_color%', // Category background color
'%category_font_color%', // Category font color
'%permalink%', // Permalink
'%title%', // Title
'%thumbnail%', // Thumbnail image
'%excerpt%', // Excerpt
'%categories%', // Categories
);
$args = array(
'post_type' => XO_EVENT_CALENDAR_EVENT_POST_TYPE,
'post_status' => 'publish',
'posts_per_page' => $atts['limit'],
'order' => $atts['order'],
);
$class = esc_attr( $atts['class'] );
$before = wp_kses( wp_specialchars_decode( $atts['before'] ), $allowedposttags );
$after = wp_kses( wp_specialchars_decode( $atts['after'] ), $allowedposttags );
$type_class = '';
$format = '';
$default_thumbnail = '';
switch ( $atts['type'] ) {
case 'short':
$type_class = 'type-short';
if ( $atts['format'] ) {
$format = $atts['format'];
} else {
$format =
'<dt><span class="date">%date%</span></dt>' .
'<dd><span class="title"><a href="%permalink%">%title%</a></span></dd>';
}
break;
case 'standard':
$type_class = 'type-standard';
if ( $atts['format'] ) {
$format = $atts['format'];
} else {
$format =
'<dt><span class="date">%date%</span></dt>' .
'<dd>' .
'<span class="category" style="color:%category_font_color%;background-color:%category_color%;">%category%</span>' .
'<span class="title"><a href="%permalink%">%title%</a></span>' .
'</dd>';
}
break;
case 'detail':
$type_class = 'type-detail';
if ( $atts['format'] ) {
$format = $atts['format'];
} else {
$format =
'<dt><span class="thumbnail"><a href="%permalink%">%thumbnail%</a></span></dt>' .
'<dd>' .
'<p>' .
'<span class="date">%date%</span>' .
// '<span class="category" style="color:%category_font_color%;background-color:%category_color%;">%category%</span>' .
'%categories%' .
'</p>' .
'<p class="title"><a href="%permalink%">%title%</a></p>' .
'<p class="event-date">' . esc_html__( 'Event date:', 'xo-event-calendar' ) . ' %event_date%</p>' .
'<p class="excerpt">%excerpt%</p>' .
'</dd>';
}
break;
default:
$type_class = esc_attr( "type-{$atts['type']}" );
$format = wp_kses( wp_specialchars_decode( $atts['format'] ), $allowedposttags );
break;
}
if ( $atts['orderby'] == 'event_start_date' || $atts['orderby'] == 'event_end_date' ) {
$args['orderby'] = 'meta_value';
$args['meta_key'] = $atts['orderby'];
$args['type'] = 'DATE';
} else {
$args['orderby'] = $atts['orderby'];
}
if ( $atts['category'] ) {
$tarms = $categories = explode( ',', $atts['category'] );
$args['tax_query'] = array( array(
'taxonomy' => XO_EVENT_CALENDAR_EVENT_TAXONOMY,
'field' => 'slug',
'terms' => $tarms,
) );
}
if ( $atts['exclude_end_events'] ) {
$date = date_i18n( 'Y-m-d' );
$args['meta_query'] = array(
'event_end_date' => array( 'key' => 'event_end_date', 'value' => $date, 'compare' => '>=', 'type' => 'DATE' ),
);
}
$date_format = get_option( 'date_format' );
$has_category = ( strpos( $format, '%category%' ) !== false || strpos( $format, '%category_color%' ) !== false || strpos( $format, '%category_font_color%' ) !== false || strpos( $format, '%categories%' ) !== false );
$has_thumbnail = ( strpos( $format, '%thumbnail%' ) !== false );
if ( $has_thumbnail ) {
if ( strpos( $atts['thumbnail_size'], ',' ) !== false ) {
$sizes = explode( ',', $atts['thumbnail_size'] );
if ( count( $sizes ) === 2 ) {
$thumbnail_size = array( (int)$sizes[0], (int)$sizes[1] );
}
} else {
$thumbnail_size = $atts['thumbnail_size'];
}
if ( !$thumbnail_size ) {
$has_thumbnail = false;
}
}
if ( $has_thumbnail ) {
if ( ( $default_thumbnail_id = intval( $atts[ 'default_thumbnail_id' ] ) ) !== 0 ) {
$default_thumbnail = wp_get_attachment_image( $default_thumbnail_id, $thumbnail_size );
}
}
$has_excerpt = ( strpos( $format, '%excerpt%' ) !== false );
$output = '';
$get_posts = new WP_Query( $args );
while ( $get_posts->have_posts() ) {
$get_posts->the_post();
$post_id = $get_posts->post->ID;
if ( $has_thumbnail ) {
if ( has_post_thumbnail( $post_id ) ) {
$thumbnail = get_the_post_thumbnail( $post_id, $thumbnail_size );
} else {
$thumbnail = $default_thumbnail;
}
} else {
$thumbnail = '';
}
if ( $has_category ) {
$cats = get_the_terms( $post_id, XO_EVENT_CALENDAR_EVENT_TAXONOMY );
if ( $cats && ! is_wp_error( $cats ) && count( $cats ) > 0 ) {
$cat = $cats[0];
$category = $cat->name;
$cat_data = get_option( 'xo_event_calendar_cat_' . intval( $cat->term_id ) );
$cat_color = esc_html( $cat_data['category_color'] );
$category_color = $cat_color ? $cat_color : '#ccc';
$hsv = XO_Color::getHsv( XO_Color::getRgb( $category_color ) );
$category_font_color = $hsv['v'] > 0.8 ? '#333' : '#eee';
$categories = '';
foreach ( $cats as $cat ) {
$category = $cat->name;
$cat_data = get_option( 'xo_event_calendar_cat_' . intval( $cat->term_id ) );
$cat_color = esc_html( $cat_data['category_color'] );
$category_color = $cat_color ? $cat_color : '#ccc';
$hsv = XO_Color::getHsv( XO_Color::getRgb( $category_color ) );
$category_font_color = $hsv['v'] > 0.8 ? '#333' : '#eee';
$categories .= '<span class="category" style="color:' . $category_font_color . ';background-color:' . $category_color . ';">' . $cat->name . '</span>';
}
}
} else {
$category = '';
$category_color = '';
$category_font_color = '';
$categories = '';
}
if ( $has_excerpt ) {
$excerpt = get_the_custom_excerpt( get_the_content(), $atts['excerpt_length'], $atts['more'] );
} else {
$excerpt = '';
}
$values = array(
get_the_date(),
mysql2date( $date_format, get_post_meta( $post_id, 'event_start_date', true ) ),
mysql2date( $date_format, get_post_meta( $post_id, 'event_end_date', true ) ),
$xo_event_calendar->get_event_date( $post_id ),
$category,
$category_color,
$category_font_color,
get_permalink(),
get_the_title(),
$thumbnail,
$excerpt,
$categories,
);
$line = str_replace( $tags, $values, $format );
$output .= "{$before}{$line}{$after}";
}
wp_reset_postdata();
$output = "<div class=\"{$class} {$type_class}\">{$output}</div>\n";
return $output;
}
/**
* Retrieves the post excerpt.
*
* @since 0.5.0
*
* @param string $content Post content.
* @param int $length Optional. Maximum length of the excerpt. Default 120 characters.
* @param string $more Optional. What to append if $content needs to be trimmed. Default '…'.
* @return string Post excerpt.
*/
function get_the_custom_excerpt( $content, $length = 120, $more = null ) {
if ( null === $more ) {
$more = __( '…' );
}
$content = preg_replace( '/<!--more-->.+/is', '', $content );
$content = strip_shortcodes( $content );
$content = strip_tags( $content );
$content = str_replace( ' ', '', $content );
if ( mb_strlen( $content ) > $length ) {
$content = mb_substr( $content, 0, $length ) . ' ' . $more;
}
return $content;
}
add_shortcode( 'xo_event_list', 'xo_event_list_shortcode' );
}
XO Event Calendar イベント日付で休日設定を反映した jQuery Datepicker を使用するサンプルコード
if ( defined( 'XO_EVENT_CALENDAR_URL' ) ) {
add_action( 'admin_enqueue_scripts', function() {
wp_enqueue_style( 'jquery-ui', XO_EVENT_CALENDAR_URL . 'jquery-ui/jquery-ui.min.css' );
wp_enqueue_script( 'jquery-ui-datepicker' );
wp_enqueue_script( 'jquery-ui-datepicker-ja', XO_EVENT_CALENDAR_URL . 'jquery-ui/datepicker-ja.js', array( 'jquery-ui-datepicker' ), false, true );
$holiday_settings = get_option( 'xo_event_calendar_holiday_settings' );
$holiday = 'all'; // 休日の名前
$json_weeks = '[]';
$json_special_holidays = '[]';
$json_non_holidays = '[]';
if ( $holiday_settings ) {
if ( isset( $holiday_settings[$holiday] ) ) {
$weeks = array();
foreach ( array_values( $holiday_settings[$holiday]['dayofweek'] ) as $key => $value ) {
if ( $value ) {
$weeks[] = $key;
}
}
$json_weeks = json_encode( $weeks );
if ( isset( $holiday_settings[$holiday]['special_holiday'] ) ) {
$json_special_holidays = json_encode( explode( "\n", trim( $holiday_settings[$holiday]['special_holiday'] ) ) );
}
if ( isset( $holiday_settings[$holiday]['non_holiday'] ) ) {
$json_non_holidays = json_encode( explode( "\n", trim( $holiday_settings[$holiday]['non_holiday'] ) ) );
}
}
}
$script = <<<EOT
(function ($) {
const weeks = {$json_weeks};
const specialHolidays = {$json_special_holidays};
const nonHolidays = {$json_non_holidays};
$('#event_start_date').removeAttr('type');
$('#event_end_date').removeAttr('type');
$('.datepicker').datepicker({
dateFormat: 'yy-mm-dd',
beforeShowDay: function (date) {
var holiday = false,
formatDate = date.getFullYear() + '-' + ('0'+(date.getMonth() + 1)).slice(-2) + '-' + ('0'+(date.getDate())).slice(-2);
if (-1 !== weeks.indexOf(date.getDay())) {
holiday = true;
}
if (-1 !== specialHolidays.indexOf(formatDate)) {
holiday = true;
}
if (-1 !== nonHolidays.indexOf(formatDate)) {
holiday = false;
}
return holiday ? [false, 'ui-state-disabled'] : [true, ''];
}
});
})(jQuery);
EOT;
wp_add_inline_script( 'jquery-ui-datepicker-ja', $script );
} );
}
XO Event Calendar is_holiday ショートコード
add_action( 'init', function() {
add_shortcode( 'is_holiday', function( $args ) {
$args = shortcode_atts( array(
'holidays' => null,
'date' => date_i18n( 'Y-m-d' ),
'message' => '営業です。',
'holiday_message' => '休日です。',
), $args );
$weeks_table = array( 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' );
// 休日設定の設定情報を取得
$settings = get_option( 'xo_event_calendar_holiday_settings' );
$html = '';
if ( $settings ) {
$holidays = array();
if ( $args['holidays'] ) {
$holidays = explode( ',', $args['holidays'] );
} else {
foreach ( $settings as $key => $value ) {
$holidays[] = $key;
}
}
$date = date( 'Y-m-d', strtotime( ( $args['date'] ) ? $args['date'] : date_i18n( 'Y-m-d' ) ) );
$week = $weeks_table[date( 'w', strtotime( $date ) )];
$is_holiday = false;
foreach ( $holidays as $holiday ) {
if ( isset( $settings[$holiday] ) ) {
$setting = $settings[$holiday];
if ( $setting['dayofweek'][$week] ) {
$is_holiday = true;
}
if ( isset( $setting['special_holiday'] ) ) {
if ( in_array( $date, explode( "\n", trim( $setting['special_holiday'] ) ) ) ) {
$is_holiday = true;
}
}
if ( isset( $setting['non_holiday'] ) ) {
if ( in_array( $date, explode( "\n", trim( $setting['non_holiday'] ) ) ) ) {
$is_holiday = false;
}
}
}
if ( $is_holiday ) {
break;
}
}
if ( $is_holiday ) {
$html = $args['holiday_message'];
} else {
$html = $args['message'];
}
} else {
$html = '休日設定が設定されていません。';
}
return $html;
} );
} );