Tribe__Events__Aggregator__Record__Abstract::queue_import( array $args = array() )

Queues the import on the Aggregator service

See also


Top ↑

Parameters

$args

(array) (Optional) Arguments to pass to the API.

Default value: array()


Top ↑

Return

(stdClass|WP_Error|int) A response object, a WP_Error instance on failure or a record post ID if the record had to be re-scheduled due to HTTP request limit.


Top ↑

Source

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

	public function queue_import( $args = array() ) {
		$aggregator = tribe( 'events-aggregator.main' );

		$is_previewing = (
			! empty( $_GET['action'] )
			&& (
				'tribe_aggregator_create_import' === $_GET['action']
				|| 'tribe_aggregator_preview_import' === $_GET['action']
			)
		);

		$error = null;

		$defaults = array(
			'type'                => $this->meta['type'],
			'origin'              => $this->meta['origin'],
			'source'              => isset( $this->meta['source'] ) ? $this->meta['source'] : '',
			'callback'            => $is_previewing ? null : home_url( '/event-aggregator/insert/?key=' . urlencode( $this->meta['hash'] ) ),
			'resolve_geolocation' => 1,
		);

		if ( ! empty( $this->meta['frequency'] ) ) {
			$defaults['frequency'] = $this->meta['frequency'];
		}

		if ( ! empty( $this->meta['file'] ) ) {
			$defaults['file'] = $this->meta['file'];
		}

		if ( ! empty( $this->meta['keywords'] ) ) {
			$defaults['keywords'] = $this->meta['keywords'];
		}

		if ( ! empty( $this->meta['location'] ) ) {
			$defaults['location'] = $this->meta['location'];
		}

		if ( ! empty( $this->meta['start'] ) ) {
			$defaults['start'] = $this->meta['start'];
		}

		if ( ! empty( $this->meta['end'] ) ) {
			$defaults['end'] = $this->meta['end'];
		}

		if ( ! empty( $this->meta['radius'] ) ) {
			$defaults['radius'] = $this->meta['radius'];
		}

		if ( ! empty( $this->meta['allow_multiple_organizers'] ) ) {
			$defaults['allow_multiple_organizers'] = $this->meta['allow_multiple_organizers'];
		}

		if ( empty( $this->meta['next_batch_hash'] ) ) {
			$next_batch_hash             = $this->generate_next_batch_hash();
			$defaults['next_batch_hash'] = $next_batch_hash;
			$this->update_meta( 'next_batch_hash', $next_batch_hash );
		}

		if ( $is_previewing ) {
			$defaults['preview'] = true;
		}

		$args = wp_parse_args( $args, $defaults );

		if ( ! empty( $args['start'] ) ) {
			$args['start'] = ! is_numeric( $args['start'] )
				? Tribe__Date_Utils::maybe_format_from_datepicker( $args['start'] )
				: date( Tribe__Date_Utils::DBDATETIMEFORMAT, $args['start'] );
		}

		if ( ! empty( $args['end'] ) ) {
			$args['end'] = ! is_numeric( $args['end'] )
				? Tribe__Date_Utils::maybe_format_from_datepicker( $args['end'] )
				: date( Tribe__Date_Utils::DBDATETIMEFORMAT, $args['end'] );
		}

		// Set site for origin(s) that need it for new token handling.
		if ( in_array( $args['origin'], array( 'eventbrite', 'facebook-dev' ), true ) ) {
			$args['site'] = site_url();
		}

		/**
		 * Allows customizing whether to resolve geolocation for events by the EA service.
		 *
		 * @since 4.6.25
		 *
		 * @param boolean $resolve_geolocation Whether the EA Geocode Address API is enabled for geocoding addresses.
		 * @param array   $args                Queued record import arguments to be sent to EA service.
		 */
		$resolve_geolocation = apply_filters( 'tribe_aggregator_resolve_geolocation', true, $args );

		if ( false === $resolve_geolocation ) {
			$args['resolve_geolocation'] = 0;
		}

		// create the import on the Event Aggregator service
		$response = $aggregator->api( 'import' )->create( $args );

		// if the Aggregator API returns a WP_Error, set this record as failed
		if ( is_wp_error( $response ) ) {
			// if the error is just a reschedule set this record as pending
			/** @var WP_Error $response */
			if ( 'core:aggregator:http_request-limit' === $response->get_error_code() ) {
				$this->should_queue_import( true );
				return $this->set_status_as_pending();
			} else {
				$error = $response;

				return $this->set_status_as_failed( $error );
			}
		}

		// if the Aggregator response has an unexpected format, set this record as failed
		if ( empty( $response->message_code ) ) {
			return $this->set_status_as_failed( tribe_error( 'core:aggregator:invalid-service-response' ) );
		}

		// if the Import creation was unsuccessful, set this record as failed
		if (
			'success:create-import' != $response->message_code
			&& 'queued' != $response->message_code
		) {
			$data = ! empty( $response->data ) ? $response->data : array();

			$error = new WP_Error(
				$response->message_code,
				Tribe__Events__Aggregator__Errors::build(
					esc_html__( $response->message, 'the-events-calendar' ),
					$data
				),
				$data
			);

			return $this->set_status_as_failed( $error );
		}

		// if the Import creation didn't provide an import id, the response was invalid so mark as failed
		if ( empty( $response->data->import_id ) ) {
			return $this->set_status_as_failed( tribe_error( 'core:aggregator:invalid-service-response' ) );
		}

		// only set as pending if we aren't previewing the record
		if ( ! $is_previewing ) {
			// if we get here, we're good! Set the status to pending
			$this->set_status_as_pending();
		}

		$service_supports_batch_push = ! empty( $response->batch_push );

		/**
		 * Whether batch pushing is supported for this record or not.
		 *
		 * @since 4.6.15
		 *
		 * @param bool $service_supports_batch_push Whether the Service supports batch pushing or not.
		 * @param Tribe__Events__Aggregator__Record__Abstract $this
		 */
		$allow_batch_push = apply_filters( 'tribe_aggregator_allow_batch_push', $service_supports_batch_push, $this );
		if ( $allow_batch_push ) {
			$this->update_meta( 'allow_batch_push', true );
		}

		// store the import id
		$this->update_meta( 'import_id', $response->data->import_id );
		$this->should_queue_import( false );

		return $response;
	}