function handle_coupons_updates()
 {
     global $action, $page;
     wp_reset_vars(array('action', 'page'));
     if (isset($_GET['doaction']) || isset($_GET['doaction2'])) {
         if (isset($_GET['action']) && addslashes($_GET['action']) == 'delete' || isset($_GET['action2']) && addslashes($_GET['action2']) == 'delete') {
             $action = 'bulk-delete';
         }
     }
     switch (addslashes($action)) {
         case 'removeheader':
             $this->dismiss_user_help($page);
             wp_safe_redirect(remove_query_arg('action'));
             break;
         case 'added':
             $id = (int) $_POST['ID'];
             check_admin_referer('add-coupon');
             if (!$id) {
                 $coupon = new M_Coupon($id);
                 $errors = $coupon->add($_POST);
                 if ($errors !== true) {
                     wp_safe_redirect(add_query_arg('msg', 1, 'admin.php?page=' . $page));
                 } else {
                     //
                     //wp_safe_redirect( add_query_arg( 'msg', 4, 'admin.php?page=' . $page ) );
                 }
             } else {
                 wp_safe_redirect(add_query_arg('msg', 4, 'admin.php?page=' . $page));
             }
             break;
         case 'updated':
             $id = (int) $_POST['ID'];
             check_admin_referer('update-coupon_' . $id);
             if ($id) {
                 $coupon = new M_Coupon($id);
                 $errors = $coupon->update($_POST);
                 if ($errors !== true) {
                     wp_safe_redirect(add_query_arg('msg', 3, 'admin.php?page=' . $page));
                 } else {
                     wp_safe_redirect(add_query_arg('msg', 5, 'admin.php?page=' . $page));
                 }
             } else {
                 wp_safe_redirect(add_query_arg('msg', 5, 'admin.php?page=' . $page));
             }
             break;
         case 'delete':
             if (isset($_GET['coupon_id'])) {
                 $coupon_id = (int) $_GET['coupon_id'];
                 check_admin_referer('delete-coupon_' . $coupon_id);
                 $coupon = new M_Coupon($coupon_id);
                 if ($coupon->delete()) {
                     wp_safe_redirect(add_query_arg('msg', 5, wp_get_referer()));
                 } else {
                     wp_safe_redirect(add_query_arg('msg', 6, wp_get_referer()));
                 }
             }
             break;
         case 'bulk-delete':
             check_admin_referer('bulk-coupon-actions');
             foreach ($_GET['coupons_checks'] as $value) {
                 if (is_numeric($value)) {
                     $coupon_id = (int) $value;
                     $coupon = new M_Coupon($coupon_id);
                     $coupon->delete($coupon_id);
                 }
             }
             wp_safe_redirect(add_query_arg('msg', 7, wp_get_referer()));
             exit;
             break;
     }
 }
Example #2
0
 function create_subscription($sub_id, $gateway = 'admin')
 {
     global $blog_id;
     if (!$this->active_member()) {
         $this->toggle_activation();
     }
     $subscription = new M_Subscription($sub_id);
     $levels = $subscription->get_levels();
     if (!empty($levels)) {
         foreach ($levels as $key => $level) {
             if ($level->level_order == 1) {
                 $this->add_subscription($sub_id, $level->level_id, $level->level_order, $gateway);
                 // Check if a coupon transient already exists
                 if (defined('MEMBERSHIP_GLOBAL_TABLES') && MEMBERSHIP_GLOBAL_TABLES === true) {
                     if (function_exists('get_site_transient')) {
                         $trying = get_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                     } else {
                         $trying = get_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                     }
                 } else {
                     $trying = get_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                 }
                 // If there is a coupon transient do our coupon count magic
                 if ($trying != false && is_array($trying)) {
                     if (!empty($trying['coupon_id'])) {
                         $coupon = new M_Coupon($trying['coupon_id']);
                         // Add one to the coupon count
                         $coupon->increment_coupon_used();
                         // Store the coupon details in the usermeta
                         update_user_meta($this->ID, 'm_coupon_' . $sub_id, $trying);
                     }
                     if (defined('MEMBERSHIP_GLOBAL_TABLES') && MEMBERSHIP_GLOBAL_TABLES === true) {
                         if (function_exists('delete_site_transient')) {
                             delete_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                         } else {
                             delete_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                         }
                     } else {
                         delete_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                     }
                 }
                 break;
             }
         }
         return true;
     } else {
         return false;
     }
 }
