user_email; // Handle posted subscription email and persist it $email_update_message = ''; if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] && isset( $_POST['aistma_subscription_email_nonce'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Verified via nonce and sanitized below if ( wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['aistma_subscription_email_nonce'] ) ), 'aistma_subscription_email' ) ) { $posted_email_raw = isset( $_POST['aistma_subscription_email'] ) ? sanitize_text_field( wp_unslash( $_POST['aistma_subscription_email'] ) ) : ''; $posted_email = sanitize_email( $posted_email_raw ); if ( ! empty( $posted_email ) && is_email( $posted_email ) ) { update_option( 'aistma_subscription_email', $posted_email ); // Always update the original subscription email to allow users to change it update_option( 'aistma_original_subscription_email', $posted_email ); $current_user_email = $posted_email; // reflect immediately $email_update_message = __( 'Subscription email updated. This email will be used for all future subscription management.', 'ai-story-maker' ); } else { $email_update_message = __( 'Please enter a valid email address.', 'ai-story-maker' ); } } else { $email_update_message = __( 'Security check failed. Please try again.', 'ai-story-maker' ); } } // Use saved subscription email if available $saved_subscription_email = get_option( 'aistma_subscription_email' ); if ( ! empty( $saved_subscription_email ) ) { $current_user_email = $saved_subscription_email; } // If original subscription email exists (for subscription cancellation), use that for subscription requests $original_subscription_email = get_option( 'aistma_original_subscription_email' ); if ( ! empty( $original_subscription_email ) ) { $subscription_email_for_requests = $original_subscription_email; } else { $subscription_email_for_requests = $current_user_email; } ?>

" . 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="" >
0) { ?> Price: $ 1) { echo esc_html($stories_count . ' stories'); } else { echo esc_html('1 story'); } echo ' every '; // Handle interval display - singular if count is 1, plural if more if ($interval_count > 1) { echo esc_html($interval_count . ' ' . $interval . 's'); } else { echo esc_html($interval); } ?> 0) { echo 'Enhancements: ' . esc_html($enhancements_per_post . ' per post') . ''; } elseif ($enhancements_per_post === 0) { echo 'Enhancements: Unlimited per post'; } ?>
Your Subscription Details
Email:
Credits: / used
Since:
' . esc_html( $status_line ) . '
'; } ?>