PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/notes.php

https://gitlab.com/Gashler/sg
PHP | 184 lines | 132 code | 33 blank | 19 comment | 20 complexity | 3b3f7c0b7ae6b396d65bffea0ca37383 MD5 | raw file
  1. <?php
  2. /**
  3. * Module Name: Notifications
  4. * Module Description: Receive notification of site activity via the admin toolbar and your Mobile devices.
  5. * Sort Order: 13
  6. * First Introduced: 1.9
  7. * Requires Connection: Yes
  8. * Auto Activate: Yes
  9. * Module Tags: Other
  10. */
  11. if ( !defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) );
  12. Jetpack_Sync::sync_options( __FILE__,
  13. 'home',
  14. 'blogname',
  15. 'siteurl',
  16. 'permalink_structure',
  17. 'category_base',
  18. 'tag_base',
  19. 'comment_moderation',
  20. 'default_comment_status',
  21. 'thread_comments',
  22. 'thread_comments_depth'
  23. );
  24. class Jetpack_Notifications {
  25. var $jetpack = false;
  26. /**
  27. * Singleton
  28. * @static
  29. */
  30. public static function init() {
  31. static $instance = array();
  32. if ( !$instance ) {
  33. $instance[0] = new Jetpack_Notifications;
  34. }
  35. return $instance[0];
  36. }
  37. function __construct() {
  38. $this->jetpack = Jetpack::init();
  39. add_action( 'init', array( &$this, 'action_init' ) );
  40. }
  41. function wpcom_static_url($file) {
  42. $i = hexdec( substr( md5( $file ), -1 ) ) % 2;
  43. $url = 'http://s' . $i . '.wp.com' . $file;
  44. return set_url_scheme( $url );
  45. }
  46. // return the major version of Internet Explorer the viewer is using or false if it's not IE
  47. public static function get_internet_explorer_version() {
  48. static $version;
  49. if ( isset( $version ) ) {
  50. return $version;
  51. }
  52. $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
  53. preg_match( '/MSIE (\d+)/', $user_agent, $matches );
  54. $version = empty( $matches[1] ) ? null : $matches[1];
  55. if ( empty( $version ) || !$version ) {
  56. return false;
  57. }
  58. return $version;
  59. }
  60. public static function current_browser_is_supported() {
  61. static $supported;
  62. if ( isset( $supported ) ) {
  63. return $supported;
  64. }
  65. $ie_version = self::get_internet_explorer_version();
  66. if ( false === $ie_version ) {
  67. return $supported = true;
  68. }
  69. if ( $ie_version < 8 ) {
  70. return $supported = false;
  71. }
  72. return $supported = true;
  73. }
  74. function action_init() {
  75. //syncing must wait until after init so
  76. //post types that support comments
  77. $filt_post_types = array();
  78. $all_post_types = get_post_types();
  79. foreach ( $all_post_types as $post_type ) {
  80. if ( post_type_supports( $post_type, 'comments' ) ) {
  81. $filt_post_types[] = $post_type;
  82. }
  83. }
  84. Jetpack_Sync::sync_posts( __FILE__, array(
  85. 'post_types' => $filt_post_types,
  86. 'post_stati' => array( 'publish' ),
  87. ) );
  88. Jetpack_Sync::sync_comments( __FILE__, array(
  89. 'post_types' => $filt_post_types,
  90. 'post_stati' => array( 'publish' ),
  91. 'comment_stati' => array( 'approve', 'approved', '1', 'hold', 'unapproved', 'unapprove', '0', 'spam', 'trash' ),
  92. ) );
  93. if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  94. return;
  95. if ( !has_filter( 'show_admin_bar', '__return_true' ) && !is_user_logged_in() )
  96. return;
  97. if ( !self::current_browser_is_supported() )
  98. return;
  99. add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu'), 120 );
  100. add_action( 'wp_head', array( &$this, 'styles_and_scripts'), 120 );
  101. add_action( 'admin_head', array( &$this, 'styles_and_scripts') );
  102. }
  103. function styles_and_scripts() {
  104. 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 );
  105. wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER );
  106. $this->print_js();
  107. // attempt to use core or plugin libraries if registered
  108. if ( !wp_script_is( 'mustache', 'registered' ) ) {
  109. wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
  110. }
  111. if ( !wp_script_is( 'underscore', 'registered' ) ) {
  112. wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
  113. }
  114. if ( !wp_script_is( 'backbone', 'registered' ) ) {
  115. wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER );
  116. }
  117. 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 );
  118. 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 );
  119. }
  120. function admin_bar_menu() {
  121. global $wp_admin_bar, $current_blog;
  122. if ( !is_object( $wp_admin_bar ) )
  123. return;
  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-panel" style="display:none" lang="'. esc_attr( get_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. <?php if ( $link_accounts_url ) : ?>
  144. var wpNotesLinkAccountsURL = '<?php print $link_accounts_url; ?>';
  145. <?php endif; ?>
  146. /* ]]> */
  147. </script>
  148. <?php
  149. }
  150. }
  151. Jetpack_Notifications::init();