Skip to content

Commit

Permalink
Adding admin notices for legacy plugin, allow dismissible notices; me…
Browse files Browse the repository at this point in the history
…tadata: change author.
  • Loading branch information
ndeet committed Jan 13, 2022
1 parent 4ab2677 commit 01e7aa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
30 changes: 27 additions & 3 deletions btcpay-greenfield-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: BTCPay Greenfield For Woocommerce
* Plugin URI: https://wordpress.org/plugins/btcpay-greenfield-for-woocommerce/
* Description: BTCPay Server is a free and open-source bitcoin payment processor which allows you to receive payments in Bitcoin and altcoins directly, with no fees, transaction cost or a middleman.
* Author: BTCPay Server, ndeet
* Author: BTCPay Server
* Author URI: https://btcpayserver.org
* Text Domain: btcpay-greenfield-for-woocommerce
* Domain Path: /languages
Expand Down Expand Up @@ -39,6 +39,8 @@ function ($settings) {
);
add_action( 'wp_ajax_handle_ajax_api_url', [$this, 'processAjaxApiUrl'] );

$this->dependenciesNotification();
$this->legacyPluginNotification();
$this->notConfiguredNotification();
}
}
Expand Down Expand Up @@ -74,6 +76,9 @@ public function initPaymentGateways(): array {
return $gateways;
}

/**
* Displays notice (and link to config page) on admin dashboard if the plugin is not configured yet.
*/
public function notConfiguredNotification(): void {
if (!\BTCPayServer\WC\Helper\GreenfieldApiHelper::getConfig()) {
$message = sprintf(
Expand All @@ -89,11 +94,14 @@ public function notConfiguredNotification(): void {
}
}

/**
* Checks and displays notice on admin dashboard if PHP version is too low or WooCommerce not installed.
*/
public function dependenciesNotification() {
// Check PHP version.
if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
$versionMessage = sprintf( __( 'Your PHP version is %s but BTCPay Greenfield Payment plugin requires version 7.4+.', 'btcpay-greenfield-for-woocommerce' ), PHP_VERSION );
\BTcpayServer\WC\Admin\Notice::addNotice('error', $versionMessage);
\BTCPayServer\WC\Admin\Notice::addNotice('error', $versionMessage);
}

// Check if WooCommerce is installed (taken from WC docs).
Expand All @@ -106,11 +114,27 @@ public function dependenciesNotification() {
// All good.
} else {
$wcMessage = __('WooCommerce seems to be not installed. Make sure you do before you activate BTCPayServer Payment Gateway.', 'btcpay-greenfield-for-woocommerce');
\BTcpayServer\WC\Admin\Notice::addNotice('error', $wcMessage);
\BTCPayServer\WC\Admin\Notice::addNotice('error', $wcMessage);
}

}

/**
* Checks and displays notice on admin dashboard if the legacy BTCPay plugin is installed.
*/
public function legacyPluginNotification() {
// Check if WooCommerce is installed (taken from WC docs).
$plugin_path = trailingslashit( WP_PLUGIN_DIR ) . 'btcpay-for-woocommerce/class-wc-gateway-btcpay.php';

if (
in_array( $plugin_path, wp_get_active_and_valid_plugins() )
|| in_array( $plugin_path, wp_get_active_network_plugins() )
) {
$legacyMessage = __('Seems you have the old BTCPay for WooCommerce plugin installed. While it should work it is strongly recommended to not run both versions but rely on the maintained version (BTCPay Greenfield for WooCommerce).', 'btcpay-greenfield-for-woocommerce');
\BTCPayServer\WC\Admin\Notice::addNotice('warning', $legacyMessage, true);
}
}

/**
* Handles the AJAX callback from the GlobalSettings form. Unfortunately with namespaces it seems to not work
* to have this method on the GlobalSettings class. So keeping it here for the time being.
Expand Down
8 changes: 5 additions & 3 deletions src/Admin/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class Notice {
/**
* Adds notice to the admin UI.
*/
public static function addNotice( string $level, string $message ) {
public static function addNotice( string $level, string $message, bool $dismissible = false): void {
add_action(
'admin_notices',
function () use ( $level, $message ) {
function () use ( $level, $message, $dismissible ) {
$levelC = esc_attr( $level );
$dismiss = $dismissible ? ' is-dismissible' : '';
?>
<div class="notice <?php echo esc_attr( $level ) ?>" style="padding:12px 12px">
<div class="notice notice-<?php echo $levelC . $dismiss; ?>" style="padding:12px 12px">
<?php echo '<strong>BTCPay Server:</strong> ' . wp_kses_post( $message ) ?>
</div>
<?php
Expand Down

0 comments on commit 01e7aa5

Please sign in to comment.