テストコード置き場

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' => __( '&hellip;' ),
			'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 '&hellip;'.
	 * @return string Post excerpt.
	 */
	function get_the_custom_excerpt( $content, $length = 120, $more = null ) {
		if ( null === $more ) {
			$more = __( '&hellip;' );
		}

		$content = preg_replace( '/<!--more-->.+/is', '', $content );
		$content = strip_shortcodes( $content );
		$content = strip_tags( $content );
		$content = str_replace( '&nbsp;', '', $content );
		if ( mb_strlen( $content ) > $length ) {
			$content = mb_substr( $content, 0, $length ) . ' ' . $more;
		}
		return $content;
	}

	add_shortcode( 'xo_event_list', 'xo_event_list_shortcode' );
}