| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin-bar payment-mode indicator. |
| 4 |
* |
| 5 |
* Adds a wp-admin toolbar item (front-end and admin) that surfaces the current |
| 6 |
* payment mode to admins once a gateway is connected: an amber "Test Mode" |
| 7 |
* badge while the site is in test mode, or a green "Live" badge otherwise. It |
| 8 |
* links to the payment settings so admins can switch modes in one click, and |
| 9 |
* makes it obvious — while browsing the live site — that no real payments are |
| 10 |
* being accepted. Modeled on the toolbar indicators in GiveWP and Charitable. |
| 11 |
* |
| 12 |
* @package SureDonation |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace SureDonation\Inc\Admin; |
| 16 |
|
| 17 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 18 |
use WP_Admin_Bar; |
| 19 |
|
| 20 |
// Exit if accessed directly. |
| 21 |
if ( ! defined( 'ABSPATH' ) ) { |
| 22 |
exit; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Admin_Bar class. |
| 27 |
* |
| 28 |
* @since 1.3.0 |
| 29 |
*/ |
| 30 |
class Admin_Bar { |
| 31 |
|
| 32 |
/** |
| 33 |
* Node id for the payment-mode toolbar item. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
* @since 1.3.0 |
| 37 |
*/ |
| 38 |
private const NODE_ID = 'suredonation-payment-mode'; |
| 39 |
|
| 40 |
/** |
| 41 |
* Instance of this class. |
| 42 |
* |
| 43 |
* @var Admin_Bar|null |
| 44 |
* @since 1.3.0 |
| 45 |
*/ |
| 46 |
private static $instance = null; |
| 47 |
|
| 48 |
/** |
| 49 |
* Constructor. |
| 50 |
* |
| 51 |
* @since 1.3.0 |
| 52 |
*/ |
| 53 |
private function __construct() { |
| 54 |
// Priority 100 so the badge is added after the core toolbar items. |
| 55 |
add_action( 'admin_bar_menu', [ $this, 'add_mode_indicator' ], 100 ); |
| 56 |
// The toolbar shows on the front-end too, so register the styles on both |
| 57 |
// contexts. |
| 58 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_style' ] ); |
| 59 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_style' ] ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get instance of this class. |
| 64 |
* |
| 65 |
* @return Admin_Bar |
| 66 |
* @since 1.3.0 |
| 67 |
*/ |
| 68 |
public static function get_instance() { |
| 69 |
if ( null === self::$instance ) { |
| 70 |
self::$instance = new self(); |
| 71 |
} |
| 72 |
return self::$instance; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Add the payment-mode indicator node to the admin bar. |
| 77 |
* |
| 78 |
* Only rendered for admins, and only once a gateway is connected — there is |
| 79 |
* nothing to indicate before a gateway exists (the setup notice covers that |
| 80 |
* state instead). |
| 81 |
* |
| 82 |
* @param WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 83 |
* @return void |
| 84 |
* @since 1.3.0 |
| 85 |
*/ |
| 86 |
public function add_mode_indicator( $wp_admin_bar ) { |
| 87 |
if ( ! $this->should_display() ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$is_test = 'test' === Payment_Helper::get_payment_mode(); |
| 92 |
$variant = $is_test ? 'test' : 'live'; |
| 93 |
$label = $is_test |
| 94 |
? __( 'SureDonation: Test Mode', 'suredonation' ) |
| 95 |
: __( 'SureDonation: Live', 'suredonation' ); |
| 96 |
$tooltip = $is_test |
| 97 |
? __( 'SureDonation is in test mode — no real payments are being accepted. Click to switch to live mode.', 'suredonation' ) |
| 98 |
: __( 'SureDonation is in live mode — real payments are being accepted.', 'suredonation' ); |
| 99 |
|
| 100 |
$title = sprintf( |
| 101 |
'<span class="sd-admin-bar-mode sd-admin-bar-mode--%1$s"><span class="sd-admin-bar-mode__dot" aria-hidden="true"></span>%2$s</span>', |
| 102 |
esc_attr( $variant ), |
| 103 |
esc_html( $label ) |
| 104 |
); |
| 105 |
|
| 106 |
$wp_admin_bar->add_node( |
| 107 |
[ |
| 108 |
'id' => self::NODE_ID, |
| 109 |
// Anchor to the right-hand group so the mode reads as a status |
| 110 |
// indicator rather than a navigation item. |
| 111 |
'parent' => 'top-secondary', |
| 112 |
'title' => $title, |
| 113 |
'href' => Payment_Helper::get_settings_url(), |
| 114 |
'meta' => [ 'title' => $tooltip ], |
| 115 |
] |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Register the inline toolbar styles when the indicator will show. |
| 121 |
* |
| 122 |
* Attached to the always-registered core `admin-bar` stylesheet so the |
| 123 |
* badge is styled in both the admin and the front-end toolbar. |
| 124 |
* |
| 125 |
* @return void |
| 126 |
* @since 1.3.0 |
| 127 |
*/ |
| 128 |
public function enqueue_style() { |
| 129 |
if ( ! is_admin_bar_showing() || ! $this->should_display() ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
$css = '#wpadminbar #wp-admin-bar-' . self::NODE_ID . ' .ab-item{display:flex;align-items:center;}' |
| 134 |
. '#wpadminbar .sd-admin-bar-mode__dot{display:inline-block;width:8px;height:8px;margin-right:7px;border-radius:50%;background:currentColor;}' |
| 135 |
. '#wpadminbar .sd-admin-bar-mode--test{color:#f0c33c;}' |
| 136 |
. '#wpadminbar .sd-admin-bar-mode--live{color:#5fd07a;}'; |
| 137 |
|
| 138 |
wp_add_inline_style( 'admin-bar', $css ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Whether the payment-mode indicator should be shown to the current user. |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
* @since 1.3.0 |
| 146 |
*/ |
| 147 |
private function should_display() { |
| 148 |
return current_user_can( 'manage_options' ) && Payment_Helper::is_any_gateway_connected(); |
| 149 |
} |
| 150 |
} |
| 151 |
|