diff --git a/duplicate-tec-event.php b/duplicate-tec-event.php index 63c186a..764524d 100644 --- a/duplicate-tec-event.php +++ b/duplicate-tec-event.php @@ -1,145 +1,145 @@ - -

You must install The Events Calendar plugin before enabling the Duplicate TEC Event plugin!

- '; -} - - -/** - * Adds additional actions to the TEC listing page - * @param array $actions - * @param type $post - * @return string - */ -function dte_row_actions( $actions, $post ) { - // Before altering the available actions, ensure we are on the tribe events page - if( $post->post_type != 'tribe_events' ) return $actions; - - $nonce = wp_create_nonce( 'dte_duplicate_event' ); - $actions['duplicate_tribe_event'] = 'ID).'&_nonce=' . $nonce . '\'>Duplicate';; - - return $actions; -} - -/** - * Performs the work of duplicating the TEC event - * @return boolean - */ -function dte_duplicate_tribe_event() { - if( !isset( $_GET['_nonce'] ) || !wp_verify_nonce( $_GET['_nonce'], 'dte_duplicate_event' ) ) - return false; - - $event_id = $_GET['post']; - - if ( !class_exists( 'TribeEventsAPI' ) ) - return false; - - - $event = (array)get_post( $event_id ); - unset( $event['ID'] ); // Remove ID to prevent an update from happening - $event['post_status'] = 'draft'; - - $meta = get_post_custom( $event_id ); - - // Flatten out the meta array (WTF?) - $fmeta = array(); - foreach( $meta AS $k => $v ) { - $fmeta[$k] = $v[0]; - } - - - // TEC expects a couple fields to exist without the _ upon creation - $event['EventStartDate'] = date( 'Y-m-d', strtotime( $fmeta['_EventStartDate'] ) ); - $event['EventStartHour'] = date( 'h', strtotime( $fmeta['_EventStartDate'] ) ); - $event['EventStartMinute'] = date( 'i', strtotime( $fmeta['_EventStartDate'] ) ); - $event['EventEndDate'] = date( 'Y-m-d', strtotime( $fmeta['_EventEndDate'] ) ); - $event['EventEndHour'] = date( 'h', strtotime( $fmeta['_EventEndDate'] ) ); - $event['EventEndMinute'] = date( 'j', strtotime( $fmeta['_EventEndDate'] ) ); - - // Unset recurrence to prevent potentially thousands of new events being created - // This will also unlink an individual recurrence from its parent - unset( $fmeta['_EventRecurrence'] ); - - $event = array_merge( $event, $fmeta ); - - $new_event_id = TribeEventsAPI::createEvent( $event ); - - // Merge in any additional meta that may have been missed by createEvent - foreach( $fmeta AS $k => $v ) { - update_post_meta( $new_event_id, $k, $v ); - } - - // Copy the taxonomies - $taxonomies = get_object_taxonomies( 'tribe_events' ); - - foreach( $taxonomies AS $tax ) { - $terms = wp_get_object_terms( $event_id, $tax ); - $term = array(); - foreach( $terms AS $t ) { - $term[] = $t->slug; - } - - wp_set_object_terms( $new_event_id, $term, $tax ); - } - - // Send back to the original page - wp_redirect(admin_url("edit.php?post_type=tribe_events" ) ); exit; -} + +

You must install The Events Calendar plugin before enabling the Duplicate TEC Event plugin!

+ '; +} + + +/** + * Adds additional actions to the TEC listing page + * @param array $actions + * @param type $post + * @return string + */ +function dte_row_actions( $actions, $post ) { + // Before altering the available actions, ensure we are on the tribe events page + if( $post->post_type != 'tribe_events' ) return $actions; + + $nonce = wp_create_nonce( 'dte_duplicate_event' ); + $actions['duplicate_tribe_event'] = 'ID).'&_nonce=' . $nonce . '\'>Duplicate';; + + return $actions; +} + +/** + * Performs the work of duplicating the TEC event + * @return boolean + */ +function dte_duplicate_tribe_event() { + if( !isset( $_GET['_nonce'] ) || !wp_verify_nonce( $_GET['_nonce'], 'dte_duplicate_event' ) ) + return false; + + $event_id = $_GET['post']; + + if ( !class_exists( 'Tribe__Events__API' ) ) + return false; + + + $event = (array)get_post( $event_id ); + unset( $event['ID'] ); // Remove ID to prevent an update from happening + $event['post_status'] = 'draft'; + + $meta = get_post_custom( $event_id ); + + // Flatten out the meta array (WTF?) + $fmeta = array(); + foreach( $meta AS $k => $v ) { + $fmeta[$k] = $v[0]; + } + + + // TEC expects a couple fields to exist without the _ upon creation + $event['EventStartDate'] = date( 'Y-m-d', strtotime( $fmeta['_EventStartDate'] ) ); + $event['EventStartHour'] = date( 'h', strtotime( $fmeta['_EventStartDate'] ) ); + $event['EventStartMinute'] = date( 'i', strtotime( $fmeta['_EventStartDate'] ) ); + $event['EventEndDate'] = date( 'Y-m-d', strtotime( $fmeta['_EventEndDate'] ) ); + $event['EventEndHour'] = date( 'h', strtotime( $fmeta['_EventEndDate'] ) ); + $event['EventEndMinute'] = date( 'j', strtotime( $fmeta['_EventEndDate'] ) ); + + // Unset recurrence to prevent potentially thousands of new events being created + // This will also unlink an individual recurrence from its parent + unset( $fmeta['_EventRecurrence'] ); + + $event = array_merge( $event, $fmeta ); + + $new_event_id = Tribe__Events__API::createEvent( $event ); + + // Merge in any additional meta that may have been missed by createEvent + foreach( $fmeta AS $k => $v ) { + update_post_meta( $new_event_id, $k, $v ); + } + + // Copy the taxonomies + $taxonomies = get_object_taxonomies( 'tribe_events' ); + + foreach( $taxonomies AS $tax ) { + $terms = wp_get_object_terms( $event_id, $tax ); + $term = array(); + foreach( $terms AS $t ) { + $term[] = $t->slug; + } + + wp_set_object_terms( $new_event_id, $term, $tax ); + } + + // Send back to the original page + wp_redirect(admin_url("edit.php?post_type=tribe_events" ) ); exit; +}