/** * Validate and sanitize input values. * * @param array $settings Input fields. * * @return array Sanitized and validated output. * @throws ValidationException When some items are not valid. */ public function validate($settings) { if (!in_array($settings['weight_unit'], array_keys($this->weightUnit))) { $this->messages->addWarning(sprintf(__('Invalid weight unit: "%s". Value set to %s.', 'jigoshop'), $settings['weight_unit'], $this->weightUnit['kg'])); $settings['weight_unit'] = 'kg'; } if (!in_array($settings['dimensions_unit'], array_keys($this->dimensionUnit))) { $this->messages->addWarning(sprintf(__('Invalid dimensions unit: "%s". Value set to %s.', 'jigoshop'), $settings['dimensions_unit'], $this->dimensionUnit['cm'])); $settings['dimensions_unit'] = 'cm'; } if (!in_array($settings['stock_status'], array_keys($this->stockStatuses))) { $this->messages->addWarning(sprintf(__('Invalid default stock status: "%s". Value set to %s.', 'jigoshop'), $settings['stock_status'], $this->stockStatuses[StockStatus::IN_STOCK])); $settings['stock_status'] = StockStatus::IN_STOCK; } $settings['manage_stock'] = $settings['manage_stock'] == 'on'; $settings['show_stock'] = $settings['show_stock'] == 'on'; $settings['related'] = $settings['related'] == 'on'; $settings['low_stock_threshold'] = (int) $settings['low_stock_threshold']; if ($settings['low_stock_threshold'] < 0) { $this->messages->addWarning(sprintf(__('Invalid low stock threshold: "%d". Value set to 2.', 'jigoshop'), $settings['low_stock_threshold'])); $settings['low_stock_threshold'] = 2; } $settings['notify_low_stock'] = $settings['notify_low_stock'] == 'on'; $settings['notify_out_of_stock'] = $settings['notify_out_of_stock'] == 'on'; $settings['notify_on_backorders'] = $settings['notify_on_backorders'] == 'on'; $settings['images']['tiny'] = array('width' => (int) $settings['images']['tiny']['width'], 'height' => (int) $settings['images']['tiny']['height'], 'crop' => $settings['images']['tiny']['crop'] == 'on'); $settings['images']['thumbnail'] = array('width' => (int) $settings['images']['thumbnail']['width'], 'height' => (int) $settings['images']['thumbnail']['height'], 'crop' => $settings['images']['thumbnail']['crop'] == 'on'); $settings['images']['small'] = array('width' => (int) $settings['images']['small']['width'], 'height' => (int) $settings['images']['small']['height'], 'crop' => $settings['images']['small']['crop'] == 'on'); $settings['images']['large'] = array('width' => (int) $settings['images']['large']['width'], 'height' => (int) $settings['images']['large']['height'], 'crop' => $settings['images']['large']['crop'] == 'on'); return $settings; }
public function action() { if (isset($_POST['action']) && $_POST['action'] == 'change_password') { $errors = array(); $user = $this->wp->wpGetCurrentUser(); /** @noinspection PhpUndefinedFieldInspection */ if (!$this->wp->wpCheckPassword($_POST['password'], $user->user_pass, $user->ID)) { $errors[] = __('Current password is invalid.', 'jigoshop'); } if (empty($_POST['new-password'])) { $errors[] = __('Please enter new password.', 'jigoshop'); } else { if ($_POST['new-password'] != $_POST['new-password-2']) { $errors[] = __('Passwords do not match.', 'jigoshop'); } } if (!empty($errors)) { $this->messages->addError(join('<br/>', $errors), false); } else { $this->wp->wpUpdateUser(array('ID' => $user->ID, 'user_pass' => $_POST['new-password'])); $this->messages->addNotice(__('Password changed.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT)); } } }
public function action() { /** @var Order $order */ $order = $this->orderService->find((int) $this->wp->getQueryParameter('pay')); if ($order->getKey() !== $_GET['key']) { $this->messages->addError(__('Invalid security key. Unable to process order.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::ACCOUNT)); } if (isset($_POST['action']) && $_POST['action'] == 'purchase') { try { if ($this->options->get('advanced.pages.terms') > 0 && (!isset($_POST['terms']) || $_POST['terms'] != 'on')) { throw new Exception(__('You need to accept terms & conditions!', 'jigoshop')); } if (!isset($_POST['payment_method'])) { throw new Exception(__('Please select one of available payment methods.', 'jigoshop')); } $payment = $this->paymentService->get($_POST['payment_method']); $order->setPaymentMethod($payment); if (!$payment->isEnabled()) { throw new Exception(__('Selected payment method is not available. Please select another one.', 'jigoshop')); } $this->orderService->save($order); $url = $payment->process($order); // Redirect to thank you page if (empty($url)) { $url = $this->wp->getPermalink($this->wp->applyFilters('jigoshop\\checkout\\redirect_page_id', $this->options->getPageId(Pages::THANK_YOU))); $url = $this->wp->getHelpers()->addQueryArg(array('order' => $order->getId(), 'key' => $order->getKey()), $url); } $this->wp->wpRedirect($url); exit; } catch (Exception $e) { $this->messages->addError($e->getMessage()); } } }
/** * Validate and sanitize input values. * * @param array $settings Input fields. * * @return array Sanitized and validated output. * @throws ValidationException When some items are not valid. */ public function validate($settings) { $settings['show_message'] = $settings['show_message'] == 'on'; $settings['demo_store'] = $settings['demo_store'] == 'on'; if (!in_array($settings['country'], array_keys(Country::getAll()))) { $this->messages->addError(__('Invalid shop location (country), please select again.', 'jigoshop')); $settings['country'] = ''; } return $settings; }
public function action() { if (isset($_POST['action']) && $_POST['action'] == 'add-to-cart') { /** @var Entity $product */ $product = $this->productService->find($_POST['item']); try { $item = $this->wp->applyFilters('jigoshop\\cart\\add', null, $product); if ($item === null) { throw new Exception(__('Unable to add product to the cart.', 'jigoshop')); } $cart = $this->cartService->get($this->cartService->getCartIdForCurrentUser()); $cart->addItem($item); $this->cartService->save($cart); $url = false; $button = ''; switch ($this->options->get('shopping.redirect_add_to_cart')) { case 'cart': $url = $this->wp->getPermalink($this->options->getPageId(Pages::CART)); break; case 'checkout': $url = $this->wp->getPermalink($this->options->getPageId(Pages::CHECKOUT)); break; case 'product': default: $url = $this->wp->getPermalink($product->getId()); case 'same_page': case 'product_list': $button = sprintf('<a href="%s" class="btn btn-warning pull-right">%s</a>', $this->wp->getPermalink($this->options->getPageId(Pages::CART)), __('View cart', 'jigoshop')); } $this->messages->addNotice(sprintf(__('%s successfully added to your cart. %s', 'jigoshop'), $product->getName(), $button)); if ($url !== false) { $this->messages->preserveMessages(); $this->wp->wpRedirect($url); exit; } } catch (NotEnoughStockException $e) { if ($e->getStock() == 0) { $message = sprintf(__('Sorry, we do not have "%s" in stock.', 'jigoshop'), $product->getName()); } else { if ($this->options->get('products.show_stock')) { $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. We only have %d available at this time. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName(), $e->getStock()); } else { $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName()); } } $this->messages->addError($message); } catch (Exception $e) { $this->messages->addError(sprintf(__('A problem ocurred when adding to cart: %s', 'jigoshop'), $e->getMessage()), false); } } }
/** * Validates and returns properly sanitized options. * * @param $settings array Input options. * * @return array Sanitized result. */ public function validateOptions($settings) { $settings['enabled'] = $settings['enabled'] == 'on'; if (!is_numeric($settings['minimum'])) { $settings['minimum'] = $this->options['minimum']; $this->messages->addWarning(__('Minimum cart value was invalid - value is left unchanged.', 'jigoshop')); } if ($settings['minimum'] >= 0) { $settings['minimum'] = (int) $settings['minimum']; } else { $settings['minimum'] = $this->options['minimum']; $this->messages->addWarning(__('Minimum cart value was below 0 - value is left unchanged.', 'jigoshop')); } if (!in_array($settings['available_for'], array_keys($this->availability))) { $settings['available_for'] = $this->options['available_for']; $this->messages->addWarning(__('Availability is invalid - value is left unchanged.', 'jigoshop')); } if ($settings['available_for'] === 'specific') { $settings['countries'] = array_filter($settings['countries'], function ($item) { return Country::exists($item); }); } else { $settings['countries'] = array(); } return $settings; }
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)); } } }
public function render() { $content = $this->wp->getPostField('post_content', $this->options->getPageId(Pages::THANK_YOU)); /** @var Order $order */ $order = $this->orderService->find((int) $_REQUEST['order']); if ($order->getKey() != $_REQUEST['key']) { $this->messages->addError(__('Invalid security key. The order was processed.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::SHOP)); } return Render::get('shop/checkout/thanks', array('content' => $content, 'messages' => $this->messages, 'order' => $order, 'showWithTax' => $this->options->get('tax.price_tax') == 'with_tax', 'shopUrl' => $this->wp->getPermalink($this->options->getPageId(Pages::SHOP)), 'cancelUrl' => \Jigoshop\Helper\Order::getCancelLink($order), 'getTaxLabel' => function ($taxClass) use($order) { return Tax::getLabel($taxClass, $order); })); }
/** * Validate and sanitize input values. * * @param array $settings Input fields. * * @return array Sanitized and validated output. * @throws ValidationException When some items are not valid. */ public function validate($settings) { $settings['catalog_per_page'] = (int) $settings['catalog_per_page']; if ($settings['catalog_per_page'] <= 0) { $this->messages->addWarning(sprintf(__('Invalid products per page value: "%d". Value set to 12.', 'jigoshop'), $settings['catalog_per_page'])); $settings['catalog_per_page'] = 12; } if (!in_array($settings['catalog_order_by'], array_keys($this->catalogOrderBy))) { $this->messages->addWarning(sprintf(__('Invalid products sorting: "%s". Value set to %s.', 'jigoshop'), $settings['catalog_order_by'], $this->catalogOrderBy['post_date'])); $settings['catalog_order_by'] = 'post_date'; } if (!in_array($settings['catalog_order'], array_keys($this->catalogOrder))) { $this->messages->addWarning(sprintf(__('Invalid products sorting orientation: "%s". Value set to %s.', 'jigoshop'), $settings['catalog_order'], $this->catalogOrder['DESC'])); $settings['catalog_order'] = 'DESC'; } $settings['hide_out_of_stock'] = $settings['hide_out_of_stock'] == 'on'; $settings['enable_verification_message'] = $settings['enable_verification_message'] == 'on'; $settings['guest_purchases'] = $settings['guest_purchases'] == 'on'; $settings['show_login_form'] = $settings['show_login_form'] == 'on'; $settings['allow_registration'] = $settings['allow_registration'] == 'on'; $settings['login_for_downloads'] = $settings['login_for_downloads'] == 'on'; $settings['force_ssl'] = $settings['force_ssl'] == 'on'; $settings['validate_zip'] = $settings['validate_zip'] == 'on'; $settings['restrict_selling_locations'] = $settings['restrict_selling_locations'] == 'on'; if (!$settings['restrict_selling_locations']) { $settings['selling_locations'] = array(); } else { $settings['selling_locations'] = array_intersect($settings['selling_locations'], array_keys(Country::getAll())); } if (!in_array($settings['redirect_add_to_cart'], array_keys($this->addToCartRedirectionOptions))) { $this->messages->addWarning(sprintf(__('Invalid add to cart redirection: "%s". Value set to %s.', 'jigoshop'), $settings['redirect_add_to_cart'], $this->addToCartRedirectionOptions['same_page'])); $settings['redirect_add_to_cart'] = 'same_page'; } if (!in_array($settings['redirect_continue_shopping'], array_keys($this->backToShopRedirectionOptions))) { $this->messages->addWarning(sprintf(__('Invalid continue shopping redirection: "%s". Value set to %s.', 'jigoshop'), $settings['redirect_continue_shopping'], $this->backToShopRedirectionOptions['product_list'])); $settings['redirect_continue_shopping'] = 'product_list'; } return $settings; }
/** * Validates settings for WordPress to save. * * @param array $input Input data to validate. * * @return array Sanitized output for saving. */ public function validate($input) { try { $currentTab = $this->getCurrentTab(); /** @var TabInterface $tab */ $tab = $this->tabs[$currentTab]; $this->options->update($currentTab, $tab->validate($input)); } catch (Admin\Settings\ValidationException $e) { $this->messages->addError($e->getMessage(), true); $this->wp->wpSafeRedirect(admin_url(sprintf('admin.php?page=%s&tab=%s', self::NAME, $tab->getSlug()))); exit; } return $this->options->getAll(); }
/** * Action method to run tools. */ public function action() { if (!isset($_GET['tool'])) { return; } $id = trim(htmlspecialchars(strip_tags($_GET['tool']))); if (isset($this->tools[$id])) { /** @var Tool $tool */ $tool = $this->tools[$id]; $this->wp->doAction('jigoshop\\migration\\before', $tool); $tool->migrate(null); $this->messages->addNotice(__('Migration complete', 'jigoshop')); $this->wp->wpRedirect($this->wp->adminUrl('admin.php?page=' . self::NAME)); } }
/** * 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; }
/** * Validate and sanitize input values. * * @param array $settings Input fields. * * @return array Sanitized and validated output. * @throws ValidationException When some items are not valid. */ public function validate($settings) { // This is required when installin emails this function is used twice, // once for advanced settings and once for all jigoshop settings. if (isset($settings['general']) && is_array($settings['general'])) { return $settings; } if (isset($settings['install_emails'])) { unset($settings['install_emails']); // TODO add this to WPAL remove_all_actions('save_post_' . Types\Email::NAME); $this->di->get('jigoshop.installer')->installEmails(); $this->messages->addNotice(__('Emails created.', 'jigoshop')); } $settings['automatic_complete'] = $settings['automatic_complete'] == 'on'; $settings['automatic_reset'] = $settings['automatic_reset'] == 'on'; $settings['products_list']['variations_sku_stock'] = $settings['products_list']['variations_sku_stock'] == 'on'; if (!in_array($settings['cache'], array_keys($this->caches))) { $this->messages->addWarning(sprintf(__('Invalid cache mechanism: "%s". Value set to %s.', 'jigoshop'), $settings['cache'], $this->caches['simple'])); $settings['cache'] = 'simple'; } $settings['ignore_meta_queries'] = $settings['ignore_meta_queries'] == 'on'; if (isset($settings['api'], $settings['api']['keys'])) { $settings['api']['keys'] = array_filter($settings['api']['keys'], function ($item) { return !empty($item['key']); }); $settings['api']['keys'] = array_map(function ($item) { return array_merge(array('key' => '', 'permissions' => array()), $item); }, $settings['api']['keys']); } $pages = $this->_getPages(); if (!in_array($settings['pages']['shop'], array_keys($pages))) { $this->messages->addError(__('Invalid shop page, please select again.', 'jigoshop')); } else { $this->options->setPageId(Pages::SHOP, $settings['pages']['shop']); } if (!in_array($settings['pages']['cart'], array_keys($pages))) { $this->messages->addError(__('Invalid cart page, please select again.', 'jigoshop')); } else { $this->options->setPageId(Pages::CART, $settings['pages']['cart']); } if (!in_array($settings['pages']['checkout'], array_keys($pages))) { $this->messages->addError(__('Invalid checkout page, please select again.', 'jigoshop')); } else { $this->options->setPageId(Pages::CHECKOUT, $settings['pages']['checkout']); } if (!in_array($settings['pages']['checkout_thank_you'], array_keys($pages))) { $this->messages->addError(__('Invalid thank you page, please select again.', 'jigoshop')); } else { $this->options->setPageId(Pages::THANK_YOU, $settings['pages']['checkout_thank_you']); } if (!in_array($settings['pages']['account'], array_keys($pages))) { $this->messages->addError(__('Invalid My account page, please select again.', 'jigoshop')); } else { $this->options->setPageId(Pages::ACCOUNT, $settings['pages']['account']); } if (!empty($settings['pages']['terms']) && $settings['pages']['terms'] != 0 && !in_array($settings['pages']['terms'], array_keys($pages))) { $this->messages->addError(__('Invalid terms page, please select again.', 'jigoshop')); } return $settings; }
/** * Migrates data from old format to new one. * @param array $orders * @return bool migration product status: success or not */ public function migrate($orders) { $wpdb = $this->wp->getWPDB(); // Open transaction for save migration products $var_autocommit_sql = $wpdb->get_var("SELECT @@AUTOCOMMIT"); try { $this->checkSql(); $wpdb->query("SET AUTOCOMMIT=0"); $this->checkSql(); $wpdb->query("START TRANSACTION"); $this->checkSql(); // Register order status taxonomy to fetch old statuses $this->wp->registerTaxonomy('shop_order_status', array('shop_order'), array('hierarchical' => true, 'update_count_callback' => '_update_post_term_count', 'labels' => array('name' => __('Order statuses', 'jigoshop'), 'singular_name' => __('Order status', 'jigoshop'), 'search_items' => __('Search Order statuses', 'jigoshop'), 'all_items' => __('All Order statuses', 'jigoshop'), 'parent_item' => __('Parent Order status', 'jigoshop'), 'parent_item_colon' => __('Parent Order status:', 'jigoshop'), 'edit_item' => __('Edit Order status', 'jigoshop'), 'update_item' => __('Update Order status', 'jigoshop'), 'add_new_item' => __('Add New Order status', 'jigoshop'), 'new_item_name' => __('New Order status Name', 'jigoshop')), 'public' => false, 'show_ui' => false, 'show_in_nav_menus' => false, 'query_var' => true, 'rewrite' => false)); for ($i = 0, $endI = count($orders); $i < $endI;) { $order = $orders[$i]; // Update central order data $status = $this->wp->getTheTerms($order->ID, 'shop_order_status'); $this->checkSql(); if (!empty($status)) { $status = $this->_transformStatus($status[0]->slug); } else { $status = Status::PENDING; } $query = $wpdb->prepare("UPDATE {$wpdb->posts} SET post_status = %s WHERE ID = %d", $status, $order->ID); $wpdb->query($query); $this->checkSql(); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'number', $order->ID)); $this->checkSql(); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'updated_at', time())); $this->checkSql(); // Update columns do { switch ($orders[$i]->meta_key) { case '_js_completed_date': $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_key = %s, meta_value = %d WHERE meta_id = %d", 'completed_at', strtotime($orders[$i]->meta_value), $orders[$i]->meta_id)); $this->checkSql(); break; case 'order_key': $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_key = %s WHERE meta_id = %d", 'key', $orders[$i]->meta_id)); $this->checkSql(); break; case 'order_data': $data = unserialize($orders[$i]->meta_value); $data = $this->_fetchOrderData($data); // Migrate customer if ($this->customer == null) { $customer = $this->wp->getPostMeta($order->ID, 'customer', true); $this->customer = $customer; } $this->customer = $this->_migrateCustomer($this->customer, $data); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'customer', serialize(serialize($this->customer)))); $this->checkSql(); // Migrate coupons $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'coupons', serialize($data['order_discount_coupons']))); // TODO: HERE $this->checkSql(); // Migrate shipping method try { $method = $this->shippingService->get($data['shipping_method']); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'shipping', serialize(array('method' => $method->getState(), 'price' => $data['order_shipping'], 'rate' => '')))); $this->checkSql(); } catch (Exception $e) { $this->messages->addWarning(sprintf(__('Shipping method "%s" not found. Order with ID "%d" has no shipping method now.'), $data['shipping_method'], $order->ID)); } // Migrate payment method try { $method = $this->paymentService->get($data['payment_method']); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'payment', $method->getId())); $this->checkSql(); } catch (Exception $e) { $this->messages->addWarning(sprintf(__('Payment method "%s" not found. Order with ID "%d" has no payment method now.'), $data['payment_method'], $order->ID)); } // Migrate order totals $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'subtotal', $data['order_subtotal'])); $this->checkSql(); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'discount', $data['order_discount'])); $this->checkSql(); $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'total', $data['order_total'])); $this->checkSql(); // TODO: Add new meta for shipping total price /*$wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %s)", $order->ID, 'shipping', $data['order_shipping'] )); $this->checkSql();*/ break; case 'customer_user': if ($this->customer == null) { $customer = $this->wp->getPostMeta($order->ID, 'customer', true); if ($customer !== false) { /** @var Customer $customer */ $customer = maybe_unserialize(maybe_unserialize($customer)); if (!$customer) { $customer = new Customer(); } } else { $customer = new Customer(); } $this->customer = $customer; } /** @var \WP_User $user */ if (($user = $this->wp->getUserBy('id', $orders[$i]->meta_value)) !== false) { $this->checkSql(); $this->customer->setId($user->ID); $this->customer->setLogin($user->get('login')); $this->customer->setEmail($user->get('user_email')); $this->customer->setName($user->get('display_name')); $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = %d WHERE post_id = %d AND meta_key = %s", serialize(serialize($this->customer)), $orders[$i]->meta_id, 'customer')); $this->checkSql(); $userId = $orders[$i]->meta_value; } else { $userId = 0; $guest = new Customer\Guest(); $guest->setBillingAddress($this->customer->getBillingAddress()); $guest->setShippingAddress($this->customer->getShippingAddress()); $this->customer = $guest; } $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value) VALUES (%d, %s, %d)", $order->ID, 'customer_id', $userId)); break; case 'order_items': $data = unserialize($orders[$i]->meta_value); $globalTaxRate = 0.0; foreach ($data as $itemData) { /** @var Product $product */ $itemData = $this->_fetchItemData($itemData); $product = null; $productGetId = null; if ($wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->posts} WHERE ID = %d", $itemData['id'])) > 0) { $product = $this->productService->find($itemData['id']); $productGetId = $product->getId(); } $tax = 0.0; $taxRate = 0; if ($itemData['qty'] == 0) { $itemData['qty'] = 1; } $price = $itemData['cost'] / $itemData['qty']; if (!empty($itemData['taxrate']) && $itemData['taxrate'] > 0) { $tax = $price * $itemData['taxrate'] / 100; $taxRate = $itemData['taxrate']; } else { if (isset($itemData['cost_inc_tax']) && $itemData['cost'] < $itemData['cost_inc_tax']) { $tax = ($itemData['cost_inc_tax'] - $itemData['cost']) / $itemData['qty']; $taxRate = $tax / $itemData['cost']; } } $globalTaxRate += $taxRate; $productGetType = false; if ($productGetId == null) { if (isset($itemData['variation_id']) && !empty($itemData['variation_id'])) { $productGetType = Product\Variable::TYPE; } else { $productGetType = Product\Simple::TYPE; } } else { $productGetType = $product->getType(); } $insertOrderData = array('order_id' => $order->ID, 'product_type' => $productGetType, 'title' => $itemData['name'], 'price' => $price, 'tax' => $tax, 'quantity' => $itemData['qty'], 'cost' => $itemData['cost']); if ($productGetId != null) { $insertOrderData['product_id'] = $productGetId; } $wpdb->insert($wpdb->prefix . 'jigoshop_order_item', $insertOrderData); $this->checkSql(); $itemId = $wpdb->insert_id; if (isset($itemData['variation_id']) && !empty($itemData['variation_id']) && ($productGetId == null || $product instanceof Product\Variable)) { $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}jigoshop_order_item_meta (item_id, meta_key, meta_value) VALUES (%d, %s, %s)", $itemId, 'variation_id', $itemData['variation_id'])); $this->checkSql(); if ($productGetId !== null) { /** @var Product\Variable\Variation $variationProduct */ /** @var Product\Variable $product */ $variationProduct = $product->getVariation($itemData['variation_id']); if (is_array($itemData['variation']) && $variationProduct && $variationProduct instanceof Product\Variable\Variation) { foreach ($itemData['variation'] as $variation => $variationValue) { $variation = str_replace('tax_', '', $variation); $attribute = $this->getAttribute($variationProduct, $variation); if ($attribute === null) { $this->messages->addWarning(sprintf(__('Attribute "%s" not found for variation ID "%d".', 'jigoshop'), $variation, $variationProduct->getId())); continue; } $option = $this->getAttributeOption($attribute, $variationValue); if ($option === null) { $this->messages->addWarning(sprintf(__('Attribute "%s" option "%s" not found for variation ID "%d".', 'jigoshop'), $variation, $variationValue, $variationProduct->getId())); continue; } $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}jigoshop_order_item_meta (item_id, meta_key, meta_value) VALUES (%d, %s, %s)", $itemId, $attribute->getAttribute()->getId(), $option->getId())); $this->checkSql(); } } } } } $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->prefix}jigoshop_order_tax (order_id, label, tax_class, rate, is_compound) VALUES (%d, %s, %s, %d, %d)", $order->ID, __('Standard', 'jigoshop'), 'standard', $globalTaxRate / (count($data) == 0 ? 1 : count($data)), false)); $this->checkSql(); break; } $i++; } while ($i < $endI && $orders[$i]->ID == $order->ID); } // commit sql transation and restore value of autocommit $wpdb->query("COMMIT"); $wpdb->query("SET AUTOCOMMIT=" . $var_autocommit_sql); return true; } catch (Exception $e) { // rollback sql transation and restore value of autocommit if (WP_DEBUG) { \Monolog\Registry::getInstance(JIGOSHOP_LOGGER)->addDebug($e); } $wpdb->query("ROLLBACK"); $wpdb->query("SET AUTOCOMMIT=" . $var_autocommit_sql); Migration::saveLog(__('Migration orders end with error: ', 'jigoshop') . $e); return false; } }
public function fill(OrderInterface $order, array $data) { if (!empty($data['customer']) && is_numeric($data['customer'])) { $data['customer'] = $this->customerService->find($data['customer']); } if (isset($data['customer'])) { if (!empty($data['customer'])) { $data['customer'] = $this->wp->getHelpers()->maybeUnserialize($data['customer']); } else { $data['customer'] = new CustomerEntity\Guest(); } if (isset($data['billing_address'])) { $data['billing_address'] = array_merge(array_flip(array_keys(ProductHelper::getBasicBillingFields())), $data['billing_address']); /** @var CustomerEntity $customer */ $customer = $data['customer']; $customer->setBillingAddress($this->createAddress($data['billing_address'])); } if (isset($data['shipping_address'])) { $data['shipping_address'] = array_merge(array_flip(array_keys(ProductHelper::getBasicShippingFields())), $data['shipping_address']); /** @var CustomerEntity $customer */ $customer = $data['customer']; $customer->setShippingAddress($this->createAddress($data['shipping_address'])); } $order->setCustomer($data['customer']); unset($data['customer']); } /** @var OrderInterface $order */ $order = $this->wp->applyFilters('jigoshop\\factory\\order\\fetch\\after_customer', $order); if (isset($data['items'])) { $order->removeItems(); } //We do not want to add coupons and from directly, without validation. $coupons = null; if (isset($data['coupons'])) { $coupons = $data['coupons']; unset($data['coupons']); } if (isset($data['discount'])) { unset($data['discount']); } $order->restoreState($data); if ($coupons) { $coupons = $this->wp->getHelpers()->maybeUnserialize($coupons); if (isset($coupons[0]) && is_array($coupons[0])) { $codes = array_map(function ($coupon) { return $coupon['code']; }, $coupons); } else { $codes = $coupons; } $coupons = $this->couponService->getByCodes($codes); foreach ($coupons as $coupon) { /** @var Coupon $coupon */ try { $order->addCoupon($coupon); } catch (Exception $e) { $this->messages->addWarning($e->getMessage(), false); } } } return $this->wp->applyFilters('jigoshop\\factory\\order\\fill', $order); }
public function processResponse() { if (isset($_GET['file'])) { try { $data = explode('.', $_GET['file']); if (count($data) != 3) { throw new Exception(__('Invalid download key. Unable to download file.', 'jigoshop')); } list($key, $id, $itemKey) = $data; $order = $this->orderService->find((int) $id); /** @var $order Order */ if ($order->getKey() !== $key) { throw new Exception(__('Invalid security key. Unable to download file.', 'jigoshop')); } if (!in_array($order->getStatus(), array(Order\Status::COMPLETED, Order\Status::PROCESSING))) { throw new Exception(__('Invalid order.', 'jigoshop')); } $item = $order->getItem($itemKey); if ($item === null) { throw new Exception(__('Product not found.', 'jigoshop')); } if ($item->getType() !== Downloadable::TYPE) { throw new Exception(__('Invalid file to download.', 'jigoshop')); } $downloads = $item->getMeta('downloads')->getValue(); if (!empty($downloads) && $downloads == 0) { throw new Exception(__('Sorry, you have reached your download limit for this file.', 'jigoshop')); } if ($this->options->get('shopping.login_for_downloads')) { if (!$this->wp->isUserLoggedIn()) { throw new Exception(__('You have to log in before you can download a file.', 'jigoshop')); } else { if ($order->getCustomer()->getId() != $this->wp->getCurrentUserId()) { throw new Exception(__('This is not your download link.', 'jigoshop')); } } } $file = $item->getMeta('file')->getValue(); if (!$file) { throw new Exception(__('File not found.', 'jigoshop')); } if (!empty($downloads)) { $item->getMeta('downloads')->setValue($downloads - 1); $this->orderService->saveItemMeta($item, $item->getMeta('downloads')); } if (!$this->wp->isMultisite()) { $site_url = $this->wp->siteUrl(); $site_url = str_replace('https:', 'http:', $site_url); $file = str_replace($this->wp->getHelpers()->trailingslashit($site_url), ABSPATH, $file); } else { $network_url = $this->wp->networkAdminUrl(); $network_url = str_replace('https:', 'http:', $network_url); $upload_dir = $this->wp->wpUploadDir(); // Try to replace network url $file = str_replace($this->wp->getHelpers()->trailingslashit($network_url), ABSPATH, $file); // Now try to replace upload URL $file = str_replace($upload_dir['baseurl'], $upload_dir['basedir'], $file); } $file = $this->wp->applyFilters('jigoshop\\downloadable\\file_path', $file, $itemKey, $order); // See if its local or remote if (strstr($file, 'http:') || strstr($file, 'https:') || strstr($file, 'ftp:')) { $isRemote = true; } else { $isRemote = false; $file = realpath($file); } // Download the file $extension = strtolower(substr(strrchr($file, '.'), 1)); switch ($extension) { case 'pdf': $type = 'application/pdf'; break; case 'exe': $type = 'application/octet-stream'; break; case 'zip': $type = 'application/zip'; break; case 'doc': $type = 'application/msword'; break; case 'xls': $type = 'application/vnd.ms-excel'; break; case 'ppt': $type = 'application/vnd.ms-powerpoint'; break; case 'gif': $type = 'image/gif'; break; case 'png': $type = 'image/png'; break; case 'jpe': case 'jpeg': case 'jpg': $type = 'image/jpg'; break; default: $type = 'application/force-download'; } $this->wp->doAction('jigoshop\\downloadable\\before_download', $file, $order); @session_write_close(); @set_time_limit(0); @ob_end_clean(); // required for IE, otherwise Content-Disposition may be ignored if (ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'Off'); } header('Pragma: no-cache'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Robots: none'); header('Content-Type: ' . $type); header('Content-Description: File Transfer'); header('Content-Transfer-Encoding: binary'); if (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) { // workaround for IE filename bug with multiple periods / multiple dots in filename header('Content-Disposition: attachment; filename="' . preg_replace('/\\./', '%2e', basename($file), substr_count(basename($file), '.') - 1) . '";'); } else { header('Content-Disposition: attachment; filename="' . basename($file) . '";'); } if ($isRemote) { header('Location: ' . $file); } else { if (file_exists($file)) { header('Content-Length: ' . filesize($file)); readfile($file); } else { throw new Exception(__('File not found.', 'jigoshop')); } } } catch (Exception $e) { $this->messages->addError($e->getMessage()); $this->wp->redirectTo($this->options->getPageId(Pages::SHOP)); } exit; } }
/** * Executes actions associated with selected page. */ public function action() { $cart = $this->cartService->getCurrent(); if ($cart->isEmpty()) { $this->messages->addWarning(__('Your cart is empty, please add products before proceeding.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::SHOP)); } if (!$this->isAllowedToEnterCheckout()) { $this->messages->addError(__('You need to log in before processing to checkout.', 'jigoshop')); $this->wp->redirectTo($this->options->getPageId(Pages::CART)); } if (isset($_POST['action']) && $_POST['action'] == 'purchase') { try { $allowRegistration = $this->options->get('shopping.allow_registration'); if ($allowRegistration && !$this->wp->isUserLoggedIn()) { $this->createUserAccount(); } if (!$this->isAllowedToCheckout($cart)) { if ($allowRegistration) { throw new Exception(__('You need either to log in or create account to purchase.', 'jigoshop')); } throw new Exception(__('You need to log in before purchasing.', 'jigoshop')); } if ($this->options->get('advanced.pages.terms') > 0 && (!isset($_POST['terms']) || $_POST['terms'] != 'on')) { throw new Exception(__('You need to accept terms & conditions!', 'jigoshop')); } $this->cartService->validate($cart); $this->customerService->save($cart->getCustomer()); if (!Country::isAllowed($cart->getCustomer()->getBillingAddress()->getCountry())) { $locations = array_map(function ($location) { return Country::getName($location); }, $this->options->get('shopping.selling_locations')); throw new Exception(sprintf(__('This location is not supported, we sell only to %s.'), join(', ', $locations))); } $shipping = $cart->getShippingMethod(); if ($this->isShippingRequired($cart) && (!$shipping || !$shipping->isEnabled())) { throw new Exception(__('Shipping is required for this order. Please select shipping method.', 'jigoshop')); } $payment = $cart->getPaymentMethod(); $isPaymentRequired = $this->isPaymentRequired($cart); $this->wp->doAction('jigoshop\\checkout\\payment', $payment); if ($isPaymentRequired && (!$payment || !$payment->isEnabled())) { throw new Exception(__('Payment is required for this order. Please select payment method.', 'jigoshop')); } $order = $this->orderService->createFromCart($cart); /** @var Order $order */ $order = $this->wp->applyFilters('jigoshop\\checkout\\order', $order); $this->orderService->save($order); $this->cartService->remove($cart); $url = ''; if ($isPaymentRequired) { $url = $payment->process($order); } else { $order->setStatus(\Jigoshop\Helper\Order::getStatusAfterCompletePayment($order)); $this->orderService->save($order); } // Redirect to thank you page if (empty($url)) { $url = $this->wp->getPermalink($this->wp->applyFilters('jigoshop\\checkout\\redirect_page_id', $this->options->getPageId(Pages::THANK_YOU))); $url = $this->wp->getHelpers()->addQueryArg(array('order' => $order->getId(), 'key' => $order->getKey()), $url); } $this->wp->wpRedirect($url); exit; } catch (Exception $e) { $this->messages->addError($e->getMessage()); } } }
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); } }