' . esc_html__( 'MainWP dashboard is not available. Activate MainWP to use this extension.', 'adsanity-for-mainwp' ) . '
';
do_action( 'mainwp_pagefooter_extensions', $this->page_slug );
return;
}
$tab = $this->current_tab();
// One MainWP-style padded segment supplies the themed background and the
// page inset (matching every other MainWP page); the tab menu + panels
// live inside it. Both panels render server-side — Semantic's .ui.tab CSS
// hides the inactive one, so it degrades to URL-driven tabs without JS,
// while the JS init makes switching instant.
echo '
';
do_action( 'mainwp_pagefooter_extensions', $this->page_slug );
}
/**
* Render the network dashboard tab body.
*
* @return void
*/
private function render_dashboard_view(): void {
list( $start, $end ) = $this->selected_range();
list( $orderby, $order ) = $this->selected_sort();
$force = $this->refresh_requested();
// Only reach out to the fleet on a trusted request: the nonce-verified
// refresh, a default (unparameterized) range, or a range/sort link this
// screen generated (those carry the range nonce). A hand-crafted
// ?start=&end= URL is served from cache only and never triggers a fresh
// N-site fan-out.
$allow_remote = $force || $this->range_request_is_trusted();
$data = $this->aggregator->network_stats( $start, $end, $force, $allow_remote );
$this->render_range_form( $start, $end );
if ( ! empty( $data['deferred'] ) ) {
echo '
' . esc_html__( 'This date range isn’t loaded yet. Adjust the range and press Apply, or use “Refresh data”, to fetch it from your sites.', 'adsanity-for-mainwp' ) . '
';
return;
}
$this->render_totals( $data['totals'], $data['sites'] );
$this->render_chart( isset( $data['series'] ) && is_array( $data['series'] ) ? $data['series'] : array() );
$this->render_site_table( $data['sites'], $orderby, $order, $start, $end );
$this->render_footer_meta( $data['generated'] );
}
/**
* Whether the current request may trigger a live cross-site fan-out.
*
* True when no custom range was supplied (the default trailing window shown
* on a normal page load) or when the request carries this screen's range
* nonce — the Apply button, the presets, and the sortable column links all
* include it. A bare, hand-edited ?start=&end= URL is therefore served from
* cache only, so it cannot amplify into an N-site HTTP sweep.
*
* @return bool
*/
private function range_request_is_trusted(): bool {
// phpcs:disable WordPress.Security.NonceVerification.Recommended -- this method IS the nonce verification.
$has_custom_range = isset( $_GET['start'] ) || isset( $_GET['end'] );
$nonce = isset( $_GET['adsanity_mwp_rnonce'] ) ? sanitize_key( wp_unslash( $_GET['adsanity_mwp_rnonce'] ) ) : '';
// phpcs:enable WordPress.Security.NonceVerification.Recommended
if ( ! $has_custom_range ) {
return true;
}
return (bool) wp_verify_nonce( $nonce, 'adsanity_mwp_range' );
}
/**
* Render the alerts tab body.
*
* @return void
*/
private function render_alerts_view(): void {
$alerts = $this->aggregator->alerts( $this->refresh_requested() );
$refresh_url = wp_nonce_url(
add_query_arg(
array(
'page' => $this->page_slug,
'tab' => 'alerts',
'adsanity_mwp_refresh' => 1,
),
admin_url( 'admin.php' )
),
'adsanity_mwp_refresh'
);
printf( '
';
// Gate the per-site drill-down on both this extension's access right and
// MainWP's individual-site dashboard right (the right MainWP itself uses
// to gate per-site views). Fails closed if the permission API is absent.
if ( ! function_exists( 'mainwp_current_user_can' )
|| ! mainwp_current_user_can( 'extension', 'adsanity-for-mainwp' )
|| ! mainwp_current_user_can( 'dashboard', 'access_individual_dashboard' ) ) {
echo '
' . esc_html__( 'You do not have permission to view AdSanity data for this site.', 'adsanity-for-mainwp' ) . '
' . esc_html(
sprintf(
/* translators: %s: error label */
__( 'Could not reach the AdSanity Connector on this site (%s). Confirm AdSanity and the Connector are active.', 'adsanity-for-mainwp' ),
$this->error_label( $error )
)
) . '
';
}
echo '';
do_action( 'mainwp_pagefooter_sites', 'AdSanity' );
}
/**
* Render the AdSanity overview metabox (MainWP widget).
*
* Site-aware: MainWP fires `mainwp_getmetaboxes` on both the network
* overview page (site id 0 → fleet rollup) and an individual site's
* overview page (that site's id → its own inventory). Both read cached
* data, so neither adds a per-page-load fan-out.
*
* @param int $site_id Current site id from MainWP (0 on the network overview).
* @return void
*/
public function render_metabox( int $site_id ): void {
if ( $site_id > 0 ) {
$this->render_site_metabox( $site_id );
return;
}
$snap = Adsanity_For_MainWP_Aggregator::overview_snapshot();
$page_url = add_query_arg( array( 'page' => $this->page_slug ), admin_url( 'admin.php' ) );
// Mirror MainWP's built-in overview widgets exactly (e.g. Clients,
// Recent Posts): the `ui header handle-drag` inside `mainwp-widget-header`
// supplies both the drag handle and the themed title/divider styling, the
// body sits in `mainwp-scrolly-overflow`, and the action lives in a
// `mainwp-widget-footer` grid. Everything uses Semantic vocabulary so it
// themes for light + dark without this extension's stylesheet (which is
// not enqueued on the MainWP overview page where this widget renders).
echo '
' . esc_html__( 'Waiting for connector data from synced sites.', 'adsanity-for-mainwp' ) . '
';
}
}
echo '
';
echo '';
}
/**
* Render the AdSanity metabox for a single managed site.
*
* Shows that one site's inventory from its stored sync summary (not the
* fleet rollup). Empty until the site has synced with the Connector.
*
* @param int $site_id Managed-site id.
* @return void
*/
private function render_site_metabox( int $site_id ): void {
$summary = Adsanity_For_MainWP_Aggregator::site_summary( $site_id );
// Deep-link to this site's own AdSanity subpage (Manage Sites > AdSanity),
// not the network rollup. MainWP derives the slug as 'ManageSites' + the
// subpage slug and passes the site via ?id=.
$page_url = add_query_arg(
array(
'page' => 'ManageSitesAdSanity',
'id' => $site_id,
),
admin_url( 'admin.php' )
);
echo '
',
esc_html__( 'AdSanity is inactive on this site', 'adsanity-for-mainwp' )
);
} else {
$bits = array();
if ( ! empty( $summary['version'] ) ) {
/* translators: %s: AdSanity core version */
$bits[] = sprintf( __( 'AdSanity %s', 'adsanity-for-mainwp' ), (string) $summary['version'] );
}
if ( ! empty( $summary['connector'] ) ) {
/* translators: %s: Connector version */
$bits[] = sprintf( __( 'Connector %s', 'adsanity-for-mainwp' ), (string) $summary['connector'] );
}
if ( ! empty( $bits ) ) {
echo '
' . esc_html( implode( ' · ', $bits ) ) . '
';
}
}
}
echo '
';
echo '';
}
/**
* Render a row of stat cards (icon + value + label).
*
* Uses the same `ui mainwp-cards` markup as the overview metabox widget so
* the reporting screens read consistently with it (and theme for light +
* dark without bespoke CSS).
*
* @param array $cards Card definitions.
* @return void
*/
private function render_stat_cards( array $cards ): void {
echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- label markup built from an escaped date in expiry_cell().
echo '
';
}
echo '
';
}
/**
* Render the "Expires" cell for an inventory row.
*
* Mirrors the Alerts "Expiring ads" rollup: an end date within the coming
* week is wrapped in a red label, within 30 days a yellow label (the same
* `ui small {color} label` markup and 7/30-day thresholds the aggregator
* uses for the alerts list). Dates further out, already past, or absent
* render as plain text so the colored labels stay an "expiring soon" signal.
*
* @param int|null $end Ad end-date timestamp, or null when the ad never expires.
* @param string $date_format Date format for display.
* @return string The cell's inner HTML (escaped).
*/
private function expiry_cell( ?int $end, string $date_format ): string {
if ( null === $end ) {
return '—';
}
$label = esc_html( date_i18n( $date_format, $end ) );
$now = time();
$week = $now + 7 * DAY_IN_SECONDS;
$month = $now + 30 * DAY_IN_SECONDS;
if ( $end < $now || $end > $month ) {
return $label;
}
$color = ( $end <= $week ) ? 'red' : 'yellow';
return sprintf( '