diff --git a/README.md b/README.md index 9f9d19b..429f081 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Tested up to: 6.6 Contributors: Kelson License: GPLv2 or later -Follows shortened links (t.co, bit.ly, etc) and expands them so that your blog post will point directly to the destination. +Follows shortened links (t.co, bit.ly, etc) and WordPress' ?p=123-style links, and expands them so that your blog post will point directly to the destination. ## Description @@ -35,7 +35,7 @@ Install the plugin and activate it. The next time you paste a supported short UR ### What if the destination redirects to another link? -If the destination is another known URL shortener (ex. t.co pointing to bit.ly pointing to wp.me pointing to a blog post), it'll keep going up to 5 levels (to avoid infinite loops!) or until it gets a URL that isn't on the list. +If the destination is another known URL shortener (ex. t.co pointing to bit.ly pointing to wp.me pointing to a blog post), it'll keep going up to 8 levels (to avoid infinite loops!) or until it gets a URL that isn't on the list. ### How do I add a shortener to the list? @@ -60,6 +60,7 @@ It should work going back to the classic editor and forward to the block editor. ## Changelog ### [0.3.4] - 2024-07-07 +* Also follow WordPress' ?p=123 links if they redirect to a permalink. * tinyurl.com is accepting curl requests again, so I've added it back to the list. ### [0.3.3] - 2023-07-22 diff --git a/linkchain.txt b/linkchain.txt index 9800573..8832931 100644 --- a/linkchain.txt +++ b/linkchain.txt @@ -9,10 +9,4 @@ http://ow.ly/Vcq050JGjmM https://bit.ly/3xT4SwX https://t.co/kTzFTX65HT -1. Following the t.co link should get you to the post permalink. -2. Unwrap-shortlinks used to replace the t.co link with the blog ?p=xyz link, - but since TinyURL started screening requests using CloudFlare (sometime before - July 2023, requiring a JavaScript-capable browser), we can't follow it anymore. - So now the plugin can only unwrap the t.co link as far as the tinyurl.com link. - -TODO: Also check URLs ending in ?p=[0-9]+, follow them once and accept the result if the URLs match up to the previous / +Following the t.co link should get you to the post permalink. diff --git a/unwrap-shortlinks.php b/unwrap-shortlinks.php index b1f8472..8d8866a 100644 --- a/unwrap-shortlinks.php +++ b/unwrap-shortlinks.php @@ -16,9 +16,9 @@ // ini_set('display_errors', '1'); ini_set('error_reporting', E_ALL); function ktv_unwrap_shortlinks($content) { - preg_match_all('/\b(https?:\/\/(?:t\.co|bit\.ly|j\.mp|ow\.ly|is\.gd|trib\.al|buff\.ly|tmblr\.co|wp\.me|goo\.gl|dlvr\.it|fb\.me|qr\.ae|aka\.ms|tinyurl\.com)\/[^\s"\'<>]+)\b/', $content, $matches, PREG_PATTERN_ORDER); + preg_match_all('/\b(https?:\/\/(?:[^\s"\'<>]+\/\?p=[0-9]+|(?:t\.co|bit\.ly|j\.mp|ow\.ly|is\.gd|trib\.al|buff\.ly|tmblr\.co|wp\.me|goo\.gl|dlvr\.it|fb\.me|qr\.ae|aka\.ms|tinyurl\.com)\/[^\s"\'<>]+))\b/', $content, $matches, PREG_PATTERN_ORDER); foreach ($matches[1] as $link) { - $getlink = ktv_unwrap_shortlinks_replace($link, 5); + $getlink = ktv_unwrap_shortlinks_replace($link, 8); if ($getlink != "") $content = str_replace($link, $getlink, $content); } @@ -35,9 +35,9 @@ function ktv_unwrap_shortlinks_replace($url, $countdown) { // If it was a redirect, get the next URL if ($status == 301 || $status == 302 || $status == 307 || $status == 308) { - // TODO Is it also a redirector? Do we have iterations left? + // Is it also a redirector? Do we have iterations left? // If so, try to follow that one! - if( $countdown > 0 && preg_match('/\b(https?:\/\/(?:t\.co|bit\.ly|j\.mp|ow\.ly|is\.gd|trib\.al|buff\.ly|tmblr\.co|wp\.me|goo\.gl|dlvr\.it|fb\.me|qr\.ae|aka\.ms|tinyurl\.com)\/[^\s"\'<>]+)\b/', $finalURL) ) { + if( $countdown > 0 && preg_match('/\b(https?:\/\/(?:[^\s"\'<>]+\/\?p=[0-9]+|(?:t\.co|bit\.ly|j\.mp|ow\.ly|is\.gd|trib\.al|buff\.ly|tmblr\.co|wp\.me|goo\.gl|dlvr\.it|fb\.me|qr\.ae|aka\.ms|tinyurl\.com)\/[^\s"\'<>]+))\b/', $finalURL ) ) { return ktv_unwrap_shortlinks_replace($finalURL, $countdown - 1); } else { // Otherwise, send it back up the chain!