100 lines
No EOL
5.5 KiB
PHP
100 lines
No EOL
5.5 KiB
PHP
<?php
|
|
/**
|
|
* Template Name: Manage Credits
|
|
*
|
|
* This template is used for the page where Quiz Managers can view their credit balance and purchase more.
|
|
*
|
|
* @package Quiztech
|
|
*/
|
|
|
|
// Ensure the user is logged in and has the appropriate capability.
|
|
// Using basic 'read' capability for now, as specific credit management caps aren't defined.
|
|
$required_capability = 'read';
|
|
|
|
if ( ! is_user_logged_in() || ! current_user_can( $required_capability ) ) {
|
|
// Redirect to login page or show an error message.
|
|
wp_safe_redirect( wp_login_url( get_permalink() ) );
|
|
exit;
|
|
// Alternatively, display an error message:
|
|
// wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'quiztech' ), 403 );
|
|
}
|
|
|
|
// Get current user's credit balance
|
|
$user_id = get_current_user_id();
|
|
// Use the fully qualified function name including the namespace
|
|
$current_balance = function_exists('\Quiztech\AssessmentPlatform\Includes\quiztech_get_user_credit_balance') ? \Quiztech\AssessmentPlatform\Includes\quiztech_get_user_credit_balance( $user_id ) : __( 'N/A (Function not found)', 'quiztech' );
|
|
|
|
get_header(); ?>
|
|
|
|
<div id="primary" class="content-area">
|
|
<main id="main" class="site-main">
|
|
|
|
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
|
|
<header class="entry-header">
|
|
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
|
|
</header><!-- .entry-header -->
|
|
|
|
<div class="entry-content">
|
|
|
|
<h2><?php esc_html_e( 'Your Credit Balance', 'quiztech' ); ?></h2>
|
|
<p class="quiztech-credit-balance" style="font-size: 1.5em; font-weight: bold;">
|
|
<?php echo esc_html( $current_balance ); ?> <?php esc_html_e( 'Credits', 'quiztech' ); ?>
|
|
</p>
|
|
|
|
<hr>
|
|
|
|
<h2><?php esc_html_e( 'Purchase Credits', 'quiztech' ); ?></h2>
|
|
<p><?php esc_html_e( 'Select a package below to add credits to your account.', 'quiztech' ); ?></p>
|
|
|
|
<?php
|
|
// Define credit packages (could be moved to settings later)
|
|
$credit_packages = array(
|
|
'10_credits' => array( 'amount' => 10, 'price' => '10.00', 'currency' => 'USD' ),
|
|
'50_credits' => array( 'amount' => 50, 'price' => '45.00', 'currency' => 'USD' ),
|
|
'100_credits' => array( 'amount' => 100, 'price' => '80.00', 'currency' => 'USD' ),
|
|
);
|
|
?>
|
|
|
|
<div class="quiztech-credit-packages" style="display: flex; gap: 20px; flex-wrap: wrap;">
|
|
<?php foreach ( $credit_packages as $package_id => $package ) : ?>
|
|
<div class="quiztech-credit-package" style="border: 1px solid #eee; padding: 15px; text-align: center;">
|
|
<h3><?php printf( esc_html__( '%d Credits', 'quiztech' ), intval( $package['amount'] ) ); ?></h3>
|
|
<p><?php printf( esc_html__( 'Price: %s %s', 'quiztech' ), esc_html( $package['price'] ), esc_html( $package['currency'] ) ); ?></p>
|
|
<form method="post" action="<?php echo esc_url( get_permalink() ); ?>">
|
|
<?php wp_nonce_field( 'quiztech_buy_credits_action', 'quiztech_buy_credits_nonce' ); ?>
|
|
<input type="hidden" name="quiztech_action" value="initiate_credit_purchase">
|
|
<input type="hidden" name="package_id" value="<?php echo esc_attr( $package_id ); ?>">
|
|
<input type="hidden" name="credit_amount" value="<?php echo esc_attr( $package['amount'] ); ?>">
|
|
<input type="hidden" name="price" value="<?php echo esc_attr( $package['price'] ); ?>">
|
|
<input type="hidden" name="currency" value="<?php echo esc_attr( $package['currency'] ); ?>">
|
|
<button type="submit" class="button button-primary"><?php esc_html_e( 'Buy Now', 'quiztech' ); ?></button>
|
|
</form>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
|
|
<?php
|
|
// Display feedback messages if any (e.g., from the purchase initiation)
|
|
if ( isset( $_GET['purchase_status'] ) ) {
|
|
if ( $_GET['purchase_status'] === 'initiated' ) {
|
|
echo '<div class="notice notice-info is-dismissible"><p>' . esc_html__( 'Redirecting to payment gateway...', 'quiztech' ) . '</p></div>';
|
|
} elseif ( $_GET['purchase_status'] === 'error' ) {
|
|
$error_message = isset( $_GET['message'] ) ? sanitize_text_field( urldecode( $_GET['message'] ) ) : __( 'An unknown error occurred.', 'quiztech' );
|
|
echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( $error_message ) . '</p></div>';
|
|
} elseif ( $_GET['purchase_status'] === 'success' ) {
|
|
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Credits successfully added!', 'quiztech' ) . '</p></div>';
|
|
} elseif ( $_GET['purchase_status'] === 'cancelled' ) {
|
|
echo '<div class="notice notice-warning is-dismissible"><p>' . esc_html__( 'Payment cancelled.', 'quiztech' ) . '</p></div>';
|
|
}
|
|
}
|
|
?>
|
|
|
|
</div><!-- .entry-content -->
|
|
|
|
</article><!-- #post-<?php the_ID(); ?> -->
|
|
|
|
</main><!-- #main -->
|
|
</div><!-- #primary -->
|
|
|
|
<?php
|
|
get_footer();
|