Forked from andrasguseo/delete-events-before-import.php
Created
April 2, 2019 16:48
-
-
Save hiphopsmurf/3a58eae50a580cd74211ab2cbee24a4f to your computer and use it in GitHub Desktop.
Event Aggregator - delete events before import
This file contains hidden or 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