Açıklama Yok

notes.php 7.4KB

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