Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active October 22, 2020 03:46
Show Gist options
  • Save andrasguseo/7cd929b9188bf83f1090fcc72a30ac95 to your computer and use it in GitHub Desktop.
Save andrasguseo/7cd929b9188bf83f1090fcc72a30ac95 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();
@skyshab
Copy link

skyshab commented Sep 14, 2018

Note that $this_category_term_id is used in the query for "terms" and removing that would likely cause an error. Also, this will delete all events that are not in the current import. This may not be desired if you have more than one import source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment