Tribe__Events__Pro__Repositories__Event::save( bool $return_promise = false )

Overrides the base method to save, along with the events, their additional recurring event instances.

This method will not try to "predict" the load like the base save method does. While the save method knows for certain how many events it will update the current recurrence implementation does not allow to have that forecast.


Parameters

$return_promise

(bool) (Optional) Whether to return a promise object or just the ids of the updated posts; if true then a promise will be returned whether the update is happening in background or not.

Default value: false


Top ↑

Return

(array|Tribe__Promise) A list of the post IDs that have been (synchronous) or will be (asynchronous) updated if $return_promise is set to false; the Promise object if $return_promise is set to true.


Top ↑

Source

File: src/Tribe/Repositories/Event.php

	public function save( $return_promise = false ) {
		$base_return = parent::save( $return_promise );

		if ( empty( $this->update_recurrence_payloads ) ) {
			return $base_return;
		}

		// Let's make sure we're iterating on arrays with an equal number of elements and same order.
		ksort( $this->update_recurrence_payloads );
		ksort( $this->update_postarrs );
		// Each payload should have a post array and viceversa.
		$valid_payloads = array_intersect_key( $this->update_recurrence_payloads, $this->update_postarrs );
		$valid_postarrs = array_intersect_key( $this->update_postarrs, $valid_payloads );

		$iterator = new MultipleIterator();
		$iterator->attachIterator( new ArrayIterator( array_keys( $valid_payloads ) ), 'post_id' );
		$iterator->attachIterator( new ArrayIterator( $valid_payloads ), 'recurrence_payload' );
		$iterator->attachIterator( new ArrayIterator( $valid_postarrs ), 'postarr' );

		set_error_handler( array( $this, 'cast_error_to_exception' ) );

		foreach ( $iterator as $item ) {
			list( $post_id, $recurrence_payload, $postarr ) = $item;

			$event_post = get_post( $post_id );

			if ( empty( $event_post ) || ! $event_post instanceof WP_Post ) {
				// Might have been deleted in the meanwhile, bail.
				continue;
			}

			try {

				/*
				 * During update callbacks might expect more information about the event.
				 * For the purpose of completeness and back-compatibility let's fetch more information from the event
				 * meta and apply the post array meta input on top of it to use the very last version.
				 */
				$event_meta = Tribe__Utils__Array::flatten(
					Tribe__Utils__Array::filter_prefixed( get_post_meta( $post_id ), '_Event' )
				);

				if ( isset( $postarr['meta_input'] ) ) {
					$event_meta = array_merge( $event_meta, $postarr['meta_input'] );
				}

				/*
				 * 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_payload = Tribe__Utils__Array::add_unprefixed_keys_to( array_merge( $event_meta, $recurrence_payload ) );

				$callback = $this->get_recurrence_update_callback( $event_post->ID, $recurrence_payload, $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();
			}
		}

		restore_error_handler();

		return $base_return;
	}

Top ↑

Changelog

Changelog
Version Description
4.7 Introduced.