should_display() ) { return; } $is_test = 'test' === Payment_Helper::get_payment_mode(); $variant = $is_test ? 'test' : 'live'; $label = $is_test ? __( 'SureDonation: Test Mode', 'suredonation' ) : __( 'SureDonation: Live', 'suredonation' ); $tooltip = $is_test ? __( 'SureDonation is in test mode — no real payments are being accepted. Click to switch to live mode.', 'suredonation' ) : __( 'SureDonation is in live mode — real payments are being accepted.', 'suredonation' ); $title = sprintf( '%2$s', esc_attr( $variant ), esc_html( $label ) ); $wp_admin_bar->add_node( [ 'id' => self::NODE_ID, // Anchor to the right-hand group so the mode reads as a status // indicator rather than a navigation item. 'parent' => 'top-secondary', 'title' => $title, 'href' => Payment_Helper::get_settings_url(), 'meta' => [ 'title' => $tooltip ], ] ); } /** * Point the "+ New → SureDonation Campaign" item at the creation drawer. * * Core links the item to post-new.php, which opens an empty block editor and * skips the drawer where a campaign's goal, currency and default form are * set. Re-adding the node under the same id keeps core's placement and * label while swapping the destination. * * The item is dropped entirely for anyone who cannot manage options. Core * shows it to any role with `edit_posts` (Editor, Author, Contributor), but * every step of campaign creation — the admin app, the REST routes and the * campaign meta — requires `manage_options`, so for those roles the entry * only leads to a permission error. * * @param WP_Admin_Bar $wp_admin_bar The admin bar instance. * @return void * @since 1.5.0 */ public function retarget_new_campaign_link( $wp_admin_bar ) { $post_type = get_post_type_object( Campaign_Cpt::POST_TYPE ); if ( ! $post_type instanceof WP_Post_Type ) { return; } $node_id = 'new-' . Campaign_Cpt::POST_TYPE; if ( ! current_user_can( 'manage_options' ) ) { $wp_admin_bar->remove_node( $node_id ); return; } /* * add_node() creates the node when the id is absent, so mirror core's * own capability check too — otherwise this would resurrect the entry * for users core deliberately hid it from. */ if ( ! current_user_can( $post_type->cap->create_posts ) ) { return; } $wp_admin_bar->add_node( [ 'id' => $node_id, 'parent' => 'new-content', 'title' => $post_type->labels->name_admin_bar, 'href' => Campaign_Cpt::get_create_url(), ] ); } /** * Register the inline toolbar styles when the indicator will show. * * Attached to the always-registered core `admin-bar` stylesheet so the * badge is styled in both the admin and the front-end toolbar. * * @return void * @since 1.3.0 */ public function enqueue_style() { if ( ! is_admin_bar_showing() || ! $this->should_display() ) { return; } $css = '#wpadminbar #wp-admin-bar-' . self::NODE_ID . ' .ab-item{display:flex;align-items:center;}' . '#wpadminbar .sd-admin-bar-mode__dot{display:inline-block;width:8px;height:8px;margin-right:7px;border-radius:50%;background:currentColor;}' . '#wpadminbar .sd-admin-bar-mode--test{color:#f0c33c;}' . '#wpadminbar .sd-admin-bar-mode--live{color:#5fd07a;}'; wp_add_inline_style( 'admin-bar', $css ); } /** * Whether the payment-mode indicator should be shown to the current user. * * @return bool * @since 1.3.0 */ private function should_display() { return current_user_can( 'manage_options' ) && Payment_Helper::is_any_gateway_connected(); } }