Skip to content

Commit

Permalink
Merge pull request #187 from Codeinwp/fix/install-plugins-buttons
Browse files Browse the repository at this point in the history
Disable install buttons if users can not install them [#4071]
  • Loading branch information
cristian-ungureanu authored Aug 22, 2023
2 parents d225b15 + a9c18de commit a7ab9d8
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 83 deletions.
2 changes: 1 addition & 1 deletion assets/js/build/about/about.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-components', 'wp-element'), 'version' => 'b9d5790c82f421f0773c');
<?php return array('dependencies' => array('wp-components', 'wp-element'), 'version' => 'abd2fd2f3b61120a53af');
1 change: 1 addition & 0 deletions assets/js/build/about/about.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 44 additions & 13 deletions assets/js/build/about/about.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

144 changes: 88 additions & 56 deletions assets/js/src/about/components/ProductCard.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,99 @@
import {useState} from '@wordpress/element';
import {Button} from '@wordpress/components';
import {Button, Tooltip} from '@wordpress/components';

import {activatePlugin, installPluginOrTheme} from "../../common/utils";
import {installPluginOrTheme} from "../../common/utils";

export default function ProductCard({product, slug}) {
const {icon, name, description, status, premiumUrl, activationLink} = product;
const {strings} = window.tiSDKAboutData;
const {
installNow,
installed,
notInstalled,
active,
activate,
learnMore
} = strings;
const {icon, name, description, status, premiumUrl, activationLink} = product;
const {strings, canInstallPlugins} = window.tiSDKAboutData;
const {
installNow,
installed,
notInstalled,
active,
activate,
learnMore
} = strings;

const isPremium = !!premiumUrl;
const [productStatus, setProductStatus] = useState(status);
const [loading, setLoading] = useState(false);
const isPremium = !!premiumUrl;
const [productStatus, setProductStatus] = useState(status);
const [loading, setLoading] = useState(false);

const runInstall = async () => {
setLoading(true);
await installPluginOrTheme(slug, slug === 'neve').then((res) => {
if (res.success) {
setProductStatus('installed');
}
});
setLoading(false);
}
const runInstall = async () => {
if ( ! canInstallPlugins ) {
return;
}
setLoading(true);
await installPluginOrTheme(slug, slug === 'neve').then((res) => {
if (res.success) {
setProductStatus('installed');
}
});
setLoading(false);
}

const runActivate = async () => {
setLoading(true);
window.location.href = activationLink;
}
const runActivate = async () => {
if ( ! canInstallPlugins ) {
return;
}
setLoading(true);
window.location.href = activationLink;
}

return (<div className="product-card">
<div className="header">
{icon && <img src={icon} alt={name}/>}
<h2>{name}</h2>
</div>
<div className="body">
<p dangerouslySetInnerHTML={{__html: description}}/>
</div>
<div className="footer">
<p>Status:
{" "}
<span className={productStatus}>
const buttonContent = ( () => {
if ( productStatus === 'not-installed' && isPremium ) {
return (
<Button isLink icon={'external'} href={premiumUrl} target="_blank">
{learnMore}
</Button>
);
}

if ( productStatus === 'not-installed' && !isPremium ) {
return (
<Button isPrimary onClick={runInstall} disabled={loading || ! canInstallPlugins}>
{installNow}
</Button>
);
}

if ( productStatus === 'installed' ) {
return (
<Button isSecondary onClick={runActivate} disabled={loading || ! canInstallPlugins}>
{activate}
</Button>
)
}

return null;
});

const wrappedButtonContent = ! canInstallPlugins ? (
<Tooltip text={`Ask your admin to enable ${name} on your site`} position="top center">{buttonContent()}</Tooltip>
) : (
buttonContent()
);

return (<div className="product-card">
<div className="header">
{icon && <img src={icon} alt={name}/>}
<h2>{name}</h2>
</div>
<div className="body">
<p dangerouslySetInnerHTML={{__html: description}}/>
</div>
<div className="footer">
<p>Status:
{" "}
<span className={productStatus}>
{productStatus === 'installed' && installed}
{productStatus === 'not-installed' && notInstalled}
{productStatus === 'active' && active}
{productStatus === 'not-installed' && notInstalled}
{productStatus === 'active' && active}
</span>
</p>
{productStatus !== 'active' && !loading && (
<>
{productStatus === 'not-installed' && isPremium &&
<Button isLink icon={'external'} href={premiumUrl} target="_blank">{learnMore}</Button>}
{productStatus === 'not-installed' && !isPremium &&
<Button isPrimary onClick={runInstall}>{installNow}</Button>}
{productStatus === 'installed' && <Button isSecondary onClick={runActivate}>{activate}</Button>}
</>
)}
</p>
{productStatus !== 'active' && !loading && wrappedButtonContent }

{loading && <span className="dashicons dashicons-update spin"/>}
</div>
</div>)
}
{loading && <span className="dashicons dashicons-update spin"/>}
</div>
</div>)
}
3 changes: 2 additions & 1 deletion assets/js/src/about/scss/_product-cards.scss
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
align-items: center;
padding: 15px;
align-self: flex-end;
justify-content: space-between;

