Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active October 22, 2020 03:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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();
@joeykb
Copy link

joeykb commented Jul 20, 2018

I am in need of this function for my Event Calendar in Wordpress. However, I am not too familiar with where to put the file and make sure the function is executed. Would you be able to give me a very short tutorial on where to put this file and how to make sure it executes?
Thanks

@andrasguseo
Copy link
Author

Hi @joeykb
Sorry, only saw your comment now.
Create an empty file in your wp-content/plugins folder, like wp-content/plugins/import-fixer.php
Then copy the full contents which you see above in that file. Save it.
In your admin go to Plugins and activate the "Berean Baptist Church Calendar Import Fixer" plugin.
That should be it.
Note, I think it only works for events that have a category. If you want to use this for any and all events, then delete lines 24-28. Also note, I haven't tested this part. :)

@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