Skip to content

Commit

Permalink
Fix bugs with tinyurl.com and URLs inside HTML links, bump to 0.2.3
Browse files Browse the repository at this point in the history
Plaintext URLs were working fine.
  • Loading branch information
kvibber committed Jun 11, 2022
1 parent 5524ef3 commit 475296b
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
release-svn
24 changes: 24 additions & 0 deletions packagezip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

ZIPNAME="unwrap-shortlinks"

echo "Removing old build folder and zip if present"
if test -d "build/$ZIPNAME"; then
rm -rf "build/$ZIPNAME"
fi
if test -f "build/$ZIPNAME.zip"; then
rm "build/$ZIPNAME.zip"
fi

echo "Copying files to build folder"
if [ ! -d build ]; then
mkdir build
fi

mkdir build/$ZIPNAME
cp -p *.php *.txt *.md LICENSE "build/$ZIPNAME"
rm "build/$ZIPNAME/readme.md"

echo "Building $ZIPNAME"
cd build && zip -r "$ZIPNAME.zip" "$ZIPNAME"

29 changes: 21 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Unwrap Shortlinks

Stable tag: 0.2.2
Tags: urls, links
Stable tag: 0.2.3
Tags: urls, links, classicpress
Requires at least: 3.0
Tested up to: 6.0
Requires PHP: 7.0
Contributors: Kelson
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
For ClassicPress
Requires: 1.0
Tested: 1.4.1

Follow shortened links (t.co, bit.ly, etc) and expand them so that your blog post will point directly to the destination.

Expand Down Expand Up @@ -36,23 +40,32 @@ Install the plugin and activate it. The next time you paste a supported short UR

### What if the destination redirects to another link?

It won't keep following the trail. Just in case there's a loop, or the destination page has been replaced with a redirection to a home page, etc.

But if the destination is another known shortener (say you had a t.co link that redirected to a bit.ly link), the next time you save the post, it will follow that one to its destination.
At the moment it'll stop at the first one to avoid infinite redirect loops. But if the destination is another known shortener (say you had a t.co link that redirected to a bit.ly link), the next time you save the post, it will follow that one to its destination. TODO: I plan to make it follow a limited chain of known shorteners.

### How do I add a shortener to the list?

It's not supported yet, but I plan on adding an options page for it.

### I'm running on a local server and this doesn't do anything!

Most likely your system is configured to block your local webserver
from making outgoing connections. This is a sensible default for security!
Depending on your system, it may be blocked by a local or network firewall.
If you're running Fedora Workstation with SELinux (like I am), this will enable it:
```
sudo setsebool -P httpd_can_network_connect true
```

