Make WordPress Core

Ticket #20166: 20166.patch

File 20166.patch, 2.9 KB (added by chriscct7, 10 years ago)
  • src/wp-admin/includes/misc.php

     
    271271        }
    272272}
    273273
    274 /**
    275  * Shorten an URL, to be used as link text
    276  *
    277  * @since 1.2.0
    278  *
    279  * @param string $url
    280  * @return string
    281  */
    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 }
    289274
    290275/**
    291276 * Resets global variables based on $_GET and $_POST
  • src/wp-includes/formatting.php

     
    46934693
    46944694        return $mail;
    46954695}
     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*/
     4707function url_shorten( $url, $length = 35 ) {
     4708        $short_url = str_replace( array( '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 */
     6class 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/#&hellip;', // 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/&hellip;', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 ); // shorten to 31 if > 34 after cleaning
     27        }
     28}