diff --git a/src/wp-admin/edit-form-advanced.php b/src/wp-admin/edit-form-advanced.php
index e656dd5d60..4fef80f0eb 100644
--- a/src/wp-admin/edit-form-advanced.php
+++ b/src/wp-admin/edit-form-advanced.php
@@ -368,7 +368,7 @@ wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
public ? get_sample_permalink_html($post->ID) : '';
$shortlink = wp_get_shortlink($post->ID, 'post');
-if ( !empty($shortlink) )
+if ( !empty( $shortlink ) && $shortlink !== get_permalink( $post->ID ) )
$sample_permalink_html .= '' . __('Get Shortlink') . '';
if ( $post_type_object->public && ! ( 'pending' == get_post_status( $post ) && !current_user_can( $post_type_object->cap->publish_posts ) ) ) {
diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php
index 5fb61726fb..91c375d628 100644
--- a/src/wp-includes/link-template.php
+++ b/src/wp-includes/link-template.php
@@ -2351,20 +2351,21 @@ function wp_get_shortlink($id = 0, $context = 'post', $allow_slugs = true) {
global $wp_query;
$post_id = 0;
- if ( 'query' == $context && is_single() ) {
+ if ( 'query' == $context && is_singular() ) {
$post_id = $wp_query->get_queried_object_id();
+ $post = get_post( $post_id );
} elseif ( 'post' == $context ) {
- $post = get_post($id);
+ $post = get_post( $id );
$post_id = $post->ID;
}
$shortlink = '';
- // Return p= link for posts.
- if ( !empty($post_id) && '' != get_option('permalink_structure') ) {
- $post = get_post($post_id);
- if ( isset($post->post_type) && 'post' == $post->post_type )
- $shortlink = home_url('?p=' . $post->ID);
+ // Return p= link for all public post types.
+ if ( ! empty( $post_id ) ) {
+ $post_type = get_post_type_object( $post->post_type );
+ if ( $post_type->public )
+ $shortlink = home_url('?p=' . $post_id);
}
return apply_filters('get_shortlink', $shortlink, $id, $context, $allow_slugs);
diff --git a/tests/tests/link.php b/tests/tests/link.php
index 18ee8c0459..13ab21f176 100644
--- a/tests/tests/link.php
+++ b/tests/tests/link.php
@@ -2,7 +2,7 @@
/**
* @group link
*/
-class Tests_Link_Functions extends WP_UnitTestCase {
+class Tests_Link extends WP_UnitTestCase {
function _get_pagenum_link_cb( $url ) {
return $url . '/WooHoo';
@@ -27,4 +27,71 @@ class Tests_Link_Functions extends WP_UnitTestCase {
$_SERVER['REQUEST_URI'] = $old_req_uri;
}
+
+ function test_wp_get_shortlink() {
+ $post_id = $this->factory->post->create();
+ $post_id2 = $this->factory->post->create();
+
+ // Basic case
+ $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
+
+ // Global post is not set
+ $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) );
+ $this->assertEquals( '', wp_get_shortlink( 0 ) );
+ $this->assertEquals( '', wp_get_shortlink() );
+
+ $GLOBALS['post'] = get_post( $post_id );
+
+ // Global post is set
+ $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0, 'post' ) );
+ $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink( 0 ) );
+ $this->assertEquals( get_permalink( $post_id ), wp_get_shortlink() );
+
+ // Not the global post
+ $this->assertEquals( get_permalink( $post_id2 ), wp_get_shortlink( $post_id2, 'post' ) );
+
+ unset( $GLOBALS['post'] );
+
+ // Global post is not set, once again
+ $this->assertEquals( '', wp_get_shortlink( 0, 'post' ) );
+ $this->assertEquals( '', wp_get_shortlink( 0 ) );
+ $this->assertEquals( '', wp_get_shortlink() );
+
+ global $wp_rewrite;
+ $wp_rewrite->permalink_structure = '';
+ $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
+ $wp_rewrite->flush_rules();
+
+ // With a permalink structure set, get_permalink() will no longer match.
+ $this->assertNotEquals( get_permalink( $post_id ), wp_get_shortlink( $post_id, 'post' ) );
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
+
+ // Global post and permalink structure are set
+ $GLOBALS['post'] = get_post( $post_id );
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0, 'post' ) );
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( 0 ) );
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink() );
+
+ $wp_rewrite->set_permalink_structure( '' );
+ $wp_rewrite->flush_rules();
+ }
+
+ function test_wp_get_shortlink_with_page() {
+ $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
+
+ // Basic case
+ // Don't test against get_permalink() since it uses ?page_id= for pages.
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
+
+ global $wp_rewrite;
+ $wp_rewrite->permalink_structure = '';
+ $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
+ $wp_rewrite->flush_rules();
+
+ $this->assertEquals( home_url( '?p=' . $post_id ), wp_get_shortlink( $post_id, 'post' ) );
+
+ $wp_rewrite->set_permalink_structure( '' );
+ $wp_rewrite->flush_rules();
+ }
+
}
\ No newline at end of file