示例#1
1
 /**
  * @param array $process_data
  * @param $blog_id
  * @param $domain
  *
  * @return bool
  */
 public static function process_checkout_form($process_data = array(), $blog_id, $domain)
 {
     global $psts, $current_user, $current_site;
     $site_name = $current_site->site_name;
     $img_base = $psts->plugin_url . 'images/';
     $session_keys = array('new_blog_details', 'upgraded_blog_details', 'COUPON_CODE', 'activation_key');
     foreach ($session_keys as $key) {
         $process_data[$key] = isset($process_data[$key]) ? $process_data[$key] : ProSites_Helper_Session::session($key);
     }
     if (!empty($domain)) {
         $site_name = !empty($_POST['blogname']) ? $_POST['blogname'] : !empty($_POST['signup_email']) ? $_POST['signup_email'] : '';
     }
     //Process Checkout
     if (isset($_POST['cc_stripe_checkout']) && 1 == (int) $_POST['cc_stripe_checkout']) {
         //check for level, if empty don't go ahead and return
         if (empty($_POST['level']) || empty($_POST['period'])) {
             $psts->errors->add('general', __('Please choose your desired level and payment plan.', 'psts'));
             return false;
         } else {
             if (!isset($_POST['stripeToken']) && empty($_POST['wp_password'])) {
                 $psts->errors->add('general', __('There was an error processing your Credit Card with Stripe. Please try again.', 'psts'));
             }
         }
         // TAX Object
         $tax_object = ProSites_Helper_Session::session('tax_object');
         if (empty($tax_object) || empty($tax_object->evidence)) {
             $tax_object = ProSites_Helper_Tax::get_tax_object();
             ProSites_Helper_Session::session('tax_object', $tax_object);
         }
         $evidence_string = ProSites_Helper_Tax::get_evidence_string($tax_object);
         $error = '';
         $success = '';
         $plan = self::get_plan_id($_POST['level'], $_POST['period']);
         $customer_id = '';
         $current_plan_level = 0;
         $activation_key = isset($_POST['activation']) ? $_POST['activation'] : '';
         $email = !empty($_POST['user_email']) ? $_POST['user_email'] : (!empty($_POST['signup_email']) ? $_POST['signup_email'] : (!empty($_POST['blog_email']) ? $_POST['blog_email'] : ''));
         $blog_id = !empty($blog_id) ? $blog_id : isset($_REQUEST['bid']) ? (int) $_REQUEST['bid'] : 0;
         //If there is a blog id, fetch existing customer details (Upgrade)
         if (!empty($blog_id)) {
             $customer_id = self::get_customer_data($blog_id)->customer_id;
             $email = isset($current_user->user_email) ? $current_user->user_email : get_blog_option($blog_id, 'admin_email');
             //Get current plan for the user
             if ($current_plan = self::get_current_plan($blog_id)) {
                 list($current_plan_level, $current_plan_period) = explode('_', $current_plan);
             }
         } else {
             //New Signup
             if (empty($email) && isset($process_data['new_blog_details']) && isset($process_data['new_blog_details']['user_email'])) {
                 $email = $process_data['new_blog_details']['user_email'];
             }
             $customer_id = self::get_customer_data(false, false, $email)->customer_id;
         }
         if (!self::plan_exists($plan)) {
             $psts->errors->add('general', sprintf(__('Stripe plan %1$s does not exist.', 'psts'), $plan));
             return;
         }
         //Create s Stripe profile for the customer with all the available details,
         //If customer already exists, retrieve customer from stripe
         try {
             if (!$customer_id) {
                 try {
                     $customer_args = array('email' => $email, 'description' => sprintf(__('%s user', 'psts'), $site_name), 'card' => $_POST['stripeToken'], 'metadata' => array('domain' => $domain));
                     $user = get_user_by('email', $email);
                     if ($user) {
                         $blog_string = '';
                         $customer_args['metadata']['user'] = $user->user_login;
                         $customer_args['description'] = sprintf(__('%s user - %s ', 'psts'), $site_name, $user->first_name . ' ' . $user->last_name);
                         $user_blogs = get_blogs_of_user($user->ID);
                         foreach ($user_blogs as $user_blog) {
                             $blog_string .= $user_blog->blogname . ', ';
                         }
                         $customer_args['metadata']['blogs'] = $blog_string;
                     }
                     if (!$domain) {
                         unset($customer_args['metadata']['domain']);
                     }
                     $c = Stripe_Customer::create($customer_args);
                 } catch (Exception $e) {
                     $psts->errors->add('general', __('The Stripe customer could not be created. Please try again.', 'psts'));
                     return;
                 }
                 //Update the stripe customer id, this is temporary, will be overridden by subscription or charge id
                 self::set_customer_data($blog_id, $c->id, 'ak_' . $activation_key);
                 $customer_id = $c->id;
                 $new = true;
             } else {
                 // Get a customer if they exist
                 try {
                     $c = Stripe_Customer::retrieve($customer_id);
                 } catch (Exception $e) {
                     $psts->errors->add('general', __('The Stripe customer could not be retrieved. Please try again.', 'psts'));
                     return;
                 }
                 $c->description = sprintf(__('%s user', 'psts'), $site_name);
                 $c->email = $email;
                 $user = get_user_by('email', $email);
                 if ($user) {
                     $blog_string = '';
                     $c->metadata->user = $user->user_login;
                     $c->description = sprintf(__('%s user - %s ', 'psts'), $site_name, $user->first_name . ' ' . $user->last_name);
                     $user_blogs = get_blogs_of_user($user->ID);
                     foreach ($user_blogs as $user_blog) {
                         $blog_string .= $user_blog->blogname . ', ';
                     }
                     $c->metadata->blogs = $blog_string;
                 }
                 $c->save();
                 $new = false;
                 //validate wp password (if applicable)
                 if (!empty($_POST['wp_password']) && !wp_check_password($_POST['wp_password'], $current_user->data->user_pass, $current_user->ID)) {
                     $psts->errors->add('general', __('The password you entered is incorrect.', 'psts'));
                     return;
                 }
             }
             //prepare vars
             $currency = self::currency();
             $amount_off = false;
             $paymentAmount = $initAmount = $psts->get_level_setting($_POST['level'], 'price_' . $_POST['period']);
             $trial_days = $psts->get_setting('trial_days', 0);
             $cp_code = false;
             $setup_fee = (double) $psts->get_setting('setup_fee', 0);
             $has_coupon = isset($process_data['COUPON_CODE']) && ProSites_Helper_Coupons::check_coupon($process_data['COUPON_CODE'], $blog_id, $_POST['level'], $_POST['period'], $domain) ? true : false;
             $has_setup_fee = $psts->has_setup_fee($blog_id, $_POST['level']);
             $recurring = $psts->get_setting('recurring_subscriptions', 1);
             if ($has_setup_fee) {
                 $initAmount = $setup_fee + $paymentAmount;
             }
             if ($has_coupon || $has_setup_fee) {
                 $lifetime = 'once';
                 if ($has_coupon) {
                     //apply coupon
                     $adjusted_values = ProSites_Helper_Coupons::get_adjusted_level_amounts($process_data['COUPON_CODE']);
                     $coupon_obj = ProSites_Helper_Coupons::get_coupon($process_data['COUPON_CODE']);
                     $lifetime = isset($coupon_obj['lifetime']) && 'indefinite' == $coupon_obj['lifetime'] ? 'forever' : 'once';
                     //	$coupon_value = $psts->coupon_value( $process_data['COUPON_CODE'], $paymentAmount );
                     $coupon_value = $adjusted_values[$_POST['level']]['price_' . $_POST['period']];
                     // $amount_off   = $paymentAmount - $coupon_value['new_total'];
                     $amount_off = $paymentAmount - $coupon_value;
                     $initAmount -= $amount_off;
                     $initAmount = 0 > $initAmount ? 0 : $initAmount;
                     // avoid negative
                     $cpn = false;
                     //Create a stripe coupon if it doesn't exists already
                     try {
                         $cpn = Stripe_Coupon::create(array('amount_off' => $amount_off * 100, 'duration' => $lifetime, 'currency' => $currency, 'max_redemptions' => 1));
                     } catch (Exception $e) {
                         $psts->errors->add('general', __('Temporary Stripe coupon could not be generated correctly. Please try again.', 'psts'));
                         return;
                     }
                     $cp_code = $cpn->id;
                 }
                 //Check if it's a recurring subscription
                 if ($recurring) {
                     $recurringAmmount = 'forever' == $lifetime && $has_coupon ? $coupon_value : $paymentAmount;
                     if ($_POST['period'] == 1) {
                         $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first month, then %2$s each month', 'psts'), $psts->format_currency($currency, $initAmount), $psts->format_currency($currency, $recurringAmmount));
                     } else {
                         $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first %2$s month period, then %3$s every %4$s months', 'psts'), $psts->format_currency($currency, $initAmount), $_POST['period'], $psts->format_currency($currency, $recurringAmmount), $_POST['period']);
                     }
                 } else {
                     if (!empty($blog_id)) {
                         $initAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $initAmount);
                     }
                     if ($_POST['period'] == 1) {
                         $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for 1 month', 'psts'), $psts->format_currency($currency, $initAmount));
                     } else {
                         $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for %2$s months', 'psts'), $psts->format_currency($currency, $initAmount), $_POST['period']);
                     }
                 }
             } elseif ($recurring) {
                 if ($_POST['period'] == 1) {
                     $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s each month', 'psts'), $psts->format_currency($currency, $paymentAmount), $currency);
                 } else {
                     $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s every %3$s months', 'psts'), $psts->format_currency($currency, $paymentAmount), $currency, $_POST['period']);
                 }
             } else {
                 //New Signups
                 if (!empty($blog_id)) {
                     $paymentAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $paymentAmount);
                 }
                 if ($_POST['period'] == 1) {
                     $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for 1 month', 'psts'), $psts->format_currency($currency, $paymentAmount));
                 } else {
                     $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for %2$s months', 'psts'), $psts->format_currency($currency, $paymentAmount), $_POST['period']);
                 }
             }
             // Override the Stripe description
             $desc = apply_filters('psts_stripe_checkout_desc', $desc, $_POST['period'], $_POST['level'], $paymentAmount, $initAmount, $blog_id, $domain);
             // Time to process invoices with Stripe
             if ($recurring) {
                 // Recurring subscription
                 // Assign plan to customer
                 $args = array("plan" => $plan, "prorate" => true);
                 // Apply tax?
                 if ($tax_object->apply_tax) {
                     $args['tax_percent'] = $tax_object->tax_rate * 100;
                 }
                 // If there is a coupon, add its reference
                 if ($cp_code) {
                     $args["coupon"] = $cp_code;
                 }
                 // If this is a trial before the subscription starts
                 if ($psts->is_trial_allowed($blog_id)) {
                     if (isset($process_data['new_blog_details']) || !$psts->is_existing($blog_id)) {
                         //customer is new - add trial days
                         $args['trial_end'] = strtotime('+ ' . $trial_days . ' days');
                     } elseif (is_pro_trial($blog_id) && $psts->get_expire($blog_id) > time()) {
                         //customer's trial is still valid - carry over existing expiration date
                         $args['trial_end'] = $psts->get_expire($blog_id);
                     }
                 }
                 // Meta data for `pay before blog` creation
                 $args['metadata'] = array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level']);
                 if ($tax_object->apply_tax) {
                     $args['metadata']['tax_evidence'] = $evidence_string;
                 }
                 if (!$domain) {
                     unset($args['metadata']['domain']);
                 }
                 // new blog
                 if (isset($_POST['activation'])) {
                     $args['metadata']['activation'] = $_POST['activation'];
                 }
                 // Create Stripe Invoice for the setup fee
                 if ($has_setup_fee) {
                     try {
                         $customer_args = array('customer' => $customer_id, 'amount' => $setup_fee * 100, 'currency' => $currency, 'description' => __('One-time setup fee', 'psts'), 'metadata' => array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level'], 'setup_fee' => 'yes'));
                         if ($tax_object->apply_tax) {
                             $customer_args['metadata']['tax_evidence'] = $evidence_string;
                         }
                         if (!$domain) {
                             unset($customer_args['metadata']['domain']);
                         }
                         // new blog
                         if (isset($_POST['activation'])) {
                             $customer_args['metadata']['activation'] = $_POST['activation'];
                         }
                         Stripe_InvoiceItem::create($customer_args);
                     } catch (Exception $e) {
                         wp_mail(get_blog_option($blog_id, 'admin_email'), __('Error charging setup fee. Attention required!', 'psts'), sprintf(__('An error occurred while charging a setup fee of %1$s to Stripe customer %2$s. You will need to manually process this amount.', 'psts'), $psts->format_currency($currency, $setup_fee), $customer_id));
                     }
                 }
                 // Create/update subscription
                 try {
                     $result = false;
                     $sub = false;
                     // Brand new blog...
                     if (empty($blog_id)) {
                         $result = $c->subscriptions->create($args);
                     } else {
                         $customer_data = self::get_customer_data($blog_id);
                         try {
                             $sub = $c->subscriptions->retrieve($customer_data->subscription_id);
                             $sub_id = $sub->id;
                             $prev_plan = $sub->plan->id;
                             $sub->plan = isset($args['plan']) ? $args['plan'] : $sub->plan;
                             $changed_plan = $sub->plan;
                             $sub->prorate = isset($args['prorate']) ? $args['prorate'] : $sub->prorate;
                             if (isset($args['coupon'])) {
                                 $sub->coupon = $args['coupon'];
                             }
                             if (isset($args['trial_end'])) {
                                 $sub->trial_end = $args['trial_end'];
                             }
                             $sub->metadata->period = $args['metadata']['period'];
                             $sub->metadata->level = $args['metadata']['level'];
                             if (isset($args['metadata']['activation'])) {
                                 $sub->metadata->activation = $args['metadata']['activation'];
                             } else {
                                 $activation_key = ProSites_Helper_ProSite::get_activation_key($blog_id);
                                 if (!empty($activation_key)) {
                                     $sub->metadata->activation = $activation_key;
                                 }
                             }
                             $sub->metadata->blog_id = $blog_id;
                             if (isset($args['metadata']['domain'])) {
                                 $sub->metadata->domain = $args['metadata']['domain'];
                             }
                             // Apply tax?
                             if ($tax_object->apply_tax) {
                                 $sub->tax_percent = $tax_object->tax_rate * 100;
                                 $sub->metadata->tax_evidence = $evidence_string;
                             }
                             $sub->save();
                             // As per Stripe API, to charge immediately, apply an invoice now
                             if ($prev_plan != $changed_plan) {
                                 $customer_args = array('customer' => $customer_id, 'subscription' => $sub_id, 'metadata' => array('plan_change' => 'yes'));
                                 $invoice = Stripe_Invoice::create($customer_args);
                                 $invoice = $invoice->pay();
                                 $plan_parts = explode('_', $changed_plan);
                                 $new_period = array_pop($plan_parts);
                                 $new_level = array_pop($plan_parts);
                                 $plan_parts = explode('_', $prev_plan);
                                 $prev_period = array_pop($plan_parts);
                                 $prev_level = array_pop($plan_parts);
                                 $updated = array('render' => true, 'blog_id' => $blog_id, 'level' => $new_level, 'period' => $new_period, 'prev_level' => $prev_level, 'prev_period' => $prev_period);
                                 ProSites_Helper_Session::session('plan_updated', $updated);
                             }
                         } catch (Exception $e) {
                             // Fall through...
                         }
                         // Additional blog
                         if (empty($sub)) {
                             $args['metadata']['blog_id'] = $blog_id;
                             if (!isset($args['metadata']['activation'])) {
                                 $activation_key = ProSites_Helper_ProSite::get_activation_key($blog_id);
                                 if (!empty($activation_key)) {
                                     $args['metadata']['activation'] = $activation_key;
                                 }
                             }
                             $result = $c->subscriptions->create($args);
                         }
                         // This one is now deprecated
                         // $result = $c->updateSubscription( $args );
                     }
                     // Capture success as soon as we can!
                     if (!empty($result)) {
                         $sub_id = $result->id;
                         $plan = $result->plan;
                         $plan_parts = explode('_', $plan->id);
                         $period = array_pop($plan_parts);
                         $level = array_pop($plan_parts);
                         $trial = isset($plan->status) && 'trialing' == $plan->status ? true : false;
                         $expire = $trial ? $plan->trial_end : $result->current_period_end;
                         $result = ProSites_Helper_Registration::activate_blog($activation_key, $trial, $period, $level, $expire);
                         $blog_id = $result['blog_id'];
                         if (isset($process_data['new_blog_details'])) {
                             ProSites_Helper_Session::session(array('new_blog_details', 'blog_id'), $blog_id);
                             ProSites_Helper_Session::session(array('new_blog_details', 'payment_success'), true);
                         } else {
                             ProSites_Helper_Session::session('upgrade_blog_details', array());
                             ProSites_Helper_Session::session(array('upgrade_blog_details', 'blog_id'), $blog_id);
                             ProSites_Helper_Session::session(array('upgrade_blog_details', 'level'), $level);
                             ProSites_Helper_Session::session(array('upgrade_blog_details', 'period'), $period);
                             ProSites_Helper_Session::session(array('upgrade_blog_details', 'payment_success'), true);
                         }
                         self::set_customer_data($blog_id, $customer_id, $sub_id);
                     }
                     if ($current_plan = self::get_current_plan($blog_id)) {
                         list($current_plan_level, $current_plan_period) = explode('_', $current_plan);
                     }
                     //Extend the Blog Subscription
                     $old_expire = $psts->get_expire($blog_id);
                     $new_expire = $old_expire && $old_expire > time() ? $old_expire : false;
                     $psts->extend($blog_id, $_POST['period'], self::get_slug(), $_POST['level'], $initAmount, $new_expire, false);
                     //$psts->email_notification( $blog_id, 'receipt' );
                     if (isset($current_plan_level)) {
                         if ($current_plan_level > $_POST['level']) {
                             $psts->record_stat($blog_id, 'upgrade');
                         } else {
                             $psts->record_stat($blog_id, 'modify');
                         }
                     } else {
                         $psts->record_stat($blog_id, 'signup');
                     }
                     // Update the sub with the new blog id (old subscriptions will update later).
                     if (!empty($blog_id)) {
                         $sub = $c->subscriptions->retrieve($sub_id);
                         $sub->metadata->blog_id = $blog_id;
                         $sub->save();
                     }
                 } catch (Exception $e) {
                     $body = $e->getJsonBody();
                     $error = $body['error'];
                     $psts->errors->add('general', $error['message']);
                     return;
                 }
             } else {
                 // Not a subscription, this is a one of payment, charged for 1 term
                 try {
                     if (!empty($blog_id)) {
                         $initAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $initAmount);
                         //If activation key is empty
                         $activation_key = !empty($activation_key) ? $activation_key : ProSites_Helper_ProSite::get_activation_key($blog_id);
                     }
                     if ($tax_object->apply_tax) {
                         $amount = $initAmount + $initAmount * $tax_object->tax_rate;
                         $desc += sprintf(__('(includes tax of %s%% [%s])', 'psts'), $tax_object->tax_rate * 100, $tax_object->country);
                     } else {
                         $amount = $initAmount;
                     }
                     $customer_args = array('customer' => $customer_id, 'amount' => $amount * 100, 'currency' => $currency, 'description' => $desc, 'metadata' => array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level']));
                     if ($tax_object->apply_tax) {
                         $customer_args['metadata']['tax_evidence'] = $evidence_string;
                     }
                     if (!$domain) {
                         unset($customer_args['metadata']['domain']);
                     }
                     // new blog
                     if (isset($_POST['activation'])) {
                         $customer_args['metadata']['activation'] = $_POST['activation'];
                     }
                     /**
                      * 1 off charge of not trialing, but if trialing, just send a zero-dollar invoice
                      */
                     if (empty($trial_days) || 0 == $customer_args['amount']) {
                         $result = Stripe_Charge::create($customer_args);
                     } else {
                         $result = Stripe_InvoiceItem::create($customer_args);
                     }
                     // Capture success as soon as we can!
                     if ($result) {
                         $period = (int) $_POST['period'];
                         $level = (int) $_POST['level'];
                         $signup_details = ProSites_Helper_Registration::activate_blog($activation_key, false, $period, $level);
                         $blog_id = $signup_details['blog_id'];
                         if (isset($process_data['new_blog_details'])) {
                             ProSites_Helper_Session::session(array('new_blog_details', 'blog_id'), $blog_id);
                             ProSites_Helper_Session::session(array('new_blog_details', 'payment_success'), true);
                         }
                         self::set_customer_data($blog_id, $customer_id, $result->id);
                     }
                     if ($current_plan = self::get_current_plan($blog_id)) {
                         list($current_plan_level, $current_plan_period) = explode('_', $current_plan);
                     }
                     $old_expire = $psts->get_expire($blog_id);
                     $new_expire = $old_expire && $old_expire > time() ? $old_expire : false;
                     $psts->extend($blog_id, $_POST['period'], self::get_slug(), $_POST['level'], $initAmount, $new_expire, false);
                     //$psts->email_notification( $blog_id, 'receipt' );
                     if (isset($current_plan_level)) {
                         if ($current_plan_level > $_POST['level']) {
                             $psts->record_stat($blog_id, 'upgrade');
                         } else {
                             $psts->record_stat($blog_id, 'modify');
                         }
                     } else {
                         $psts->record_stat($blog_id, 'signup');
                     }
                 } catch (Stripe_CardError $e) {
                     $body = $e->getJsonBody();
                     $err = $body['error'];
                     $psts->errors->add('general', $e['message']);
                 } catch (Exception $e) {
                     $psts->errors->add('general', __('An unknown error occurred while processing your payment. Please try again.', 'psts'));
                 }
             }
             //delete the temporary coupon code
             if ($cp_code) {
                 try {
                     $cpn = Stripe_Coupon::retrieve($cp_code);
                     $cpn->delete();
                 } catch (Exception $e) {
                     wp_mail(get_blog_option($blog_id, 'admin_email'), __('Error deleting temporary Stripe coupon code. Attention required!.', 'psts'), sprintf(__('An error occurred when attempting to delete temporary Stripe coupon code %1$s. You will need to manually delete this coupon via your Stripe account.', 'psts'), $cp_code));
                 }
                 $psts->use_coupon($process_data['COUPON_CODE'], $blog_id, $domain);
             }
             if ($new || $psts->is_blog_canceled($blog_id)) {
                 // Added for affiliate system link
                 if ($recurring) {
                     $psts->log_action($blog_id, sprintf(__('User creating new subscription via CC: Subscription created (%1$s) - Customer ID: %2$s', 'psts'), $desc, $customer_id), $domain);
                 } else {
                     $psts->log_action($blog_id, sprintf(__('User completed new payment via CC: Site created/extended (%1$s) - Customer ID: %2$s', 'psts'), $desc, $customer_id), $domain);
                 }
                 do_action('supporter_payment_processed', $blog_id, $paymentAmount, $_POST['period'], $_POST['level']);
             } else {
                 $psts->log_action($blog_id, sprintf(__('User modifying subscription via CC: Plan changed to (%1$s) - %2$s', 'psts'), $desc, $customer_id), $domain);
             }
             //display GA ecommerce in footer
             $psts->create_ga_ecommerce($blog_id, $_POST['period'], $initAmount, $_POST['level'], $site_name, $domain);
             if (!empty($blog_id)) {
                 update_blog_option($blog_id, 'psts_stripe_canceled', 0);
                 /* 	some times there is a lag receiving webhooks from Stripe. we want to be able to check for that
                 				and display an appropriate message to the customer (e.g. there are changes pending to your account) */
                 update_blog_option($blog_id, 'psts_stripe_waiting', 1);
             } else {
                 if (isset($process_data['activation_key'])) {
                     //Update signup meta
                     $key = $process_data['activation_key'];
                     $signup_meta = '';
                     $signup_meta = $psts->get_signup_meta($key);
                     $signup_meta['psts_stripe_canceled'] = 0;
                     $signup_meta['psts_stripe_waiting'] = 1;
                     $psts->update_signup_meta($signup_meta, $key);
                 }
             }
             update_blog_option($blog_id, 'psts_stripe_waiting', 1);
             if (empty(self::$complete_message)) {
                 // Message is redundant now, but still used as a flag.
                 self::$complete_message = __('Your payment was successfully recorded! You should be receiving an email receipt shortly.', 'psts');
             }
         } catch (Exception $e) {
             $psts->errors->add('general', $e->getMessage());
         }
     }
 }
