Skip to content

Commit

Permalink
Ability to update usage counters. See: wpsharks/s2member#285, wpshark…
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswsinc committed Jan 28, 2015
1 parent 23e25b9 commit 3d4eb2d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 17 additions & 5 deletions s2member-pro/includes/classes/coupons.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class c_ws_plugin__s2member_pro_coupons

public function __construct($args = array())
{
$default_args = array(); // Defaults.
$default_args = array('update' => TRUE); // Defaults.
$args = array_merge($default_args, (array)$args);
$args = array_intersect_key($args, $default_args);

$this->list_to_coupons($GLOBALS['WS_PLUGIN__']['s2member']['o']['pro_coupon_codes']);
$this->list_to_coupons($GLOBALS['WS_PLUGIN__']['s2member']['o']['pro_coupon_codes'], $args['update']);
}

public function list_to_coupons($list, $update = TRUE)
Expand Down Expand Up @@ -106,6 +106,9 @@ public function list_to_coupons($list, $update = TRUE)

$_coupon['max_uses'] = !empty($_coupon_parts[6]) ? (integer)$_coupon_parts[6] : 0;

if(strpos((string)$update, 'counters') !== FALSE && isset($_coupon_parts[7]))
$this->update_uses($_coupon['code'], $_coupon_parts[7]);

$_coupon['is_gift'] = FALSE; // Hard-coded coupons are never gifts.

$coupons[$_coupon['code']] = $_coupon; // Add this coupon to the array now.
Expand Down Expand Up @@ -550,16 +553,16 @@ public function get_uses($coupon_code)
return (integer)get_option($this->uses_option_key($coupon_code));
}

public function update_uses($coupon_code)
public function update_uses($coupon_code, $to = NULL)
{
if(!($coupon_code = trim((string)$coupon_code)))
return; // Not possible.

$uses_option_key = $this->uses_option_key($coupon_code);

if(($current_uses = get_option($uses_option_key)) === FALSE)
add_option($uses_option_key, 1, '', 'no');
else update_option($uses_option_key, $current_uses + 1);
add_option($uses_option_key, isset($to) ? (integer)$to : 1, '', 'no');
else update_option($uses_option_key, isset($to) ? (integer)$to : $current_uses + 1);
}

public function delete_uses($coupon_code)
Expand Down Expand Up @@ -601,5 +604,14 @@ public function n_code($coupon_code)

return strtoupper(preg_replace('/\-+/', '', $coupon_code));
}

public static function after_update_all_options()
{
if(is_admin() && !empty($_REQUEST['page']) && $_REQUEST['page'] === 'ws-plugin--s2member-pro-coupon-codes' && !empty($_POST['ws_plugin__s2member_options_save']))
{
$coupons = new c_ws_plugin__s2member_pro_coupons(array('update' => 'counters'));
$GLOBALS['WS_PLUGIN__']['s2member']['o']['pro_coupon_codes'] = $coupons->list;
}
}
}
}
2 changes: 2 additions & 0 deletions s2member-pro/includes/hooks.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
add_action('ws_plugin__s2member_during_scripting_page_during_left_sections_during_list_of_api_constants', 'c_ws_plugin__s2member_pro_menu_pages::scripting_page_api_constants');
add_action('ws_plugin__s2member_during_scripting_page_during_left_sections_during_list_of_api_constants_farm', 'c_ws_plugin__s2member_pro_menu_pages::scripting_page_api_constants');

add_action('ws_plugin__s2member_after_update_all_options', 'c_ws_plugin__s2member_pro_coupons::after_update_all_options');

add_filter('ws_plugin__s2member_recaptcha_keys', 'c_ws_plugin__s2member_pro_utils_captchas::recaptcha_keys', 10, 2);

add_filter('ws_plugin__s2member_login_redirect', 'c_ws_plugin__s2member_pro_login_redirects::login_redirect', 11, 2);
Expand Down

0 comments on commit 3d4eb2d

Please sign in to comment.