Skip to content

Commit

Permalink
Follow WordPress ?p=123 redirects to the permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
kvibber committed Jul 8, 2024
1 parent aa6e915 commit 769c948
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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?

Expand All @@ -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
Expand Down
8 changes: 1 addition & 7 deletions linkchain.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 4 additions & 4 deletions unwrap-shortlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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!
Expand Down

0 comments on commit 769c948

Please sign in to comment.