示例#2
0
 /**
  * Get geo data (or force init and return geo data).
  */
 public static function get_geodata()
 {
     $geodata = ProSites_Helper_Session::session('geodata');
     if (empty($geodata)) {
         $geodata = self::init_geolocation();
     }
     return $geodata;
 }
示例#3
0
 public static function render_free($style, $blog_id)
 {
     global $psts;
     $session_data = ProSites_Helper_Session::session('new_blog_details');
     $free_text = $psts->get_setting('free_msg');
     $content = '';
     if (!isset($_GET['bid']) && empty($blog_id) && !isset($session_data['new_blog_details']['blogname'])) {
         $content = '<div class="free-plan-link" style="' . esc_attr($style) . '">';
         $content .= apply_filters('prosites_checkout_free_link', '<a>' . esc_html($free_text) . '</a>', $blog_id);
         $content .= '</div>';
     } else {
         if (empty($blog_id) && !empty($_GET['bid'])) {
             $blog_id = (int) $_GET['bid'];
         }
         if (!is_pro_site($blog_id)) {
             $free_link = apply_filters('prosites_checkout_free_link', '<a class="pblg-checkout-opt" style="width:100%" id="psts-free-option" href="' . get_admin_url($blog_id, 'index.php?psts_dismiss=1', 'http') . '" title="' . __('Dismiss', 'psts') . '">' . $psts->get_setting('free_msg', __('No thank you, I will continue with a basic site for now', 'psts')) . '</a>', $blog_id);
             $content = '<div class="free-plan-link-logged-in" style="' . esc_attr($style) . '"><p>' . esc_html__('Your current site is a basic site with no extra features. Upgrade now by selecting a plan above.', 'psts') . '</p><p>' . $free_link . '</p></div>';
         }
     }
     return $content;
 }
 /**
  * Update session variables for new signup or upgrade
  *
  * @param $process_data
  * @param array $data
  *
  * @return bool|void
  */
 public static function update_session_vars($process_data, $data = array())
 {
     if (empty($process_data) || empty($data) || empty($data['blog_id'])) {
         return false;
     }
     $blog_id = $data['blog_id'];
     //Store Payment , for rendering the confirmation on checkout page
     if (isset($process_data['new_blog_details'])) {
         ProSites_Helper_Session::session(array('new_blog_details', 'blog_id'), $blog_id);
         ProSites_Helper_Session::session(array('new_blog_details', 'payment_success'), true);
     } else {
         if (empty($data['level']) || empty($data['period'])) {
             return;
         }
         $level = $data['level'];
         $period = $data['period'];
         ProSites_Helper_Session::session('upgrade_blog_details', array());
         ProSites_Helper_Session::session(array('upgrade_blog_details', 'blog_id'), $blog_id);
         ProSites_Helper_Session::session(array('upgrade_blog_details', 'level'), $level);
         ProSites_Helper_Session::session(array('upgrade_blog_details', 'period'), $period);
         ProSites_Helper_Session::session(array('upgrade_blog_details', 'payment_success'), true);
     }
 }