Example #3
0
 function popover_sendpayment_form($user_id = false)
 {
     global $M_options;
     $sub = $to_sub_id = false;
     $logged_in = is_user_logged_in();
     $subscription = isset($_REQUEST['subscription']) ? $_REQUEST['subscription'] : 0;
     // free subscription processing
     if ($logged_in && $subscription) {
         $sub = Membership_Plugin::factory()->get_subscription($subscription);
         if ($sub->is_free()) {
             $to_sub_id = $subscription;
         }
     }
     // coupon processing
     $coupon = filter_input(INPUT_POST, 'coupon_code');
     if ($logged_in && $coupon && $subscription) {
         $coupon = new M_Coupon($coupon);
         $coupon_obj = $coupon->get_coupon();
         if ($coupon->valid_coupon() && $coupon_obj->discount >= 100 && $coupon_obj->discount_type == 'pct') {
             $to_sub_id = $subscription;
             $coupon->increment_coupon_used();
         }
     }
     if ($to_sub_id) {
         $membership = Membership_Plugin::factory()->get_member(get_current_user_id());
         $membership->create_subscription($to_sub_id);
         if (!empty($M_options['registrationcompleted_message'])) {
             $html = '<div class="header"><h1>';
             $html .= sprintf(__('Subscription %s has been added.', 'membership'), $sub ? $sub->sub_name() : '');
             $html .= '</h1></div><div class="fullwidth">';
             $html .= stripslashes(wpautop($M_options['registrationcompleted_message']));
             $html .= '<a class="button button-primary ' . esc_attr(apply_filters('membership_subscription_button_color', '')) . '" href="' . M_get_account_permalink() . '">' . __('Go to your account', 'membership') . '</a>';
             $html .= '</div>';
             echo $html;
         } else {
             wp_send_json(array('redirect' => strpos(home_url(), 'https://') === 0 ? str_replace('https:', 'http:', M_get_registrationcompleted_permalink()) : M_get_registrationcompleted_permalink()));
         }
         exit;
     }
     // render template
     ob_start();
     echo apply_filters('membership_popover_sendpayment_form_before_content', '');
     if (defined('MEMBERSHIP_POPOVER_SENDPAYMENT_FORM') && is_readable(MEMBERSHIP_POPOVER_SENDPAYMENT_FORM)) {
         include MEMBERSHIP_POPOVER_SENDPAYMENT_FORM;
     } else {
         $filename = apply_filters('membership_override_popover_sendpayment_form', membership_dir('membershipincludes/includes/popover_payment.form.php'));
         if (is_readable($filename)) {
             include $filename;
         }
     }
     echo apply_filters('membership_popover_sendpayment_form_after_content', ob_get_clean());
     exit;
 }
