Tribe__Events__Aggregator__Tabs__Abstract::handle_submit()


Source

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

	public function handle_submit() {
		$data = array(
			'message' => __( 'There was a problem processing your import. Please try again.', 'the-events-calendar' ),
		);

		if ( ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && ! $this->is_active() ) {
			return;
		}

		if ( 'POST' !== $_SERVER['REQUEST_METHOD'] ) {
			return;
		}

		if ( empty( $_POST['aggregator'] ) ) {
			return;
		}

		// validate nonce
		if ( empty( $_POST['tribe_aggregator_nonce'] ) || ! wp_verify_nonce( $_POST['tribe_aggregator_nonce'], 'tribe-aggregator-save-import' ) ) {
			wp_send_json_error( $data );
		}

		$post_data = $_POST['aggregator'];

		if ( empty( $post_data['origin'] ) || empty( $post_data[ $post_data['origin'] ] ) ) {
			wp_send_json_error( $data );
		}

		$data = $post_data[ $post_data['origin'] ];

		// If we are dealing with Other URL made
		if ( 'url' === $post_data['origin'] ) {
			$new_origin = tribe( 'events-aggregator.settings' )->match_source_origin( $data['source'] );

			// If we found a valid new origin we overwrite
			if ( false !== $new_origin ) {
				$post_data['origin'] = $new_origin;
			}
		}

		$record = Tribe__Events__Aggregator__Records::instance()->get_by_origin( $post_data['origin'] );

		$meta = array(
			'origin'        => $post_data['origin'],
			'type'          => empty( $data['import_type'] ) ? 'manual' : $data['import_type'],
			'frequency'     => empty( $data['import_frequency'] ) ? null : $data['import_frequency'],
			'file'          => empty( $data['file'] ) ? null : $data['file'],
			'keywords'      => ! isset( $data['keywords'] ) ? null : trim( $data['keywords'] ),
			'location'      => ! isset( $data['location'] ) ? null : trim( $data['location'] ),
			'start'         => ! isset( $data['start'] ) ? null : trim( $data['start'] ),
			'end'           => ! isset( $data['end'] ) ? null : trim( $data['end'] ),
			'radius'        => empty( $data['radius'] ) ? null : $data['radius'],
			'source'        => empty( $data['source'] ) ? null : $data['source'],
			'source_type'   => empty( $data['source_type'] ) ? null : $data['source_type'],
			'content_type'  => empty( $data['content_type'] ) ? null : $data['content_type'],
			'schedule_day'  => empty( $data['schedule_day'] ) ? null : $data['schedule_day'],
			'schedule_time' => empty( $data['schedule_time'] ) ? null : $data['schedule_time'],
		);

		// Special source types can override source (Eventbrite current profile URL)
		if ( ! empty( $meta['source_type'] ) ) {
			$meta['source'] = $meta['source_type'];
		}

		// Only apply this verification when dealing with Creating new items
		if ( ! empty( $post_data['action'] ) && 'new' === $post_data['action'] ) {
			$hash = array_filter( $meta );

			// remove non-needed data from the Hash of the Record
			unset( $hash['schedule_day'], $hash['schedule_time'] );
			ksort( $hash );
			$hash = md5( maybe_serialize( $hash ) );

			/** @var Tribe__Events__Aggregator__Record__Abstract $match */
			$match = tribe( 'events-aggregator.records' )->find_by_data_hash( $meta['source'], $hash );

			if ( $match instanceof Tribe__Events__Aggregator__Record__Abstract ) {
				$url     = get_edit_post_link( $match->id );
				$anchor  = '<a href="' . esc_url( $url ) . '">' . esc_attr__( 'click here to edit it', 'the-events-calendar' ) . '</a>';
				$message = sprintf( __( 'A record already exists with these settings, %1$s.', 'the-events-calendar' ), $anchor );
				wp_send_json_error( array( 'message' => $message ) );
			}
		}

		$meta = $this->validate_meta_by_origin( $meta['origin'], $meta );

		if ( is_wp_error( $meta ) ) {
			/** @var WP_Error $validated */
			wp_send_json_error( $meta->get_error_message() );
		}

		return array(
			'record' => $record,
			'post_data' => $post_data,
			'meta' => $meta,
		);
	}