Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Aug 7th, 2011  |  syntax: None  |  size: 3.35 KB  |  hits: 92  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Display Browser Nag Meta Box
  2. function wp_dashboard_browser_nag() {
  3.         $notice = '';
  4.         $response = wp_check_browser_version();
  5.  
  6.         if ( $response ) {
  7.                 if ( $response['insecure'] ) {
  8.                         $msg = sprintf( __( "It looks like you're using an insecure version of <a href='%s'>%s</a>. Using an outdated browser makes your computer unsafe. For the best WordPress experience, please update your browser." ), esc_attr( $response['update_url'] ), esc_html( $response['name'] ) );
  9.                 } else {
  10.                         $msg = sprintf( __( "It looks like you're using an old version of <a href='%s'>%s</a>. For the best WordPress experience, please update your browser." ), esc_attr( $response['update_url'] ), esc_html( $response['name'] ) );
  11.                 }
  12.  
  13.                 $browser_nag_class = '';
  14.                 if ( !empty( $response['img_src'] ) ) {
  15.                         $img_src = ( is_ssl() && ! empty( $response['img_src_ssl'] ) )? $response['img_src_ssl'] : $response['img_src'];
  16.  
  17.                         $notice .= '<div class="alignright browser-icon"><a href="' . esc_attr($response['update_url']) . '"><img src="' . esc_attr( $img_src ) . '" alt="" /></a></div>';
  18.                         $browser_nag_class = ' has-browser-icon';
  19.                 }
  20.                 $notice .= "<p class='browser-update-nag{$browser_nag_class}'>{$msg}</p>";
  21.                 $notice .= sprintf( __( '<p><a href="%1$s" class="update-browser-link">Update %2$s</a> or learn how to <a href="%3$s" class="browse-happy-link">browse happy</a></p>' ), esc_attr( $response['update_url'] ), esc_html( $response['name'] ), 'http://browsehappy.com/' );
  22.                 $notice .= '<p class="hide-if-no-js"><a href="" class="dismiss">' . __( 'Dismiss' ) . '</a></p>';
  23.                 $notice .= '<div class="clear"></div>';
  24.         }
  25.  
  26.         echo apply_filters( 'browse-happy-notice', $notice, $response );
  27. }
  28.  
  29. function dashboard_browser_nag_class( $classes ) {
  30.         $response = wp_check_browser_version();
  31.  
  32.         if ( $response && $response['insecure'] )
  33.                 $classes[] = 'browser-insecure';
  34.  
  35.         return $classes;
  36. }
  37.  
  38.  
  39. function wp_check_browser_version() {
  40.  
  41.         if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  42.  
  43.                 return false;
  44.  
  45.         $key = md5( $_SERVER['HTTP_USER_AGENT'] );
  46.  
  47.         if ( false === ($response = get_site_transient('browser_' . $key) ) ) {
  48.  
  49.                 global $wp_version;
  50.  
  51.                 $options = array(
  52.  
  53.                         'body'                  => array( 'useragent' => $_SERVER['HTTP_USER_AGENT'] ),
  54.  
  55.                         'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
  56.  
  57.                 );
  58.  
  59.                 $response = wp_remote_post( 'http://api.wordpress.org/core/browse-happy/1.0/', $options );
  60.  
  61.                 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) )
  62.  
  63.                         return false;
  64.  
  65.                 /**
  66.  
  67.                  * Response should be an array with:
  68.  
  69.                  *  'name' - string - A user friendly browser name
  70.  
  71.                  *  'version' - string - The most recent version of the browser
  72.  
  73.                  *  'current_version' - string - The version of the browser the user is using
  74.  
  75.                  *  'upgrade' - boolean - Whether the browser needs an upgrade
  76.  
  77.                  *  'insecure' - boolean - Whether the browser is deemed insecure
  78.  
  79.                  *  'upgrade_url' - string - The url to visit to upgrade
  80.  
  81.                  *  'img_src' - string - An image representing the browser
  82.  
  83.                  *  'img_src_ssl' - string - An image (over SSL) representing the browser
  84.  
  85.                  */
  86.  
  87.                 $response = unserialize( wp_remote_retrieve_body( $response ) );
  88.  
  89.                 if ( ! $response )
  90.  
  91.                         return false;
  92.  
  93.                 set_site_transient( 'browser_' . $key, $response, 604800 ); // cache for 1 week
  94.  
  95.         }
  96.  
  97.         return $response;
  98.  
  99. }
  100.  
  101. wp_dashboard_browser_nag();
  102.  
  103. ?>