Example #4
0
 function create_subscription($sub_id, $gateway = 'admin')
 {
     if (!$this->active_member()) {
         $this->toggle_activation();
     }
     $subscription = new M_Subscription($sub_id);
     $levels = $subscription->get_levels();
     if (function_exists('is_multisite') && is_multisite()) {
         global $blog_id;
     }
     if (!empty($levels)) {
         foreach ($levels as $key => $level) {
             if ($level->level_order == 1) {
                 $this->add_subscription($sub_id, $level->level_id, $level->level_order, $gateway);
                 // Check for a coupon transient
                 if (function_exists('is_multisite') && is_multisite()) {
                     $trans = get_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                 } else {
                     $trans = get_transient('m_coupon_' . $this->ID . '_' . $sub_id);
                 }
                 // If there is a coupon transient do our coupon count magic
                 if ($trans && is_array($trans)) {
                     $code = strtoupper($code);
                     $coupon = new M_Coupon($trans['code']);
                     $price = $coupon->apply_price($level->level_price);
                     $coupon_data = $coupon->get_coupon(true);
                     $coupon_data['ID'] = $coupon_data['id'];
                     unset($coupon_data['id']);
                     // Ok they for sure used the coupon so lets delete the transient
                     if (function_exists('is_multisite') && is_multisite()) {
                         delete_site_transient('m_coupon_' . $blog_id . '_' . $this->ID . '_' . $sub_id);
                     } else {
                         delete_transient('m_coupon_' . $this->ID . '_' . $sub_id);
                     }
                     // Now lets just add a use to the coupon it self and daz it...
                     $new_total = (int) $coupon_data['coupon_used'] + 1;
                     $coupon_data['coupon_used'] = $new_total;
                     $coupon->increment_coupon_used();
                 }
                 break;
             }
         }
         return true;
     } else {
         return false;
     }
 }
 function process_subscription_form()
 {
     global $M_options, $bp;
     $logged_in = is_user_logged_in();
     $subscription = isset($_REQUEST['subscription']) ? $_REQUEST['subscription'] : 0;
     $page = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'subscriptionform';
     switch ($page) {
         case 'validatepage1':
             if ($_SERVER['REQUEST_METHOD'] != 'POST') {
                 return;
             }
             $required = array('user_login' => __('Username', 'membership'), 'user_email' => __('Email address', 'membership'), 'password' => __('Password', 'membership'), 'password2' => __('Password confirmation', 'membership'));
             $this->_register_errors = new WP_Error();
             foreach ($required as $key => $message) {
                 if (empty($_POST[$key])) {
                     $this->_register_errors->add($key, __('Please ensure that the ', 'membership') . "<strong>" . $message . "</strong>" . __(' information is completed.', 'membership'));
                 }
             }
             if ($_POST['password'] != $_POST['password2']) {
                 $this->_register_errors->add('passmatch', __('Please ensure the passwords match.', 'membership'));
             }
             if (!validate_username($_POST['user_login'])) {
                 $this->_register_errors->add('usernamenotvalid', __('The username is not valid, sorry.', 'membership'));
             }
             if (username_exists(sanitize_user($_POST['user_login']))) {
                 $this->_register_errors->add('usernameexists', __('That username is already taken, sorry.', 'membership'));
             }
             if (!is_email($_POST['user_email'])) {
                 $this->_register_errors->add('emailnotvalid', __('The email address is not valid, sorry.', 'membership'));
             }
             if (email_exists($_POST['user_email'])) {
                 $this->_register_errors->add('emailexists', __('That email address is already taken, sorry.', 'membership'));
             }
             $this->_register_errors = apply_filters('membership_subscription_form_before_registration_process', $this->_register_errors);
             $result = apply_filters('wpmu_validate_user_signup', array('user_name' => $_POST['user_login'], 'orig_username' => $_POST['user_login'], 'user_email' => $_POST['user_email'], 'errors' => $this->_register_errors));
             $this->_register_errors = $result['errors'];
             // Hack for now - eeek
             $anyerrors = $this->_register_errors->get_error_code();
             if (empty($anyerrors)) {
                 // No errors so far - error reporting check for final add user *note $error should always be an error object becuase we created it as such.
                 $user_id = wp_create_user(sanitize_user($_POST['user_login']), $_POST['password'], $_POST['user_email']);
                 if (is_wp_error($user_id)) {
                     $this->_register_errors->add('userid', $user_id->get_error_message());
                 } else {
                     $member = Membership_Plugin::factory()->get_member($user_id);
                     if (!headers_sent()) {
                         $user = @wp_signon(array('user_login' => $_POST['user_login'], 'user_password' => $_POST['password'], 'remember' => true));
                         if (is_wp_error($user) && method_exists($user, 'get_error_message')) {
                             $this->_register_errors->add('userlogin', $user->get_error_message());
                         } else {
                             // Set the current user up
                             wp_set_current_user($user_id);
                         }
                     } else {
                         // Set the current user up
                         wp_set_current_user($user_id);
                     }
                     if (has_action('membership_susbcription_form_registration_notification')) {
                         do_action('membership_susbcription_form_registration_notification', $user_id, $_POST['password']);
                     } else {
                         wp_new_user_notification($user_id, $_POST['password']);
                     }
                     if (!empty($M_options['freeusersubscription'])) {
                         $level = !empty($M_options['strangerlevel']) ? $M_options['strangerlevel'] : 0;
                         //free subscription is active - do 'membership_add_subscription' action so pings are triggered, etc
                         do_action('membership_add_subscription', $M_options['freeusersubscription'], $level, false, $user_id);
                     }
                 }
                 do_action('membership_subscription_form_registration_process', $this->_register_errors, $user_id);
             } else {
                 do_action('membership_subscription_form_registration_process', $this->_register_errors, 0);
             }
             // Hack for now - eeek
             $anyerrors = $this->_register_errors->get_error_code();
             if (empty($anyerrors)) {
                 // redirect to payments page
                 wp_redirect(esc_url_raw(add_query_arg(array('action' => 'subscriptionsignup', 'subscription' => $subscription))));
                 exit;
             }
             break;
         case 'validatepage1bp':
             if ($_SERVER['REQUEST_METHOD'] != 'POST') {
                 return;
             }
             $required = array('signup_username' => __('Username', 'membership'), 'signup_email' => __('Email address', 'membership'), 'signup_password' => __('Password', 'membership'), 'signup_password_confirm' => __('Password confirmation', 'membership'));
             $this->_register_errors = new WP_Error();
             foreach ($required as $key => $message) {
                 if (empty($_POST[$key])) {
                     $this->_register_errors->add($key, __('Please ensure that the ', 'membership') . "<strong>" . $message . "</strong>" . __(' information is completed.', 'membership'));
                 }
             }
             if ($_POST['signup_password'] != $_POST['signup_password_confirm']) {
                 $this->_register_errors->add('passmatch', __('Please ensure the passwords match.', 'membership'));
             }
             if (!validate_username($_POST['signup_username'])) {
                 $this->_register_errors->add('usernamenotvalid', __('The username is not valid, sorry.', 'membership'));
             }
             if (username_exists(sanitize_user($_POST['signup_username']))) {
                 $this->_register_errors->add('usernameexists', __('That username is already taken, sorry.', 'membership'));
             }
             if (!is_email($_POST['signup_email'])) {
                 $this->_register_errors->add('emailnotvalid', __('The email address is not valid, sorry.', 'membership'));
             }
             if (email_exists($_POST['signup_email'])) {
                 $this->_register_errors->add('emailexists', __('That email address is already taken, sorry.', 'membership'));
             }
             // Initial fix provided by user: cmurtagh - modified to add extra checks and rejigged a bit
             // Run the buddypress validation
             do_action('bp_signup_validate');
             // Add any errors to the action for the field in the template for display.
             if (!empty($bp->signup->errors)) {
                 foreach ((array) $bp->signup->errors as $fieldname => $error_message) {
                     $this->_register_errors->add($fieldname, $error_message);
                 }
             }
             $meta_array = array();
             // xprofile required fields
             /* Now we've checked account details, we can check profile information */
             //if ( function_exists( 'xprofile_check_is_required_field' ) ) {
             if (function_exists('bp_is_active') && bp_is_active('xprofile')) {
                 /* Make sure hidden field is passed and populated */
                 if (isset($_POST['signup_profile_field_ids']) && !empty($_POST['signup_profile_field_ids'])) {
                     /* Let's compact any profile field info into an array */
                     $profile_field_ids = explode(',', $_POST['signup_profile_field_ids']);
                     /* Loop through the posted fields formatting any datebox values then validate the field */
                     foreach ((array) $profile_field_ids as $field_id) {
                         if (!isset($_POST['field_' . $field_id])) {
                             if (isset($_POST['field_' . $field_id . '_day'])) {
                                 $_POST['field_' . $field_id] = strtotime($_POST['field_' . $field_id . '_day'] . $_POST['field_' . $field_id . '_month'] . $_POST['field_' . $field_id . '_year']);
                             }
                         }
                         /* Create errors for required fields without values */
                         if (xprofile_check_is_required_field($field_id) && empty($_POST['field_' . $field_id])) {
                             $field = new BP_Xprofile_Field($field_id);
                             $this->_register_errors->add($field->name, __('Please ensure that the ', 'membership') . "<strong>" . $field->name . "</strong>" . __(' information is completed.', 'membership'));
                         }
                         $meta_array[$field_id] = $_POST['field_' . $field_id];
                     }
                 }
             }
             $this->_register_errors = apply_filters('membership_subscription_form_before_registration_process', $this->_register_errors);
             // Hack for now - eeek
             $anyerrors = $this->_register_errors->get_error_code();
             if (empty($anyerrors)) {
                 // No errors so far - error reporting check for final add user *note $error should always be an error object becuase we created it as such.
                 $user_id = wp_create_user(sanitize_user($_POST['signup_username']), $_POST['signup_password'], $_POST['signup_email']);
                 if (is_wp_error($user_id)) {
                     $this->_register_errors->add('userid', $user_id->get_error_message());
                 } else {
                     $member = Membership_Plugin::factory()->get_member($user_id);
                     if (!headers_sent()) {
                         $user = @wp_signon(array('user_login' => $_POST['signup_username'], 'user_password' => $_POST['signup_password'], 'remember' => true));
                         if (is_wp_error($user) && method_exists($user, 'get_error_message')) {
                             $this->_register_errors->add('userlogin', $user->get_error_message());
                         } else {
                             // Set the current user up
                             wp_set_current_user($user_id);
                         }
                     } else {
                         // Set the current user up
                         wp_set_current_user($user_id);
                     }
                     if (has_action('membership_susbcription_form_registration_notification')) {
                         do_action('membership_susbcription_form_registration_notification', $user_id, $_POST['signup_password']);
                     } else {
                         wp_new_user_notification($user_id, $_POST['signup_password']);
                     }
                     if (function_exists('xprofile_set_field_data')) {
                         // Add the bp filter for usermeta signup
                         $meta_array = apply_filters('bp_signup_usermeta', $meta_array);
                         foreach ((array) $meta_array as $field_id => $field_content) {
                             xprofile_set_field_data($field_id, $user_id, $field_content);
                             $visibility_level = !empty($_POST['field_' . $field_id . '_visibility']) ? $_POST['field_' . $field_id . '_visibility'] : 'public';
                             xprofile_set_field_visibility_level($field_id, $user_id, $visibility_level);
                         }
                         // Make sure the User Meta is updated with the xprofile name
                         $data = explode(' ', xprofile_get_field_data('Name', $user_id, 'array'));
                         $firstname = array_shift($data);
                         $lastname = implode(' ', $data);
                         update_user_meta($user_id, 'first_name', $firstname);
                         update_user_meta($user_id, 'last_name', $lastname);
                     }
                 }
                 do_action('membership_subscription_form_registration_process', $this->_register_errors, $user_id);
                 // Hack for now - eeek
                 $anyerrors = $this->_register_errors->get_error_code();
                 if (empty($anyerrors)) {
                     // everything seems fine (so far), so we have our queued user so let's
                     // run the bp complete signup action
                     do_action('bp_complete_signup');
                     // redirect to payments page
                     wp_redirect(esc_url_raw(add_query_arg(array('action' => 'subscriptionsignup', 'subscription' => $subscription))));
                     exit;
                 }
             } else {
                 do_action('membership_subscription_form_registration_process', $this->_register_errors, 0);
             }
             break;
         case 'registeruser':
         case 'subscriptionsignup':
             $to_sub_id = false;
             // free subscription processing
             if ($logged_in && $subscription) {
                 $sub = Membership_Plugin::factory()->get_subscription($subscription);
                 if ($sub->is_free()) {
                     $to_sub_id = $subscription;
                 }
             }
             // coupon processing
             $coupon = filter_input(INPUT_POST, 'coupon_code');
             $sub_id = filter_input(INPUT_POST, 'coupon_sub_id', FILTER_VALIDATE_INT);
             if ($logged_in && $coupon && $sub_id) {
                 $coupon = new M_Coupon($coupon);
                 $coupon_obj = $coupon->get_coupon();
                 //if ( $coupon->valid_coupon() && $coupon_obj->discount >= 100 && $coupon_obj->discount_type == 'pct' ) {
                 if ($coupon->valid_for_subscription($sub_id) && $coupon_obj->discount >= 100 && $coupon_obj->discount_type == 'pct') {
                     $to_sub_id = $sub_id;
                     $coupon->increment_coupon_used();
                 }
             }
             if ($to_sub_id) {
                 $member = Membership_Plugin::factory()->get_member(get_current_user_id());
                 $from_sub_id = isset($_REQUEST['from_subscription']) ? absint($_REQUEST['from_subscription']) : 0;
                 if ($from_sub_id) {
                     $member->drop_subscription($from_sub_id);
                 }
                 $member->create_subscription($to_sub_id);
                 if (isset($M_options['registrationcompleted_page']) && absint($M_options['registrationcompleted_page'])) {
                     wp_redirect(get_permalink($M_options['registrationcompleted_page']));
                     exit;
                 }
             }
             break;
     }
 }