示例#5
0
 public static function process_checkout_form($process_data = array(), $blog_id, $domain)
 {
     global $psts;
     $session_keys = array('new_blog_details', 'upgraded_blog_details', 'COUPON_CODE', 'activation_key');
     foreach ($session_keys as $key) {
         $process_data[$key] = isset($process_data[$key]) ? $process_data[$key] : ProSites_Helper_Session::session($key);
     }
     if (isset($_POST['psts_mp_submit'])) {
         //check for level
         if (!isset($_POST['level']) || !isset($_POST['period'])) {
             $psts->errors->add('general', __('Please choose your desired level and payment plan.', 'psts'));
             return;
         }
         if (is_user_logged_in()) {
             $user = wp_get_current_user();
             $email = $user->user_email;
             $username = $user->user_login;
         } else {
             if (isset($process_data['new_blog_details'])) {
                 if (isset($process_data['new_blog_details']['email'])) {
                     $email = sanitize_email($process_data['new_blog_details']['email']);
                 }
                 if (isset($process_data['new_blog_details']['username'])) {
                     $username = sanitize_text_field($process_data['new_blog_details']['username']);
                 }
             }
         }
         if (empty($email)) {
             $psts->errors->add('general', __('No valid email given.', 'psts'));
             return;
         }
         // Get the blog id... try the session or get it from the database
         $blog_id = isset($process_data['upgraded_blog_details']['blog_id']) ? $process_data['upgraded_blog_details']['blog_id'] : 0;
         $blog_id = !empty($blog_id) ? $blog_id : isset($process_data['new_blog_details']['blog_id']) ? $process_data['new_blog_details']['blog_id'] : isset($process_data['new_blog_details']['blogname']) ? get_id_from_blogname($process_data['new_blog_details']['blogname']) : 0;
         switch_to_blog($blog_id);
         $blog_admin_url = admin_url();
         restore_current_blog();
         if ($blog_admin_url == admin_url()) {
             $blog_admin_url = __('Not activated yet.', 'psts');
         }
         $activation_key = '';
         if (isset($process_data['activation_key'])) {
             $activation_key = $process_data['activation_key'];
         }
         $subject = __('Pro Sites Manual Payment Submission', 'psts');
         $message_fields = apply_filters('prosites_manual_payment_email_info_fields', array('username' => $username, 'level' => intval($_POST['level']), 'level_name' => $psts->get_level_setting(intval($_POST['level']), 'name'), 'period' => intval($_POST['period']), 'user_email' => $email, 'activation_key' => $activation_key, 'site_address' => get_home_url(), 'manage_link' => $blog_admin_url));
         $message_parts = apply_filters('prosites_manual_payment_email_info', array('description' => sprintf(__('The user "%s" has submitted a manual payment request via the Pro Sites checkout form.', 'psts'), $message_fields['username']) . "\n", 'level_text' => __('Level: ', 'psts') . $message_fields['level'] . ' - ' . $message_fields['level_name'], 'period_text' => __('Period: ', 'psts') . sprintf(__('Every %d Months', 'psts'), $message_fields['period']), 'email_text' => sprintf(__("User Email: %s", 'psts'), $message_fields['user_email']), 'activation_text' => sprintf(__("Activation Key: %s", 'psts'), $message_fields['activation_key']), 'site_text' => sprintf(__("Site Address: %s", 'psts'), $message_fields['site_address']), 'manage_text' => sprintf(__("Manage Site: %s", 'psts'), $blog_admin_url)), $message_fields);
         if (!empty($_POST['psts_mp_text'])) {
             $message_parts['mp_text'] = __('User-Entered Comments:', 'psts') . "\n";
             $message_parts['mp_text'] .= wp_specialchars_decode(stripslashes(wp_filter_nohtml_kses($_POST['psts_mp_text'])), ENT_QUOTES);
         }
         $message = apply_filters('prosites_manual_payment_email_body', implode("\n", $message_parts) . "\n", $message_parts, $message_fields);
         wp_mail($psts->get_setting('mp_email', get_site_option("admin_email")), $subject, $message);
         add_action('prosites_manual_payment_email_sent', $message, $message_parts, $message_fields);
         ProSites_Helper_Session::session(array('new_blog_details', 'reserved_message'), __('Manual payment request submitted.', 'psts'));
         // Payment pending...
         ProSites_Helper_Session::session(array('new_blog_details', 'manual_submitted'), true);
     }
 }
