" . esc_html( $message ) . "";
} else {
// Server-side subscription status fetch (no frontend AJAX)
$subscription_status = null;
try {
$generator = new \exedotcom\aistorymaker\AISTMA_Story_Generator();
$subscription_status = $generator->aistma_get_subscription_status();
} catch ( \Throwable $e ) {
$subscription_status = null;
}
$current_package_name = ( is_array( $subscription_status ) && ! empty( $subscription_status['valid'] ) ) ? ( $subscription_status['package_name'] ?? '' ) : '';
// Build concise status line
$status_line = '';
if ( is_array( $subscription_status ) && ! empty( $subscription_status['valid'] ) ) {
$credits_remaining = isset( $subscription_status['credits_remaining'] ) ? intval( $subscription_status['credits_remaining'] ) : null;
$next_billing_raw = $subscription_status['next_billing_date'] ?? '';
$next_billing = 'N/A';
$next_billing_timestamp = null;
$time_remaining = '';
if ( is_array( $next_billing_raw ) ) {
$next_billing = $next_billing_raw['formatted_date'] ?? $next_billing_raw['date'] ?? 'N/A';
$next_billing_timestamp = isset( $next_billing_raw['date'] ) ? strtotime( $next_billing_raw['date'] ) : null;
} elseif ( is_string( $next_billing_raw ) && $next_billing_raw !== '' ) {
$next_billing_timestamp = strtotime( $next_billing_raw );
$next_billing = $next_billing_timestamp ? gmdate( 'M-d', $next_billing_timestamp ) : $next_billing_raw;
}
// Calculate time remaining until next billing
if ( $next_billing_timestamp && $next_billing_timestamp > time() ) {
$time_diff = $next_billing_timestamp - time();
$days = floor( $time_diff / ( 24 * 60 * 60 ) );
$hours = floor( ( $time_diff % ( 24 * 60 * 60 ) ) / ( 60 * 60 ) );
$minutes = floor( ( $time_diff % ( 60 * 60 ) ) / 60 );
if ( $days > 0 ) {
// Show only days when more than 1 day remaining
if ( $days === 1 ) {
$time_remaining = '1 day';
} else {
$time_remaining = $days . ' days';
}
} elseif ( $hours > 0 ) {
// Show only hours when less than 1 day but more than 1 hour
$time_remaining = $hours . ' hour' . ( $hours === 1 ? '' : 's' );
} elseif ( $minutes > 0 ) {
// Show only minutes when less than 1 hour but more than 1 minute
$time_remaining = $minutes . ' minute' . ( $minutes === 1 ? '' : 's' );
} else {
// Show seconds when less than 1 minute
$seconds = $time_diff;
$time_remaining = $seconds . ' second' . ( $seconds === 1 ? '' : 's' );
}
$time_remaining = ' (' . $time_remaining . ' remaining)';
}
$parts = [];
if ( null === $credits_remaining ) {
$parts[] = __( 'Managed subscription detected', 'ai-story-maker' );
} elseif ($credits_remaining === 0) {
$parts[] = "No credits remaining";
if ( $next_billing && 'N/A' !== $next_billing ) {
$parts[] = 'Renewal: ' . $next_billing . $time_remaining;
}
} elseif ($credits_remaining === 1) {
$parts[] = "1 story remaining";
if ( $next_billing && 'N/A' !== $next_billing ) {
$parts[] = 'Renewal: ' . $next_billing . $time_remaining;
}
} else {
$parts[] = sprintf("%d stories left", $credits_remaining);
if ( $next_billing && 'N/A' !== $next_billing ) {
$parts[] = 'Renewal: ' . $next_billing . $time_remaining;
}
}
if ( ! empty( $current_package_name ) ) {
array_unshift( $parts, 'Current plan: ' . $current_package_name );
}
$status_line = implode( '. ', $parts ) . '.';
}
?>
rawurlencode($current_domain),
'port' => $current_port ? rawurlencode($current_port) : '',
'email' => rawurlencode(string: $subscription_email_for_requests),
'package_name' => isset($package['name']) ? $package['name'] : '',
),
$package_registration_url
);
$is_current = $current_package_name && isset($package['name']) && strcasecmp($package['name'], $current_package_name) === 0;
if ( $is_current ) { $matched = true; }
// Check if this package has subscription info from the API gateway
$has_subscription = isset($package['subscription_status']) && isset($package['subscription_info']);
$subscription_status = $has_subscription ? $package['subscription_status'] : '';
$subscription_info = $has_subscription ? $package['subscription_info'] : null;
?>
aria-current="true"
data-package-name=""
>
' . esc_html( $status_line ) . '
';
}
?>