notes.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Module Name: Notifications
  4. * Module Description: Receive instant notifications of site comments and likes.
  5. * Sort Order: 13
  6. * First Introduced: 1.9
  7. * Requires Connection: Yes
  8. * Auto Activate: Yes
  9. * Module Tags: Other
  10. * Feature: General
  11. * Additional Search Queries: notification, notifications, toolbar, adminbar, push, comments
  12. */
  13. if ( !defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) );
  14. class Jetpack_Notifications {
  15. public $jetpack = false;
  16. /**
  17. * Singleton
  18. * @static
  19. */
  20. public static function init() {
  21. static $instance = array();
  22. if ( !$instance ) {
  23. $instance[0] = new Jetpack_Notifications;
  24. }
  25. return $instance[0];
  26. }
  27. function __construct() {
  28. $this->jetpack = Jetpack::init();
  29. add_action( 'init', array( &$this, 'action_init' ) );
  30. }
  31. function wpcom_static_url($file) {
  32. $i = hexdec( substr( md5( $file ), -1 ) ) % 2;
  33. $url = 'http://s' . $i . '.wp.com' . $file;
  34. return set_url_scheme( $url );
  35. }
  36. // return the major version of Internet Explorer the viewer is using or false if it's not IE
  37. public static function get_internet_explorer_version() {
  38. static $version;
  39. if ( isset( $version ) ) {
  40. return $version;
  41. }
  42. $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
  43. preg_match( '/MSIE (\d+)/', $user_agent, $matches );
  44. $version = empty( $matches[1] ) ? null : $matches[1];
  45. if ( empty( $version ) || !$version ) {
  46. return false;
  47. }
  48. return $version;
  49. }
  50. public static function current_browser_is_supported() {
  51. static $supported;
  52. if ( isset( $supported ) ) {
  53. return $supported;
  54. }
  55. $ie_version = self::get_internet_explorer_version();
  56. if ( false === $ie_version ) {
  57. return $supported = true;
  58. }
  59. if ( $ie_version < 8 ) {
  60. return $supported = false;
  61. }
  62. return $supported = true;
  63. }
  64. function action_init() {
  65. //syncing must wait until after init so
  66. //post types that support comments
  67. $filt_post_types = array();
  68. $all_post_types = get_post_types();
  69. foreach ( $all_post_types as $post_type ) {
  70. if ( post_type_supports( $post_type, 'comments' ) ) {
  71. $filt_post_types[] = $post_type;
  72. }
  73. }
  74. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  75. return;
  76. if ( !has_filter( 'show_admin_bar', '__return_true' ) && !is_user_logged_in() )
  77. return;
  78. if ( !self::current_browser_is_supported() )
  79. return;
  80. add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu'), 120 );
  81. add_action( 'wp_head', array( &$this, 'styles_and_scripts'), 120 );
  82. add_action( 'admin_head', array( &$this, 'styles_and_scripts') );
  83. }
  84. function styles_and_scripts() {
  85. $is_rtl = is_rtl();
  86. if ( Jetpack::is_module_active( 'masterbar' ) ) {
  87. /**
  88. * Can be used to force Notifications to display in RTL style.
  89. *
  90. * @module notes
  91. *
  92. * @since 4.8.0
  93. *
  94. * @param bool true Should notifications be displayed in RTL style. Defaults to false.
  95. */
  96. $is_rtl = apply_filters( 'a8c_wpcom_masterbar_enqueue_rtl_notification_styles', false );
  97. }
  98. if ( ! $is_rtl ) {
  99. 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 );
  100. } else {
  101. 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 );
  102. }
  103. wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER );
  104. $this->print_js();
  105. // attempt to use core or plugin libraries if registered
  106. if ( !wp_script_is( 'mustache', 'registered' ) ) {
  107. wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
  108. }
  109. if ( !wp_script_is( 'underscore', 'registered' ) ) {
  110. wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
  111. }
  112. if ( !wp_script_is( 'backbone', 'registered' ) ) {
  113. wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER );
  114. }
  115. 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 );
  116. 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 );
  117. }
  118. function admin_bar_menu() {
  119. global $wp_admin_bar, $current_blog;
  120. if ( !is_object( $wp_admin_bar ) )
  121. return;
  122. $wpcom_locale = get_locale();
  123. if ( !class_exists( 'GP_Locales' ) ) {
  124. if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
  125. require JETPACK__GLOTPRESS_LOCALES_PATH;
  126. }
  127. }
  128. if ( class_exists( 'GP_Locales' ) ) {
  129. $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', $wpcom_locale );
  130. if ( $wpcom_locale_object instanceof GP_Locale ) {
  131. $wpcom_locale = $wpcom_locale_object->slug;
  132. }
  133. }
  134. $classes = 'wpnt-loading wpn-read';
  135. $wp_admin_bar->add_menu( array(
  136. 'id' => 'notes',
  137. 'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '">
  138. <span class="noticon noticon-notification"></span>
  139. </span>',
  140. 'meta' => array(
  141. '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>',
  142. 'class' => 'menupop',
  143. ),
  144. 'parent' => 'top-secondary',
  145. ) );
  146. }
  147. function print_js() {
  148. $link_accounts_url = is_user_logged_in() && !Jetpack::is_user_connected() ? Jetpack::admin_url() : false;
  149. ?>
  150. <script type="text/javascript">
  151. /* <![CDATA[ */
  152. var wpNotesIsJetpackClient = true;
  153. var wpNotesIsJetpackClientV2 = true;
  154. <?php if ( $link_accounts_url ) : ?>
  155. var wpNotesLinkAccountsURL = '<?php print $link_accounts_url; ?>';
  156. <?php endif; ?>
  157. /* ]]> */
  158. </script>
  159. <?php
  160. }
  161. }
  162. Jetpack_Notifications::init();