From 8fa72aa1b8514e2fbb3f82d50c1797ae06118f36 Mon Sep 17 00:00:00 2001 From: Ruben Ramirez Date: Fri, 4 Apr 2025 03:11:48 -0500 Subject: [PATCH] Added log_to_file() debug function --- quiztech-assessment-platform.php | 32 ++++++++++++++++++++++++++++++++ src/Includes/payments.php | 9 ++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/quiztech-assessment-platform.php b/quiztech-assessment-platform.php index 8ff88f8..e2a7b91 100644 --- a/quiztech-assessment-platform.php +++ b/quiztech-assessment-platform.php @@ -309,3 +309,35 @@ function quiztech_register_rest_routes() { } add_action( 'rest_api_init', 'quiztech_register_rest_routes' ); +/** + * Basic logging function for debugging. Allows passing of an object or array for the $data paramater + * + * Set the CUSTOM_DEBUG_LOG file in wp-config.php + * + */ +function log_to_file($message, $data = false){ + // Only proceed if a message is provided + if ($message) { + // Check if the custom log file constant is defined + if (defined('CUSTOM_DEBUG_LOG') && CUSTOM_DEBUG_LOG) { + $log_File = CUSTOM_DEBUG_LOG; + + $date = new DateTime(); + $date = $date->format("Y/m/d h:i:s"); + + // Convert arrays and objects to JSON format + if (is_array($data) || is_object($data)) { + $data = json_encode($data, JSON_PRETTY_PRINT); + $message = $message . "\r\n" . $data; + } else if ($data) { + $message = $message . " " . $data; + } + + // Log the message to the specified file + error_log("[$date] " . $message ."\r\n", 3, $log_File); + } + // If CUSTOM_DEBUG_LOG is not defined, do nothing (fail silently) + // Alternatively, could log to default PHP error log: + // else { error_log("Quiztech Debug: " . $message); } + } +} \ No newline at end of file diff --git a/src/Includes/payments.php b/src/Includes/payments.php index e36ffec..359eef8 100644 --- a/src/Includes/payments.php +++ b/src/Includes/payments.php @@ -25,7 +25,8 @@ if ( ! \defined( 'WPINC' ) ) { * @return mixed Gateway-specific response (e.g., redirect URL, session ID) or WP_Error on failure. */ function quiztech_initiate_credit_purchase( $user_id, $item_id, $quantity = 1 ) { - // 1. Validate input +log_to_file("quiztech_initiate_credit_purchase - 1"); +// 1. Validate input if ( ! $user_id || ! \get_user_by( 'ID', $user_id ) ) { return new \WP_Error( 'invalid_user', \__( 'Invalid user ID provided.', 'quiztech' ) ); } @@ -37,6 +38,7 @@ function quiztech_initiate_credit_purchase( $user_id, $item_id, $quantity = 1 ) return new \WP_Error( 'invalid_quantity', \__( 'Quantity must be at least 1.', 'quiztech' ) ); } + log_to_file("quiztech_initiate_credit_purchase - 2"); // 2. Get Item Details (Using hardcoded packages for now, mirroring the theme) // TODO: Move credit packages to a filterable array or WP options for better management. $credit_packages = array( @@ -57,6 +59,7 @@ function quiztech_initiate_credit_purchase( $user_id, $item_id, $quantity = 1 ) // Convert price to cents for Stripe $price_in_cents = (int) ( (float) $price_decimal * 100 ); + log_to_file("quiztech_initiate_credit_purchase - 3"); // 3. Get Active Gateway // TODO: Read active gateway from plugin settings (e.g., get_option('quiztech_settings')['payment_gateway']) $active_gateway = 'stripe'; // Placeholder @@ -68,6 +71,7 @@ function quiztech_initiate_credit_purchase( $user_id, $item_id, $quantity = 1 ) } $gateway = new StripeGateway(); + log_to_file("quiztech_initiate_credit_purchase - 4"); // 4. Prepare Metadata for Stripe and Webhook $metadata = [ 'user_id' => $user_id, @@ -79,9 +83,12 @@ function quiztech_initiate_credit_purchase( $user_id, $item_id, $quantity = 1 ) 'transaction_type'=> 'credit_purchase', // Identify the transaction type ]; + log_to_file("quiztech_initiate_credit_purchase - 5"); // 5. Call Gateway's initiatePayment method $result = $gateway->initiatePayment( $user_id, $item_id, $quantity, $price_in_cents, $currency, $metadata ); + + log_to_file("quiztech_initiate_credit_purchase - Result: ", $result); // The gateway method should handle the redirect on success or return WP_Error return $result; }