Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active March 12, 2024 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrasguseo/863c6de98b779df2aca1297f6f8f183b to your computer and use it in GitHub Desktop.
Save andrasguseo/863c6de98b779df2aca1297f6f8f183b to your computer and use it in GitHub Desktop.
TEC > Add support for Event Status to CSV import
<?php
/**
* Add support for Event Status to CSV import
*
* Usage:
* - Add the snippet to your functions.php file or with a plugin like Code Snippets.
*
* @author Andras Guseo
*
* Plugins required: The Events Calendar
* Created: March 11, 2024
*/
class tec_csv_import_custom_meta_data {
/**
* Define your custom data here.
* key => the meta key that will be used to save the data
* value => The column name used for column mapping
*
* @var string[]
*/
public $custom_import_data = [
'tec_import_event_status' => 'Event Status',
'meta_key_1' => 'Meta Data 1',
'meta_key_2' => 'Meta Data 2',
];
/**
* Constructor.
*/
public function __construct() {
add_filter( 'tribe_events_importer_event_column_names', [ $this, 'add_column_name' ] );
add_filter( 'tribe_events_csv_import_event_additional_fields', [ $this, 'process_custom_data' ] );
add_action( 'tribe_events_update_meta', [ $this, 'save_custom_data' ], 10, 3 );
}
/**
* Add the field name to the dropdown.
*
* @param array<string|string> $column_names An array of column names for event import.
*
* @return string[]
*/
public function add_column_name( $column_names ) {
foreach ( $this->custom_import_data as $key => $value ) {
$column_names[ $key ] = $value;
}
return $column_names;
}
/**
* Make sure that the added column gets processed on import.
*
* @return array
*/
function process_custom_data() {
// key (meta field name, table column name) => CSV column ID, the key from the above function.
$extra_fields[ 'tec_import_event_status' ] = 'tec_import_event_status';
foreach ( $this->custom_import_data as $key => $value ) {
$extra_fields[ $key ] = $key;
}
return $extra_fields;
}
/**
* Save metadata in database.
*
* @param int $event_id The event ID we are modifying meta for.
* @param array $data The meta fields we want saved.
* @param WP_Post $event The event itself.
*
* @return void
*/
function save_custom_data( $event_id, $data, $event ) {
foreach ( $this->custom_import_data as $key => $value ) {
if ( isset( $data[ $key ] ) && $data[ $key ] != '' ) {
update_post_meta( $event_id, $key, esc_html( $data[ $key ] ) );
}
}
}
}
new tec_csv_import_custom_meta_data();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment