| 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 |
* Additional Search Queries: notification, notifications, toolbar, adminbar, push, comments |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( !defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) ); |
| 14 |
|
| 15 |
class Jetpack_Notifications { |
| 16 |
public $jetpack = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* Singleton |
| 20 |
* @static |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
static $instance = array(); |
| 24 |
|
| 25 |
if ( !$instance ) { |
| 26 |
$instance[0] = new Jetpack_Notifications; |
| 27 |
} |
| 28 |
|
| 29 |
return $instance[0]; |
| 30 |
} |
| 31 |
|
| 32 |
function __construct() { |
| 33 |
$this->jetpack = Jetpack::init(); |
| 34 |
|
| 35 |
add_action( 'init', array( &$this, 'action_init' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
function wpcom_static_url($file) { |
| 39 |
$i = hexdec( substr( md5( $file ), -1 ) ) % 2; |
| 40 |
$url = 'http://s' . $i . '.wp.com' . $file; |
| 41 |
return set_url_scheme( $url ); |
| 42 |
} |
| 43 |
|
| 44 |
// return the major version of Internet Explorer the viewer is using or false if it's not IE |
| 45 |
public static function get_internet_explorer_version() { |
| 46 |
static $version; |
| 47 |
if ( isset( $version ) ) { |
| 48 |
return $version; |
| 49 |
} |
| 50 |
|
| 51 |
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; |
| 52 |
|
| 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 |
|
| 61 |
public static function current_browser_is_supported() { |
| 62 |
static $supported; |
| 63 |
|
| 64 |
if ( isset( $supported ) ) { |
| 65 |
return $supported; |
| 66 |
} |
| 67 |
|
| 68 |
$ie_version = self::get_internet_explorer_version(); |
| 69 |
if ( false === $ie_version ) { |
| 70 |
return $supported = true; |
| 71 |
} |
| 72 |
|
| 73 |
if ( $ie_version < 8 ) { |
| 74 |
return $supported = false; |
| 75 |
} |
| 76 |
|
| 77 |
return $supported = true; |
| 78 |
} |
| 79 |
|
| 80 |
function action_init() { |
| 81 |
//syncing must wait until after init so |
| 82 |
//post types that support comments |
| 83 |
$filt_post_types = array(); |
| 84 |
$all_post_types = get_post_types(); |
| 85 |
foreach ( $all_post_types as $post_type ) { |
| 86 |
if ( post_type_supports( $post_type, 'comments' ) ) { |
| 87 |
$filt_post_types[] = $post_type; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) |
| 92 |
return; |
| 93 |
|
| 94 |
if ( !has_filter( 'show_admin_bar', '__return_true' ) && !is_user_logged_in() ) |
| 95 |
return; |
| 96 |
|
| 97 |
if ( !self::current_browser_is_supported() ) |
| 98 |
return; |
| 99 |
|
| 100 |
add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu'), 120 ); |
| 101 |
add_action( 'wp_head', array( &$this, 'styles_and_scripts'), 120 ); |
| 102 |
add_action( 'admin_head', array( &$this, 'styles_and_scripts') ); |
| 103 |
} |
| 104 |
|
| 105 |
function styles_and_scripts() { |
| 106 |
if ( !is_rtl() ) { |
| 107 |
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 ); |
| 108 |
} else { |
| 109 |
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 ); |
| 110 |
} |
| 111 |
wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER ); |
| 112 |
|
| 113 |
$this->print_js(); |
| 114 |
|
| 115 |
// attempt to use core or plugin libraries if registered |
| 116 |
if ( !wp_script_is( 'mustache', 'registered' ) ) { |
| 117 |
wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER ); |
| 118 |
} |
| 119 |
if ( !wp_script_is( 'underscore', 'registered' ) ) { |
| 120 |
wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER ); |
| 121 |
} |
| 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 |
|
| 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 |
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 ); |
| 128 |
} |
| 129 |
|
| 130 |
function admin_bar_menu() { |
| 131 |
global $wp_admin_bar, $current_blog; |
| 132 |
|
| 133 |
if ( !is_object( $wp_admin_bar ) ) |
| 134 |
return; |
| 135 |
|
| 136 |
$wpcom_locale = get_locale(); |
| 137 |
|
| 138 |
if ( !class_exists( 'GP_Locales' ) ) { |
| 139 |
if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
| 140 |
require JETPACK__GLOTPRESS_LOCALES_PATH; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if ( class_exists( 'GP_Locales' ) ) { |
| 145 |
$wpcom_locale_object = GP_Locales::by_field( 'wp_locale', get_locale() ); |
| 146 |
if ( $wpcom_locale_object instanceof GP_Locale ) { |
| 147 |
$wpcom_locale = $wpcom_locale_object->slug; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$classes = 'wpnt-loading wpn-read'; |
| 152 |
$wp_admin_bar->add_menu( array( |
| 153 |
'id' => 'notes', |
| 154 |
'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '"> |
| 155 |
<span class="noticon noticon-notification"></span> |
| 156 |
</span>', |
| 157 |
'meta' => array( |
| 158 |
'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>', |
| 159 |
'class' => 'menupop', |
| 160 |
), |
| 161 |
'parent' => 'top-secondary', |
| 162 |
) ); |
| 163 |
} |
| 164 |
|
| 165 |
function print_js() { |
| 166 |
$link_accounts_url = is_user_logged_in() && !Jetpack::is_user_connected() ? Jetpack::admin_url() : false; |
| 167 |
?> |
| 168 |
<script type="text/javascript"> |
| 169 |
/* <![CDATA[ */ |
| 170 |
var wpNotesIsJetpackClient = true; |
| 171 |
var wpNotesIsJetpackClientV2 = true; |
| 172 |
<?php if ( $link_accounts_url ) : ?> |
| 173 |
var wpNotesLinkAccountsURL = '<?php print $link_accounts_url; ?>'; |
| 174 |
<?php endif; ?> |
| 175 |
/* ]]> */ |
| 176 |
</script> |
| 177 |
<?php |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
Jetpack_Notifications::init(); |
| 183 |
|