mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 21:08:44 +01:00
Formatting: move url_shorten()
from wp-admin/includes/misc.php
to wp-includes/formatting.php
for more global access.
Adds unit tests. Props mulvane, chriscct7. Fixes #20166. git-svn-id: https://develop.svn.wordpress.org/trunk@35314 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
be739bd6c0
commit
51637379a2
@ -288,21 +288,6 @@ function update_home_siteurl( $old_value, $value ) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorten an URL, to be used as link text
|
||||
*
|
||||
* @since 1.2.0
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
function url_shorten( $url ) {
|
||||
$short_url = str_replace( array( 'http://', 'www.' ), '', $url );
|
||||
$short_url = untrailingslashit( $short_url );
|
||||
if ( strlen( $short_url ) > 35 )
|
||||
$short_url = substr( $short_url, 0, 32 ) . '…';
|
||||
return $short_url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets global variables based on $_GET and $_POST
|
||||
|
@ -4769,3 +4769,23 @@ function wp_staticize_emoji_for_email( $mail ) {
|
||||
|
||||
return $mail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shorten an URL, to be used as link text
|
||||
*
|
||||
* @since 1.2.0
|
||||
* @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param
|
||||
*
|
||||
* @param string $url URL to shorten
|
||||
* @param int $length Maxiumum length of url to return
|
||||
* @return string
|
||||
*/
|
||||
function url_shorten( $url, $length = 35 ) {
|
||||
$stripped = str_replace( array( 'https://', 'http://', 'www.' ), '', $url );
|
||||
$short_url = untrailingslashit( $stripped );
|
||||
|
||||
if ( strlen( $short_url ) > $length ) {
|
||||
$short_url = substr( $short_url, 0, $length - 3 ) . '…';
|
||||
}
|
||||
return $short_url;
|
||||
}
|
22
tests/phpunit/tests/formatting/URLShorten.php
Normal file
22
tests/phpunit/tests/formatting/URLShorten.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @group formatting
|
||||
*/
|
||||
class Tests_Formatting_URLShorten extends WP_UnitTestCase {
|
||||
function test_shorten_url() {
|
||||
$tests = array(
|
||||
'wordpress\.org/about/philosophy' => 'wordpress\.org/about/philosophy', // no longer strips slashes
|
||||
'wordpress.org/about/philosophy' => 'wordpress.org/about/philosophy',
|
||||
'http://wordpress.org/about/philosophy/' => 'wordpress.org/about/philosophy', // remove http, trailing slash
|
||||
'http://www.wordpress.org/about/philosophy/' => 'wordpress.org/about/philosophy', // remove http, www
|
||||
'http://wordpress.org/about/philosophy/#box' => 'wordpress.org/about/philosophy/#box', // don't shorten 35 characters
|
||||
'http://wordpress.org/about/philosophy/#decisions' => 'wordpress.org/about/philosophy/#…', // shorten to 32 if > 35 after cleaning
|
||||
);
|
||||
foreach ( $tests as $k => $v ) {
|
||||
$this->assertEquals( $v, url_shorten( $k ) );
|
||||
}
|
||||
|
||||
$this->assertEquals( 'wordpress.org/about/philosophy/#…', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 ); // shorten to 31 if > 34 after cleaning
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user