]+' . '[.]' # conditionally match everything except for special characters after . . '(?:' . '\([\w\d]+\)|' . '(?:' . '[^`!()\[\]{}:\'".,<>«»“”‘’\s]|' . '(?:[:]\d+)?/?' . ')+' . ')' . ')\\1#'; $shortcodes = new Shortcodes(); // extract shortcodes with [url:*] format $shortcodes = $shortcodes->extract($text, $limit = array('link')); // extract links preg_match_all($regex, $text, $links); return array_merge( array_unique($links[2]), $shortcodes ); } static function replace($text, $links = false, $process_link_shortcodes = false) { if ($process_link_shortcodes) { // process shortcodes with [url:*] format $shortcodes = new Shortcodes(); $text = $shortcodes->replace($text, $limit = array('link')); } $links = ($links) ? $links : self::extract($text, $process_link_shortcodes); $processed_links = array(); foreach($links as $link) { $hash = Security::generateRandomString(5); $processed_links[] = array( 'hash' => $hash, 'url' => $link ); $encoded_link = sprintf( '%s/?mailpoet&endpoint=track&action=click&data=%s', home_url(), '[mailpoet_data]-' . $hash ); $link_regex = '/' . preg_quote($link, '/') . '/'; $text = preg_replace($link_regex, $encoded_link, $text); } return array($text, $processed_links); } }