- Fixes URL extraction (undefined index notice)

- Updates link replacement in text body
- Updates links saving logic
This commit is contained in:
Vlad
2016-06-16 15:14:10 -04:00
parent 4bb1acf493
commit 6daecd6466
2 changed files with 17 additions and 6 deletions

View File

@ -3,6 +3,7 @@ namespace MailPoet\Newsletter\Links;
use MailPoet\Models\NewsletterLink; use MailPoet\Models\NewsletterLink;
use MailPoet\Newsletter\Shortcodes\Shortcodes; use MailPoet\Newsletter\Shortcodes\Shortcodes;
use MailPoet\Util\Helpers;
use MailPoet\Util\Security; use MailPoet\Util\Security;
class Links { class Links {
@ -39,16 +40,16 @@ class Links {
}, $shortcodes); }, $shortcodes);
// extract urls with href="url" format // extract urls with href="url" format
preg_match_all($regex, $content, $matched_urls); preg_match_all($regex, $content, $matched_urls);
$matched_urls_count = count($matched_urls[1]); $matched_urls_count = count($matched_urls[0]);
if($matched_urls_count) { if($matched_urls_count) {
for($index = 0; $index <= $matched_urls_count; $index++) { for($index = 0; $index < $matched_urls_count; $index++) {
$extracted_links[] = array( $extracted_links[] = array(
'html' => $matched_urls[0][$index], 'html' => $matched_urls[0][$index],
'link' => $matched_urls[2][$index] 'link' => $matched_urls[2][$index]
); );
} }
} }
return $extracted_links; return Helpers::arrayUnique($extracted_links);
} }
static function process($content) { static function process($content) {
@ -79,9 +80,9 @@ class Links {
// third, replace text version URL with tracked link: [description](url) // third, replace text version URL with tracked link: [description](url)
// regex is used to avoid replacing description URLs that are wrapped in round brackets // regex is used to avoid replacing description URLs that are wrapped in round brackets
// i.e., <a href="http://google.com">(http://google.com)</a> => [(http://google.com)](http://tracked_link) // i.e., <a href="http://google.com">(http://google.com)</a> => [(http://google.com)](http://tracked_link)
$regex_escaped_tracked_link = preg_quote($tracked_link, '/'); $regex_escaped_extracted_link = preg_quote($extracted_link['link'], '/');
$content = preg_replace( $content = preg_replace(
'/(\[' . $regex_escaped_tracked_link . '\])(\(' . $regex_escaped_tracked_link . '\))/', '/\[(' . $regex_escaped_extracted_link . ')\](\(' . $regex_escaped_extracted_link . '\))/',
'[$1](' . $tracked_link . ')', '[$1](' . $tracked_link . ')',
$content $content
); );
@ -117,8 +118,9 @@ class Links {
return $content; return $content;
} }
static function save($links, $newsletter_id, $queue_id) { static function save(array $links, $newsletter_id, $queue_id) {
foreach($links as $link) { foreach($links as $link) {
if(empty($link['hash'] || empty($link['url']))) continue;
$newsletter_link = NewsletterLink::create(); $newsletter_link = NewsletterLink::create();
$newsletter_link->newsletter_id = $newsletter_id; $newsletter_link->newsletter_id = $newsletter_id;
$newsletter_link->queue_id = $queue_id; $newsletter_link->queue_id = $queue_id;

View File

@ -113,4 +113,13 @@ class Helpers {
$func = create_function('$c', 'return "_" . strtolower($c[1]);'); $func = create_function('$c', 'return "_" . strtolower($c[1]);');
return preg_replace_callback('/([A-Z])/', $func, $str); return preg_replace_callback('/([A-Z])/', $func, $str);
} }
static function arrayUnique($arr) {
return array_map(
'unserialize',
array_unique(
array_map('serialize', $arr)
)
);
}
} }