示例#6
0
 public static function apply_coupon_to_checkout()
 {
     $doing_ajax = defined('DOING_AJAX') && DOING_AJAX ? true : false;
     $ajax_response = array();
     if ($doing_ajax) {
         $coupon_code = sanitize_text_field($_POST['coupon_code']);
         $valid_coupon = self::check_coupon($coupon_code);
         if (!empty($valid_coupon)) {
             $ajax_response['valid'] = true;
             ProSites_Helper_Session::session('COUPON_CODE', $coupon_code);
         } else {
             $ajax_response['valid'] = false;
             ProSites_Helper_Session::unset_session('COUPON_CODE');
         }
         //				$ajax_response['value'] = self::coupon_value( $coupon_code, '200' );
         $first_periods = array('price_1' => __('first month only', 'psts'), 'price_3' => __('first 3 months only', 'psts'), 'price_12' => __('first 12 months only', 'psts'));
         // New pricing
         if ($valid_coupon) {
             $original_levels = get_site_option('psts_levels');
             $level_list = self::get_adjusted_level_amounts($coupon_code);
             $coupon_obj = self::get_coupon($coupon_code);
             foreach ($level_list as $key => $level) {
                 unset($level_list[$key]['is_visible']);
                 unset($level_list[$key]['name']);
                 unset($level_list[$key]['setup_fee']);
                 if ($original_levels[$key]['price_1'] == $level['price_1']) {
                     $level_list[$key]['price_1_adjust'] = false;
                     unset($level_list[$key]['price_1']);
                 } else {
                     $level_list[$key]['price_1'] = '<div class="plan-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($level['price_1']) . '</div>';
                     if ('first' == $coupon_obj['lifetime']) {
                         $level_list[$key]['price_1_period'] = '<div class="period coupon-period">' . $first_periods['price_1'] . '</div>';
                     } else {
                         $level_list[$key]['price_1_period'] = '';
                     }
                     $level_list[$key]['price_1_adjust'] = true;
                 }
                 if ($original_levels[$key]['price_3'] == $level['price_3']) {
                     $level_list[$key]['price_3_adjust'] = false;
                     unset($level_list[$key]['price_3']);
                 } else {
                     $level_list[$key]['price_3'] = '<div class="plan-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($level['price_3']) . '</div>';
                     $total_1 = $original_levels[$key]['price_1'] * 3;
                     $total_3 = $level['price_3'];
                     $monthly = $level['price_3'] / 3;
                     $saving = $total_1 - $total_3;
                     $level_list[$key]['price_3_monthly'] = '<div class="monthly-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($monthly) . '</div>';
                     $level_list[$key]['price_3_savings'] = '<div class="savings-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($saving) . '</div>';
                     if ('first' == $coupon_obj['lifetime']) {
                         $level_list[$key]['price_3_period'] = '<div class="period coupon-period">' . $first_periods['price_3'] . '</div>';
                     } else {
                         $level_list[$key]['price_3_period'] = '';
                     }
                     $level_list[$key]['price_3_adjust'] = true;
                 }
                 if ($original_levels[$key]['price_12'] == $level['price_12']) {
                     $level_list[$key]['price_12_adjust'] = false;
                     unset($level_list[$key]['price_12']);
                 } else {
                     $level_list[$key]['price_12'] = '<div class="plan-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($level['price_12']) . '</div>';
                     $total_1 = $original_levels[$key]['price_1'] * 12;
                     $total_12 = $level['price_12'];
                     $monthly = $level['price_12'] / 12;
                     $saving = $total_1 - $total_12;
                     $level_list[$key]['price_12_monthly'] = '<div class="monthly-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($monthly) . '</div>';
                     $level_list[$key]['price_12_savings'] = '<div class="savings-price coupon-amount">' . ProSites_Helper_UI::rich_currency_format($saving) . '</div>';
                     if ('first' == $coupon_obj['lifetime']) {
                         $level_list[$key]['price_12_period'] = '<div class="period coupon-period">' . $first_periods['price_12'] . '</div>';
                     } else {
                         $level_list[$key]['price_12_period'] = '';
                     }
                     $level_list[$key]['price_12_adjust'] = true;
                 }
             }
             $ajax_response['levels'] = $level_list;
         }
         $response = array('what' => 'response', 'action' => 'apply_coupon_to_checkout', 'id' => 1, 'data' => json_encode($ajax_response));
         // Buffer used to isolate AJAX response from unexpected output
         ob_end_clean();
         ob_start();
         $xmlResponse = new WP_Ajax_Response($response);
         $xmlResponse->send();
         ob_end_flush();
     }
 }
