Skip to content

Commit 6dfc387

Browse files
westonruterkraftbj
authored andcommitted
Add support for AMP Dev Mode in Notes module (#13450)
* Add AMP dev mode support for stats in admin bar * Use regular img for admin-bar stats on AMP pages thanks to dev mode * Add data-ampdevmode attribute to inline style * Add AMP dev mode support for Notes modules * Use array() syntax instead of [] * Simplify marking styles for AMP Dev Mode
1 parent 428f23f commit 6dfc387

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

modules/notes.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,27 +119,51 @@ function styles_and_scripts() {
119119
}
120120

121121
if ( ! $is_rtl ) {
122-
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 );
122+
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 );
123123
} else {
124-
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 );
124+
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 );
125125
}
126-
wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array(), JETPACK_NOTES__CACHE_BUSTER );
126+
127+
wp_enqueue_style( 'noticons', $this->wpcom_static_url( '/i/noticons/noticons.css' ), array( 'wpcom-notes-admin-bar' ), JETPACK_NOTES__CACHE_BUSTER );
127128

128129
$this->print_js();
129130

130131
// attempt to use core or plugin libraries if registered
132+
$script_handles = array();
131133
if ( !wp_script_is( 'mustache', 'registered' ) ) {
132134
wp_register_script( 'mustache', $this->wpcom_static_url( '/wp-content/js/mustache.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
133135
}
136+
$script_handles[] = 'mustache';
134137
if ( !wp_script_is( 'underscore', 'registered' ) ) {
135138
wp_register_script( 'underscore', $this->wpcom_static_url( '/wp-includes/js/underscore.min.js' ), null, JETPACK_NOTES__CACHE_BUSTER );
136139
}
140+
$script_handles[] = 'underscore';
137141
if ( !wp_script_is( 'backbone', 'registered' ) ) {
138142
wp_register_script( 'backbone', $this->wpcom_static_url( '/wp-includes/js/backbone.min.js' ), array( 'underscore' ), JETPACK_NOTES__CACHE_BUSTER );
139143
}
144+
$script_handles[] = 'backbone';
140145

141146
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 );
147+
$script_handles[] = 'wpcom-notes-common';
148+
$script_handles[] = 'jquery';
149+
$script_handles[] = 'jquery-migrate';
150+
$script_handles[] = 'jquery-core';
142151
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 );
152+
$script_handles[] = 'wpcom-notes-admin-bar';
153+
154+
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
155+
add_filter(
156+
'script_loader_tag',
157+
function ( $tag, $handle ) use ( $script_handles ) {
158+
if ( in_array( $handle, $script_handles, true ) ) {
159+
$tag = preg_replace( '/(?<=<script)(?=\s|>)/i', ' data-ampdevmode', $tag );
160+
}
161+
return $tag;
162+
},
163+
10,
164+
2
165+
);
166+
}
143167
}
144168

145169
function admin_bar_menu() {
@@ -180,7 +204,7 @@ function admin_bar_menu() {
180204
function print_js() {
181205
$link_accounts_url = is_user_logged_in() && !Jetpack::is_user_connected() ? Jetpack::admin_url() : false;
182206
?>
183-
<script type="text/javascript">
207+
<script data-ampdevmode type="text/javascript">
184208
/* <![CDATA[ */
185209
var wpNotesIsJetpackClient = true;
186210
var wpNotesIsJetpackClientV2 = true;

modules/stats.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ function stats_admin_bar_head() {
832832
add_action( 'admin_bar_menu', 'stats_admin_bar_menu', 100 );
833833
?>
834834

835-
<style type='text/css'>
835+
<style data-ampdevmode type='text/css'>
836836
#wpadminbar .quicklinks li#wp-admin-bar-stats {
837837
height: 32px;
838838
}
@@ -878,14 +878,10 @@ function stats_admin_bar_menu( &$wp_admin_bar ) {
878878
$title = esc_attr( __( 'Views over 48 hours. Click for more Site Stats.', 'jetpack' ) );
879879

880880
$menu = array(
881-
'id' => 'stats',
882-
'href' => $url,
881+
'id' => 'stats',
882+
'href' => $url,
883+
'title' => "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>",
883884
);
884-
if ( Jetpack_AMP_Support::is_amp_request() ) {
885-
$menu['title'] = "<amp-img src='$img_src_2x' width=112 height=24 layout=fixed alt='$alt' title='$title'></amp-img>";
886-
} else {
887-
$menu['title'] = "<div><img src='$img_src' srcset='$img_src 1x, $img_src_2x 2x' width='112' height='24' alt='$alt' title='$title'></div>";
888-
}
889885

890886
$wp_admin_bar->add_menu( $menu );
891887
}

0 commit comments

Comments
 (0)