| 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 |
* Also owns SureDonation's other toolbar adjustments, such as pointing the |
| 13 |
* "+ New" campaign entry at the campaign creation drawer. |
| 14 |
* |
| 15 |
* @package SureDonation |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace SureDonation\Inc\Admin; |
| 19 |
|
| 20 |
use SureDonation\Inc\Campaigns\Campaign_Cpt; |
| 21 |
use SureDonation\Inc\Payments\Payment_Helper; |
| 22 |
use WP_Admin_Bar; |
| 23 |
use WP_Post_Type; |
| 24 |
|
| 25 |
// Exit if accessed directly. |
| 26 |
if ( ! defined( 'ABSPATH' ) ) { |
| 27 |
exit; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Admin_Bar class. |
| 32 |
* |
| 33 |
* @since 1.3.0 |
| 34 |
*/ |
| 35 |
class Admin_Bar { |
| 36 |
|
| 37 |
/** |
| 38 |
* Node id for the payment-mode toolbar item. |
| 39 |
* |
| 40 |
* @var string |
| 41 |
* @since 1.3.0 |
| 42 |
*/ |
| 43 |
private const NODE_ID = 'suredonation-payment-mode'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Instance of this class. |
| 47 |
* |
| 48 |
* @var Admin_Bar|null |
| 49 |
* @since 1.3.0 |
| 50 |
*/ |
| 51 |
private static $instance = null; |
| 52 |
|
| 53 |
/** |
| 54 |
* Constructor. |
| 55 |
* |
| 56 |
* @since 1.3.0 |
| 57 |
*/ |
| 58 |
private function __construct() { |
| 59 |
// Priority 100 so the badge is added after the core toolbar items. |
| 60 |
add_action( 'admin_bar_menu', [ $this, 'add_mode_indicator' ], 100 ); |
| 61 |
// Core builds the "+ New" menu at priority 70, so run after it to |
| 62 |
// repoint the campaign entry. |
| 63 |
add_action( 'admin_bar_menu', [ $this, 'retarget_new_campaign_link' ], 100 ); |
| 64 |
// The toolbar shows on the front-end too, so register the styles on both |
| 65 |
// contexts. |
| 66 |
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_style' ] ); |
| 67 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_style' ] ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get instance of this class. |
| 72 |
* |
| 73 |
* @return Admin_Bar |
| 74 |
* @since 1.3.0 |
| 75 |
*/ |
| 76 |
public static function get_instance() { |
| 77 |
if ( null === self::$instance ) { |
| 78 |
self::$instance = new self(); |
| 79 |
} |
| 80 |
return self::$instance; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Add the payment-mode indicator node to the admin bar. |
| 85 |
* |
| 86 |
* Only rendered for admins, and only once a gateway is connected — there is |
| 87 |
* nothing to indicate before a gateway exists (the setup notice covers that |
| 88 |
* state instead). |
| 89 |
* |
| 90 |
* @param WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 91 |
* @return void |
| 92 |
* @since 1.3.0 |
| 93 |
*/ |
| 94 |
public function add_mode_indicator( $wp_admin_bar ) { |
| 95 |
if ( ! $this->should_display() ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$is_test = 'test' === Payment_Helper::get_payment_mode(); |
| 100 |
$variant = $is_test ? 'test' : 'live'; |
| 101 |
$label = $is_test |
| 102 |
? __( 'SureDonation: Test Mode', 'suredonation' ) |
| 103 |
: __( 'SureDonation: Live', 'suredonation' ); |
| 104 |
$tooltip = $is_test |
| 105 |
? __( 'SureDonation is in test mode — no real payments are being accepted. Click to switch to live mode.', 'suredonation' ) |
| 106 |
: __( 'SureDonation is in live mode — real payments are being accepted.', 'suredonation' ); |
| 107 |
|
| 108 |
$title = sprintf( |
| 109 |
'<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>', |
| 110 |
esc_attr( $variant ), |
| 111 |
esc_html( $label ) |
| 112 |
); |
| 113 |
|
| 114 |
$wp_admin_bar->add_node( |
| 115 |
[ |
| 116 |
'id' => self::NODE_ID, |
| 117 |
// Anchor to the right-hand group so the mode reads as a status |
| 118 |
// indicator rather than a navigation item. |
| 119 |
'parent' => 'top-secondary', |
| 120 |
'title' => $title, |
| 121 |
'href' => Payment_Helper::get_settings_url(), |
| 122 |
'meta' => [ 'title' => $tooltip ], |
| 123 |
] |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Point the "+ New → SureDonation Campaign" item at the creation drawer. |
| 129 |
* |
| 130 |
* Core links the item to post-new.php, which opens an empty block editor and |
| 131 |
* skips the drawer where a campaign's goal, currency and default form are |
| 132 |
* set. Re-adding the node under the same id keeps core's placement and |
| 133 |
* label while swapping the destination. |
| 134 |
* |
| 135 |
* The item is dropped entirely for anyone who cannot manage options. Core |
| 136 |
* shows it to any role with `edit_posts` (Editor, Author, Contributor), but |
| 137 |
* every step of campaign creation — the admin app, the REST routes and the |
| 138 |
* campaign meta — requires `manage_options`, so for those roles the entry |
| 139 |
* only leads to a permission error. |
| 140 |
* |
| 141 |
* @param WP_Admin_Bar $wp_admin_bar The admin bar instance. |
| 142 |
* @return void |
| 143 |
* @since 1.5.0 |
| 144 |
*/ |
| 145 |
public function retarget_new_campaign_link( $wp_admin_bar ) { |
| 146 |
$post_type = get_post_type_object( Campaign_Cpt::POST_TYPE ); |
| 147 |
|
| 148 |
if ( ! $post_type instanceof WP_Post_Type ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$node_id = 'new-' . Campaign_Cpt::POST_TYPE; |
| 153 |
|
| 154 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 155 |
$wp_admin_bar->remove_node( $node_id ); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* add_node() creates the node when the id is absent, so mirror core's |
| 161 |
* own capability check too — otherwise this would resurrect the entry |
| 162 |
* for users core deliberately hid it from. |
| 163 |
*/ |
| 164 |
if ( ! current_user_can( $post_type->cap->create_posts ) ) { |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$wp_admin_bar->add_node( |
| 169 |
[ |
| 170 |
'id' => $node_id, |
| 171 |
'parent' => 'new-content', |
| 172 |
'title' => $post_type->labels->name_admin_bar, |
| 173 |
'href' => Campaign_Cpt::get_create_url(), |
| 174 |
] |
| 175 |
); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Register the inline toolbar styles when the indicator will show. |
| 180 |
* |
| 181 |
* Attached to the always-registered core `admin-bar` stylesheet so the |
| 182 |
* badge is styled in both the admin and the front-end toolbar. |
| 183 |
* |
| 184 |
* @return void |
| 185 |
* @since 1.3.0 |
| 186 |
*/ |
| 187 |
public function enqueue_style() { |
| 188 |
if ( ! is_admin_bar_showing() || ! $this->should_display() ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$css = '#wpadminbar #wp-admin-bar-' . self::NODE_ID . ' .ab-item{display:flex;align-items:center;}' |
| 193 |
. '#wpadminbar .sd-admin-bar-mode__dot{display:inline-block;width:8px;height:8px;margin-right:7px;border-radius:50%;background:currentColor;}' |
| 194 |
. '#wpadminbar .sd-admin-bar-mode--test{color:#f0c33c;}' |
| 195 |
. '#wpadminbar .sd-admin-bar-mode--live{color:#5fd07a;}'; |
| 196 |
|
| 197 |
wp_add_inline_style( 'admin-bar', $css ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Whether the payment-mode indicator should be shown to the current user. |
| 202 |
* |
| 203 |
* @return bool |
| 204 |
* @since 1.3.0 |
| 205 |
*/ |
| 206 |
private function should_display() { |
| 207 |
return current_user_can( 'manage_options' ) && Payment_Helper::is_any_gateway_connected(); |
| 208 |
} |
| 209 |
} |
| 210 |
|