Jetpack_Notifications

The Jetpack by WordPress.com Jetpack Notifications class.

Defined (1)

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

/modules/notes.php  
  1. class Jetpack_Notifications { 
  2. public $jetpack = false; 
  3.  
  4. /** 
  5. * Singleton 
  6. * @static 
  7. */ 
  8. public static function init() { 
  9. static $instance = array(); 
  10.  
  11. if ( !$instance ) { 
  12. $instance[0] = new Jetpack_Notifications; 
  13.  
  14. return $instance[0]; 
  15.  
  16. function __construct() { 
  17. $this->jetpack = Jetpack::init(); 
  18.  
  19. add_action( 'init', array( &$this, 'action_init' ) ); 
  20.  
  21. function wpcom_static_url($file) { 
  22. $i = hexdec( substr( md5( $file ), -1 ) ) % 2; 
  23. $url = 'http://s' . $i . '.wp.com' . $file; 
  24. return set_url_scheme( $url ); 
  25.  
  26. // return the major version of Internet Explorer the viewer is using or false if it's not IE 
  27. public static function get_internet_explorer_version() { 
  28. static $version; 
  29. if ( isset( $version ) ) { 
  30. return $version; 
  31.  
  32. $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; 
  33.  
  34. preg_match( '/MSIE (\d+)/', $user_agent, $matches ); 
  35. $version = empty( $matches[1] ) ? null : $matches[1]; 
  36. if ( empty( $version ) || !$version ) { 
  37. return false; 
  38. return $version; 
  39.  
  40. public static function current_browser_is_supported() { 
  41. static $supported; 
  42.  
  43. if ( isset( $supported ) ) { 
  44. return $supported; 
  45.  
  46. $ie_version = self::get_internet_explorer_version(); 
  47. if ( false === $ie_version ) { 
  48. return $supported = true; 
  49.  
  50. if ( $ie_version < 8 ) { 
  51. return $supported = false; 
  52.  
  53. return $supported = true; 
  54.  
  55. function action_init() { 
  56. //syncing must wait until after init so 
  57. //post types that support comments 
  58. $filt_post_types = array(); 
  59. $all_post_types = get_post_types(); 
  60. foreach ( $all_post_types as $post_type ) { 
  61. if ( post_type_supports( $post_type, 'comments' ) ) { 
  62. $filt_post_types[] = $post_type; 
  63.  
  64. Jetpack_Sync::sync_posts( __FILE__, array( 
  65. 'post_types' => $filt_post_types,  
  66. 'post_stati' => array( 'publish' ),  
  67. ) ); 
  68. Jetpack_Sync::sync_comments( __FILE__, array( 
  69. 'post_types' => $filt_post_types,  
  70. 'post_stati' => array( 'publish' ),  
  71. 'comment_stati' => array( 'approve', 'approved', '1', 'hold', 'unapproved', 'unapprove', '0', 'spam', 'trash' ),  
  72. ) ); 
  73.  
  74. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 
  75. return; 
  76.  
  77. if ( !has_filter( 'show_admin_bar', '__return_true' ) && !is_user_logged_in() ) 
  78. return; 
  79.  
  80. if ( !self::current_browser_is_supported() ) 
  81. return; 
  82.  
  83. add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu'), 120 ); 
  84. add_action( 'wp_head', array( &$this, 'styles_and_scripts'), 120 ); 
  85. add_action( 'admin_head', array( &$this, 'styles_and_scripts') ); 
  86.  
  87. function styles_and_scripts() { 
  88. if ( !is_rtl() ) { 
  89. wp_enqueue_style( 'wpcom-notes-admin-bar', $this->wpcom_static_url( '/wp-content/mu-plugins/notes/admin-bar-v2.css' ), array(), JETPACK_NOTES__CACHE_BUSTER ); 
  90. } else { 
  91. wp_enqueue_style( 'wpcom-notes-admin-bar', $this->wpcom_static_url( '/wp-content/mu-plugins/notes/rtl/admin-bar-v2-rtl.css' ), array(), JETPACK_NOTES__CACHE_BUSTER ); 
  92. wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER ); 
  93.  
  94. $this->print_js(); 
  95.  
  96. // attempt to use core or plugin libraries if registered 
  97. if ( !wp_script_is( 'mustache', 'registered' ) ) { 
  98. wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER ); 
  99. if ( !wp_script_is( 'underscore', 'registered' ) ) { 
  100. wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER ); 
  101. if ( !wp_script_is( 'backbone', 'registered' ) ) { 
  102. wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER ); 
  103.  
  104. wp_register_script( 'wpcom-notes-common', $this->wpcom_static_url( '/wp-content/mu-plugins/notes/notes-common-v2.js' ), array( 'jquery', 'underscore', 'backbone', 'mustache' ), JETPACK_NOTES__CACHE_BUSTER ); 
  105. wp_enqueue_script( 'wpcom-notes-admin-bar', $this->wpcom_static_url( '/wp-content/mu-plugins/notes/admin-bar-v2.js' ), array( 'wpcom-notes-common' ), JETPACK_NOTES__CACHE_BUSTER ); 
  106.  
  107. function admin_bar_menu() { 
  108. global $wp_admin_bar, $current_blog; 
  109.  
  110. if ( !is_object( $wp_admin_bar ) ) 
  111. return; 
  112.  
  113. $wpcom_locale = get_locale(); 
  114.  
  115. if ( !class_exists( 'GP_Locales' ) ) { 
  116. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { 
  117. require JETPACK__GLOTPRESS_LOCALES_PATH; 
  118.  
  119. if ( class_exists( 'GP_Locales' ) ) { 
  120. $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() ); 
  121. if ( $wpcom_locale_object instanceof GP_Locale ) { 
  122. $wpcom_locale = $wpcom_locale_object->slug; 
  123.  
  124. $classes = 'wpnt-loading wpn-read'; 
  125. $wp_admin_bar->add_menu( array( 
  126. 'id' => 'notes',  
  127. 'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '"> 
  128. <span class="noticon noticon-notification"></span> 
  129. </span>',  
  130. 'meta' => array( 
  131. 'html' => '<div id="wpnt-notes-panel2" style="display:none" lang="'. esc_attr( $wpcom_locale ) . '" dir="' . ( is_rtl() ? 'rtl' : 'ltr' ) . '"><div class="wpnt-notes-panel-header"><span class="wpnt-notes-header">' . __( 'Notifications', 'jetpack' ) . '</span><span class="wpnt-notes-panel-link"></span></div></div>',  
  132. 'class' => 'menupop',  
  133. ),  
  134. 'parent' => 'top-secondary',  
  135. ) ); 
  136.  
  137. function print_js() { 
  138. $link_accounts_url = is_user_logged_in() && !Jetpack::is_user_connected() ? Jetpack::admin_url() : false; 
  139. ?> 
  140. <script type="text/javascript"> 
  141. /* <![CDATA[ */ 
  142. var wpNotesIsJetpackClient = true; 
  143. var wpNotesIsJetpackClientV2 = true; 
  144. <?php if ( $link_accounts_url ) : ?> 
  145. var wpNotesLinkAccountsURL = '<?php print $link_accounts_url; ?>'; 
  146. <?php endif; ?> 
  147. /* ]]> */ 
  148. </script> 
  149. <?php 
  150.