示例#7
0
 public static function activate_blog($data, $trial = false, $period = 1, $level = 1, $expire = false)
 {
     global $psts, $wpdb;
     $user_pass = false;
     if (!is_array($data)) {
         $key = $data;
     } else {
         $key = isset($data['activation_key']) ? $data['activation_key'] : false;
         $user_pass = isset($data['new_blog_details']['user_pass']) ? $data['new_blog_details']['user_pass'] : false;
     }
     if (empty($key)) {
         return false;
     }
     // In case we're in session
     $session_data['new_blog_details'] = ProSites_Helper_Session::session('new_blog_details');
     $user_pass = empty($user_pass) && isset($session_data['new_blog_details']['user_pass']) ? $session_data['new_blog_details']['user_pass'] : $user_pass;
     if (!empty($user_pass)) {
         self::$temp_pass = $user_pass;
         add_filter('update_welcome_email', array('ProSites_Helper_Registration', 'update_welcome_email'), 10, 6);
     }
     // Activate the user signup
     $result = wpmu_activate_signup($key);
     $signup = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->signups} WHERE activation_key = %s", $key));
     // If the blog has already been activated, we still need some information from the signup table
     if (is_wp_error($result)) {
         $result = array();
         if (empty($signup)) {
             return 0;
         }
         $user_id = username_exists($signup->user_login);
         $blog_id = domain_exists($signup->domain, $signup->path, $wpdb->siteid);
         // As a fallback, try the site domain
         if (empty($blog_id)) {
             $domain = $wpdb->get_var($wpdb->prepare("SELECT domain FROM {$wpdb->site} WHERE id = %d", $wpdb->siteid));
             $blog_id = domain_exists($domain, $signup->path, $wpdb->siteid);
         }
         $result['user_id'] = $user_id;
         $result['blog_id'] = (int) $blog_id;
     }
     /**
      * Update coupon information
      */
     if (!empty($signup)) {
         //				$blog_id = $result['blog_id'];
         //				$signup_meta = maybe_unserialize( $signup->meta );
         //
         //				// Unlikely that this will have a coupon, but make sure
         //				$used = (array) get_blog_option( $blog_id, 'psts_used_coupons' );
         //
         //				// Is there a coupon stored in the signup_meta?
         //				if( isset( $signup_meta['psts_used_coupons'] ) && ! empty( $signup_meta['psts_used_coupons'] ) && is_array( $signup_meta['psts_used_coupons'] ) ) {
         //					// Merge and make sure we don't record the same coupon twice
         //					$used = array_merge( $used, $signup_meta['psts_used_coupons'] );
         //					$used = array_unique( $used );
         //					// Remove from signup meta
         //					unset( $signup_meta['psts_used_coupons'] );
         //					$psts->update_signup_meta( $signup_meta, $key );
         //				}
         //				if( ! empty( $used ) ) {
         //					// Add to blog options
         //					update_blog_option( $blog_id, 'psts_used_coupons', $used );
         //				}
     }
     /**
      * @todo: Make sure we dont over extend
      */
     //Set Trial
     if ($trial) {
         $trial_days = $psts->get_setting('trial_days', 0);
         // Set to first level for $trial_days
         $psts->extend($result['blog_id'], $period, 'Trial', $level, '', strtotime('+ ' . $trial_days . ' days'));
         //Redirect to checkout on next signup
         /**
          * @todo May not be needed here anymore
          */
         //update_blog_option( $result['blog_id'], 'psts_signed_up', 1 );
     }
     if (!empty($user_pass)) {
         $result['password'] = $user_pass;
     }
     // Contains $result['password'] for new users
     return $result;
 }
