wpme_get_shortlink

The Jetpack by WordPress.com wpme get shortlink function.

Description

wpme_get_shortlink( (int) $id = 0, (string) $context = 'post', (bool) $allow_slugs = true ); 

Parameters (3)

0. $id — Optional. (int)
The id.
1. $context — Optional. (string) => 'post'
The context.
2. $allow_slugs — Optional. (bool) => true
The allow slugs.

Usage

  1. if ( !function_exists( 'wpme_get_shortlink' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/shortlinks.php'; 
  3.  
  4. // The id. 
  5. $id = -1; 
  6.  
  7. // The context. 
  8. $context = 'post'; 
  9.  
  10. // The allow slugs. 
  11. $allow_slugs = true; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wpme_get_shortlink($id, $context, $allow_slugs); 
  15.  

Defined (1)

The function is defined in the following location(s).

/modules/shortlinks.php  
  1. function wpme_get_shortlink( $id = 0, $context = 'post', $allow_slugs = true ) { 
  2. global $wp_query; 
  3.  
  4. $blog_id = Jetpack_Options::get_option( 'id' ); 
  5.  
  6. if ( 'query' == $context ) { 
  7. if ( is_singular() ) { 
  8. $id = $wp_query->get_queried_object_id(); 
  9. $context = 'post'; 
  10. } elseif ( is_front_page() ) { 
  11. $context = 'blog'; 
  12. } else { 
  13. return ''; 
  14.  
  15. if ( 'blog' == $context ) { 
  16. if ( empty( $id ) ) 
  17. $id = $blog_id; 
  18.  
  19. $wpme_url = 'http://wp.me/' . wpme_dec2sixtwo( $id ); 
  20. return set_url_scheme( $wpme_url ); 
  21.  
  22. $post = get_post( $id ); 
  23.  
  24. if ( empty( $post ) ) 
  25. return ''; 
  26.  
  27. $post_id = $post->ID; 
  28. $type = ''; 
  29.  
  30. if ( $allow_slugs && 'publish' == $post->post_status && 'post' == $post->post_type && strlen( $post->post_name ) <= 8 && false === strpos( $post->post_name, '%' ) 
  31. && false === strpos( $post->post_name, '-' ) ) { 
  32. $id = $post->post_name; 
  33. $type = 's'; 
  34. } else { 
  35. $id = wpme_dec2sixtwo( $post_id ); 
  36. if ( 'page' == $post->post_type ) 
  37. $type = 'P'; 
  38. elseif ( 'post' == $post->post_type || post_type_supports( $post->post_type, 'shortlinks' ) ) 
  39. $type= 'p'; 
  40. elseif ( 'attachment' == $post->post_type ) 
  41. $type = 'a'; 
  42.  
  43. if ( empty( $type ) ) 
  44. return ''; 
  45.  
  46. $url = 'http://wp.me/' . $type . wpme_dec2sixtwo( $blog_id ) . '-' . $id; 
  47. return set_url_scheme( $url );