Thanks to [igienger's post](https://wordpress.org/support/topic/error-curl-error-7-3/#post-12637512) on the WordPress support forums!

### What about compatibility?

It should work going back to the classic editor and forward to the block editor. It even works on [ClassicPress](https://www.classicpress.net/).


## Changelog

### 2022-05-24
* Confirmed compatible with WordPress 6.0
### [0.2.3] - 2022-06-10
* Fix bugs with tinyurl.com and URLs inside HTML links. (Plaintext URLs were working fine.)

### [0.2.2] - 2022-01-29

Expand Down
77 changes: 77 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
=== Unwrap Shortlinks ===

Stable tag: 0.2.3
Tags: urls, links, classicpress
Requires at least: 3.0
Tested up to: 6.0
Requires PHP: 7.0
Contributors: Kelson
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
For ClassicPress
Requires: 1.0
Tested: 1.4.1

Follow shortened links (t.co, bit.ly, etc) and expand them so that your blog post will point directly to the destination.

== Description ==

Automatically expands URLs at the following known link shorteners when you save a post:
* t.co
* bit.ly
* ow.ly
* j.mp
* is.gd
* trib.al
* buff.ly
* wp.me
* tmblr.co
* tinyurl.com
* goo.gl
* dlvr.it
* fb.me
* qr.ae

== Installation ==

Install the plugin and activate it. The next time you paste a supported short URL into a post, it will be updated when the post is saved.

== Frequently Asked Questions ==

= What if the destination redirects to another link? =

At the moment it'll stop at the first one to avoid infinite redirect loops. But if the destination is another known shortener (say you had a t.co link that redirected to a bit.ly link), the next time you save the post, it will follow that one to its destination. TODO: I plan to make it follow a limited chain of known shorteners.

= How do I add a shortener to the list? =

It's not supported yet, but I plan on adding an options page for it.

= I'm running on a local server and this doesn't do anything! =

Most likely your system is configured to block your local webserver
from making outgoing connections. This is a sensible default for security!
Depending on your system, it may be blocked by a local or network firewall.
If you're running Fedora Workstation with SELinux (like I am), this will enable it:
```
sudo setsebool -P httpd_can_network_connect true
```

Thanks to [igienger's post](https://wordpress.org/support/topic/error-curl-error-7-3/#post-12637512) on the WordPress support forums!

= What about compatibility? =

It should work going back to the classic editor and forward to the block editor. It even works on [ClassicPress](https://www.classicpress.net/).

== Changelog ==

= [0.2.3] - 2022-06-10 =
* Fix bugs with tinyurl.com and URLs inside HTML links. (Plaintext URLs were working fine.)

= [0.2.2] - 2022-01-29 =

* Initial release based on the code I've been running locally for years, plus changes requested by the WordPress plugin review team.

[Source on Codeberg](https://codeberg.org/kvibber/unwrap-shortlinks).
[Plugin page at WordPress](https://wordpress.org/plugins/unwrap-shortlinks/).

(c) 2016-2022 [Kelson Vibber](https://kvibber.com/)
6 changes: 3 additions & 3 deletions unwrap-shortlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Unwrap Shortlinks
Plugin URI: https://codeberg.org/kvibber/unwrap-shortlinks
Description: Follow shortened links (t.co, bit.ly, etc) and expand them so that your blog post will point directly to the destination.
Version: 0.2.2
Version: 0.2.3
Author: Kelson Vibber
Author URI: https://kvibber.com
License: GPLv2 or later
Expand All @@ -13,7 +13,7 @@
// 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|tinyurl\.com|goo\.gl|dlvr\.it|fb\.me|qr\.ae)\/[^\s"\']+)/', $content, $matches, PREG_PATTERN_ORDER);
preg_match_all('/\b(https?:\/\/(?:t\.co|bit\.ly|j\.mp|ow\.ly|is\.gd|trib\.al|buff\.ly|tmblr\.co|wp\.me|tinyurl\.com|goo\.gl|dlvr\.it|fb\.me|qr\.ae)\/[^\s"\']+)\b/', $content, $matches, PREG_PATTERN_ORDER);
foreach ($matches[1] as $link) {
$getlink = ktv_unwrap_shortlinks_replace($link);
if ($getlink != "")
Expand All @@ -31,7 +31,7 @@ function ktv_unwrap_shortlinks_replace($url) {
$finalURL = wp_remote_retrieve_header( $response, 'location' );

// If it was a redirect, return the next URL.
if ($status == 301 || $status == 302 || $status == 307) {
if ($status == 301 || $status == 302 || $status == 307 || $status == 308) {
return esc_url($finalURL);
}
// We didn't get anything, or it didn't redirect, so let's return a blank.
Expand Down
69 changes: 69 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/bin/bash
# https://developer.wordpress.org/plugins/wordpress-org/how-to-use-subversion/

VERSION=$1
ACTION=""
MESSAGE=$2

echo "Updating files in trunk."
cp -p *.php *.md release-svn/trunk



if [[ $VERSION == "trunk" ]]; then
if [[ "x$MESSAGE" == "x" ]]; then
echo "Message?"
read MESSAGE
fi
if [[ "x$MESSAGE" == "x" ]]; then
echo "Sorry, a message is required"
exit -1
fi

echo "Checking in the updated trunk"
cd release-svn/trunk
svn ci -m "$MESSAGE"
exit 0
fi



if [[ "x$VERSION" == "x" ]]; then
echo "No release version specified, stopping here."
exit 0
fi




if [ ! -f ".git/refs/tags/v$VERSION" ]; then
echo "Error: v$VERSION not found in git tags."
exit -1
fi

cd release-svn

if [ -d "tags/$VERSION" ]; then
echo "Warning: Release $VERSION already exists in local svn."
echo "(r)eplace, (u)pload as-is, or (c)ancel?"
read ACTION
if [[ $ACTION == "r" ]]; then
echo "removing old tag $VERSION"
svn rm --force "tags/$VERSION"
elif [[ ! $ACTION == "u" ]]; then
echo "Cancelling"
exit -1
fi
fi

if [[ $ACTION == "u" ]]; then
echo "Leaving release tag unchanged."
else
echo "Tagging release $VERSION in svn."
svn cp trunk "tags/$VERSION"
fi

echo "Checking in updated version $VERSION"
svn ci -m "tagging version $VERSION"


0 comments on commit 475296b

Please sign in to comment.