示例#8
0
 public static function attempt_force_sessions()
 {
     // Activate Sessions by putting in a false var
     ProSites_Helper_Session::session('psts_sessions_active', true);
 }
示例#9
0
	/**
	 * Activates the user blog if a domain is specified and if the blog is not already active
	 *
	 * @param bool $domain
	 * @param bool $trial
	 * @param bool $period
	 * @param bool $level
	 *
	 * @return bool
	 */
	function activate_user_blog( $domain = false, $trial = true, $period = false, $level = false ) {
		global $wpdb, $path;

		$trial_days = $this->get_setting( 'trial_days', 0 );
		if ( ! $domain ) {
			return false;
		}

		//Get activation key from db
		$signup         = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE domain = %s", $domain ) );
		$activation_key = ! empty( $signup->activation_key ) ? $signup->activation_key : '';
		if ( ! $activation_key || $signup->active ) {
			//get blog id
			$fields = array(
				'domain' => $domain,
				'path'   => '/'
			);
			$blog   = get_blog_details( $fields );

			return ! empty( $blog->blog_id ) ? $blog->blog_id : false;
		}
		$result = wpmu_activate_signup( $activation_key );

		if ( empty( $result['user_id'] ) ) {
			return false;
		}
		//Get user login by user id
		$user = get_user_by( 'id', $result['user_id'] );

		if ( empty( $user ) || is_wp_error( $user ) ) {
			return false;
		}

		//Login user to follow up the rest of Pro Site process
		$creds = array(
			'user_login'    => $user->user_login,
			'user_password' => $result['password']
		);
		$user  = wp_signon( $creds, true );
		wp_set_current_user( $user->ID );

		//Set Trial
		if ( $trial ) {
			$this->extend( $result['blog_id'], $period, 'Trial', $level, '', strtotime( '+ ' . $trial_days . ' days' ) );
			//Redirect to checkout on next signup
			update_blog_option( $result['blog_id'], 'psts_signed_up', 1 );
		}

		// Unset Domain name from session if its still there
		ProSites_Helper_Session::unset_session( 'domain' );

		if ( isset( $result['blog_id'] ) ) {
			return $result['blog_id'];
		} else {
			return false;
		}
	}
示例#10
0
        /**
         * Renders the user/site signup form.
         *
         * OR the completed message.
         *
         * @param mixed $render_data
         * @param bool $errors
         *
         * @return string
         */
        public static function render_signup_form($render_data = array(), $errors = false)
        {
            global $psts;
            $current_site = get_current_site();
            $img_base = $psts->plugin_url . 'images/';
            if (!$errors) {
                $errors = new WP_Error();
            }
            // Try going stateless, or check the session
            if (empty($render_data)) {
                $render_data = array();
                $render_data['new_blog_details'] = ProSites_Helper_Session::session('new_blog_details');
                $render_data['transaction_completed'] = ProSites_Helper_Session::session('transaction_completed');
            }
            $content = '';
            /**
             * Avoid rendering the form if its already been done.
             *
             * This means registration is completed. Trial is activated (non-recurring) or user provided
             * payment information for trial (recurring) or normal recurring plan.
             */
            if (isset($render_data['new_blog_details']) && isset($render_data['new_blog_details']['reserved_message']) || isset($render_data['transaction_completed'])) {
                if (isset($render_data['new_blog_details'])) {
                    // This variable is populated by ProSites_Model_Registration::ajax_check_prosite_blog()
                    $content .= $render_data['new_blog_details']['reserved_message'];
                    // Debugging only.
                    //ProSites_Helper_Session::unset_session( 'new_blog_details' );
                    //ProSites_Helper_Session::unset_session( 'upgraded_blog_details' );
                    return $content;
                } else {
                    $content = $render_data['transaction_completed']['message'];
                    return $content;
                }
            }
            $content .= '<div id="prosites-signup-form-checkout" class="hidden">';
            $action = '';
            $active_signup = get_site_option('registration', 'none');
            $active_signup = apply_filters('wpmu_active_signup', $active_signup);
            // Determine action...
            if (is_user_logged_in() && ($active_signup == 'all' || $active_signup == 'blog')) {
                $action = 'another_blog';
            } elseif (is_user_logged_in() == false && ($active_signup == 'all' || $active_signup == 'user')) {
                $action = 'sign_up';
            } elseif (is_user_logged_in() == false && $active_signup == 'blog') {
                $action = 'no_register';
            } else {
                $action = 'no_new_blog';
            }
            // WP hook
            // Render regardless if user can sign up
            ob_start();
            do_action('preprocess_signup_form');
            $content .= ob_get_clean();
            if ('sign_up' == $action || 'another_blog' == $action) {
                // Need to first check if user can sign up
                // WP hook
                ob_start();
                do_action('before_signup_form');
                $content .= ob_get_clean();
                $user_name = '';
                $user_email = '';
                $content .= '<h2>' . esc_html__('Setup your site', 'psts') . '</h2>';
                $content .= '<form method="post" id="prosites-user-register">';
                // USER SECTION
                ob_start();
                do_action('signup_hidden_fields', 'validate-user');
                $content .= ob_get_clean();
                $content .= self::render_user_section($render_data, $errors, $user_name, $user_email);
                // BLOG SECTION
                ob_start();
                do_action('signup_hidden_fields', 'validate-site');
                // do_action( 'signup_hidden_fields', 'create-another-site' );
                $content .= ob_get_clean();
                $content .= self::render_blog_section($render_data, $errors);
                $content .= '<div><input type="button" id="check-prosite-blog" value="' . esc_attr__('Reserve your site', 'psts') . '" /></div>';
                $content .= '<div class="hidden" id="registration_processing">
							<img src="' . $img_base . 'loading.gif"> Processing...
							</div>';
                $content .= '</form>';
                // WP hook
                ob_start();
                do_action('after_signup_form');
                $content .= '</div>';
                $content .= ob_get_clean();
            }
            return $content;
        }
