Put a Discounts card on the shared Nolimit dashboard

The dashboard showed Cache, Security and Assistant and not this plugin. Being
absent from the dashboard is a different failure from being absent from the
menu, and Kristian hit them in that order — so each now has its own assertion
rather than one standing in for the other.

The card registers on wpmgr_dashboard_cards with the counts this plugin exists
to report: groups, and addresses currently receiving a discount.

Cached for five minutes. The contract says implementations must not measure
anything in that filter, because it runs on an admin page load and a slow card
makes somebody else's dashboard slow in a way they cannot attribute to us. Two
COUNT(*) on small indexed tables is the most this may cost, and the dashboard
is a page people leave open.

Status is warn rather than fail when nothing is configured: a shop that has
installed this and not set it up yet is unfinished, not broken. Reserving red
for actual breakage is what keeps red worth reading.

Two assertions, proven red by removing the add_filter:

  FAIL  this plugin puts a card on the shared dashboard — 0 card(s) registered
  FAIL  and it carries this plugin colour, not WooCommerce purple
        — expected '#1a7a52', got NULL

The colour is matched against the literal, not against a constant. The shared
file renders whatever colour it is handed, so a card asserting its own constant
would agree with itself after somebody put WooCommerce purple back — the same
self-agreeing shape as the REST namespace assertion.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kristianwind
2026-09-20 07:57:02 +02:00
co-authored by Claude Opus 5
parent 370cf30979
commit bf4b429014
2 changed files with 80 additions and 0 deletions
+53
View File
@@ -17,6 +17,9 @@ class WC_EDM_Admin {
/**
* Admin page slug.
*/
/** Cache key for the dashboard card's counts. */
const CARD_TRANSIENT = 'wc_edm_card_counts';
const PAGE_SLUG = 'wc-email-discounts';
/**
@@ -59,10 +62,60 @@ class WC_EDM_Admin {
$this->audit = class_exists( 'WC_EDM_Admin_Audit' ) ? new WC_EDM_Admin_Audit() : null;
add_action( 'admin_menu', array( $this, 'register_menu' ) );
add_filter( 'wpmgr_dashboard_cards', array( $this, 'dashboard_card' ) );
add_action( 'admin_init', array( $this, 'handle_actions' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
}
/**
* This plugin's card on the shared Nolimit dashboard.
*
* The contract says implementations must not measure anything here,
* because this filter runs on an admin page load and a slow card makes
* somebody else's dashboard slow in a way they cannot attribute. Two
* COUNT(*) on small indexed tables is the most this may cost, and even
* that is cached: the dashboard is a page people leave open.
*
* @param array $cards The cards so far.
* @return array
*/
public function dashboard_card( $cards ) {
$counts = get_transient( self::CARD_TRANSIENT );
if ( ! is_array( $counts ) ) {
$counts = array(
'groups' => WC_EDM_DB::count_groups(),
'emails' => WC_EDM_DB::count_emails( true ),
);
set_transient( self::CARD_TRANSIENT, $counts, 5 * MINUTE_IN_SECONDS );
}
// "warn", not "fail": a shop that has installed this and not set it up
// yet is not broken, it is unfinished. Reserving red for actual
// breakage is what keeps red worth reading.
$status = $counts['groups'] > 0 && $counts['emails'] > 0 ? 'ok' : 'warn';
$cards[] = array(
'title' => __( 'Discounts', 'wc-email-discount' ),
'status' => $status,
'headline' => sprintf(
/* translators: %s: number of email addresses that currently receive a discount. */
_n( '%s address gets a discount', '%s addresses get a discount', $counts['emails'], 'wc-email-discount' ),
number_format_i18n( $counts['emails'] )
),
'lines' => array(
__( 'Groups', 'wc-email-discount' ) => number_format_i18n( $counts['groups'] ),
__( 'Active addresses', 'wc-email-discount' ) => number_format_i18n( $counts['emails'] ),
),
'url' => admin_url( 'admin.php?page=' . self::PAGE_SLUG ),
// The plugin's own colour. Deliberately not WooCommerce's brand
// purple; see the note in assets/icon.svg.
'colour' => '#1a7a52',
);
return $cards;
}
/**
* Register the page under the shared Nolimit menu.
*