-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wp-solutions-PS0-2289 - Checking plugin installation and active state #37
Changes from all commits
ef6d16d
7dcc71c
1c5e686
269c140
67573ed
eaefb22
3118738
1dd0df2
c2fa31e
f28b124
c50c1b9
b2c19c6
77cc0ec
5646ec6
a841045
d4127a4
be0dd32
afd5648
c6f1135
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,151 +1,177 @@ | ||
class MyPluginTools { | ||
constructor() { | ||
window.addEventListener( 'DOMContentLoaded', () => { | ||
fetch( | ||
nfdplugin.restApiUrl + '/newfold-solutions/v1/entitlements', | ||
{ | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-WP-Nonce': nfdplugin.restApiNonce, | ||
}, | ||
} | ||
) | ||
.then( ( response ) => response.json() ) | ||
.then( ( response ) => { | ||
const pluginsData = response?.entitlements?.filter( | ||
( data ) => data?.type === 'plugin' | ||
); | ||
const installedPlugins = Object.keys( | ||
nfdPluginDetails?.installed | ||
); | ||
const pluginWithStatus = pluginsData.map( ( val ) => ( { | ||
...val, | ||
isInstalled: installedPlugins?.includes( val.basename ), | ||
isActive: Object.values( | ||
nfdPluginDetails?.active | ||
).find( ( plugin ) => plugin === val.basename ), | ||
} ) ); | ||
const sortedPluginNames = pluginWithStatus.sort( ( a, b ) => | ||
a.name.localeCompare( b.name ) | ||
); | ||
this.setUpContainer( sortedPluginNames ); | ||
} ); | ||
} ); | ||
} | ||
|
||
constructor() { | ||
window.addEventListener("DOMContentLoaded", () => { | ||
fetch( | ||
nfdplugin.restApiUrl + | ||
'/newfold-solutions/v1/entitlements', | ||
{ | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-WP-Nonce': nfdplugin.restApiNonce, | ||
}, | ||
} | ||
) | ||
.then((response) => response.json()).then((response) => { | ||
|
||
const pluginsData = response?.entitlements?.filter(data => data?.type === 'plugin' ); | ||
const installedPlugins = Object.keys( nfdPluginDetails?.installed ) | ||
const pluginWithStatus = pluginsData.map(val => ({ | ||
...val, | ||
isInstalled: installedPlugins?.includes( val.basename ), | ||
isActive: Object.values( nfdPluginDetails?.active ).find( ( plugin ) => plugin === val.basename ) | ||
})) | ||
const sortedPluginNames = pluginWithStatus.sort( ( a, b ) => a.name.localeCompare( b.name ) ) | ||
this.setUpContainer( sortedPluginNames ); | ||
}) | ||
|
||
}) | ||
|
||
} | ||
activate_plugin( plugin_path, href ) { | ||
fetch( nfdplugin.restApiUrl + '/newfold-solutions/v1/activate_plugin', { | ||
credentials: 'same-origin', | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-WP-Nonce': nfdplugin.restApiNonce, | ||
}, | ||
body: JSON.stringify( { | ||
plugin: plugin_path, | ||
} ), | ||
} ) | ||
.then( ( response ) => response.json() ) | ||
.then( ( response ) => { | ||
if ( response?.message ) { | ||
window.location.href = href; | ||
} | ||
} ); | ||
} | ||
|
||
activate_plugin ( plugin_path, href ){ | ||
fetch( | ||
nfdplugin.restApiUrl + | ||
'/newfold-solutions/v1/activate_plugin', | ||
{ | ||
credentials: 'same-origin', | ||
method: "POST", | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-WP-Nonce': nfdplugin.restApiNonce, | ||
}, | ||
body: JSON.stringify({ | ||
plugin: plugin_path | ||
}) | ||
} | ||
).then((response) => response.json() ).then((response) => { | ||
if( response?.message ){ | ||
window.location.href = href; | ||
} | ||
}) | ||
|
||
} | ||
renderNameAsClass( name ) { | ||
return name | ||
.replaceAll( ' & ', '-' ) | ||
.replaceAll( ' ', '-' ) | ||
.replaceAll( '&', '-' ) | ||
.toLowerCase(); | ||
} | ||
|
||
renderCTAUrl( url ) { | ||
if ( ! window.NewfoldRuntime || ! window.NewfoldRuntime.siteUrl ) { | ||
// fallback to site relative url if no base_url is found | ||
return url.replace( '{siteUrl}', '' ); | ||
} | ||
return url.replace( '{siteUrl}', window.NewfoldRuntime.siteUrl ); | ||
} | ||
renderCTAUrl( url ) { | ||
if ( ! window.NewfoldRuntime || ! window.NewfoldRuntime.siteUrl ) { | ||
// fallback to site relative url if no base_url is found | ||
return url.replace( '{siteUrl}', '' ); | ||
} | ||
return url.replace( '{siteUrl}', window.NewfoldRuntime.siteUrl ); | ||
} | ||
|
||
getElementByStatus( isActive, isInstalled, pluginData ) { | ||
if( isActive && isInstalled ){ // active and installed | ||
return `<a | ||
title="Manage" | ||
href="${this.renderCTAUrl(pluginData?.cta?.url)}" | ||
class="nfd-solutions-availble-list-item-button" | ||
>${pluginData?.cta?.text}</a>`; | ||
} else if ( isInstalled ){ // already installed | ||
return `<button | ||
title="Activate Plugin" | ||
data-nfd-installer-plugin-activate="${true}" | ||
data-nfd-installer-pls-slug="${pluginData?.plsSlug}" | ||
data-nfd-installer-pls-provider="${pluginData?.plsProviderName}" | ||
data-nfd-installer-plugin-name="${pluginData?.name}" | ||
data-nfd-installer-plugin-url="${this.renderCTAUrl(pluginData?.cta?.url)}" | ||
href="${this.renderCTAUrl(pluginData?.cta?.url)}" | ||
class="nfd-solutions-availble-list-item-button nfd-activate-btn" | ||
>${pluginData?.cta?.text}</button>`; | ||
} | ||
getElementByStatus( isActive, isInstalled, pluginData ) { | ||
// active and installed - no installer attributes needed | ||
if ( isActive && isInstalled ) { | ||
return `<a | ||
title="Manage Plugin" | ||
class="nfd-solutions-availble-list-item-button nfd-solutions-${ this.renderNameAsClass( | ||
pluginData?.name | ||
) }-button" | ||
href="${ this.renderCTAUrl( pluginData?.cta?.url ) }" | ||
>${ pluginData?.cta?.text }</a>`; | ||
} | ||
|
||
if ( pluginData?.plsProviderName && pluginData?.plsSlug ){ // premium plugin with pls | ||
return `<button | ||
title="Install Premium Plugin" | ||
class="nfd-solutions-availble-list-item-button" | ||
data-nfd-installer-plugin-activate="${true}" | ||
data-nfd-installer-pls-slug="${pluginData?.plsSlug}" | ||
data-nfd-installer-pls-provider="${pluginData?.plsProviderName}" | ||
data-nfd-installer-plugin-name="${pluginData?.name}" | ||
data-nfd-installer-plugin-url="${this.renderCTAUrl(pluginData?.cta?.url)}" | ||
>${pluginData?.cta?.text}</button>`; | ||
} else if ( pluginData?.download ) { // free plugin | ||
return `<button | ||
title="Install Plugin" | ||
class="nfd-solutions-availble-list-item-button" | ||
data-nfd-installer-pls-provider="${pluginData?.plsProviderName}" | ||
data-nfd-installer-plugin-activate="${true}" | ||
data-nfd-installer-plugin-name="${pluginData?.name}" | ||
data-nfd-installer-download-url="${pluginData?.download}" | ||
data-nfd-installer-plugin-url="${this.renderCTAUrl(pluginData?.cta?.url)}" | ||
>${pluginData?.cta?.text}</button>`; | ||
} else { // fallback | ||
return `<a | ||
// premium plugin - has pls values | ||
if ( pluginData?.plsProviderName && pluginData?.plsSlug ) { | ||
return `<button | ||
title="${ isInstalled ? 'Activate' : 'Install' } Premium Plugin" | ||
class="nfd-solutions-availble-list-item-button nfd-solutions-${ this.renderNameAsClass( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this little method |
||
pluginData?.name | ||
) }-button" | ||
href="${ this.renderCTAUrl( pluginData?.cta?.url ) }" | ||
data-nfd-installer-plugin-activate="${ true }" | ||
data-nfd-installer-plugin-name="${ pluginData?.name }" | ||
data-nfd-installer-plugin-url="${ this.renderCTAUrl( | ||
pluginData?.cta?.url | ||
) }" | ||
data-nfd-installer-pls-provider="${ pluginData?.plsProviderName }" | ||
data-nfd-installer-pls-slug="${ pluginData?.plsSlug }" | ||
>${ pluginData?.cta?.text }</button>`; | ||
} | ||
// free plugin - has download url value | ||
if ( pluginData?.download ) { | ||
return `<button | ||
title="${ isInstalled ? 'Activate' : 'Install' } Plugin" | ||
class="nfd-solutions-availble-list-item-button nfd-solutions-${ this.renderNameAsClass( | ||
pluginData?.name | ||
) }-button" | ||
href="${ this.renderCTAUrl( pluginData?.cta?.url ) }" | ||
data-nfd-installer-download-url="${ pluginData?.download }" | ||
data-nfd-installer-plugin-activate="${ true }" | ||
data-nfd-installer-plugin-name="${ pluginData?.name }" | ||
data-nfd-installer-plugin-url="${ this.renderCTAUrl( | ||
pluginData?.cta?.url | ||
) }" | ||
>${ pluginData?.cta?.text }</button>`; | ||
} | ||
// fallback | ||
return `<a | ||
title="Learn More" | ||
href="${pluginData?.url}" | ||
class="nfd-solutions-availble-list-item-button" | ||
>${pluginData?.cta?.text}</a>`; | ||
} | ||
} | ||
class="nfd-solutions-availble-list-item-button nfd-solutions-${ this.renderNameAsClass( | ||
pluginData?.name | ||
) }-button" | ||
href="${ pluginData?.url }" | ||
>${ pluginData?.cta?.text }</a>`; | ||
} | ||
|
||
buildPluginsBlock( pluginData ) { | ||
return `<div class="nfd-solutions-availble-list-item"> | ||
<div><img src=${pluginData?.image?.primaryImage} width="128px" height="128px" /></div> | ||
buildPluginsBlock( pluginData ) { | ||
return `<div class="nfd-solutions-availble-list-item nfd-solutions-plugin-${ this.renderNameAsClass( | ||
pluginData?.name | ||
) }"> | ||
<div><img src=${ | ||
pluginData?.image?.primaryImage | ||
} width="128px" height="128px" /></div> | ||
<div class="details"> | ||
<h3 class="nfd-solutions-availble-list-item-title">${pluginData?.name}</h3> | ||
<h3 class="nfd-solutions-availble-list-item-title">${ | ||
pluginData?.name | ||
}</h3> | ||
<div> | ||
${this.getElementByStatus( pluginData?.isActive, pluginData?.isInstalled, pluginData )} | ||
${ this.getElementByStatus( | ||
pluginData?.isActive, | ||
pluginData?.isInstalled, | ||
pluginData | ||
) } | ||
</div> | ||
<p>${pluginData?.description}</p> | ||
<p>${ pluginData?.description }</p> | ||
</div> | ||
</div>`; | ||
} | ||
|
||
setUpContainer( entitlements ) { | ||
const wpBody = document.getElementById("wpbody-content"); | ||
} | ||
|
||
let myPlugins = document.createElement("div"); | ||
myPlugins.classList.add("nfd-solutions-availble-list") | ||
entitlements?.forEach( ( data ) => ( myPlugins.innerHTML += this.buildPluginsBlock(data) ) ); | ||
|
||
wpBody.appendChild(myPlugins); | ||
|
||
} | ||
setUpContainer( entitlements ) { | ||
const wpBody = document.getElementById( 'wpbody-content' ); | ||
|
||
bindActivateButtons() { | ||
|
||
const activateButtons = document.querySelectorAll(".nfd-activate-btn"); | ||
activateButtons.forEach((button) => { | ||
button.addEventListener("click", (event) => { | ||
const pluginPath = event.target.getAttribute("data-plugin"); | ||
const href = event.target.getAttribute("href"); | ||
this.activate_plugin(pluginPath, href); | ||
}); | ||
}); | ||
} | ||
} | ||
const myPlugins = document.createElement( 'div' ); | ||
myPlugins.classList.add( 'nfd-solutions-availble-list' ); | ||
entitlements?.forEach( | ||
( data ) => | ||
( myPlugins.innerHTML += this.buildPluginsBlock( data ) ) | ||
); | ||
|
||
wpBody.appendChild( myPlugins ); | ||
} | ||
|
||
bindActivateButtons() { | ||
const activateButtons = | ||
document.querySelectorAll( '.nfd-activate-btn' ); | ||
activateButtons.forEach( ( button ) => { | ||
button.addEventListener( 'click', ( event ) => { | ||
const pluginPath = event.target.getAttribute( 'data-plugin' ); | ||
const href = event.target.getAttribute( 'href' ); | ||
this.activate_plugin( pluginPath, href ); | ||
} ); | ||
} ); | ||
} | ||
} | ||
|
||
const pluginList = new MyPluginTools(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this isActive but not isInstalled section, and instead use the later conditionals to just check if it is a premium/pls or free plugin. Since those are the more pertinent deciders of what the markup should be. I did file a ticket with the installer module though, since if a plugin is installed and not active, the installer opens up and fails to install since the directory already exists. This change doesn't change the issue, but simplifies the logic in rendering these buttons.