示例#11
0
 public static function render_account_modified($content, $blog_id, $domain)
 {
     global $psts;
     $render_data['plan_updated'] = ProSites_Helper_Session::session('plan_updated');
     // Exit as if this never happened
     if (!isset($render_data['plan_updated']) || false == $render_data['plan_updated']['render']) {
         return $content;
     }
     $level_list = get_site_option('psts_levels');
     $periods = array(1 => __('monthly', 'psts'), 3 => __('quarterly', 'psts'), 12 => __('anually', 'psts'));
     $previous = '<strong>' . $level_list[$render_data['plan_updated']['prev_level']]['name'] . '</strong> (' . $periods[$render_data['plan_updated']['prev_period']] . ')';
     $current = '<strong>' . $level_list[$render_data['plan_updated']['level']]['name'] . '</strong> (' . $periods[$render_data['plan_updated']['period']] . ')';
     $blog_id = (int) $render_data['plan_updated']['blog_id'];
     $content = '<div id="psts-payment-info-received">';
     $user = wp_get_current_user();
     $email = $user->user_email;
     $content .= '<h2>' . esc_html__('Plan updated...', 'psts') . '</h2>';
     $content .= '<p>' . sprintf(esc_html__('Your plan was successfully modified from %s to %s. You will receive a receipt email shortly to confirm this action.', 'psts'), $previous, $current) . '</p>';
     $content .= '<p>' . esc_html__('If you did not receive an email please try the following:', 'psts') . '</p>';
     $content .= '<ul>' . '<li>' . esc_html__('Wait a little bit longer.', 'psts') . '</li>' . '<li>' . esc_html__('Check your spam folder just in case it ended up in there.', 'psts') . '</li>' . '<li>' . esc_html__('Make sure that your email address is correct (' . $email . ')', 'psts') . '</li>' . '</ul>';
     $content .= '<p>' . esc_html__('If your email address is incorrect or you noticed a problem, please contact us to resolve the issue.', 'psts') . '</p>';
     $content .= '<a href="' . $psts->checkout_url($blog_id) . '">' . esc_html__('Go back to your account.', 'psts') . '</a>';
     $content .= '</div>';
     ProSites_Helper_Session::unset_session('plan_updated');
     return $content;
 }
示例#12
0
 private function get_plan_content($period, $data, $level, $level_name)
 {
     global $psts;
     $level_prices = array(MONTHLY => $data['price_1'], QUARTERLY => $data['price_3'], YEARLY => $data['price_12']);
     $level_price = $psts->format_currency(false, $level_prices[$period]);
     $price = $level_price;
     $discount_price_level = $price;
     $recurring = $psts->get_setting('recurring_subscriptions', true);
     $upgrade_price = $recurring ? $level_prices[$period] : $psts->calc_upgrade_cost($this->blog_id, $level, $period, $level_prices[$period]);
     $coupon_code = ProSites_Helper_Session::session('COUPON_CODE');
     if (isset($coupon_code) && $psts->check_coupon($coupon_code, $this->blog_id, $level, $period) && ($coupon_value = $psts->coupon_value($coupon_code, $level_prices[$period]))) {
         $level_discount_price = $psts->format_currency(false, $coupon_value['new_total']);
         $discount_price_level = '<del>' . $level_price . '</del><strong class="coupon">' . $level_discount_price . '</strong>';
     } elseif ($upgrade_price != $level_prices[$period]) {
         $discount_price_level = '<del>' . $level_price . '</del><strong class="coupon">' . $psts->format_currency(false, $upgrade_price) . '</strong>';
     }
     $setup_fee_label = '';
     if ($psts->has_setup_fee($this->blog_id, $level)) {
         $setup_fee_amt = $psts->get_setting('setup_fee', 0);
         $setup_fee = $psts->format_currency(false, $setup_fee_amt);
         $setup_fee_label = '<p class="setup fee">+ a one time ' . $setup_fee . ' setup fee.</p>';
     }
     $level_description = $this->get_level_description($level_prices, $period);
     $choose_plan = '<a href="#" class="button choose-plan" data-level="' . $level . '" data-level-name="' . strtolower($level_name) . '">Choose Plan</a>';
     $cur_period = $this->sel_period ? $this->sel_period : MONTHLY;
     $style = $period == $cur_period ? "" : 'style="display:none;"';
     $selected = $this->sel_level == $level ? "selected" : "";
     $content = '<li class="' . strtolower($level_name) . ' column period_' . $period . ' ' . $selected . ' " ' . $style . '><p class="plan price">' . $discount_price_level . '</p><span> per ' . $period . ' month' . ($period > 1 ? "s" : "") . '</span>' . $setup_fee_label . $level_description . $choose_plan . '</li>';
     return $content;
 }
