| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | 2 | /** |
| 3 | 3 | * Module Name: Notifications |
| 4 | - * Module Description: Receive instant notifications of site comments and likes. | |
| 4 | + * Module Description: Receive real‑time notifications about site activity across your devices. | |
| 5 | 5 | * Sort Order: 13 |
| 6 | 6 | * First Introduced: 1.9 |
| 7 | 7 | * Requires Connection: Yes |
| 8 | 8 | * Requires User Connection: Yes |
| @@ -14,9 +14,14 @@ | ||
| 14 | 14 | * @package automattic/jetpack |
| 15 | 15 | */ |
| 16 | 16 | |
| 17 | 17 | use Automattic\Jetpack\Connection\Manager as Connection_Manager; |
| 18 | +use Automattic\Jetpack\Status\Host; | |
| 18 | 19 | |
| 20 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 21 | + exit( 0 ); | |
| 22 | +} | |
| 23 | + | |
| 19 | 24 | if ( ! defined( 'JETPACK_NOTES__CACHE_BUSTER' ) ) { |
| 20 | 25 | define( 'JETPACK_NOTES__CACHE_BUSTER', JETPACK__VERSION . '-' . gmdate( 'oW' ) . '-lite' ); |
| 21 | 26 | } |
| 22 | 27 | |
| @@ -79,22 +84,8 @@ | ||
| 79 | 84 | if ( ! has_filter( 'show_admin_bar', '__return_true' ) && ! is_user_logged_in() ) { |
| 80 | 85 | return; |
| 81 | 86 | } |
| 82 | 87 | |
| 83 | - // Do not show notifications in the Site Editor, which is always in fullscreen mode. | |
| 84 | - global $pagenow; | |
| 85 | - | |
| 86 | - // Pre 13.7 pages that still need to be supported if < 13.7 is | |
| 87 | - // still installed. | |
| 88 | - $allowed_old_pages = array( 'admin.php', 'themes.php' ); | |
| 89 | - $is_old_site_editor_page = in_array( $pagenow, $allowed_old_pages, true ) && isset( $_GET['page'] ) && 'gutenberg-edit-site' === $_GET['page']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 90 | - // For Gutenberg > 13.7, the core `site-editor.php` route is used instead | |
| 91 | - $is_site_editor_page = 'site-editor.php' === $pagenow; | |
| 92 | - | |
| 93 | - if ( $is_site_editor_page || $is_old_site_editor_page ) { | |
| 94 | - return; | |
| 95 | - } | |
| 96 | - | |
| 97 | 88 | add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 120 ); |
| 98 | 89 | add_action( 'wp_head', array( $this, 'styles_and_scripts' ), 120 ); |
| 99 | 90 | add_action( 'admin_head', array( $this, 'styles_and_scripts' ) ); |
| 100 | 91 | } |
| @@ -106,9 +97,9 @@ | ||
| 106 | 97 | */ |
| 107 | 98 | public function styles_and_scripts() { |
| 108 | 99 | $is_rtl = is_rtl(); |
| 109 | 100 | |
| 110 | - if ( Jetpack::is_module_active( 'masterbar' ) ) { | |
| 101 | + if ( ( new Host() )->is_woa_site() ) { | |
| 111 | 102 | /** |
| 112 | 103 | * Can be used to force Notifications to display in RTL style. |
| 113 | 104 | * |
| 114 | 105 | * @module notes |
| @@ -135,8 +126,11 @@ | ||
| 135 | 126 | $script_handles[] = 'wpcom-notes-common'; |
| 136 | 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, true ); |
| 137 | 128 | $script_handles[] = 'wpcom-notes-admin-bar'; |
| 138 | 129 | |
| 130 | + $wp_notes_args = 'var wpNotesArgs = ' . wp_json_encode( array( 'cacheBuster' => JETPACK_NOTES__CACHE_BUSTER ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; | |
| 131 | + wp_add_inline_script( 'wpcom-notes-admin-bar', $wp_notes_args, 'before' ); | |
| 132 | + | |
| 139 | 133 | if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) { |
| 140 | 134 | add_filter( |
| 141 | 135 | 'script_loader_tag', |
| 142 | 136 | function ( $tag, $handle ) use ( $script_handles ) { |
| @@ -162,9 +156,9 @@ | ||
| 162 | 156 | if ( ! is_object( $wp_admin_bar ) ) { |
| 163 | 157 | return; |
| 164 | 158 | } |
| 165 | 159 | |
| 166 | - $wpcom_locale = get_locale(); | |
| 160 | + $user_locale = get_user_locale(); | |
| 167 | 161 | |
| 168 | 162 | if ( ! class_exists( 'GP_Locales' ) ) { |
| 169 | 163 | if ( defined( 'JETPACK__GLOTPRESS_LOCALES_PATH' ) && file_exists( JETPACK__GLOTPRESS_LOCALES_PATH ) ) { |
| 170 | 164 | require JETPACK__GLOTPRESS_LOCALES_PATH; |
| @@ -171,33 +165,51 @@ | ||
| 171 | 165 | } |
| 172 | 166 | } |
| 173 | 167 | |
| 174 | 168 | if ( class_exists( 'GP_Locales' ) ) { |
| 175 | - $wpcom_locale_object = GP_Locales::by_field( 'wp_locale', $wpcom_locale ); | |
| 176 | - if ( $wpcom_locale_object instanceof GP_Locale ) { | |
| 177 | - $wpcom_locale = $wpcom_locale_object->slug; | |
| 169 | + $jetpack_locale_object = GP_Locales::by_field( 'slug', $user_locale ); | |
| 170 | + if ( $jetpack_locale_object instanceof GP_Locale ) { | |
| 171 | + $user_locale = $jetpack_locale_object->slug; | |
| 178 | 172 | } |
| 179 | 173 | } |
| 180 | 174 | |
| 181 | 175 | $third_party_cookie_check_iframe = '<span style="display:none;"><iframe class="jetpack-notes-cookie-check" src="https://widgets.wp.com/3rd-party-cookie-check/index.html"></iframe></span>'; |
| 182 | 176 | |
| 183 | - $classes = 'wpnt-loading wpn-read'; | |
| 177 | + // Opt into the v3 notifications panel/iframe via the `notifications=v3` query parameter. | |
| 178 | + $notifications_version = sanitize_key( wp_unslash( $_GET['notifications'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 179 | + $panel_id = 'v3' === $notifications_version ? 'wpnt-notes-panel3' : 'wpnt-notes-panel2'; | |
| 180 | + | |
| 181 | + $title = self::get_notes_markup(); | |
| 182 | + | |
| 183 | + // The default fallback is `en_US`. Remove underscore if present, noting that lang codes can be more than three chars. | |
| 184 | + $user_locale = strtolower( explode( '_', $user_locale, 2 )[0] ); | |
| 185 | + | |
| 184 | 186 | $wp_admin_bar->add_menu( |
| 185 | 187 | array( |
| 186 | 188 | 'id' => 'notes', |
| 187 | - 'title' => '<span id="wpnt-notes-unread-count" class="' . esc_attr( $classes ) . '"> | |
| 188 | - <span class="noticon noticon-notification"></span> | |
| 189 | - </span>', | |
| 189 | + 'title' => $title, | |
| 190 | 190 | 'meta' => array( |
| 191 | - '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>' . $third_party_cookie_check_iframe, | |
| 191 | + 'html' => '<div id="' . esc_attr( $panel_id ) . '" class="intrinsic-ignore" style="display:none" lang="' . esc_attr( $user_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>' . $third_party_cookie_check_iframe, | |
| 192 | 192 | 'class' => 'menupop', |
| 193 | 193 | ), |
| 194 | 194 | 'parent' => 'top-secondary', |
| 195 | + 'href' => 'https://wordpress.com/reader/notifications', | |
| 195 | 196 | ) |
| 196 | 197 | ); |
| 197 | 198 | } |
| 198 | 199 | |
| 199 | 200 | /** |
| 201 | + * Returns the HTML markup for used by notification in top bar | |
| 202 | + * | |
| 203 | + * @return string | |
| 204 | + */ | |
| 205 | + private static function get_notes_markup() { | |
| 206 | + return '<span id="wpnt-notes-unread-count" class="wpnt-loading wpn-read"></span> | |
| 207 | +<span class="noticon noticon-bell ab-icon"></span> | |
| 208 | +<span class="screen-reader-text">' . esc_html__( 'Notifications', 'jetpack' ) . '</span>'; | |
| 209 | + } | |
| 210 | + | |
| 211 | + /** | |
| 200 | 212 | * Echos the Notes JS. |
| 201 | 213 | * |
| 202 | 214 | * @return void |
| 203 | 215 | */ |
| @@ -202,35 +214,34 @@ | ||
| 202 | 214 | * @return void |
| 203 | 215 | */ |
| 204 | 216 | public function print_js() { |
| 205 | 217 | $link_accounts_url = is_user_logged_in() && ! ( new Connection_Manager( 'jetpack' ) )->is_user_connected() ? Jetpack::admin_url() : false; |
| 206 | - ?> | |
| 207 | -<script data-ampdevmode type="text/javascript"> | |
| 208 | -/* <![CDATA[ */ | |
| 209 | - var wpNotesIsJetpackClient = true; | |
| 210 | - var wpNotesIsJetpackClientV2 = true; | |
| 211 | - <?php if ( $link_accounts_url ) : ?> | |
| 212 | - var wpNotesLinkAccountsURL = '<?php echo esc_url( $link_accounts_url ); ?>'; | |
| 213 | -<?php endif; ?> | |
| 214 | -/* ]]> */ | |
| 215 | - window.addEventListener('message', function ( event ) { | |
| 216 | - // Confirm that the message is from the right origin. | |
| 217 | - if ('https://widgets.wp.com' !== event.origin) { | |
| 218 | - return; | |
| 218 | + $script_contents = <<<'JS' | |
| 219 | +var wpNotesIsJetpackClient = true; | |
| 220 | +var wpNotesIsJetpackClientV2 = true; | |
| 221 | +JS; | |
| 222 | + if ( $link_accounts_url ) { | |
| 223 | + $script_contents .= "\nvar wpNotesLinkAccountsURL = " . wp_json_encode( $link_accounts_url, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; | |
| 219 | 224 | } |
| 220 | - // Check whether 3rd Party Cookies are blocked | |
| 221 | - var has3PCBlocked = 'WPCOM:3PC:blocked' === event.data; | |
| 225 | + wp_print_inline_script_tag( | |
| 226 | + $script_contents, | |
| 227 | + array( | |
| 228 | + 'data-ampdevmode' => true, | |
| 229 | + ) | |
| 230 | + ); | |
| 231 | + } | |
| 222 | 232 | |
| 223 | - var tagerElement = document.getElementById('wp-admin-bar-notes'); | |
| 224 | - | |
| 225 | - if ( has3PCBlocked && tagerElement ) { | |
| 226 | - // Hide the notification button/icon | |
| 227 | - tagerElement.style.display = 'none'; | |
| 233 | + /** | |
| 234 | + * Checks to see if we're in the block editor. | |
| 235 | + */ | |
| 236 | + public static function is_block_editor() { | |
| 237 | + if ( function_exists( 'get_current_screen' ) ) { | |
| 238 | + $current_screen = get_current_screen(); | |
| 239 | + if ( ! empty( $current_screen ) && $current_screen->is_block_editor() ) { | |
| 240 | + return true; | |
| 241 | + } | |
| 228 | 242 | } |
| 229 | - }, false ); | |
| 230 | -</script> | |
| 231 | - <?php | |
| 243 | + return false; | |
| 232 | 244 | } |
| 233 | - | |
| 234 | 245 | } |
| 235 | 246 | |
| 236 | 247 | Jetpack_Notifications::init(); |