public function action() { if (isset($_POST['action']) && $_POST['action'] == 'save_address') { $customer = $this->customerService->getCurrent(); switch ($this->wp->getQueryParameter('edit-address')) { case 'shipping': $address = $customer->getShippingAddress(); break; case 'billing': default: $address = $customer->getBillingAddress(); break; } $errors = array(); if ($address instanceof CompanyAddress) { $address->setCompany(trim(htmlspecialchars(strip_tags($_POST['address']['company'])))); $address->setVatNumber(trim(htmlspecialchars(strip_tags($_POST['address']['euvatno'])))); } $address->setPhone(trim(htmlspecialchars(strip_tags($_POST['address']['phone'])))); $address->setFirstName(trim(htmlspecialchars(strip_tags($_POST['address']['first_name'])))); $address->setLastName(trim(htmlspecialchars(strip_tags($_POST['address']['last_name'])))); $address->setAddress(trim(htmlspecialchars(strip_tags($_POST['address']['address'])))); $address->setCity(trim(htmlspecialchars(strip_tags($_POST['address']['city'])))); $postcode = trim(htmlspecialchars(strip_tags($_POST['address']['postcode']))); if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($postcode, $address->getCountry())) { $errors[] = __('Postcode is not valid!', 'jigoshop'); } else { $address->setPostcode($postcode); } $country = trim(htmlspecialchars(strip_tags($_POST['address']['country']))); if (!Country::exists($country)) { $errors[] = sprintf(__('Country "%s" does not exists.', 'jigoshop'), $country); } else { $address->setCountry($country); } $state = trim(htmlspecialchars(strip_tags($_POST['address']['state']))); if (Country::hasStates($address->getCountry()) && !Country::hasState($address->getCountry(), $state)) { $errors[] = sprintf(__('Country "%s" does not have state "%s".', 'jigoshop'), Country::getName($address->getCountry()), $state); } else { $address->setState($state); } $email = trim(htmlspecialchars(strip_tags($_POST['address']['email']))); if (!Validation::isEmail($email)) { $errors[] = __('Invalid email address', 'jigoshop'); } else { $address->setEmail($email); } if (!empty($errors)) { $this->messages->addError(join('<br/>', $errors), false); } else { $this->customerService->save($customer); $this->messages->addNotice(__('Address saved.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT)); } } }
/** * @param $address Address * * @return array */ private function validateAddress($address) { $errors = array(); if ($address->isValid()) { if ($address->getFirstName() == null) { $errors[] = __('First name is empty.', 'jigoshop'); } if ($address->getLastName() == null) { $errors[] = __('Last name is empty.', 'jigoshop'); } if ($address->getAddress() == null) { $errors[] = __('Address is empty.', 'jigoshop'); } if ($address->getCountry() == null) { $errors[] = __('Country is not selected.', 'jigoshop'); } if ($address->getState() == null) { $errors[] = __('State or province is not selected.', 'jigoshop'); } if ($address->getCity() == null) { $errors[] = __('City is empty.', 'jigoshop'); } if ($address->getPostcode() == null) { $errors[] = __('Postcode is empty.', 'jigoshop'); } if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($address->getPostcode(), $address->getCountry())) { $errors[] = __('Invalid postcode.', 'jigoshop'); } } if (!Country::exists($address->getCountry())) { $errors[] = sprintf(__('Country "%s" does not exist.', 'jigoshop'), $address->getCountry()); } if (Country::hasStates($address->getCountry()) && !Country::hasState($address->getCountry(), $address->getState())) { $errors[] = sprintf(__('Country "%s" does not have state "%s".', 'jigoshop'), $address->getCountry(), $address->getState()); } return $errors; }
public function action() { if (isset($_REQUEST['action'])) { switch ($_REQUEST['action']) { case 'cancel_order': if ($this->wp->getHelpers()->verifyNonce($_REQUEST['nonce'], 'cancel_order')) { /** @var Order $order */ $order = $this->orderService->find((int) $_REQUEST['id']); if ($order->getKey() != $_REQUEST['key']) { $this->messages->addError(__('Invalid order key.', 'jigoshop')); return; } if ($order->getStatus() != Status::PENDING) { $this->messages->addError(__('Unable to cancel order.', 'jigoshop')); return; } $order->setStatus(Status::CANCELLED); $cart = $this->cartService->createFromOrder($this->cartService->getCartIdForCurrentUser(), $order); $this->orderService->save($order); $this->cartService->save($cart); $this->messages->addNotice(__('The order has been cancelled', 'jigoshop')); } break; case 'update-shipping': $customer = $this->customerService->getCurrent(); $this->updateCustomer($customer); break; case 'checkout': try { $cart = $this->cartService->getCurrent(); // Update quantities $this->updateQuantities($cart); // Update customer (if needed) if ($this->options->get('shipping.calculator')) { $customer = $this->customerService->getCurrent(); $this->updateCustomer($customer); } if (isset($_POST['jigoshop_order']['shipping_method'])) { // Select shipping method $method = $this->shippingService->get($_POST['jigoshop_order']['shipping_method']); $cart->setShippingMethod($method); } if ($cart->getShippingMethod() && !$cart->getShippingMethod()->isEnabled()) { $cart->removeShippingMethod(); $this->messages->addWarning(__('Previous shipping method is unavailable. Please select different one.', 'jigoshop')); } if ($this->options->get('shopping.validate_zip')) { $address = $cart->getCustomer()->getShippingAddress(); if ($address->getPostcode() && !Validation::isPostcode($address->getPostcode(), $address->getCountry())) { throw new Exception(__('Postcode is not valid!', 'jigoshop')); } } do_action('jigoshop\\cart\\before_checkout', $cart); $this->cartService->save($cart); $this->messages->preserveMessages(); $this->wp->redirectTo($this->options->getPageId(Pages::CHECKOUT)); } catch (Exception $e) { $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage())); } break; case 'update-cart': if (isset($_POST['cart']) && is_array($_POST['cart'])) { try { $cart = $this->cartService->getCurrent(); $this->updateQuantities($cart); $this->cartService->save($cart); $this->messages->addNotice(__('Successfully updated the cart.', 'jigoshop')); } catch (Exception $e) { $this->messages->addError(sprintf(__('Error occurred while updating cart: %s', 'jigoshop'), $e->getMessage())); } } } } if (isset($_GET['action']) && isset($_GET['item']) && $_GET['action'] === 'remove-item' && is_numeric($_GET['item'])) { $cart = $this->cartService->getCurrent(); $cart->removeItem((int) $_GET['item']); $this->cartService->save($cart); $this->messages->addNotice(__('Successfully removed item from cart.', 'jigoshop'), false); } }
/** * Ajax action for changing postcode. */ public function ajaxChangePostcode() { $customer = $this->customerService->getCurrent(); switch ($_POST['field']) { case 'shipping_address': if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($_POST['value'], $customer->getShippingAddress()->getCountry())) { echo json_encode(array('success' => false, 'error' => __('Shipping postcode is not valid!', 'jigoshop'))); exit; } $customer->getShippingAddress()->setPostcode($_POST['value']); if ($customer->getBillingAddress()->getPostcode() == null) { $customer->getBillingAddress()->setPostcode($_POST['value']); } break; case 'billing_address': if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($_POST['value'], $customer->getBillingAddress()->getCountry())) { echo json_encode(array('success' => false, 'error' => __('Billing postcode is not valid!', 'jigoshop'))); exit; } $customer->getBillingAddress()->setPostcode($_POST['value']); if ($_POST['differentShipping'] === 'false') { $customer->getShippingAddress()->setPostcode($_POST['value']); } break; } $this->customerService->save($customer); $cart = $this->cartService->getCurrent(); $cart->setCustomer($customer); $response = $this->getAjaxLocationResponse($customer, $cart); echo json_encode($response); exit; }
/** * Validates and returns properly sanitized options. * * @param $settings array Input options. * * @return array Sanitized result. */ public function validateOptions($settings) { $settings['enabled'] = $settings['enabled'] == 'on'; $settings['title'] = trim(htmlspecialchars(strip_tags($settings['title']))); $settings['description'] = trim(htmlspecialchars(strip_tags($settings['description'], '<p><a><strong><em><b><i>'))); if (!Validation::isEmail($settings['email'])) { $settings['email'] = ''; if ($settings['enabled']) { $this->messages->addWarning(__('Email address is not valid.', 'jigoshop')); } } $settings['send_shipping'] = $settings['send_shipping'] == 'on'; $settings['force_payment'] = $settings['force_payment'] == 'on'; $settings['test_mode'] = $settings['test_mode'] == 'on'; if (!Validation::isEmail($settings['test_email'])) { $settings['test_email'] = ''; if ($settings['enabled']) { $this->messages->addWarning(__('Test email address is not valid.', 'jigoshop')); } } return $settings; }
/** * Ajax action for changing postcode. */ public function ajaxChangePostcode() { try { $post = $this->wp->getPost((int) $_POST['order']); $this->wp->updateGlobalPost($post); /** @var \Jigoshop\Entity\Order $order */ $order = $this->orderService->findForPost($post); if ($order->getId() === null) { throw new Exception(__('Order not found.', 'jigoshop')); } switch ($_POST['type']) { case 'shipping': $address = $order->getCustomer()->getShippingAddress(); break; case 'billing': default: $address = $order->getCustomer()->getBillingAddress(); } if ($this->options->get('shopping.validate_zip') && !Validation::isPostcode($_POST['value'], $address->getCountry())) { throw new Exception(__('Invalid postcode.', 'jigoshop')); } $address->setPostcode($_POST['value']); $order = $this->rebuildOrder($order); $this->orderService->save($order); $result = $this->getAjaxResponse($order); } catch (Exception $e) { $result = array('success' => false, 'error' => $e->getMessage()); } echo json_encode($result); exit; }