cliff
11/15/2017 - 4:32 AM

The Events Calendar: Event Aggregator: Find imported iCal events missing the required UID.

The Events Calendar: Event Aggregator: Find imported iCal events missing the required UID.

<?php

/**
 * The Events Calendar: Event Aggregator: Find imported iCal events missing the
 * required UID.
 *
 * If logged-in as an Administrator: event post IDs with links to their wp-admin
 * edit screen will appear on your Front Page (will not work if your home page
 * is your Blog page).
 *
 * @link https://gist.github.com/cliffordp/63f285cf719eda762b3349bdf03401f9
 * @link https://theeventscalendar.com/support/forums/topic/scheduled-imports-not-working-11/#post-1383206
 */
add_filter( 'the_content', 'cliff_events_imported_by_ea_missing_uid', 50 );
function cliff_events_imported_by_ea_missing_uid( $content ) {
	if (
		// only display our results on the home page (won't appear if is_home()
		! is_front_page()
		// or current user is not an Administrator
		|| ! current_user_can( 'create_users' )
	) {
		return $content;
	}

	$args = array(
		'post_type'      => 'tribe_events',
		// 'post_status' => 'any',
		'posts_per_page' => - 1,
		'orderby'        => 'ID',
		// 'order' => 'ASC',
		'fields'         => 'ids', // just the post ID
		'meta_query'     => array(
			array(
				'key'     => '_EventOrigin',
				'value'   => 'event-aggregator',
				'compare' => '=',
			),
			array(
				'key'     => '_tribe_aggregator_origin',
				'value'   => array( 'ical', 'ics', 'gcal' ), // not 'facebook', 'meetup', or 'url', since those sources use their respective APIs instead of their iCal URLs
				'compare' => 'IN',
			),
			array(
				'key'     => '_uid',
				'compare' => 'NOT EXISTS', // MAY NEED TO INSTEAD LOOK FOR EQUALS ''
			),
		),
	);

	$ids = get_posts( $args );

	$output = '';

	if ( ! empty( $ids ) ) {
		$output .= PHP_EOL . 'iCal Events Imported by Event Aggregator missing their UID: ';

		foreach ( $ids as $key => $value ) {
			$output .= sprintf( '<a href="/wp-admin/post.php?post=%1$s&action=edit">%1$s</a>', $value ); // edit links

			// add comma if not last in array (this method only works for arrays with unique values)
			if ( $value !== end( $ids ) ) {
				$output .= ', ' . PHP_EOL;
			}
		}

		$output .= PHP_EOL;
		$output .= '<br><br>';
	}

	$content = $output . $content;

	return $content;
}