| 1 |
<?php |
| 2 |
// @codingStandardsIgnoreFile |
| 3 |
/** |
| 4 |
* Shortcode `[s2Member-PayPal-Button]` (encryption sub-routines). |
| 5 |
* |
| 6 |
* Copyright: © 2009-2011 |
| 7 |
* {@link http://websharks-inc.com/ WebSharks, Inc.} |
| 8 |
* (coded in the USA) |
| 9 |
* |
| 10 |
* Released under the terms of the GNU General Public License. |
| 11 |
* You should have received a copy of the GNU General Public License, |
| 12 |
* along with this software. In the main directory, see: /licensing/ |
| 13 |
* If not, see: {@link http://www.gnu.org/licenses/}. |
| 14 |
* |
| 15 |
* @package s2Member\PayPal |
| 16 |
* @since 3.5 |
| 17 |
*/ |
| 18 |
if(!defined('WPINC')) // MUST have WordPress. |
| 19 |
exit("Do not access this file directly."); |
| 20 |
|
| 21 |
if (!class_exists ("c_ws_plugin__s2member_sc_paypal_button_e")) |
| 22 |
{ |
| 23 |
/** |
| 24 |
* Shortcode `[s2Member-PayPal-Button]` (encryption sub-routines). |
| 25 |
* |
| 26 |
* @package s2Member\PayPal |
| 27 |
* @since 3.5 |
| 28 |
*/ |
| 29 |
class c_ws_plugin__s2member_sc_paypal_button_e |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Handles PayPal Button encryption. |
| 33 |
* |
| 34 |
* This uses the PayPal API. s2Member will NOT attempt to encrypt Buttons until there is at least a Business Email Address and API Username configured. |
| 35 |
* s2Member also maintains a log of communication with the PayPal API. If logging is enabled, check: `/wp-content/plugins/s2member-logs/paypal-api.log`. |
| 36 |
* |
| 37 |
* @package s2Member\PayPal |
| 38 |
* @since 3.5 |
| 39 |
* |
| 40 |
* @param string $code The PayPal Button Code before encryption. |
| 41 |
* @param array $vars An array of defined variables in the scope of the calling Filter. |
| 42 |
* @return string The Resulting PayPal Button Code *(possibly encrypted, depending on configuration)*. |
| 43 |
*/ |
| 44 |
public static function sc_paypal_button_encryption ($code = FALSE, $vars = FALSE) |
| 45 |
{ |
| 46 |
foreach(array_keys(get_defined_vars())as$__v)$__refs[$__v]=&$$__v; |
| 47 |
do_action("ws_plugin__s2member_before_sc_paypal_button_encryption", get_defined_vars ()); |
| 48 |
unset($__refs, $__v); |
| 49 |
|
| 50 |
if ($GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_btn_encryption"] |
| 51 |
&& $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_business"] |
| 52 |
&& $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_username"] |
| 53 |
&& $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_password"] |
| 54 |
&& $GLOBALS["WS_PLUGIN__"]["s2member"]["o"]["paypal_api_signature"] |
| 55 |
) // Only if it is possible to connect to the PayPal API. |
| 56 |
{ |
| 57 |
$cache = /* Are we caching? */ apply_filters("ws_plugin__s2member_sc_paypal_button_encryption_cache", true, get_defined_vars ()); |
| 58 |
|
| 59 |
$_code = $vars["_code"]; $attr = $vars["attr"]; // Let's unpack (i.e., use shorter references) to these two important vars. |
| 60 |
|
| 61 |
if ($cache && ($transient = "s2m_btn_" . md5 ($code . c_ws_plugin__s2member_utilities::ver_checksum ())) && ($cache = get_transient ($transient))) |
| 62 |
$code = /* Great, so we can use the cached version here to save processing time. The MD5 hash uses ``$code`` and NOT ``$_code``. */ $cache; |
| 63 |
|
| 64 |
else if /* Are we able to parse hidden input variables? */ (is_array($inputs = c_ws_plugin__s2member_utils_forms::form_whips_2_array($_code)) && !empty($inputs)) |
| 65 |
{ |
| 66 |
$paypal = array("METHOD" => "BMCreateButton", "BUTTONCODE" => "ENCRYPTED", "BUTTONTYPE" => (($attr["sp"] || $attr["rr"] === "BN") ? "BUYNOW" : "SUBSCRIBE")); |
| 67 |
|
| 68 |
$i = 0; // Initialize incremental variable counter. PayPal wants these numbered using L_BUTTONVAR{n}; where {n} starts at zero. |
| 69 |
foreach ($inputs as $input => $value) // Now run through each of the input variables that we parsed from the Full Button Code |
| 70 |
if (!preg_match ("/^cmd$/i", $input)) // Don't include the `cmd` var; it will produce major errors in the API response. |
| 71 |
{ |
| 72 |
// The PayPal API method `BMCreateButton` expects (amount|a1|a3) to include 2 decimal places. |
| 73 |
if (preg_match ("/^(amount|a1|a3)$/i", $input)) |
| 74 |
$value = number_format ($value, 2, ".", ""); |
| 75 |
|
| 76 |
$paypal["L_BUTTONVAR" . $i] = $input . "=" . $value; |
| 77 |
$i++; // Increment variable counter. |
| 78 |
} |
| 79 |
|
| 80 |
if (($paypal = c_ws_plugin__s2member_paypal_utilities::paypal_api_response ($paypal)) && empty($paypal["__error"]) && !empty($paypal["WEBSITECODE"]) && ($code = $paypal["WEBSITECODE"])) |
| 81 |
// Only proceed if we DID get a valid response from the PayPal API. This works as a nice fallback; just in case the API connection fails. |
| 82 |
{ |
| 83 |
$default_image = "https://www.paypal.com/" . (($attr["lang"]) ? $attr["lang"] : _x ("en_US", "s2member-front paypal-button-lang-code", "s2member")) . "/i/btn/btn_xpressCheckout.gif"; |
| 84 |
|
| 85 |
$code = preg_replace ("/\<img[^\>]+\>/i", "", $code); // Remove 1x1 pixel tracking image that PayPal sticks in there. |
| 86 |
$code = preg_replace_callback ("/(\<input)([^\>]+)(\>)/i", "c_ws_plugin__s2member_sc_paypal_button_e::_sc_paypal_button_encryption_preg_replace_xhtml_callback", $code); |
| 87 |
|
| 88 |
$code = ($attr["image"] && $attr["image"] !== "default") ? preg_replace ('/ src\="(.*?)"/', ' src="' . c_ws_plugin__s2member_utils_strings::esc_refs (esc_attr ($attr["image"])) . '"', $code) : preg_replace ('/ src\="(.*?)"/', ' src="' . c_ws_plugin__s2member_utils_strings::esc_refs (esc_attr ($default_image)) . '"', $code); |
| 89 |
|
| 90 |
$code = ($attr["output"] === "anchor") ? '<a href="' . esc_attr (c_ws_plugin__s2member_utils_forms::form_whips_2_url ($code)) . '"><img src="' . esc_attr (($attr["image"] && $attr["image"] !== "default") ? $attr["image"] : $default_image) . '" style="width:auto; height:auto; border:0;" alt="PayPal" /></a>' : $code; |
| 91 |
$code = ($attr["output"] === "url") ? c_ws_plugin__s2member_utils_forms::form_whips_2_url ($code) : $code; |
| 92 |
|
| 93 |
($cache && $transient) ? set_transient ($transient, $code, apply_filters("ws_plugin__s2member_sc_paypal_button_encryption_cache_exp_time", 3600, get_defined_vars ())) : null; // Caching? |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
// No WordPress Filters apply here. |
| 98 |
// Instead, use: `ws_plugin__s2member_sc_paypal_button`. |
| 99 |
return $code; // Button Code. Possibly w/ API encryption now. |
| 100 |
} |
| 101 |
|
| 102 |
public static function _sc_paypal_button_encryption_preg_replace_xhtml_callback($m) |
| 103 |
{ |
| 104 |
return $m[1].rtrim($m[2], " /\t\n\r\0\x0B").' /'.$m[3]; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|