Last active
October 22, 2020 03:46
-
-
Save andrasguseo/7cd929b9188bf83f1090fcc72a30ac95 to your computer and use it in GitHub Desktop.
Event Aggregator - delete events before import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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.