Tribe__Events__Aggregator__Tabs__New::handle_import_finalize( $data )


Source

File: src/Tribe/Aggregator/Tabs/New.php

	public function handle_import_finalize( $data ) {
		$this->messages = array(
			'error'   => array(),
			'success' => array(),
			'warning' => array(),
		);

		$record = Tribe__Events__Aggregator__Records::instance()->get_by_import_id( $data['import_id'] );

		if ( tribe_is_error( $record ) ) {
			$this->messages['error'][] = $record->get_error_message();
			return $this->messages;
		}

		// Make sure we have a post status set no matter what
		if ( empty( $data['post_status'] ) ) {
			$data['post_status'] = tribe( 'events-aggregator.settings' )->default_post_status( $data['origin'] );
		}

		// If the submitted category is null, that means the user intended to de-select the default
		// category if there is one, so setting it to null is ok here
		$record->update_meta( 'category', empty( $data['category'] ) ? null : $data['category'] );
		$record->update_meta( 'post_status', $data['post_status'] );
		$record->update_meta( 'ids_to_import', empty( $data['selected_rows'] ) ? 'all' : json_decode( stripslashes( $data['selected_rows'] ) ) );

		// if we get here, we're good! Set the status to pending
		$record->set_status_as_pending();

		$record->finalize();

		if ( 'schedule' === $record->meta['type'] ) {
			$this->messages['success'][] = __( '1 import was scheduled.', 'the-events-calendar' );
			$create_schedule_result = $record->create_schedule_record();

			if ( is_wp_error( $create_schedule_result ) ) {
				$this->messages[ 'error' ][] = $create_schedule_result->get_error_message();

				tribe_notice( 'tribe-aggregator-import-failed', array( $this, 'render_notice_import_failed' ), 'type=error' );

				$record->set_status_as_failed( $create_schedule_result );
				return $create_schedule_result;
			}
		}

		$record->update_meta( 'interactive', true );

		if ( 'csv' === $data['origin'] ) {
            // here generate a global_id for the data
			$result = $record->process_posts( $data );
		} else {
			// let the record fetch the data and start immediately if possible
			$result = $record->process_posts( array(), true );
		}

		$result->record = $record;

		$this->messages = $this->get_result_messages( $result );

		if (
			empty( $this->messages['error'] )
			|| ! empty( $this->messages['success'] )
			|| ! empty( $this->messages['warning'] )
		) {
			tribe_notice( 'tribe-aggregator-import-complete', array( $this, 'render_notice_import_complete' ), 'type=success' );
		}
	}