示例#13
0
 public static function ajax_check_prosite_blog()
 {
     global $psts, $current_site;
     $blog_data = array();
     // Add ajax session var
     ProSites_Helper_Session::session('psts_ajax_session_activated', true);
     // Introduce a fake error because we don't want to actually create the blog yet.
     add_filter('registration_errors', array('ProSites_Model_Registration', 'prosite_blog_check_only'), 10, 3);
     // replace $_POST with array data
     $params = array();
     parse_str($_POST['data'], $params);
     $period = (int) $_POST['period'];
     $level = 'free' == $_POST['level'] ? $_POST['level'] : (int) $_POST['level'];
     $_POST = $params;
     $doing_ajax = defined('DOING_AJAX') && DOING_AJAX ? true : false;
     $ajax_response = array();
     if ($doing_ajax) {
         $user_name = sanitize_text_field($_POST['user_name']);
         $user_email = sanitize_email($_POST['user_email']);
         $blogname = sanitize_text_field($_POST['blogname']);
         $blog_title = sanitize_text_field(urldecode($_POST['blog_title']));
         // Process some cleaning up if needed
         do_action('prosite_register_blog_pre_validation', $user_name, $user_email, $blogname);
         $blog_validation = wpmu_validate_blog_signup($blogname, $blog_title);
         // Attempt to create a new user (knowing that it will fail, but it should only have our error)
         if (!isset($_POST['new_blog'])) {
             $validation = wpmu_validate_user_signup($user_name, $user_email);
             // nicer errors, but doesn't deal with custom fields
             $user_check = register_new_user($user_name, $user_email);
             // checks custom fields, but ugly errors
             $user_check->errors = array_merge($user_check->errors, $validation['errors']->errors);
             $user_check->errors = array_merge($user_check->errors, $blog_validation['errors']->errors);
         } else {
             $user_check = new WP_Error();
             $user_check->errors = array_merge($user_check->errors, $blog_validation['errors']->errors);
         }
         // Replaced session vars to make it semi-stateless, will pick these up in a session later
         $blog_data['new_blog_details'] = array();
         $blog_data['new_blog_details']['username'] = $user_name;
         $blog_data['new_blog_details']['email'] = $user_email;
         $blog_data['new_blog_details']['blogname'] = $blogname;
         $blog_data['new_blog_details']['title'] = $blog_title;
         $blog_data['new_blog_details']['level'] = $level;
         $blog_data['new_blog_details']['period'] = $period;
         $username_available = true;
         $email_available = true;
         $blogname_available = true;
         $blogtitle_available = true;
         // Checking passed...
         if (!empty($user_check->errors) && 1 == count($user_check->errors) && !isset($_POST['new_blog']) || 0 == count($user_check->errors) && isset($_POST['new_blog'])) {
             $keys = array_keys($user_check->errors);
             if ($keys && !in_array('availability_check_only', $keys) && !isset($_POST['new_blog'])) {
                 // Something went wrong!
                 $ajax_response['user_available'] = false;
             } else {
                 // All good!  We're ready to create the user/site
                 /** User is validated using register_new_user so that we can use the hooks and make them available,
                  * but we still need to actually create and activate the signup to get the $user_id. */
                 $blog = $blog_validation;
                 $domain = $blog['domain'];
                 $path = $blog['path'];
                 $blogname = $blog['blogname'];
                 $blog_title = $blog['blog_title'];
                 $errors = $blog['errors'];
                 // Privacy setting
                 $public = (int) $_POST['blog_public'];
                 $signup_meta = array('lang_id' => 1, 'public' => $public);
                 // Create the signup
                 $meta = apply_filters('add_signup_meta', $signup_meta);
                 $result = ProSites_Helper_Registration::signup_blog($domain, $path, $blog_title, $user_name, $user_email, $meta);
                 $blog_data['activation_key'] = $result['activation_key'];
                 if (isset($result['user_pass']) && !empty($result['user_pass'])) {
                     $blog_data['new_blog_details']['user_pass'] = $result['user_pass'];
                 }
                 $trial_days = $psts->get_setting('trial_days', 0);
                 $trial_active = !empty($trial_days);
                 $site_name = '';
                 if (!is_subdomain_install()) {
                     $site_name = $current_site->domain . $current_site->path . $blogname;
                 } else {
                     $site_name = $blogname . '.' . ($site_domain = preg_replace('|^www\\.|', '', $current_site->domain));
                 }
                 if ($trial_active) {
                     $recurring = $psts->get_setting('recurring_subscriptions', 1);
                     if ($recurring) {
                         $blog_data['new_blog_details']['reserved_message'] = sprintf('<div class="reserved_msg"><h2>' . __('Activate your site', 'psts') . '</h2>' . __('<p>Your site <strong>(%s)</strong> has been reserved but is not yet activated.</p><p>Once payment information has been verified your trial period will begin. When your trial ends you will be automatically upgraded to your chosen plan. Your reservation only last for 48 hours upon which your site name will become available again.</p><p>Please use the form below to setup your payment information.</p>', 'psts') . '</div>', $site_name);
                     } else {
                         // Non-recurring sites really should not do anything at checkout other than activate.
                         $result = ProSites_Helper_Registration::activate_blog($blog_data, true, $period, $level);
                         $blog_id = $result['blog_id'];
                         if (isset($result['password'])) {
                             $blog_data['new_blog_details']['user_pass'] = $result['password'];
                         }
                         ProSites_Helper_Registration::set_trial($blog_id, 1);
                         //Update Activation Key for blog
                         ProSites_Helper_Registration::update_activation_key($blog_id, $blog_data['activation_key']);
                         $psts->record_stat($blog_id, 'signup');
                         $ajax_response['show_finish'] = true;
                         $ajax_response['finish_content'] = ProSites_View_Front_Gateway::render_payment_submitted($blog_data, true);
                     }
                 } else {
                     $blog_data['new_blog_details']['reserved_message'] = sprintf('<div class="reserved_msg"><h2>' . __('Activate your site', 'psts') . '</h2>' . __('<p>Your site <strong>(%s)</strong> has been reserved but is not yet activated.</p><p>Once payment has been processed your site will become active with your chosen plan. Your reservation only last for 48 hours upon which your site name will become available again.</p><p>Please use the form below to setup your payment information.</p>', 'psts') . '</div>', $site_name);
                 }
                 // FREE basic site
                 if ('free' == $blog_data['new_blog_details']['level']) {
                     if (isset($blog_data['new_blog_details']['reserved_message'])) {
                         unset($blog_data['new_blog_details']['reserved_message']);
                     }
                     $result = ProSites_Helper_Registration::activate_blog($blog_data, false, false, false);
                     $blog_data['new_blog_details']['blog_id'] = $result['blog_id'];
                     if (isset($result['password'])) {
                         $blog_data['new_blog_details']['user_pass'] = $result['password'];
                     }
                     $ajax_response['show_finish'] = true;
                     $ajax_response['finish_content'] = ProSites_View_Front_Gateway::render_free_confirmation($blog_data);
                 }
                 if (isset($blog_data['new_blog_details']['reserved_message'])) {
                     $ajax_response['reserved_message'] = $blog_data['new_blog_details']['reserved_message'];
                 }
             }
             // If WP 4.0+ and user is logged in it will use WP_Session_Tokens, else $_SESSION
             ProSites_Helper_Session::session('new_blog_details', $blog_data['new_blog_details']);
             ProSites_Helper_Session::session('activation_key', $blog_data['activation_key']);
             $ajax_response['gateways_form'] = ProSites_View_Front_Gateway::render_checkout($blog_data);
         } else {
             // We had registration errors, redraw the form displaying errors
             if (!empty($user_check) && isset($user_check->errors)) {
                 $ajax_response['form'] = ProSites_View_Front_Registration::render_signup_form($blog_data, $user_check);
                 $ajax_response['user_available'] = false;
             }
             // Isolate which standard fields are valid
             $error_keys = array_keys($user_check->errors);
             foreach ($error_keys as $key) {
                 if (preg_match('/username|user_name/', $key)) {
                     $username_available = false;
                 }
                 if (preg_match('/email/', $key)) {
                     $email_available = false;
                 }
                 if (preg_match('/blogname/', $key)) {
                     $blogname_available = false;
                 }
                 if (preg_match('/blog_title/', $key)) {
                     $blogtitle_available = false;
                 }
             }
         }
         $ajax_response['username_available'] = $username_available;
         $ajax_response['email_available'] = $email_available;
         $ajax_response['blogname_available'] = $blogname_available;
         $ajax_response['blog_title_available'] = $blogtitle_available;
         $response = array('what' => 'response', 'action' => 'check_prosite_blog', 'id' => 1, 'data' => json_encode($ajax_response));
         // No longer need ajax session
         ProSites_Helper_Session::unset_session('psts_ajax_session_activated');
         // Buffer used to isolate AJAX response from unexpected output
         @ob_end_clean();
         ob_start();
         $xmlResponse = new WP_Ajax_Response($response);
         $xmlResponse->send();
         ob_end_flush();
     }
 }