summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/jetpack/modules/notes.php')
-rw-r--r--plugins/jetpack/modules/notes.php190
1 files changed, 99 insertions, 91 deletions
diff --git a/plugins/jetpack/modules/notes.php b/plugins/jetpack/modules/notes.php
index 04ef8b43..18ee3567 100644
--- a/plugins/jetpack/modules/notes.php
+++ b/plugins/jetpack/modules/notes.php
@@ -1,108 +1,103 @@
-<?php
+<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Module Name: Notifications
* Module Description: Receive instant notifications of site comments and likes.
* Sort Order: 13
* First Introduced: 1.9
* Requires Connection: Yes
+ * Requires User Connection: Yes
* Auto Activate: Yes
* Module Tags: Other
* Feature: General
* Additional Search Queries: notification, notifications, toolbar, adminbar, push, comments
+ *
+ * @package automattic/jetpack
*/
-if ( !defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) );
+use Automattic\Jetpack\Connection\Manager as Connection_Manager;
+if ( ! defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) {
+ define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) );
+}
+
+/**
+ * Notifications class.
+ */
class Jetpack_Notifications {
+ /**
+ * Jetpack object.
+ *
+ * @var bool|Jetpack Jetpack object.
+ */
public $jetpack = false;
/**
* Singleton
+ *
* @static
*/
public static function init() {
static $instance = array();
- if ( !$instance ) {
- $instance[0] = new Jetpack_Notifications;
+ if ( ! $instance ) {
+ $instance[0] = new Jetpack_Notifications();
}
return $instance[0];
}
- function __construct() {
+ /**
+ * Constructor.
+ */
+ private function __construct() {
$this->jetpack = Jetpack::init();
- add_action( 'init', array( &$this, 'action_init' ) );
- }
-
- function wpcom_static_url($file) {
- $i = hexdec( substr( md5( $file ), -1 ) ) % 2;
- return 'https://s' . $i . '.wp.com' . $file;
+ add_action( 'init', array( $this, 'action_init' ) );
}
- // return the major version of Internet Explorer the viewer is using or false if it's not IE
- public static function get_internet_explorer_version() {
- static $version;
- if ( isset( $version ) ) {
- return $version;
- }
-
- $user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';
-
- preg_match( '/MSIE (\d+)/', $user_agent, $matches );
- $version = empty( $matches[1] ) ? null : $matches[1];
- if ( empty( $version ) || !$version ) {
- return false;
- }
- return $version;
- }
-
- public static function current_browser_is_supported() {
- static $supported;
-
- if ( isset( $supported ) ) {
- return $supported;
- }
-
- $ie_version = self::get_internet_explorer_version();
- if ( false === $ie_version ) {
- return $supported = true;
- }
-
- if ( $ie_version < 8 ) {
- return $supported = false;
- }
-
- return $supported = true;
+ /**
+ * Adds s0.wp.com to a file path.
+ *
+ * @param string $file File path.
+ *
+ * @return string
+ */
+ public function wpcom_static_url( $file ) {
+ return 'https://s0.wp.com' . $file;
}
- function action_init() {
- //syncing must wait until after init so
- //post types that support comments
- $filt_post_types = array();
- $all_post_types = get_post_types();
- foreach ( $all_post_types as $post_type ) {
- if ( post_type_supports( $post_type, 'comments' ) ) {
- $filt_post_types[] = $post_type;
- }
- }
-
- if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
+ /**
+ * Init the notifications admin bar.
+ *
+ * @return void
+ */
+ public function action_init() {
+ if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
+ }
- if ( !has_filter( 'show_admin_bar', '__return_true' ) && !is_user_logged_in() )
+ if ( ! has_filter( 'show_admin_bar', '__return_true' ) && ! is_user_logged_in() ) {
return;
+ }
- if ( !self::current_browser_is_supported() )
+ // Do not show notifications in the Site Editor, which is always in fullscreen mode.
+ global $pagenow;
+ // phpcs:ignore WordPress.Security.NonceVerification
+ if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) && 'gutenberg-edit-site' === $_GET['page'] ) {
return;
+ }
- add_action( 'admin_bar_menu', array( &$this, 'admin_bar_menu'), 120 );
- add_action( 'wp_head', array( &$this, 'styles_and_scripts'), 120 );
- add_action( 'admin_head', array( &$this, 'styles_and_scripts') );
+ add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 120 );
+ add_action( 'wp_head', array( $this, 'styles_and_scripts' ), 120 );
+ add_action( 'admin_head', array( $this, 'styles_and_scripts' ) );
}
- function styles_and_scripts() {
+ /**
+ * Enqueues and registers styles/scripts for notifications.
+ *
+ * @return void
+ */
+ public function styles_and_scripts() {
$is_rtl = is_rtl();
if ( Jetpack::is_module_active( 'masterbar' ) ) {
@@ -128,27 +123,27 @@ class Jetpack_Notifications {
$this->print_js();
- // attempt to use core or plugin libraries if registered
+ // attempt to use core or plugin libraries if registered.
$script_handles = array();
- if ( !wp_script_is( 'mustache', 'registered' ) ) {
- wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
+ if ( ! wp_script_is( 'mustache', 'registered' ) ) {
+ wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER, true );
}
$script_handles[] = 'mustache';
- if ( !wp_script_is( 'underscore', 'registered' ) ) {
- wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
+ if ( ! wp_script_is( 'underscore', 'registered' ) ) {
+ wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER, true );
}
$script_handles[] = 'underscore';
- if ( !wp_script_is( 'backbone', 'registered' ) ) {
- wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER );
+ if ( ! wp_script_is( 'backbone', 'registered' ) ) {
+ wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER, true );
}
$script_handles[] = 'backbone';
- 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 );
+ 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, true );
$script_handles[] = 'wpcom-notes-common';
$script_handles[] = 'jquery';
$script_handles[] = 'jquery-migrate';
$script_handles[] = 'jquery-core';
- 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 );
+ 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, true );
$script_handles[] = 'wpcom-notes-admin-bar';
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
@@ -166,15 +161,21 @@ class Jetpack_Notifications {
}
}
- function admin_bar_menu() {
- global $wp_admin_bar, $current_blog;
+ /**
+ * Adds notifications bubble to the admin bar.
+ *
+ * @return void
+ */
+ public function admin_bar_menu() {
+ global $wp_admin_bar;
- if ( !is_object( $wp_admin_bar ) )
+ if ( ! is_object( $wp_admin_bar ) ) {
return;
+ }
$wpcom_locale = get_locale();
- if ( !class_exists( 'GP_Locales' ) ) {
+ if ( ! class_exists( 'GP_Locales' ) ) {
if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) {
require JETPACK__GLOTPRESS_LOCALES_PATH;
}
@@ -188,32 +189,39 @@ class Jetpack_Notifications {
}
$classes = 'wpnt-loading wpn-read';
- $wp_admin_bar->add_menu( array(
- 'id' => 'notes',
- 'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '">
+ $wp_admin_bar->add_menu(
+ array(
+ 'id' => 'notes',
+ 'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '">
<span class="noticon noticon-notification"></span>
</span>',
- 'meta' => array(
- '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>',
- 'class' => 'menupop',
- ),
- 'parent' => 'top-secondary',
- ) );
+ 'meta' => array(
+ '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>',
+ 'class' => 'menupop',
+ ),
+ 'parent' => 'top-secondary',
+ )
+ );
}
- function print_js() {
- $link_accounts_url = is_user_logged_in() && !Jetpack::is_user_connected() ? Jetpack::admin_url() : false;
-?>
+ /**
+ * Echos the Notes JS.
+ *
+ * @return void
+ */
+ public function print_js() {
+ $link_accounts_url = is_user_logged_in() && ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ? Jetpack::admin_url() : false;
+ ?>
<script data-ampdevmode type="text/javascript">
/* <![CDATA[ */
var wpNotesIsJetpackClient = true;
var wpNotesIsJetpackClientV2 = true;
-<?php if ( $link_accounts_url ) : ?>
- var wpNotesLinkAccountsURL = '<?php print $link_accounts_url; ?>';
+ <?php if ( $link_accounts_url ) : ?>
+ var wpNotesLinkAccountsURL = '<?php echo esc_url( $link_accounts_url ); ?>';
<?php endif; ?>
/* ]]> */
</script>
-<?php
+ <?php
}
}