Tribe__Events__Pro__Repositories__Event::create()
Overrides the base create method to additionally create recurring events after the main event is saved.
Return
(false|WP_Post) The original return value from the base repository create method.
Source
File: src/Tribe/Repositories/Event.php
public function create() {
$event = parent::create();
// We cannot be 100% this will always be a post object, so let's just try to get it.
$event_post = get_post( $event );
if ( empty( $event ) || ! $event_post instanceof WP_Post ) {
// No sense in going on.
$this->create_recurrence_payload = false;
return $event;
}
try {
set_error_handler( array( $this, 'cast_error_to_exception' ) );
/*
* Many methods "down the road" might expect prefixed or un-prefixed meta keys, e.g. `_EventStartDate`
* and `EventStartDate` so we "duplicate" them now in the payload; this covers back-compatibility too.
* Recurrence handling methods should handle the case where the recurrence data is empty.
*/
if ( empty( $this->create_recurrence_payload ) ) {
// If the recurrence payload information is still empty then fill it up w/ the event meta.
$event_meta = Tribe__Utils__Array::flatten(
Tribe__Utils__Array::filter_prefixed( get_post_meta( $event_post->ID ), '_Event' )
);
$recurrence_payload = Tribe__Utils__Array::add_unprefixed_keys_to( $event_meta );
} else {
$recurrence_payload = Tribe__Utils__Array::add_unprefixed_keys_to( $this->create_recurrence_payload );
}
$callback = $this->get_recurrence_creation_callback( $event_post->ID, $recurrence_payload, $this->postarr );
/*
* Since the burden of logging and handling falls on the callback we're not collecting this value.
* Filtering callbacks might return empty or falsy values for other reasons than a failure; an
* exception is the correct way to signal an error.
*/
$callback( $event_post->ID, $recurrence_payload );
} catch ( Exception $e ) {
// Something happened, let's log and move on.
tribe( 'logger' )->log(
'There was an error updating the recurrence rules and/or exclusions for event ' . $event_post->ID . ': ' . $e->getMessage(),
Tribe__Log::ERROR,
__CLASS__
);
restore_error_handler();
return $event;
}
restore_error_handler();
return $event;
}
Changelog
| Version | Description |
|---|---|
| 4.7 | Introduced. |