Example #6
0
 function apply_coupon_pricing($coupon_code = false, $pricing = false)
 {
     if ($coupon_code === false || $pricing === false) {
         return false;
     }
     $coupon_code = strtoupper($coupon_code);
     $coupon = new M_Coupon($coupon_code);
     $coupon_data = $coupon->get_coupon(true);
     if ((int) $coupon_data['coupon_used'] >= (int) $coupon_data['coupon_uses'] || strtotime($coupon_data['coupon_enddate']) < time()) {
         $this->coupon_label = __('The coupon you supplied is either invalid or expired.', 'membership');
         return $pricing;
     }
     // We should always have a user_id at this point so we are going to
     // create a transient to help us log when a coupon is used.
     $user = wp_get_current_user();
     $trans = array('code' => $coupon_code, 'user_id' => $user->ID, 'sub_id' => $this->id, 'prices_w_coupon' => array());
     foreach ($pricing as $key => $value) {
         // For every possible price they could have paid we put the total into the transient to check if the coupon was set and never used
         $pricing[$key]['amount'] = $coupon->apply_price($value['amount']);
         $trans['prices_w_coupon'][$key] = $coupon->apply_price($value['amount']);
         $this->coupon_label = $coupon->coupon_label;
     }
     if (function_exists('is_multisite') && is_multisite()) {
         global $blog_id;
     }
     // Check if a transient already exists and delete it if it does
     if (function_exists('is_multisite') && is_multisite()) {
         if (get_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id)) {
             delete_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id);
         }
     } else {
         if (get_transient('m_coupon_' . $user->ID . '_' . $this->id)) {
             delete_transient('m_coupon_' . $user->ID . '_' . $this->id);
         }
     }
     // Create transient for 1 hour.  This means the user has 1 hour to redeem the coupon after its been applied before it goes back into the pool.
     // If you want to use a different time limit use the filter below
     $time = apply_filters('membership_apply_coupon_redemption_time', 60 * 60);
     if (function_exists('is_multisite') && is_multisite()) {
         set_site_transient('m_coupon_' . $blog_id . '_' . $user->ID . '_' . $this->id, $trans, $time);
     } else {
         set_transient('m_coupon_' . $user->ID . '_' . $this->id, $trans, $time);
     }
     return apply_filters('membership_apply_coupon_pricingarray', $pricing, $coupon_code);
 }