Skip to content
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

Merged
merged 19 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ef6d16d
Wp-solutions-PS0-2289 - Checking plugin installtion and active state
krsomayagi Nov 6, 2024
7dcc71c
Merge branch 'main' into wp-soln-Press02289
circlecube Nov 6, 2024
1c5e686
Line no 33 code change as nfd-solutions-availble-list is not found
krsomayagi Nov 7, 2024
269c140
Merge branch 'wp-soln-Press02289' of https://github.com/newfold-labs/…
krsomayagi Nov 7, 2024
67573ed
code changes
krsomayagi Nov 7, 2024
eaefb22
Issue fix with code changes
krsomayagi Nov 7, 2024
3118738
Code changes
krsomayagi Nov 7, 2024
1dd0df2
Merge branch 'main' into wp-soln-Press02289
circlecube Nov 7, 2024
c2fa31e
checking with code changes
krsomayagi Nov 11, 2024
f28b124
Merge branch 'wp-soln-Press02289' of https://github.com/newfold-labs/…
krsomayagi Nov 11, 2024
c50c1b9
Press0-2289 code changes for jetpack issue
krsomayagi Nov 12, 2024
b2c19c6
code change for the failures
krsomayagi Nov 14, 2024
77cc0ec
more timeout is added
krsomayagi Nov 15, 2024
5646ec6
Press0-2289:Yoast SEO plugin installation code
krsomayagi Nov 18, 2024
a841045
Merge branch 'main' into wp-soln-Press02289
circlecube Nov 18, 2024
d4127a4
simplify button logic and add plugin specific class to entitlement ca…
circlecube Nov 18, 2024
be0dd32
update wpCli command to include flag for failOnNonZeroExit so the uni…
circlecube Nov 18, 2024
afd5648
add tests for all scenarios that check installer attributes
circlecube Nov 18, 2024
c6f1135
Merge branch 'main' into wp-soln-Press02289
circlecube Nov 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
292 changes: 159 additions & 133 deletions includes/js/myPluginsTools.js
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
Copy link
Member

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.

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this little method renderNameAsClass to give us unique class names for each plugin (which helps select them in tests). I also update the order of these installer attributes so they are consistent in each block. The rest of the file changes are due to lint whitespace edits.

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();
Loading
Loading