Created
March 20, 2024 13:54
-
-
Save andrasguseo/8d5727c00c07127c0541ea99bdb735bb to your computer and use it in GitHub Desktop.
EA > Preserve / reconvert links in Microsoft Teams invitations when imported from Outlook (updated)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Preserve / reconvert links in Microsoft Teams invitations when imported from Outlook. | |
* | |
* Usage: Add the snippet to your functions.php file or with a plugin like Code Snippets | |
* | |
* @param array $event Which Event data was sent | |
* @param stdClass|array $item Raw version of the data sent from EA | |
* @param object $data The record we are dealing with | |
* | |
* @return void | |
* | |
* @author: Andras Guseo | |
* | |
* Plugins required: The Events Calendar (Event Aggregator) | |
* Created: January 10, 2024 | |
* Last updated: March 20, 2024 | |
*/ | |
function tec_ea_preserve_teams_links( $event, $item, $data ) { | |
$current_source = $data->meta[ 'source' ]; | |
/** | |
* START: valid sources | |
* Limit to the defined source URLs. | |
* If the import is from a different URL, then the rest will be skipped. | |
* Add import source URLs as needed or remove completely if you want this to run on all import sources. | |
*/ | |
$valid_sources = [ | |
'https://outlook.office365.com/owa/calendar/{long-string}.ics', | |
'https://outlook.office365.com/owa/calendar/{other-long-string}.ics', | |
]; | |
// Check if it's a valid source. | |
if ( ! in_array( $current_source, $valid_sources ) ) { | |
return; | |
} | |
// Grab the unsafe description | |
$description = $item->description; | |
$unsafe_description = $item->unsafe_description; | |
$has_borked_urls = (bool) strpos( $unsafe_description, "<http" ); | |
$has_zoom = (bool) strpos( $description, "zoom.us/" ); | |
// Bail, if the text doesn't have URLs or and Zoom links. | |
if ( | |
! $has_borked_urls === false | |
&& ! $has_zoom | |
) { | |
return; | |
} | |
if ( $has_borked_urls ) { | |
// Regex for URLs starting with "<http". Typically, Microsoft Teams Meetings. | |
$search = '/(Click here to join the meeting|Download Teams|Join on the web|Learn More|Meeting options)(?:<)((https:\/\/)(.+?))(?:>)/'; | |
// Search and replace the description. | |
$description = preg_replace( $search, '<a href="$2" target="_blank" title="$1">$1</a>', $unsafe_description ); | |
} | |
if ( $has_zoom ) { | |
// Regex for Zoom URLs. | |
$search = '/(Join Zoom Meeting)(?:\\n)((https:\/\/.*zoom.us\/)(.+?))(\\nMeeting ID)/'; | |
// Search and replace the description. | |
$description = preg_replace( $search, '<br><a href="$2" target="_blank" title="$1">$1</a>$5', $description ); | |
} | |
// Set post arguments. | |
$args = [ | |
'ID' => $event[ 'ID' ], | |
'post_content' => wp_kses( | |
$description, | |
wp_kses_allowed_html( 'post' ), | |
), | |
]; | |
// Update post. | |
wp_update_post( $args ); | |
} | |
add_action( 'tribe_aggregator_after_insert_post', 'tec_ea_preserve_teams_links', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment