Created
November 29, 2024 15:29
-
-
Save andrasguseo/695ec2df1f3f872e9a13d4e1cdf23006 to your computer and use it in GitHub Desktop.
EA > If an event category is changed locally, don't import the category anymore.
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 | |
/** | |
* When importing from Other URL with Event Aggregator, only import/update the event | |
* category, if it hasn't been changed on the destination site yet. | |
* | |
* Usage: Add the snippet with a plugin like Code Snippets, or to the functions.php file. | |
* | |
* @author: Andras Guseo | |
* | |
* Plugins required: The Events Calendar, Event Aggregator | |
* | |
* @since November 29, 2024 Initial version. | |
*/ | |
/** | |
* Set a meta to record the manual term change. | |
*/ | |
add_action( 'set_object_terms', function( int $object_id, array $terms, array $tt_ids, string $taxonomy, bool $append, array $old_tt_ids ) { | |
// Bail, if not event. | |
if ( get_post_type( $object_id ) != 'tribe_events' ) { | |
return; | |
} | |
// Bail, if it's not about event categories. | |
if ( $taxonomy != 'tribe_events_cat') { | |
return; | |
} | |
// Bail, if it's an import. | |
if ( tribe_get_request_var( 'action' ) === 'tribe_queue_ea_import_events' ) { | |
return; | |
} | |
// If it's an inline-save or editpost... | |
if ( | |
( | |
tribe_get_request_var( 'action' ) === 'inline-save' | |
|| tribe_get_request_var( 'action' ) === 'editpost' | |
) | |
// ...and there was a change in terms. | |
&& $old_tt_ids != $tt_ids | |
) { | |
update_post_meta( $object_id, '_TermChanged', 1 ); | |
} | |
}, 10, 6 ); | |
// Remove categories from being imported. We'll do this later. | |
add_filter( 'tribe_aggregator_before_save_event', function( $event, $data ) { | |
$event['categories_source'] = $event['categories']; | |
unset( $event['categories'] ); | |
return $event; | |
}, 10, 2 ); | |
/** | |
* Maybe resave category if changed on import source. | |
* | |
* @param array $event Which Event data was sent. | |
* @param array $item Raw version of the data sent from EA. | |
* @param object $data The record we are dealing with. | |
*/ | |
add_action( 'tribe_aggregator_after_insert_post', function( $event, $item, $data ) { | |
// Bail, if terms for the event have changed on the destination site. | |
$term_changed = get_post_meta( $event['ID'], '_TermChanged', true ); | |
if ( $term_changed ) { | |
return; | |
} | |
// Remove all existing terms. | |
wp_delete_object_term_relationships( $event['ID'], Tribe__Events__Main::TAXONOMY ); | |
// Set up empty terms array. | |
$terms = []; | |
// Check if there are any terms to be imported. | |
if ( ! empty( $event['categories_source'] ) ) { | |
// Walk through each term. | |
foreach ( $event['categories_source'] as $cat ) { | |
// Check if terms already exist. If not, create them. | |
if ( ! $term = term_exists( $cat, Tribe__Events__Main::TAXONOMY ) ) { | |
$term = wp_insert_term( $cat, Tribe__Events__Main::TAXONOMY ); | |
if ( ! is_wp_error( $term ) ) { | |
$terms[] = (int) $term['term_id']; | |
} | |
} else { | |
$terms[] = (int) $term['term_id']; | |
} | |
} | |
} | |
// Normalize the list of terms. | |
$normalized_categories = tribe_normalize_terms_list( $terms, Tribe__Events__Main::TAXONOMY ); | |
// Save the terms. | |
wp_set_object_terms( $event['ID'], $normalized_categories, Tribe__Events__Main::TAXONOMY, false ); | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment