diff --git a/functions.php b/functions.php index a058505..706c36b 100644 --- a/functions.php +++ b/functions.php @@ -7,21 +7,29 @@ * */ function log_to_file($message, $data = false){ + // Only proceed if a message is provided if ($message) { - $log_File = CUSTOM_DEBUG_LOG; + // 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"); + $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; + // 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); } - - 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); } } } @@ -463,4 +471,4 @@ function quiztech_ajax_save_question() { } add_action( 'wp_ajax_quiztech_save_question', 'quiztech_ajax_save_question' ); -add_action( 'init', 'quiztech_handle_credit_purchase_submission' ); // Hook into init +add_action( 'template_redirect', 'quiztech_handle_credit_purchase_submission' ); // Hook into template_redirect