Ticket #20166: 20166.2.patch
File 20166.2.patch, 2.9 KB (added by , 10 years ago) |
---|
-
src/wp-admin/includes/misc.php
271 271 } 272 272 } 273 273 274 /**275 * Shorten an URL, to be used as link text276 *277 * @since 1.2.0278 *279 * @param string $url280 * @return string281 */282 function url_shorten( $url ) {283 $short_url = str_replace( array( 'http://', 'www.' ), '', $url );284 $short_url = untrailingslashit( $short_url );285 if ( strlen( $short_url ) > 35 )286 $short_url = substr( $short_url, 0, 32 ) . '…';287 return $short_url;288 }289 274 290 275 /** 291 276 * Resets global variables based on $_GET and $_POST -
src/wp-includes/formatting.php
4693 4693 4694 4694 return $mail; 4695 4695 } 4696 4697 /** 4698 * Shorten an URL, to be used as link text 4699 * 4700 * @since 1.2.0 4701 * @since 4.4.0 Moved to wp-includes/formatting.php from wp-admin/includes/misc.php and added $length param 4702 * 4703 * @param string $url URL to shorten 4704 * @param int $length Maxiumum length of url to return 4705 * @return string 4706 */ 4707 function url_shorten( $url, $length = 35 ) { 4708 $short_url = str_replace( array( 'https://', 'http://', 'www.' ), '', $url ); 4709 $short_url = untrailingslashit( $short_url ); 4710 4711 if ( strlen( $short_url ) > $length ) { 4712 $short_url = substr( $short_url, 0, $length - 3 ) . '…'; 4713 } 4714 return $short_url; 4715 } 4716 No newline at end of file -
tests/phpunit/tests/formatting/URLShorten.php
1 <?php 2 3 /** 4 * @group admin 5 */ 6 class Tests_Admin_includesMisc extends WP_UnitTestCase { 7 function test_shorten_url() { 8 $tests = array( 9 'wordpress\.org/about/philosophy' 10 => 'wordpress\.org/about/philosophy', // no longer strips slashes 11 'wordpress.org/about/philosophy' 12 => 'wordpress.org/about/philosophy', 13 'http://wordpress.org/about/philosophy/' 14 => 'wordpress.org/about/philosophy', // remove http, trailing slash 15 'http://www.wordpress.org/about/philosophy/' 16 => 'wordpress.org/about/philosophy', // remove http, www 17 'http://wordpress.org/about/philosophy/#box' 18 => 'wordpress.org/about/philosophy/#box', // don't shorten 35 characters 19 'http://wordpress.org/about/philosophy/#decisions' 20 => 'wordpress.org/about/philosophy/#…', // shorten to 32 if > 35 after cleaning 21 ); 22 foreach ( $tests as $k => $v ) { 23 $this->assertEquals( $v, url_shorten( $k ) ); 24 } 25 26 $this->assertEquals( 'wordpress.org/about/philosophy/…', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 ); // shorten to 31 if > 34 after cleaning 27 } 28 }