WordPress.org

Make WordPress Core

Changeset 25030


Ignore:
Timestamp:
08/15/13 20:08:06 (8 months ago)
Author:
ryan
Message:

wp_get_shortlink() improvements.

  • Return shortlinks for pages and public CPTs.
  • Return shortlinks even when cruft-free links are not enabled.
  • Unit tests

Props sillybean, layotte, cais
fixes #18632
see #14760

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/edit-form-advanced.php

    r24955 r25030  
    369369$sample_permalink_html = $post_type_object->public ? get_sample_permalink_html($post->ID) : ''; 
    370370$shortlink = wp_get_shortlink($post->ID, 'post'); 
    371 if ( !empty($shortlink) ) 
     371if ( !empty( $shortlink ) && $shortlink !== get_permalink( $post->ID ) ) 
    372372    $sample_permalink_html .= '<input id="shortlink" type="hidden" value="' . esc_attr($shortlink) . '" /><a href="#" class="button button-small" onclick="prompt(&#39;URL:&#39;, jQuery(\'#shortlink\').val()); return false;">' . __('Get Shortlink') . '</a>'; 
    373373 
  • trunk/src/wp-includes/link-template.php

    r24940 r25030  
    23522352    global $wp_query; 
    23532353    $post_id = 0; 
    2354     if ( 'query' == $context && is_single() ) { 
     2354    if ( 'query' == $context && is_singular() ) {  
    23552355        $post_id = $wp_query->get_queried_object_id(); 
     2356        $post = get_post( $post_id ); 
    23562357    } elseif ( 'post' == $context ) { 
    2357         $post = get_post($id); 
     2358        $post = get_post( $id ); 
    23582359        $post_id = $post->ID; 
    23592360    } 
     
    23612362    $shortlink = ''; 
    23622363 
    2363     // Return p= link for posts. 
    2364     if ( !empty($post_id) && '' != get_option('permalink_structure') ) { 
    2365         $post = get_post($post_id); 
    2366         if ( isset($post->post_type) && 'post' == $post->post_type ) 
    2367             $shortlink = home_url('?p=' . $post->ID); 
     2364    // Return p= link for all public post types. 
     2365    if ( ! empty( $post_id ) ) { 
     2366        $post_type = get_post_type_object( $post->post_type ); 
     2367        if ( $post_type->public ) 
     2368            $shortlink = home_url('?p=' . $post_id); 
    23682369    } 
    23692370 
  • trunk/tests/tests/link.php

    r25002 r25030  
    33 * @group link 
    44 */ 
    5 class Tests_Link_Functions extends WP_UnitTestCase { 
     5class Tests_Link extends WP_UnitTestCase { 
    66 
    77    function _get_pagenum_link_cb( $url ) { 
     
    2828        $_SERVER['REQUEST_URI'] = $old_req_uri; 
    2929    } 
     30 
     31    function test_wp_get_shortlink() { 
     32        $post_id = $this->factory->post->create(); 
     33        $post_id2 = $this->factory->post->create(); 
     34 
     35        // Basic case 
     36        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     37 
     38        // Global post is not set 
     39        $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) ); 
     40        $this->assertEquals( '', wp_get_shortlink( 0 ) ); 
     41        $this->assertEquals( '', wp_get_shortlink() ); 
     42 
     43        $GLOBALS['post'] = get_post( $post_id ); 
     44 
     45        // Global post is set 
     46        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0, 'post' ) ); 
     47        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0 ) ); 
     48        $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink() ); 
     49 
     50        // Not the global post 
     51        $this->assertEquals( get_permalink( $post_id2 ), wp_get_shortlink( $post_id2, 'post' ) ); 
     52 
     53        unset( $GLOBALS['post'] ); 
     54 
     55        // Global post is not set, once again 
     56        $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) ); 
     57        $this->assertEquals( '', wp_get_shortlink( 0 ) ); 
     58        $this->assertEquals( '', wp_get_shortlink() ); 
     59 
     60        global $wp_rewrite; 
     61        $wp_rewrite->permalink_structure = ''; 
     62        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 
     63        $wp_rewrite->flush_rules(); 
     64 
     65        // With a permalink structure set, get_permalink() will no longer match. 
     66        $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     67        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     68 
     69        // Global post and permalink structure are set 
     70        $GLOBALS['post'] = get_post( $post_id ); 
     71        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) ); 
     72        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) ); 
     73        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink() ); 
     74 
     75        $wp_rewrite->set_permalink_structure( '' ); 
     76        $wp_rewrite->flush_rules(); 
     77    } 
     78 
     79    function test_wp_get_shortlink_with_page() { 
     80        $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); 
     81 
     82        // Basic case 
     83        // Don't test against get_permalink() since it uses ?page_id= for pages. 
     84        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     85 
     86        global $wp_rewrite; 
     87        $wp_rewrite->permalink_structure = ''; 
     88        $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 
     89        $wp_rewrite->flush_rules(); 
     90 
     91        $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) ); 
     92 
     93        $wp_rewrite->set_permalink_structure( '' ); 
     94        $wp_rewrite->flush_rules(); 
     95    } 
     96 
    3097} 
Note: See TracChangeset for help on using the changeset viewer.