From 0f37e185c3718b5e540e4cce6c9b9bc2624bcf1c Mon Sep 17 00:00:00 2001 From: Derek Smart Date: Fri, 7 Dec 2018 12:15:14 -0500 Subject: [PATCH 1/6] JITM: Refetch based on hash change --- _inc/jetpack-jitm.js | 65 ++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/_inc/jetpack-jitm.js b/_inc/jetpack-jitm.js index 5f06d527b8b5e..30d51fc5645be 100644 --- a/_inc/jetpack-jitm.js +++ b/_inc/jetpack-jitm.js @@ -1,4 +1,41 @@ jQuery( document ).ready( function( $ ) { + var reFetch = function() { + $( '.jetpack-jitm-message' ).each( function() { + var $el = $( this ); + + var message_path = $el.data( 'message-path' ); + var query = $el.data( 'query' ); + var redirect = $el.data( 'redirect' ); + var hash = location.hash; + + hash = hash.replace( /#\//, '_' ); + message_path = message_path.replace( 'toplevel_page_jetpack', 'toplevel_page_jetpack' + hash ); + + $.get( window.jitm_config.api_root + 'jetpack/v4/jitm', { + message_path: message_path, + query: query, + _wpnonce: $el.data( 'nonce' ) + } ).then( function( response ) { + if ( 'object' === typeof response && response['1'] ) { + response = [ response['1'] ]; + } + + // properly handle the case of an empty array or no content set + if ( 0 === response.length || ! response[ 0 ].content ) { + return; + } + + // for now, always take the first response + setJITMContent( $el, response[ 0 ], redirect ); + } ); + } ); + }; + + $( window ).bind( 'hashchange', function() { + document.querySelector( '.jitm-card' ).remove(); + reFetch(); + } ); + var templates = { 'default': function( envelope ) { var html = '
Date: Thu, 20 Dec 2018 15:12:58 +0100 Subject: [PATCH 3/6] Add check for the content of the target DOM element --- _inc/jetpack-jitm.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/_inc/jetpack-jitm.js b/_inc/jetpack-jitm.js index 2d111367a75ab..3d22f2fec131f 100644 --- a/_inc/jetpack-jitm.js +++ b/_inc/jetpack-jitm.js @@ -161,7 +161,10 @@ jQuery( document ).ready( function( $ ) { reFetch(); $( window ).bind( 'hashchange', function() { - document.querySelector( '.jitm-card' ).remove(); + var jitm_card = document.querySelector( '.jitm-card' ); + if ( jitm_card ) { + jitm_card.remove(); + } reFetch(); } ); } ); From c0335a260e9b853a0f0c4564566d062dd280753a Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Fri, 21 Dec 2018 11:16:48 +0100 Subject: [PATCH 4/6] Fallback to the original message path on the main dashboard page --- _inc/jetpack-jitm.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_inc/jetpack-jitm.js b/_inc/jetpack-jitm.js index 3d22f2fec131f..2fbd57b2ea5d7 100644 --- a/_inc/jetpack-jitm.js +++ b/_inc/jetpack-jitm.js @@ -136,7 +136,9 @@ jQuery( document ).ready( function( $ ) { var hash = location.hash; hash = hash.replace( /#\//, '_' ); - message_path = message_path.replace( 'toplevel_page_jetpack', 'toplevel_page_jetpack' + hash ); + if ( '_dashboard' !== hash ) { + message_path = message_path.replace( 'toplevel_page_jetpack', 'toplevel_page_jetpack' + hash ); + } $.get( window.jitm_config.api_root + 'jetpack/v4/jitm', { message_path: message_path, From 3fdcfc0f25c1dd499bf06ba8ea9e9b0141ceeccb Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Fri, 21 Dec 2018 11:52:48 +0100 Subject: [PATCH 5/6] Make sure JITMs are only queried once when switching dash tabs. See https://github.com/Automattic/jetpack/pull/10889#issuecomment-449068399 --- _inc/jetpack-jitm.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/_inc/jetpack-jitm.js b/_inc/jetpack-jitm.js index 2fbd57b2ea5d7..f4c6c75f8eb06 100644 --- a/_inc/jetpack-jitm.js +++ b/_inc/jetpack-jitm.js @@ -162,11 +162,15 @@ jQuery( document ).ready( function( $ ) { reFetch(); - $( window ).bind( 'hashchange', function() { - var jitm_card = document.querySelector( '.jitm-card' ); - if ( jitm_card ) { - jitm_card.remove(); - } - reFetch(); + $( window ).bind( 'hashchange', function( e ) { + var newURL = e.originalEvent.newURL; + + if ( newURL.indexOf( 'jetpack#/' ) >= 0 ) { + var jitm_card = document.querySelector( '.jitm-card' ); + if ( jitm_card ) { + jitm_card.remove(); + } + reFetch(); + } } ); } ); From 80b457ebb9c9ea8fb7e4194092afcd2220786867 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Fri, 21 Dec 2018 12:29:19 +0100 Subject: [PATCH 6/6] Make sure the JITMs can be displayed on any page, not just JPDash --- _inc/jetpack-jitm.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/_inc/jetpack-jitm.js b/_inc/jetpack-jitm.js index f4c6c75f8eb06..f425a2485c426 100644 --- a/_inc/jetpack-jitm.js +++ b/_inc/jetpack-jitm.js @@ -89,10 +89,14 @@ jQuery( document ).ready( function( $ ) { var $template = templates[ template ]( response ); $template.find( '.jitm-banner__dismiss' ).click( render( $template ) ); - $el.innerHTML = $template; - - // Add to Jetpack notices within the Jetpack settings app. - $template.prependTo( $( '#jp-admin-notices' ) ); + if ( $( '#jp-admin-notices' ).length > 0 ) { + // Add to Jetpack notices within the Jetpack settings app. + $el.innerHTML = $template; + $template.prependTo( $( '#jp-admin-notices' ) ); + } else { + // Replace placeholder div on other pages. + $el.replaceWith($template); + } // Handle Module activation button if it exists. $template.find( '#jitm-banner__activate a' ).click( function() {