Autogenerated_Series::update_series_post_status( WP_Post $post, string $old_status, string $new_status )

Updates a Series post status if the Event post status is being updated.


Parameters

$post

(WP_Post) (Required) The Event post object.

$old_status

(string) (Required) The old Event post status.

$new_status

(string) (Required) The new Event post status.


Top ↑

Return

(int|WP_Error) The updated Series post ID, 0 if no Series was updated, or a WP_Error object.


Top ↑

Source

File: src/Events_Pro/Custom_Tables/V1/Series/Autogenerated_Series.php

	public function update_series_post_status( WP_Post $post, string $old_status, string $new_status ) {
		$event_post_id = Occurrence::normalize_id( $post->ID );

		if ( get_post_type( $event_post_id ) !== TEC::POSTTYPE ) {
			return 0;
		}

		$series = tec_series()
			->where( 'event_post_id', $event_post_id )
			->where( 'post_status', $old_status )
			->first();

		if ( ! $series instanceof WP_Post ) {
			return 0;
		}

		if ( ! tribe_is_truthy( get_post_meta( $series->ID, self::FLAG_META_KEY, true ) ) ) {
			return 0;
		}

		$series_post_status = get_post_status( $series->ID );

		if ( $series_post_status === $new_status ) {
			return 0;
		}

		// Only if all Series' Events will match this new status.
		global $wpdb;
		$events_table              = Events::table_name();
		$series_relationship_table = Series_Relationships::table_name();
		$query = "SELECT COUNT(*)
					FROM
					    {$wpdb->posts} AS event_post
					        INNER JOIN
					    {$events_table} ON event_post.ID = {$events_table}.post_id
					        INNER JOIN
					    {$series_relationship_table} ON {$series_relationship_table}.event_id = {$events_table}.event_id
					WHERE
					    {$series_relationship_table}.series_post_id = %d
					        AND event_post.post_status = %s";
		$query = $wpdb->prepare( $query, $series->ID, $new_status );

		// How many are in that status?
		$total_in_same_status = (int) $wpdb->get_var( $query );
		// How many total?
		$total_events = Relationship::where( 'series_post_id', $series->ID )->count();
		// If they aren't all in the same status, do not transition Autogenerated Series.
		if ( $total_events > 1 && $total_in_same_status !== $total_events ) {
			return 0;
		}

		// This update is happening because the Series is auto-generated: do not remove the flag.
		add_filter( 'tec_events_custom_tables_v1_remove_series_autogenerated_flag', '__return_false' );
		$updated = wp_update_post( [
			'ID'          => $series->ID,
			'post_status' => $new_status,
		] );
		// Update the checksum since the post status changed.
		update_post_meta( $series->ID, self::CHECKSUM_META_KEY, $this->calculate_post_checksum( $series ) );
		remove_filter( 'tec_events_custom_tables_v1_remove_series_autogenerated_flag', '__return_false' );

		if ( $updated instanceof WP_Error ) {
			do_action( 'tribe_log', 'error', __METHOD__, [
				'message'        => 'Error updating series post status following Event post status update.',
				'old_status'     => $old_status,
				'new_status'     => $new_status,
				'event_post_id'  => $event_post_id,
				'series_post_id' => $series->ID,
			] );
		}

		return $updated;
	}

Top ↑

Changelog

Changelog
Version Description
6.0.11 Introduced.