p {
margin: 8px 0;
Expand All @@ -70,4 +71,4 @@
margin-left: auto;
text-decoration: none;
}
}
}
24 changes: 12 additions & 12 deletions src/Modules/About_us.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,19 @@ public function enqueue_about_page_script() {
private function get_about_localization_data() {
$links = isset( $this->about_data['page_menu'] ) ? $this->about_data['page_menu'] : [];
$product_pages = isset( $this->about_data['product_pages'] ) ? $this->about_data['product_pages'] : [];

return [
'links' => $links,
'logoUrl' => $this->about_data['logo'],
'productPages' => $this->get_product_pages_data( $product_pages ),
'products' => $this->get_other_products_data(),
'homeUrl' => esc_url( home_url() ),
'pageSlug' => $this->get_about_page_slug(),
'currentProduct' => [
'links' => $links,
'logoUrl' => $this->about_data['logo'],
'productPages' => $this->get_product_pages_data( $product_pages ),
'products' => $this->get_other_products_data(),
'homeUrl' => esc_url( home_url() ),
'pageSlug' => $this->get_about_page_slug(),
'currentProduct' => [
'slug' => $this->product->get_key(),
'name' => $this->product->get_name(),
],
'teamImage' => $this->get_sdk_uri() . 'assets/images/team.jpg',
'strings' => [
'teamImage' => $this->get_sdk_uri() . 'assets/images/team.jpg',
'strings' => [
'aboutUs' => __( 'About us', 'textdomain' ),
'heroHeader' => __( 'Our Story', 'textdomain' ),
'heroTextFirst' => __( 'Themeisle was founded in 2012 by a group of passionate developers who wanted to create beautiful and functional WordPress themes and plugins. Since then, we have grown into a team of over 20 dedicated professionals who are committed to delivering the best possible products to our customers.', 'textdomain' ),
Expand All @@ -204,6 +203,7 @@ private function get_about_localization_data() {
'notInstalled' => __( 'Not Installed', 'textdomain' ),
'active' => __( 'Active', 'textdomain' ),
],
'canInstallPlugins' => current_user_can( 'install_plugins' ),
];
}

Expand Down Expand Up @@ -286,8 +286,8 @@ private function get_product_pages_data( $product_pages ) {
$pages,
function ( $page_data, $page_key ) use ( $product_pages ) {
return in_array( $page_key, $product_pages, true ) &&
isset( $page_data['plugin']['status'] ) &&
$page_data['plugin']['status'] === 'not-installed';
isset( $page_data['plugin']['status'] ) &&
$page_data['plugin']['status'] === 'not-installed';
},
ARRAY_FILTER_USE_BOTH
);
Expand Down

0 comments on commit a7ab9d8

Please sign in to comment.