Skip to content

Commit b4f1f8f

Browse files
authored
Open support links on the upsell wrapper inside the Help Center (#44199)
1 parent 37ffa68 commit b4f1f8f

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: changed
3+
4+
Open support links on the upsell wrapper inside the Help Center

projects/packages/jetpack-mu-wpcom/src/features/help-center/class-help-center.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace A8C\FSE;
99

1010
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
11+
use Automattic\Jetpack\Jetpack_Mu_Wpcom;
1112
use Automattic\Jetpack\Jetpack_Mu_Wpcom\Common;
1213

1314
/**
@@ -212,6 +213,14 @@ function ( $wp_admin_bar ) {
212213
$version
213214
);
214215
}
216+
217+
wp_enqueue_script(
218+
'wpcom-upsell-support-link-handler',
219+
plugins_url( 'build/wpcom-upsell-support-link-handler/wpcom-upsell-support-link-handler.js', Jetpack_Mu_Wpcom::BASE_FILE ),
220+
array(),
221+
filemtime( Jetpack_Mu_Wpcom::BASE_DIR . 'build/wpcom-upsell-support-link-handler/wpcom-upsell-support-link-handler.js' ),
222+
true
223+
);
215224
}
216225

217226
/**
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useDispatch } from '@wordpress/data';
2+
import domReady from '@wordpress/dom-ready';
3+
import { useEffect } from 'react';
4+
import { createRoot } from 'react-dom/client';
5+
6+
const UPSELL_WRAPPER_SELECTOR = '.wpcom_upsell_page_wrapper';
7+
8+
const UpsellSupportLinkHandler = () => {
9+
const helpCenterDispatch = useDispatch( 'automattic/help-center' );
10+
11+
const setShowSupportDoc = helpCenterDispatch?.setShowSupportDoc;
12+
13+
useEffect( () => {
14+
if ( ! setShowSupportDoc ) {
15+
return;
16+
}
17+
18+
const links = Array.from(
19+
document.querySelectorAll( `${ UPSELL_WRAPPER_SELECTOR } a` )
20+
).filter( link => {
21+
try {
22+
const url = new URL( link.attributes?.href?.value );
23+
const allowedHosts = [ 'wordpress.com' ];
24+
return allowedHosts.includes( url.host ) && url.pathname.startsWith( '/support/' );
25+
// eslint-disable-next-line no-unused-vars
26+
} catch ( e ) {
27+
return false;
28+
}
29+
} );
30+
31+
const handleClick = event => {
32+
event.preventDefault();
33+
setShowSupportDoc( event.currentTarget.href );
34+
};
35+
36+
links.forEach( link => {
37+
link.addEventListener( 'click', handleClick );
38+
} );
39+
40+
return () => {
41+
links.forEach( link => {
42+
link.removeEventListener( 'click', handleClick );
43+
} );
44+
};
45+
}, [ setShowSupportDoc ] );
46+
};
47+
48+
domReady( () => {
49+
const upsellWrapper = document.querySelector( UPSELL_WRAPPER_SELECTOR );
50+
51+
if ( ! upsellWrapper ) {
52+
return;
53+
}
54+
55+
const rootElement = document.createElement( 'div' );
56+
document.body.appendChild( rootElement );
57+
const root = createRoot( rootElement );
58+
59+
root.render( <UpsellSupportLinkHandler /> );
60+
} );

projects/packages/jetpack-mu-wpcom/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ module.exports = async () => {
6262
'wpcom-replace-site-visibility':
6363
'./src/features/replace-site-visibility/replace-site-visibility.tsx',
6464
'wpcom-sidebar-notice': './src/features/wpcom-sidebar-notice/wpcom-sidebar-notice.js',
65+
'wpcom-upsell-support-link-handler':
66+
'./src/features/help-center/js/upsell-support-link-handler.js',
6567
'adminbar-launch-button': './src/features/launch-button/index.js',
6668
},
6769
mode: jetpackWebpackConfig.mode,

0 commit comments

Comments
 (0)