monsterinsights_get_shortlink

The Google Analytics for WordPress by MonsterInsights monsterinsights get shortlink function.

Description

monsterinsights_get_shortlink(); 

Usage

  1. if ( !function_exists( 'monsterinsights_get_shortlink' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-analytics-for-wordpress-by-monsterinsights/includes/admin/ajax.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = monsterinsights_get_shortlink(); 
  7.  

Defined (1)

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

/includes/admin/ajax.php  
  1. function monsterinsights_get_shortlink() { 
  2. // Run a security check first. 
  3. check_ajax_referer( 'mi-admin-nonce', 'nonce' ); 
  4.  
  5. $shorten = ! empty( $_POST['url'] ) ? esc_url_raw( $_POST['url'] ) : ''; 
  6. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  7. echo $shorten; 
  8. wp_die(); 
  9.  
  10. $url = 'https://www.googleapis.com/urlshortener/v1/url'; 
  11.  
  12. // If no url passed die 
  13. if ( ! $shorten ) { 
  14. echo $shorten; 
  15. wp_die(); 
  16.  
  17. // if the url is already shortened, don't re-run 
  18. if ( strpos( $shorten, 'goo.g') !== false ) { 
  19. echo $shorten; 
  20. wp_die(); 
  21.  
  22. $result = wp_remote_post( 
  23. add_query_arg( 
  24. 'key',  
  25. 'AIzaSyCfHOlx8NbBVSpmHPqxophzULWSAzWDyio',  
  26. 'https://www.googleapis.com/urlshortener/v1/url' 
  27. ),  
  28. array( 
  29. 'body' => json_encode( array('longUrl' => esc_url_raw( $shorten ) ) ),  
  30. 'headers' => array( 'Content-Type' => 'application/json') 
  31. ); 
  32.  
  33. if ( is_wp_error( $result ) ) { 
  34. echo $shorten; 
  35. wp_die(); 
  36. $result = json_decode( $result['body'] ); 
  37. $shortlink = $result->id; 
  38. echo $shortlink; 
  39. wp_die();