' . __('Settings') . '';
$links[] = '' . __('Installation', 'yak-admin') . '';
$links[] = '' . __('FAQ', 'yak-admin') . '';
$links[] = '' . __('Handbook', 'yak-admin') . '';
}
return $links;
}
}
if (!function_exists('yak_calc_price')) {
/**
* Calculate price of a product taking into account any automatic discount
*/
function yak_calc_price($id, $cat_id = null, $custom_price = null) {
$discount = yak_get_option(AUTO_DISCOUNT, 1);
$price_rounding = yak_get_option(PRICE_ROUNDING, 0);
$prod = yak_get_product($id);
if (!empty($prod->discount_override)) {
$discount = $prod->discount_override;
}
if ($prod->custom_price && !empty($custom_price)) {
$price = $custom_price;
}
else {
$price = $prod->price;
}
if ($cat_id != null) {
$prod_type = yak_get_product_type($id, $cat_id);
$override_price = $prod_type->override_price;
if (!empty($override_price)) {
$price = $override_price;
}
}
$rtn = number_format(0.0 + ($price * $discount), $price_rounding, '.', '');
return $rtn;
}
}
if (!function_exists('yak_calc_shipping')) {
/**
* calculate the shipping cost by weight and country
*/
function yak_calc_shipping($total_weight, $total_items, $shipping_country, $selected_shipping_option) {
if (empty($selected_shipping_option)) {
return 0.0;
}
$zone = yak_get_option('yak_' . $shipping_country . '_zone');
$selected_shipping_option = str_replace(' ', '', trim($selected_shipping_option));
$prefix = 'yak_' . $selected_shipping_option . '_' . $zone;
$shipping = yak_get_option($prefix . '_shp_fixed', '');
$shipping_fixed_first = yak_get_option($prefix . '_shp_fixeditemfirst', 0);
$shipping_fixed = yak_get_option($prefix . '_shp_fixeditem', '');
$shipping_weight_first = yak_get_option($prefix . '_shp_weightfirst', '');
$shipping_weight = yak_get_option($prefix . '_shp_weight', '');
$act_shipping_cost = yak_calc_shipping_internal($total_items, $total_weight, $shipping, $shipping_fixed, $shipping_fixed_first, $shipping_weight, $shipping_weight_first);
if ($act_shipping_cost == null) {
$act_shipping_cost = 0.0;
}
return $act_shipping_cost;
}
}
if (!function_exists('yak_calc_shipping_internal')) {
/**
* The 'internals' of shipping calculation
*
* @param total_items the total number of items
* @param total_weight the total weight of all items
* @param shipping the default shipping value (returned first if present)
* @param shipping_fixed fixed shipping value (per item calc)
* @param shipping_fixed_first fixed shipping value for the first item
* @param shipping_weight weight-based shipping value (calculated by weight)
* @param shipping_weight_first weight-based shipping value for the first 100gms
*/
function yak_calc_shipping_internal($total_items, $total_weight, $shipping, $shipping_fixed, $shipping_fixed_first, $shipping_weight, $shipping_weight_first) {
if (!empty($shipping)) {
return $shipping;
}
else if (!empty($shipping_fixed)) {
$act_shipping_cost = 0.0;
if ($total_items >= 1) {
$act_shipping_cost += $shipping_fixed_first;
}
if ($total_items > 1) {
$act_shipping_cost += (($total_items - 1) * $shipping_fixed);
}
return $act_shipping_cost;
}
else if (!empty($shipping_weight) && $total_weight > 0) {
$weight_calc = yak_get_option(SHIPPING_WEIGHT_CALC, DEFAULT_SHIPPING_WEIGHT_CALC);
$act_shipping_cost = 0.0;
if (!empty($shipping_weight_first)) {
$act_shipping_cost += $shipping_weight_first;
$total_weight -= $weight_calc;
}
if ($total_weight > 0) {
$act_shipping_cost += ($shipping_weight * ceil($total_weight / $weight_calc));
}
return $act_shipping_cost;
}
else {
return null;
}
}
}
if (!function_exists('yak_checkout_error')) {
/**
* Handle an error in the checkout process.
*
* @param $model the array containing content
* @param $prompt initial error message
* @param $errors an array of error messages
*/
function yak_checkout_error(&$model, $prompt, $errors) {
if (sizeof($errors) > 0) {
$model['error_message'] = "
$prompt
";
foreach ($errors as $err) {
$model['error_message'] = $model['error_message'] . "- $err
";
}
$model['error_message'] = $model['error_message'] . '
';
return true;
}
else {
return false;
}
}
}
/**
* If all funds have been received for an order, and there is downloadable content,
* send an email containing the link for the dl.
*
* @param $order_id the id of the order to check
*/
if (!function_exists('yak_check_order')) {
function yak_check_order($order_id) {
global $wpdb, $order_table, $order_dl_table, $order_detail_table, $product_detail_table;
$orders = yak_get_orders(null, null, null, null, false, true, true, $order_id);
$order = $orders[0];
$sql = $wpdb->prepare("select count(*) as total
from $order_dl_table
where order_id = %d
and uid is not null", $order_id);
$dl = $wpdb->get_row($sql);
// final update status
$new_status = null;
$order_num = $order->order_num;
$rounding = yak_get_option(PRICE_ROUNDING, 2);
$funds_received = round($order->funds_received, $rounding);
$total_cost = round($order->total + $order->shipping_cost, $rounding);
if ($funds_received >= $total_cost) {
require_once('yak-payment-utils.php');
$payment_type = yak_get_payment($order->payment_type);
if ($payment_type != null) {
do_action('yak-finalise-order-' . $payment_type, $order_id);
}
$shipping_addr = $order->get_shipping_address();
// hook for 3rd party integration, affiliates processing, etc
$email = $shipping_addr->email;
$recipient = $shipping_addr->recipient;
$country = $shipping_addr->country;
$actual_cost = $order->total + yak_default($order->shipping_discount, 0) + yak_default($order->price_discount, 0);
$order_items = array();
foreach ($order->items as $item) {
$order_items[] = array('id' => $item->id, 'name' => $item->itemname, 'price' => $item->price, 'quantity' => $item->quantity);
}
if (defined(YAK_DEBUG)) {
yak_log("Triggering yak-order action for $order_id");
}
do_action('yak-order', array('order_id' => $order_id, 'email' => $email, 'recipient' => $recipient,
'total_cost' => $order->total, 'shipping_cost' => $order->shipping_cost,
'actual_cost' => $actual_cost, 'country' => $country, 'user_id' => $order->user_id,
'items' => $order_items));
if ($dl->total < 1) {
$msg = yak_get_option(DOWNLOAD_EMAIL, '');
$email = $shipping_addr->email;
yak_send_dl_email($order_id, $email, __('Your purchased downloads', 'yak'), $msg);
}
// how many items in this order are not downloadable (i.e. physical product)
$sql = $wpdb->prepare("select count(*) as non_dl_count
from $order_detail_table od, $product_detail_table pd
where od.id = %d
and od.post_id = pd.post_id
and od.cat_id = pd.cat_id
and (pd.dl_file is null or pd.dl_file = '')", $order_id);
$row = $wpdb->get_row($sql);
if ($row->non_dl_count < 1) {
$new_status = STOCK_SENT;
}
else if (empty($order->status)) {
$new_status = PAYMENT_PROCESSED;
}
if ($new_status != null) {
$sql = $wpdb->prepare("update $order_table set status = %s
where id = %d", $new_status, $order_id);
$wpdb->query($sql);
}
}
}
}
if (!function_exists('yak_send_dl_email')) {
function yak_send_dl_email($order_id, $recipient_email, $subject, $msg, $dryrun=false) {
global $wpdb, $order_dl_table;
$sql = $wpdb->prepare("select *
from $order_dl_table
where order_id = %d
and uid is null", $order_id);
$results = $wpdb->get_results($sql);
$dl_uri = yak_get_option(DOWNLOAD_URI, yak_get_blogurl() . '/wp-content/plugins/yak-for-wordpress/yak-dl.php');
if (empty($msg)) {
yak_log("WARNING: No message set for the download email. Unable to send");
return;
}
$uris = '';
if ($results) {
foreach ($results as $result) {
$id = $result->id;
$uid = md5(uniqid(rand(), true));
if (!$dryrun) {
$sql = $wpdb->prepare("update $order_dl_table set uid = %s
where id = %d", $uid, $id);
$wpdb->query($sql);
}
$uris = $uris . "$dl_uri?uid=$uid\n";
}
// send email with download links
if ($uris != '') {
$fromEmail = yak_get_option(DOWNLOAD_EMAIL_ADDRESS, '');
if (yak_str_contains($msg, '