Skip to content

Commit

Permalink
Merge pull request #4 from WordPress/add/support-wp-6-2
Browse files Browse the repository at this point in the history
Testing plugin up to WordPress 6.2
  • Loading branch information
ouikhuan authored May 10, 2023
2 parents bcdd614 + bc607b9 commit f31b982
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
7 changes: 4 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: wordpressdotorg, Otto42, dd32, westi, dllh
Tags: tumblr, import
Requires at least: 3.2
Tested up to: 6.1
Stable tag: 1.0
Tested up to: 6.2
Stable tag: 1.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -29,7 +29,8 @@ Version 0.9 Removes untested warning for the plugin.

== Changelog ==

= Unreleased =
= 1.1 =
* Testing the plugin up to WordPress 6.2
* Fix Tumblr bug with custom domains

= 1.0 =
Expand Down
32 changes: 16 additions & 16 deletions tumblr-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Description: Import posts from a Tumblr blog.
Author: wordpressdotorg
Author URI: http://wordpress.org/
Version: 1.0
Version: 1.1
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Text Domain: tumblr-importer
Domain Path: /languages
Expand Down Expand Up @@ -759,29 +759,29 @@ function get_blogs() {
}

/**
* Tumblr seems to return blogs with /blog/view in the URL, which is not
* a valid URL. This function strips that out.
* Make sure the URL of the tumblr blog is in the correct format.
*
* It also converts urls of the form https://tumblr.com/<id> to https://</id>.tumblr.com/
* which is the only form the API seems to accept as valid.
* If the URL is in the format https://tumblr.com/blogname, then we need to convert it to https://blogname.tumblr.com.
* If the URL is in the format https://blogname.tumblr.com, we don't need to do anything.
* Finally, we skip sanitizing custom Tumblr domains.
*
* @param $url
* @param string $url URL of Tumblr blog returned by the API.
*
* @return string
*/
private function sanitize_blog_url( $url ) {
if ( preg_match( '|tumblr.com/|', $url ) ) {
// trim any /blog/view parts
$url = preg_replace( '/\/blog\/view/', '', $url );

// parse the URL to extract the path component
$parsed_url = parse_url( $url );
$path = $parsed_url['path'];

// extract the ID from the path by removing the leading '/'
$id = ltrim( $path, '/' );
// If the URL is already in a valid format, just return it.
if ( preg_match( '#^https://.*?\.tumblr.com/?$#', $url ) ) {
return $url;
}

return "https://{$id}.tumblr.com/";
// If the URL is not in a correct format, we compose the new one with the short name of the blog.
if ( preg_match( '#^https://(?:www\.)?tumblr.com/(?:blog/view/)?(?P<id>.+?)/?$#', $url, $matches ) ) {
return sprintf( 'https://%s.tumblr.com/', $matches['id'] );
}

// Or, just return the original URL.
return $url;
}

Expand Down

0 comments on commit f31b982

Please sign in to comment.