Tribe__Events__Aggregator__Service::post_import( array $args )
Creates an import
Contents
Note: This method exists because WordPress by default doesn’t allow multipart/form-data with boundaries to happen
Parameters
- $args
-
(array) (Required) Array of arguments. See REST docs for details. 1 exception listed below:
- 'source_file'
(array) Source file array using the $_FILES array values
- 'source_file'
Return
(string)
Source
File: src/Tribe/Aggregator/Service.php
public function post_import( $args ) {
$api = $this->api();
// if the user doesn't have a license key, don't bother hitting the service
if ( is_wp_error( $api ) ) {
return $api;
}
$args = $this->apply_import_limit( $args );
/**
* Allows filtering to add a PUE key to be passed to the EA service
*
* @since 4.6.18
*
* @param bool|string $pue_key PUE key
* @param array $args Arguments to queue the import
* @param self $record Which record we are dealing with
*/
$licenses = apply_filters( 'tribe_aggregator_service_post_pue_licenses', array(), $args, $this );
// If we have a key we add that to the Arguments
if ( ! empty( $licenses ) ) {
$args['licenses'] = $licenses;
}
/**
* Allows filtering to add other arguments to be passed to the EA service.
*
* @since 4.6.24
*
* @param array $args Arguments to queue the import.
* @param self $record Which record we are dealing with.
*/
$args = apply_filters( 'tribe_aggregator_service_post_import_args', $args, $this );
$request_args = array(
'body' => $args,
);
if ( isset( $args['file'] ) ) {
$boundary = wp_generate_password( 24 );
$headers = array(
'content-type' => 'multipart/form-data; boundary=' . $boundary,
);
$payload = array();
foreach ( $args as $name => $value ) {
if ( 'file' === $name ) {
continue;
}
if ( 'source' === $name ) {
continue;
}
$payload[] = '--' . $boundary;
$payload[] = 'Content-Disposition: form-data; name="' . $name . '"'. "\r\n";
$payload[] = $value;
}
$file_path = null;
$file_name = null;
if ( is_numeric( $args['file'] ) ) {
$file_id = absint( $args['file'] );
$file_path = get_attached_file( $file_id );
if ( ! file_exists( $file_path ) ) {
$file_path = null;
} else {
$file_name = basename( $file_path );
}
} elseif ( ! empty( $args['file']['tmp_name'] ) && ! empty( $args['file']['name'] ) ) {
if ( file_exists( $args['file']['tmp_name'] ) ) {
$file_path = $args['file']['tmp_name'];
$file_name = basename( $args['file']['name'] );
}
}
if ( $file_path && $file_name ) {
$payload[] = '--' . $boundary;
$payload[] = 'Content-Disposition: form-data; name="source"; filename="' . $file_name . '"' . "\r\n";
$payload[] = file_get_contents( $file_path );
$payload[] = '--' . $boundary . '--';
}
$args = array(
'headers' => $headers,
'body' => implode( "\r\n", $payload ),
);
} else {
$args = $request_args;
}
$response = $this->post( 'import', $args );
return $response;
}