Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hiphopsmurf/3a58eae50a580cd74211ab2cbee24a4f to your computer and use it in GitHub Desktop.
Save hiphopsmurf/3a58eae50a580cd74211ab2cbee24a4f to your computer and use it in GitHub Desktop.
Event Aggregator - delete events before import
<?php
/**
* Created by PhpStorm.
* Plugin Name: Berean Baptist Church Calendar Import Fixer
* Description: Forces removal of events no longer represented in the imported calendar.
* Version: 0.0.1
* Author: Steve Dwire for Berean Baptist Church
* Author URI: https://www.berean-baptist.org/
* License: GPLv2
*/
class BBC_EC_ImportFixer {
const VERSION = '0.0.1';
public function __construct() {
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_action( 'tribe_aggregator_record_finalized', array( $this, 'handle_before_insert_posts'), 10, 2 );
}
public function handle_before_insert_posts($record_id, $record_meta ) {
$this_category_term_id = Tribe__Utils__Array::get( $record_meta, 'category', false );
if ( empty( $this_category_term_id ) ) {
return;
}
$relevant_existing_events = tribe_get_events( [
'fields' => 'ids',
'start_date' => date( 'Y-m-d H:i:s', time() ), // starting now or later
'tax_query' => [
[
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'term_id',
'terms' => [ $this_category_term_id ],
'operator' => 'IN'
]
],
'meta_key' => '_uid',
'meta_value' => $record_meta['ids_to_import'],
'meta_compare' => 'NOT IN'
] );
foreach ( $relevant_existing_events as $event_id ) {
wp_delete_post( $event_id, true );
}
}
}
new BBC_EC_ImportFixer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment