/** * Checks to see if a Coupon Code was supplied, and if so; what does it provide? * * @package s2Member\PayPal * @since 1.5 * * @param array $attr An array of Pro Form Attributes. * @param str $coupon_code Optional. A possible Coupon Code supplied by the Customer. * @param str $return Optional. Return type. One of `response|attr`. Defaults to `attr`. * @param array $process Optional. An array of additional processing routines to run here. * One or more of these values: `affiliates-1px-response|affiliates-silent-post|notifications`. * @return array|str Original array, with prices and description modified when/if a Coupon Code is accepted. * Or, if ``$return === "response"``, return a string response, indicating status. * * @todo See if it's possible to simplify this routine. * @todo Add support for tracking Coupon Code usage. * @todo Add support for a fixed number of uses. */ public static function paypal_apply_coupon($attr = FALSE, $coupon_code = FALSE, $return = FALSE, $process = FALSE) { if (($coupon_code = trim(strtolower($coupon_code))) || ($coupon_code = trim(strtolower($attr["coupon"])))) { if ($attr["accept_coupons"] && $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"]) { $cs = c_ws_plugin__s2member_utils_cur::symbol($attr["cc"]); $tx = c_ws_plugin__s2member_pro_paypal_utilities::paypal_tax_may_apply() ? _x(" + tax", "s2member-front", "s2member") : ""; $ps = _x("%", "s2member-front percentage-symbol", "s2member"); if (strlen($affiliate_suffix_chars = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_suffix_chars"])) { if (preg_match("/^(.+?)" . preg_quote($affiliate_suffix_chars, "/") . "([0-9]+)\$/i", $coupon_code, $m)) { ($full_coupon_code = $m[0]) . ($coupon_code = $m[1]) . ($affiliate_id = $m[2]); } } unset($affiliate_suffix_chars, $m); foreach (c_ws_plugin__s2member_utils_strings::trim_deep(preg_split("/[\r\n\t]+/", $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_coupon_codes"])) as $_line) { if (($_line = trim($_line, " \r\n\t\v|")) && is_array($_coupon = preg_split("/\\|/", $_line))) { $coupon["code"] = !empty($_coupon[0]) ? trim(strtolower($_coupon[0])) : ""; $coupon["percentage"] = !empty($_coupon[1]) && preg_match("/%/", $_coupon[1]) ? (double) $_coupon[1] : 0; $coupon["flat-rate"] = !empty($_coupon[1]) && !preg_match("/%/", $_coupon[1]) ? (double) $_coupon[1] : 0; $coupon["expired"] = !empty($_coupon[2]) && strtotime($_coupon[2]) < time() ? $_coupon[2] : false; $coupon["directive"] = !empty($_coupon[3]) && ($_coupon[3] = strtolower($_coupon[3])) ? preg_replace("/_/", "-", $_coupon[3]) : "all"; $coupon["directive"] = preg_match("/^(ta-only|ra-only|all)\$/", $coupon["directive"]) ? $coupon["directive"] : "all"; $coupon["singulars"] = !empty($_coupon[4]) && ($_coupon[4] = strtolower($_coupon[4])) && $_coupon[4] !== "all" ? $_coupon[4] : "all"; $coupon["singulars"] = $coupon["singulars"] !== "all" ? preg_split("/[\r\n\t\\s;,]+/", trim(preg_replace("/[^0-9,]/", "", $coupon["singulars"]), ",")) : array("all"); unset($_line, $_coupon); if ($coupon_code === $coupon["code"] && !$coupon["expired"]) { if ($coupon["singulars"] === array("all") || in_array($attr["singular"], $coupon["singulars"])) { $coupon_accepted = true; if ($coupon["flat-rate"]) { if (($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . $ra . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . $ra . $tx); } else { if ($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); } else { if ($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $ta = number_format($attr["ta"] - $coupon["flat-rate"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $ra = number_format($attr["ra"] - $coupon["flat-rate"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), $cs . number_format($coupon["flat-rate"], 2, ".", ""), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); } else { // Otherwise, we need a default response to display. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member"); } } } } } } } else { if ($coupon["percentage"]) { if (($coupon["directive"] === "ra-only" || $coupon["directive"] === "all") && $attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"] - $p, 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . $ra . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . $ra . $tx); } else { if ($coupon["directive"] === "ta-only" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"] - $p, 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"], 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "ra-only" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"] - $p, 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "all" && $attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"] - $p, 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"] - $p, 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s, then %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr["tp"] . " " . $attr["tt"]) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"])); } else { if ($coupon["directive"] === "ra-only" && !$attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"], 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"] - $p, 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); } else { if ($coupon["directive"] === "all" && !$attr["tp"] && !$attr["sp"]) { $coupon_applies = true; $p = $attr["ta"] / 100 * $coupon["percentage"]; $ta = number_format($attr["ta"] - $p, 2, ".", ""); $ta = $ta >= 0.0 ? $ta : "0.00"; $p = $attr["ra"] / 100 * $coupon["percentage"]; $ra = number_format($attr["ra"] - $p, 2, ".", ""); $ra = $ra >= 0.0 ? $ra : "0.00"; $desc = sprintf(_x("COUPON %s off. (Now: %s)", "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); $response = sprintf(_x('<div>Coupon: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', "s2member-front", "s2member"), number_format($coupon["percentage"], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr["rp"] . " " . $attr["rt"], $attr["rr"]) . $tx); } else { // Otherwise, we need a default response to display. $response = _x('<div>Sorry, your Coupon is not applicable.</div>', "s2member-front", "s2member"); } } } } } } } else { // Else there was no discount applied at all. $response = sprintf(_x('<div>Coupon: <strong>%s0.00 off</strong>.</div>', "s2member-front", "s2member"), $cs); } } } else { // Otherwise, we need a response that indicates not applicable for this purchase. $response = _x('<div>Sorry, your Coupon cannot be applied to this particular purchase.</div>', "s2member-front", "s2member"); } } else { if ($coupon_code === $coupon["code"] && $coupon["expired"]) { $response = sprintf(_x('<div>Sorry, your Coupon <strong>expired</strong>: <em>%s</em>.</div>', "s2member-front", "s2member"), $coupon["expired"]); } } } } if (isset($coupon_applies, $desc) && $coupon_applies) { // translators: `%1$s` is new price/description, after coupon applied. `%2$s` is original description. $attr["desc"] = sprintf(_x('%1$s ~ ORIGINALLY: %2$s', "s2member-front", "s2member"), $desc, $attr["desc"]); } $attr["ta"] = isset($coupon_applies, $ta) && $coupon_applies ? $ta : $attr["ta"]; $attr["ra"] = isset($coupon_applies, $ra) && $coupon_applies ? $ra : $attr["ra"]; if (is_array($process) && (in_array("affiliates-silent-post", $process) || in_array("affiliates-1px-response", $process))) { if (isset($coupon_applies) && $coupon_applies && !empty($affiliate_id)) { if (empty($_COOKIE["idev"])) { if ($_urls = $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["pro_affiliate_coupon_code_tracking_urls"]) { foreach (preg_split("/[\r\n\t]+/", $_urls) as $_url) { if ($_url = preg_replace("/%%full_coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($full_coupon_code)), $_url)) { if ($_url = preg_replace("/%%coupon_code%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($coupon_code)), $_url)) { if ($_url = preg_replace("/%%(?:coupon_affiliate_id|affiliate_id)%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($affiliate_id)), $_url)) { if ($_url = preg_replace("/%%user_ip%%/i", c_ws_plugin__s2member_utils_strings::esc_ds(urlencode($_SERVER["REMOTE_ADDR"])), $_url)) { if ($_url = trim(preg_replace("/%%(.+?)%%/i", "", $_url))) { if (!($_r = 0) && ($_url = preg_replace("/^silent-php\\|/i", "", $_url, 1, $_r)) && $_r && in_array("affiliates-silent-post", $process)) { c_ws_plugin__s2member_utils_urls::remote($_url, false, array("blocking" => false)); } else { if (!($_r = 0) && ($_url = preg_replace("/^img-1px\\|/i", "", $_url, 1, $_r)) && $_r && in_array("affiliates-1px-response", $process)) { if (!empty($response) && $return === "response") { $response .= "\n" . '<img src="' . esc_attr($_url) . '" style="width:0; height:0; border:0;" alt="" />'; } } } } } } } } } } } } } unset($_urls, $_url, $_r); if (empty($response)) { // Is ``$response`` NOT set by now? If it's not, we need a default ``$response``. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member"); } } else { // Otherwise, we need a default response to display. $response = _x('<div>Sorry, your Coupon is N/A, invalid or expired.</div>', "s2member-front", "s2member"); } } $attr["_coupon_applies"] = isset($coupon_applies) && $coupon_applies ? "1" : "0"; $attr["_coupon_code"] = isset($coupon_applies) && $coupon_applies ? $coupon_code : ""; $attr["_full_coupon_code"] = isset($coupon_applies) && $coupon_applies && !empty($full_coupon_code) ? $full_coupon_code : (isset($coupon_applies) && $coupon_applies ? $coupon_code : ""); $attr["_coupon_affiliate_id"] = isset($coupon_applies) && $coupon_applies && !empty($affiliate_id) && empty($_COOKIE["idev"]) ? $affiliate_id : ""; return $return === "response" ? (string) @$response : $attr; }
public function apply($attr, $coupon_code = '', $return = 'attr', $process = array()) { $attr = (array) $attr; // Force an array value. $process = (array) $process; // Force an array value. $coupon_code = $coupon_code ? $coupon_code : $attr['coupon']; $coupon_code = trim((string) $coupon_code); // Force string value. if ($coupon_code) { // Only if we do have a coupon code. if ($coupon = $this->valid_coupon($coupon_code, $attr)) { $coupon_applies = FALSE; $ta = $attr['ta']; $ra = $attr['ra']; $desc = $attr['desc']; $cs = c_ws_plugin__s2member_utils_cur::symbol($attr['cc']); $tx = c_ws_plugin__s2member_pro_taxes::may_apply() ? ' ' . _x('+ tax', 's2member-front', 's2member') : ''; $ps = _x('%', 's2member-front percentage-symbol', 's2member'); $full_coupon_code = $coupon_code; // Initialize. $affiliate_id = ''; // Initialize; this starts as empty. if (strlen($_affiliate_suffix_chars = $GLOBALS['WS_PLUGIN__']['s2member']['o']['pro_affiliate_coupon_code_suffix_chars'])) { if (preg_match('/^(.+?)' . preg_quote($_affiliate_suffix_chars, '/') . '([0-9]+)$/i', $coupon_code, $_m)) { list($full_coupon_code, $coupon_code, $affiliate_id) = $_m; } } unset($_affiliate_suffix_chars, $_m); // Housekeeping. if ($coupon['flat_discount']) { if ($attr['sp'] && (!$coupon['directive'] || $coupon['directive'] === 'ra-only')) { $coupon_applies = TRUE; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'] - $coupon['flat_discount'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . $ra . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . $ra . $tx); } else { if (!$attr['sp'] && $attr['tp'] && $coupon['directive'] === 'ta-only') { $coupon_applies = TRUE; $ta = number_format($attr['ta'] - $coupon['flat_discount'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && $attr['tp'] && $coupon['directive'] === 'ra-only') { $coupon_applies = TRUE; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'] - $coupon['flat_discount'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && $attr['tp'] && !$coupon['directive']) { $coupon_applies = TRUE; $ta = number_format($attr['ta'] - $coupon['flat_discount'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'] - $coupon['flat_discount'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && !$attr['tp'] && $coupon['directive'] === 'ra-only') { $coupon_applies = TRUE; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'] - $coupon['flat_discount'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); } else { if (!$attr['sp'] && !$attr['tp'] && !$coupon['directive']) { $coupon_applies = TRUE; $ta = number_format($attr['ta'] - $coupon['flat_discount'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $ra = number_format($attr['ra'] - $coupon['flat_discount'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), $cs . number_format($coupon['flat_discount'], 2, '.', ''), $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); } else { // Otherwise, we need a default response to display. $response = _x('<div>Sorry, your discount code is not applicable.</div>', 's2member-front', 's2member'); } } } } } } } else { if ($coupon['percentage_discount']) { if ($attr['sp'] && (!$coupon['directive'] || $coupon['directive'] === 'ra-only')) { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'] - $p, 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . $ra . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . $ra . $tx); } else { if (!$attr['sp'] && $attr['tp'] && $coupon['directive'] === 'ta-only') { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'] - $p, 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'], 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && $attr['tp'] && $coupon['directive'] === 'ra-only') { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'] - $p, 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && $attr['tp'] && !$coupon['directive']) { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'] - $p, 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'] - $p, 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s, then %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s, then %s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ta, $attr['tp'] . ' ' . $attr['tt']) . $tx, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr'])); } else { if (!$attr['sp'] && !$attr['tp'] && $coupon['directive'] === 'ra-only') { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'], 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'] - $p, 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); } else { if (!$attr['sp'] && !$attr['tp'] && !$coupon['directive']) { $coupon_applies = TRUE; $p = $attr['ta'] / 100 * $coupon['percentage_discount']; $ta = number_format($attr['ta'] - $p, 2, '.', ''); $ta = $ta >= 0.0 ? $ta : '0.00'; $p = $attr['ra'] / 100 * $coupon['percentage_discount']; $ra = number_format($attr['ra'] - $p, 2, '.', ''); $ra = $ra >= 0.0 ? $ra : '0.00'; $desc = sprintf(_x('Discount: %s off. (Now: %s)', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); $response = sprintf(_x('<div>Discount: <strong>%s off</strong>. (Now: <strong>%s</strong>)</div>', 's2member-front', 's2member'), number_format($coupon['percentage_discount'], 0) . $ps, $cs . c_ws_plugin__s2member_utils_time::amount_period_term($ra, $attr['rp'] . ' ' . $attr['rt'], $attr['rr']) . $tx); } else { // Otherwise, we need a default response to display. $response = _x('<div>Sorry, your discount code is not applicable.</div>', 's2member-front', 's2member'); } } } } } } } else { // Else there was no discount applied at all. $response = sprintf(_x('<div>Discount: <strong>%s0.00 off</strong>.</div>', 's2member-front', 's2member'), $cs); } } if ($coupon_applies) { $attr['ta'] = $ta < 0.5 ? '0.00' : $ta; // Apply new amount for the initial/period. $attr['ra'] = $ra < 0.5 ? '0.00' : $ra; // Apply new amount for the regular/recurring period. $attr['desc'] = sprintf(_x('%1$s ~ ORIGINALLY: %2$s', 's2member-front', 's2member'), $desc, $attr['desc']); if ($affiliate_id && empty($_COOKIE['idev']) && (in_array('affiliates-silent-post', $process) || in_array('affiliates-1px-response', $process))) { foreach (preg_split('/[' . "\r\n\t" . ']+/', $GLOBALS['WS_PLUGIN__']['s2member']['o']['pro_affiliate_coupon_code_tracking_urls'], NULL, PREG_SPLIT_NO_EMPTY) as $_url) { if ($_url = preg_replace('/%%full_coupon_code%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($full_coupon_code)), $_url)) { if ($_url = preg_replace('/%%coupon_code%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($coupon_code)), $_url)) { if ($_url = preg_replace('/%%(?:coupon_affiliate_id|affiliate_id)%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($affiliate_id)), $_url)) { if ($_url = preg_replace('/%%user_ip%%/i', c_ws_plugin__s2member_utils_strings::esc_refs(urlencode($_SERVER['REMOTE_ADDR'])), $_url)) { if ($_url = trim(preg_replace('/%%(.+?)%%/i', '', $_url))) { if (!($_r = 0) && ($_url = preg_replace('/^silent-php\\|/i', '', $_url, 1, $_r)) && $_r && in_array('affiliates-silent-post', $process)) { c_ws_plugin__s2member_utils_urls::remote($_url, FALSE, array('blocking' => FALSE)); } else { if (!($_r = 0) && ($_url = preg_replace('/^img-1px\\|/i', '', $_url, 1, $_r)) && $_r && in_array('affiliates-1px-response', $process)) { if (!empty($response) && $return === 'response') { // Now, we MUST also have a `$response`, and MUST be returning `$response`. $response .= "\n" . '<img src="' . esc_attr($_url) . '" style="width:0; height:0; border:0;" alt="" />'; } } } } } } } } } } unset($_url, $_r); // Just a little housekeeping here. Unset these variables. } } else { $response = _x('<div>Sorry, your discount code is N/A, invalid or expired.</div>', 's2member-front', 's2member'); } } $attr['_coupon_applies'] = isset($coupon_applies) && $coupon_applies ? '1' : '0'; $attr['_coupon_code'] = isset($coupon_applies, $coupon_code) && $coupon_applies ? $coupon_code : ''; $attr['_full_coupon_code'] = isset($coupon_applies, $full_coupon_code) && $coupon_applies ? $full_coupon_code : ''; $attr['_coupon_affiliate_id'] = isset($coupon_applies, $affiliate_id) && $coupon_applies && empty($_COOKIE['idev']) ? $affiliate_id : ''; return $return === 'response' ? !empty($response) ? $response : '' : $attr; }