Refactor code structure for improved readability and maintainability
This commit is contained in:
183
functions.php
183
functions.php
@@ -42,38 +42,51 @@ require dirname(__FILE__).'/lib/mail/Exception.php';
|
||||
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
function send_mail_by_PHPMailer($to, $subject, $message, $attachment, $attachment_name){
|
||||
|
||||
// SEND MAIL by PHP MAILER
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->isSMTP(); // Use SMTP
|
||||
$mail->CharSet = 'UTF-8';
|
||||
$mail->Host = email_host_name; // Specify SMTP server
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use TLS encryption
|
||||
$mail->SMTPAuth = true; // Auth. SMTP
|
||||
//$mail->SMTPDebug = 3; // To view debug output
|
||||
$mail->Username = email; // Mail who send by PHPMailer
|
||||
$mail->Password = email_outgoing_pw; // your pass mail box
|
||||
$mail->SMTPSecure = email_outgoing_security; // Accept SSL
|
||||
$mail->Port = email_outgoing_port; // port of your out server
|
||||
$mail->setFrom(email, mail_from); // Mail to send at
|
||||
$mail->addAddress($to); // Add sender
|
||||
$mail->addReplyTo(email_reply_to); // Adress to reply
|
||||
$mail->isHTML(true); // use HTML message
|
||||
$mail->Subject = $subject;
|
||||
$mail->Body = $message;
|
||||
if (!empty($attachment) || $attachment != ''){
|
||||
$mail->AddStringAttachment($attachment, $attachment_name, 'base64', 'application/pdf');
|
||||
}
|
||||
// Log email attempt
|
||||
debuglog("Attempting to send email to: $to, subject: $subject");
|
||||
|
||||
// SEND
|
||||
if( !$mail->send() ){
|
||||
// render error if it is
|
||||
$tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
|
||||
debuglog(json_encode($tab));
|
||||
exit;
|
||||
}
|
||||
else{
|
||||
// return true if message is send
|
||||
return true;
|
||||
try {
|
||||
// SEND MAIL by PHP MAILER
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->isSMTP(); // Use SMTP
|
||||
$mail->CharSet = 'UTF-8';
|
||||
$mail->Host = email_host_name; // Specify SMTP server
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; // Use TLS encryption
|
||||
$mail->SMTPAuth = true; // Auth. SMTP
|
||||
//$mail->SMTPDebug = 3; // To view debug output
|
||||
$mail->Username = email; // Mail who send by PHPMailer
|
||||
$mail->Password = email_outgoing_pw; // your pass mail box
|
||||
$mail->SMTPSecure = email_outgoing_security; // Accept SSL
|
||||
$mail->Port = email_outgoing_port; // port of your out server
|
||||
|
||||
debuglog("SMTP Config - Host: " . email_host_name . ", Port: " . email_outgoing_port . ", Security: " . email_outgoing_security);
|
||||
|
||||
$mail->setFrom(email, mail_from); // Mail to send at
|
||||
$mail->addAddress($to); // Add sender
|
||||
$mail->addReplyTo(email_reply_to); // Adress to reply
|
||||
$mail->isHTML(true); // use HTML message
|
||||
$mail->Subject = $subject;
|
||||
$mail->Body = $message;
|
||||
if (!empty($attachment) || $attachment != ''){
|
||||
$mail->AddStringAttachment($attachment, $attachment_name, 'base64', 'application/pdf');
|
||||
debuglog("Attachment added: $attachment_name");
|
||||
}
|
||||
|
||||
// SEND
|
||||
if( !$mail->send() ){
|
||||
// render error if it is
|
||||
$tab = array('error' => 'Mailer Error: '.$mail->ErrorInfo );
|
||||
debuglog("Email send failed: " . json_encode($tab));
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
// return true if message is send
|
||||
debuglog("Email sent successfully to: $to");
|
||||
return true;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
debuglog("PHPMailer Exception: " . $e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -479,13 +492,13 @@ function removeGiftCart($pdo, $orderID){
|
||||
function generateInvoice($invoice_cust,$orderID,$user_language){
|
||||
|
||||
//Variables
|
||||
$customer_email = htmlspecialchars($invoice_cust['customer']['email'] ?? '', ENT_QUOTES);
|
||||
$invoice_customer_email = htmlspecialchars($invoice_cust['customer']['email'] ?? '', ENT_QUOTES);
|
||||
//Generate invoice
|
||||
ob_start();
|
||||
include dirname(__FILE__).'/custom/email/order-invoice-template.php';
|
||||
$order_invoice_template = ob_get_clean();
|
||||
|
||||
return array($order_invoice_template,$customer_email,$orderId);
|
||||
return array($order_invoice_template,$invoice_customer_email,$orderID);
|
||||
}
|
||||
|
||||
function freeShipment($price, $type){
|
||||
@@ -613,13 +626,23 @@ function sortProducts(array $products, string $field, string $direction = 'asc')
|
||||
return $products;
|
||||
}
|
||||
|
||||
function highlightedProducts($clientsecret,$categoryID,$range, $subtitle){
|
||||
function highlightedProducts($categoryID, $range, $subtitle, $catalog, $categories){
|
||||
|
||||
include './custom/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
|
||||
|
||||
//GET CATALOG DATA
|
||||
$products = ioAPIv2('/v2/catalog/category='.$categoryID,'',$clientsecret);
|
||||
$products = json_decode($products,true);
|
||||
//GET CATALOG DATA FROM CACHE
|
||||
// Build product IDs that belong to this category
|
||||
$product_ids_in_category = [];
|
||||
foreach ($categories as $cat) {
|
||||
if ($cat['rowID'] == $categoryID && isset($cat['product_id'])) {
|
||||
$product_ids_in_category[] = $cat['product_id'];
|
||||
}
|
||||
}
|
||||
|
||||
// Filter products by category
|
||||
$products = array_filter($catalog, function($product) use ($product_ids_in_category) {
|
||||
return in_array($product['rowID'], $product_ids_in_category);
|
||||
});
|
||||
|
||||
//RANDOM SORT
|
||||
$products = sortProducts($products, 'random');
|
||||
@@ -664,13 +687,23 @@ function highlightedProducts($clientsecret,$categoryID,$range, $subtitle){
|
||||
return $section ;
|
||||
}
|
||||
|
||||
function highlightedProducts2($clientsecret,$categoryID,$range, $subtitle){
|
||||
function highlightedProducts2($categoryID, $range, $subtitle, $catalog, $categories){
|
||||
|
||||
include './custom/translations/translations_'.strtoupper($_SESSION['country_code']).'.php';
|
||||
|
||||
//GET CATALOG DATA
|
||||
$products = ioAPIv2('/v2/catalog/category='.$categoryID,'',$clientsecret);
|
||||
$products = json_decode($products,true);
|
||||
//GET CATALOG DATA FROM CACHE
|
||||
// Build product IDs that belong to this category
|
||||
$product_ids_in_category = [];
|
||||
foreach ($categories as $cat) {
|
||||
if ($cat['rowID'] == $categoryID && isset($cat['product_id'])) {
|
||||
$product_ids_in_category[] = $cat['product_id'];
|
||||
}
|
||||
}
|
||||
|
||||
// Filter products by category
|
||||
$products = array_filter($catalog, function($product) use ($product_ids_in_category) {
|
||||
return in_array($product['rowID'], $product_ids_in_category);
|
||||
});
|
||||
|
||||
//RANDOM SORT
|
||||
$products = sortProducts($products, 'random');
|
||||
@@ -730,6 +763,74 @@ function debuglog($error){
|
||||
error_log($test, 3, $filelocation);
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
// Get Cached Data from File (24-hour refresh)
|
||||
//------------------------------------------
|
||||
function getCachedData($api_endpoint, $cache_filename, $token){
|
||||
$cache_file = './cache/' . $cache_filename;
|
||||
$cache_age_limit = 24 * 60 * 60; // 24 hours in seconds
|
||||
|
||||
// Check if cache file exists and is fresh (less than 24 hours old)
|
||||
if (file_exists($cache_file) && (time() - filemtime($cache_file)) < $cache_age_limit) {
|
||||
// Return cached data from JSON file
|
||||
$json_data = file_get_contents($cache_file);
|
||||
return json_decode($json_data, true);
|
||||
}
|
||||
|
||||
// Cache is stale or doesn't exist - attempt to refresh
|
||||
try {
|
||||
$response = ioAPIv2($api_endpoint, '', $token);
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if (!empty($data) && is_array($data)) {
|
||||
// Successfully fetched new data - write to cache file as JSON
|
||||
$cache_content = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
|
||||
|
||||
// Ensure cache directory exists
|
||||
if (!is_dir('./cache')) {
|
||||
mkdir('./cache', 0755, true);
|
||||
}
|
||||
|
||||
// Attempt to write cache file
|
||||
if (@file_put_contents($cache_file, $cache_content, LOCK_EX) === false) {
|
||||
// Write failed - log error and fall back to existing cache
|
||||
debuglog('Cache write failed for ' . $cache_filename . ' - using existing cache');
|
||||
|
||||
if (file_exists($cache_file)) {
|
||||
$json_data = file_get_contents($cache_file);
|
||||
return json_decode($json_data, true);
|
||||
}
|
||||
|
||||
// No existing cache - return fresh data anyway
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $data;
|
||||
} else {
|
||||
// API call failed - log error and fall back to existing cache
|
||||
debuglog('API call failed for ' . $api_endpoint . ' - using existing cache');
|
||||
|
||||
if (file_exists($cache_file)) {
|
||||
$json_data = file_get_contents($cache_file);
|
||||
return json_decode($json_data, true);
|
||||
}
|
||||
|
||||
// No existing cache and API failed - return empty array
|
||||
return [];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Exception occurred - log and fall back to existing cache
|
||||
debuglog('Exception in getCachedData for ' . $cache_filename . ': ' . $e->getMessage());
|
||||
|
||||
if (file_exists($cache_file)) {
|
||||
$json_data = file_get_contents($cache_file);
|
||||
return json_decode($json_data, true);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
//------------------------------------------
|
||||
// Retrieve all $_GET from URL
|
||||
//------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user