Tribe__Events__Community__Submission_Handler::save()

Topics


Source #

File: src/Tribe/Submission_Handler.php

	public function save() {

		$events_label_singular           = tribe_get_event_label_singular();
		$events_label_singular_lowercase = tribe_get_event_label_singular_lowercase();

		$event = get_post( $this->event_id );

		// this entry is expected to be lowercase for the Tribe__Events__API
		if ( ! isset( $this->submission['venue'] ) && isset( $this->submission['Venue'] ) ) {
			$this->submission['venue'] = $this->submission['Venue'];
		}

		$submit_url = esc_url( $this->community->getUrl( 'add' ) );

		/**
		 * Allows to modify the default submission link on Community Events Submission Form
		 *
		 * @since 4.6.2
		 *
		 * @param string the link we want to modify
		 */
		$submit_url = apply_filters( 'tribe_events_community_submission_url', $submit_url );

		// if the post isn't an auto-draft, then we're updating a post. Otherwise, we'll consider it new
		if ( $this->event_id && 'auto-draft' !== $event->post_status ) {
			$saved = Tribe__Events__API::updateEvent( $this->event_id, $this->submission );
			if ( $saved ) {
				$this->add_message( esc_html( sprintf( __( '%s updated. ', 'tribe-events-community' ), $events_label_singular ) ) . $this->community->get_view_edit_links( $this->event_id ) );
				$this->add_message( '<a href="' . esc_url( $submit_url ) . '">' . esc_html( sprintf( __( 'Submit another %s', 'tribe-events-community' ), $events_label_singular_lowercase ) ) . '</a>' );
				do_action( 'tribe_community_event_updated', $this->event_id );
			} else {
				$this->add_message( esc_html( sprintf( __( 'There was a problem saving your %s, please try again.', 'tribe-events-community' ), $events_label_singular_lowercase ) ), 'error' );
			}
		} else {
			$this->submission['post_status'] = tribe( 'community.main' )->getOption( 'defaultStatus' );
			$this->submission['EventOrigin'] = 'community-events';

			// if we DO have an event ID, then it is an auto-draft, and thus a new post
			if ( $this->event_id ) {
				$saved = Tribe__Events__API::updateEvent( $this->event_id, $this->submission );
			} else {
				$saved = Tribe__Events__API::createEvent( $this->submission );
			}

			if ( $saved ) {
				$this->event_id = $saved;
				$this->add_message( sprintf( __( '%s submitted.', 'tribe-events-community' ), $events_label_singular ) . ' ' . $this->community->get_view_edit_links( $this->event_id ) );
				$this->add_message( '<a href="' . esc_url( $submit_url ) . '">' . esc_html( sprintf( __( 'Submit another %s', 'tribe-events-community' ), $events_label_singular_lowercase ) ) . '</a>' );
				do_action( 'tribe_community_event_created', $this->event_id );
			} else {
				$this->add_message( esc_html( sprintf( __( 'There was a problem submitting your %s, please try again.', 'tribe-events-community' ), $events_label_singular_lowercase ) ), 'error' );
			}
		}

		// Handles the Upload
		if (
			Tribe__Utils__Array::get( $_FILES, array( 'event_image', 'name' ) )
			&& ( Tribe__Utils__Array::get( $_FILES, array( 'event_image', 'size' ) ) <= $this->community->max_file_size_allowed() )
		) {
			$attachment_id = $this->insert_attachment( 'event_image', $this->event_id, true, $this );

			if ( false === $attachment_id ) {
				$this->event_id = false;
				return false;
			}
		}

		if (
			isset( $this->submission['detach_thumbnail'] )
			&& 'true' === (string) $this->submission['detach_thumbnail']
		) {
			delete_post_meta( $this->event_id, '_thumbnail_id' );
		}

		// Logged out or underprivileged users will not have terms automatically added during wp_insert_post
		if ( isset( $this->submission['tax_input'] ) ) {
			/**
			 * Allows new taxonomies to be saved using the default Methods for Community Events Edit page
			 * @param array $allowed_taxonomies
			 */
			$allowed_taxonomies = apply_filters( 'tribe_community_events_allowed_taxonomies', array( Tribe__Events__Main::TAXONOMY, 'post_tag' ) );

			foreach ( (array) $this->submission['tax_input'] as $taxonomy => $terms ) {
				// Skip the Non Valid taxonomies
				if ( ! in_array( $taxonomy, $allowed_taxonomies ) ) {
					continue;
				}

				// Fetch Tax Object
				$taxonomy_obj = get_taxonomy( $taxonomy );

				// If the user can assign terms we just skip this step
				if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) {
					continue;
				}

				wp_set_post_terms( $this->event_id, $terms, $taxonomy, true );
			}
		}

		return $this->event_id;
	}