Ejemplo n.º 1
0
 public function delete($termId)
 {
     $termId = (int) $termId;
     if (!$termId) {
         return;
     }
     $wpdb = $this->wp->getWPDB();
     /** @noinspection PhpUndefinedFieldInspection */
     $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->jigoshop_termmeta} WHERE `jigoshop_term_id` = %d", $termId));
 }
Ejemplo n.º 2
0
 /**
  * Displays "Recent Reviews" meta box.
  */
 public function recentReviews()
 {
     $wpdb = $this->wp->getWPDB();
     /** @noinspection PhpUnusedLocalVariableInspection */
     $comments = $wpdb->get_results("SELECT *, SUBSTRING(comment_content,1,100) AS comment_excerpt\n\t\t\t\tFROM {$wpdb->comments}\n\t\t\t\tLEFT JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID)\n\t\t\t\tWHERE comment_approved = '1'\n\t\t\t\tAND comment_type = ''\n\t\t\t\tAND post_password = ''\n\t\t\t\tAND post_type = 'product'\n\t\t\t\tORDER BY comment_date_gmt DESC\n\t\t\t\tLIMIT 5");
     Render::output('admin/dashboard/recentReviews', array('comments' => $comments));
 }
Ejemplo n.º 3
0
 /**
  * @param $id int Order ID.
  *
  * @return array List of items assigned to the order.
  */
 private function getItems($id)
 {
     $wpdb = $this->wp->getWPDB();
     $query = $wpdb->prepare("\n\t\t\tSELECT * FROM {$wpdb->prefix}jigoshop_order_item joi\n\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_order_item_meta joim ON joim.item_id = joi.id\n\t\t\tWHERE joi.order_id = %d\n\t\t\tORDER BY joi.id", array($id));
     $results = $wpdb->get_results($query, ARRAY_A);
     $items = array();
     for ($i = 0, $endI = count($results); $i < $endI;) {
         $id = $results[$i]['id'];
         $product = $this->productService->find($results[$i]['product_id']);
         $item = new Entity\Item();
         $item->setId($results[$i]['item_id']);
         $item->setName($results[$i]['title']);
         $item->setQuantity($results[$i]['quantity']);
         $item->setPrice($results[$i]['price']);
         $item->setTax($results[$i]['tax']);
         while ($i < $endI && $results[$i]['id'] == $id) {
             //				Securing against empty meta's, but still no piece of code does not add the meta.
             if ($results[$i]['meta_key']) {
                 $meta = new Entity\Item\Meta();
                 $meta->setKey($results[$i]['meta_key']);
                 $meta->setValue($results[$i]['meta_value']);
                 $item->addMeta($meta);
             }
             $i++;
         }
         $product = $this->wp->applyFilters('jigoshop\\factory\\order\\find_product', $product, $item);
         $item->setProduct($product);
         $item->setKey($this->productService->generateItemKey($item));
         $items[] = $item;
     }
     return $items;
 }
Ejemplo n.º 4
0
 /**
  * @param $product VariableProduct Product to fetch variations for.
  *
  * @return array List of variations.
  */
 public function getVariations($product)
 {
     $wpdb = $this->wp->getWPDB();
     $query = $wpdb->prepare("\n\t\t\tSELECT pv.ID, pva.* FROM {$wpdb->posts} pv\n\t\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_product_variation_attribute pva ON pv.ID = pva.variation_id\n\t\t\t\tWHERE pv.post_parent = %d AND pv.post_type = %s\n\t\t", array($product->getId(), \Jigoshop\Core\Types\Product\Variable::TYPE));
     $results = $wpdb->get_results($query, ARRAY_A);
     $variations = array();
     $results = array_filter($results, function ($item) {
         return $item['attribute_id'] !== null;
     });
     for ($i = 0, $endI = count($results); $i < $endI;) {
         $variation = new VariableProduct\Variation();
         $variation->setId((int) $results[$i]['ID']);
         $variation->setParent($product);
         /** @var Product $variableProduct */
         $variableProduct = $this->productService->find($results[$i]['ID']);
         $variation->setProduct($variableProduct);
         // TODO: Maybe some kind of fetching together?
         while ($i < $endI && $results[$i]['ID'] == $variation->getId()) {
             $attribute = new VariableProduct\Attribute(VariableProduct\Attribute::VARIATION_ATTRIBUTE_EXISTS);
             $attribute->setVariation($variation);
             $attribute->setAttribute($product->getAttribute($results[$i]['attribute_id']));
             $attribute->setValue($results[$i]['value']);
             if ($attribute->getAttribute() !== null) {
                 $variation->addAttribute($attribute);
             }
             $i++;
         }
         $variations[$variation->getId()] = $variation;
     }
     return $variations;
 }
Ejemplo n.º 5
0
 private function _createPage($slug, $data)
 {
     $wpdb = $this->wp->getWPDB();
     $slug = esc_sql(_x($slug, 'page_slug', 'jigoshop'));
     $page_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_status = 'publish' AND post_status <> 'trash' LIMIT 1", $slug));
     if (!$page_id) {
         Registry::getInstance(JIGOSHOP_LOGGER)->addDebug(sprintf('Installing page "%s".', $slug));
         $data['post_name'] = $slug;
         $page_id = $this->wp->wpInsertPost($data);
     }
     $this->options->setPageId($slug, $page_id);
     $this->options->update('advanced.pages.' . $slug, $page_id);
 }
Ejemplo n.º 6
0
 /**
  * @param $ids array IDs to preserve.
  */
 public function removeAllExcept($ids)
 {
     $wpdb = $this->wp->getWPDB();
     $ids = join(',', array_filter(array_map(function ($item) {
         return (int) $item;
     }, $ids)));
     // Support for removing all items
     if (empty($ids)) {
         $ids = '0';
     }
     $query = "DELETE FROM {$wpdb->prefix}jigoshop_tax WHERE id NOT IN ({$ids})";
     $wpdb->query($query);
 }
Ejemplo n.º 7
0
 private function getAdminProductListQuery($request)
 {
     if (isset($request['s']) && $request['s']) {
         $wpdb = $this->wp->getWPDB();
         $ids = $wpdb->get_results($wpdb->prepare("SELECT posts.ID as ID FROM {$wpdb->posts} as posts\n                INNER JOIN {$wpdb->postmeta} as meta ON (meta.post_id = posts.ID AND meta.meta_key = 'sku')\n                WHERE meta.meta_value LIKE %s OR posts.post_title LIKE %s OR posts.post_content LIKE %s OR posts.ID = %d", '%' . $request['s'] . '%', '%' . $request['s'] . '%', '%' . $request['s'] . '%', $request['s']), ARRAY_A);
         unset($request['s']);
         unset($request['m']);
         $request['post__in'] = array_map(function ($item) {
             return $item['ID'];
         }, $ids);
         $request['post__in'] = count($request['post__in']) ? $request['post__in'] : [0];
     }
     return $request;
 }
Ejemplo n.º 8
0
 public function ajaxMigrationProducts()
 {
     try {
         //			1 - if first time ajax request
         if ($_POST['msgLog'] == 1) {
             Migration::saveLog(__('Migration products START.', 'jigoshop'), true);
         }
         $wpdb = $this->wp->getWPDB();
         $productsIdsMigration = array();
         if (($TMP_productsIdsMigration = $this->wp->getOption('jigoshop_products_migrate_id')) === false) {
             $query = $wpdb->prepare("\n\t\t\t\tSELECT ID FROM {$wpdb->posts}\n\t\t\t\t\tWHERE post_type IN (%s, %s) AND post_status <> %s", 'product', 'product_variation', 'auto-draft');
             $products = $wpdb->get_results($query);
             $countMeta = count($products);
             for ($aa = 0; $aa < $countMeta; $aa++) {
                 $productsIdsMigration[] = $products[$aa]->ID;
             }
             $productsIdsMigration = array_unique($productsIdsMigration);
             $this->wp->updateOption('jigoshop_products_migrate_id', serialize($productsIdsMigration));
             $this->wp->updateOption('jigoshop_products_migrate_count', count($productsIdsMigration));
         } else {
             $productsIdsMigration = unserialize($TMP_productsIdsMigration);
         }
         $countAll = $this->wp->getOption('jigoshop_products_migrate_count');
         $singleProductId = array_shift($productsIdsMigration);
         $countRemain = count($productsIdsMigration);
         $query = $wpdb->prepare("\n\t\t\tSELECT DISTINCT p.ID, pm.* FROM {$wpdb->posts} p\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID\n\t\t\t\tWHERE p.post_type IN (%s, %s) AND p.post_status <> %s AND p.ID = %d", 'product', 'product_variation', 'auto-draft', $singleProductId);
         $product = $wpdb->get_results($query);
         $ajax_response = array('success' => true, 'percent' => floor(($countAll - $countRemain) / $countAll * 100), 'processed' => $countAll - $countRemain, 'remain' => $countRemain, 'total' => $countAll);
         if ($singleProductId) {
             if ($this->migrate($product)) {
                 $this->wp->updateOption('jigoshop_products_migrate_id', serialize($productsIdsMigration));
             } else {
                 $ajax_response['success'] = false;
                 Migration::saveLog(__('Migration products end with error.', 'jigoshop'));
             }
         } elseif ($countRemain == 0) {
             $this->wp->updateOption('jigoshop_products_migrate_id', serialize($productsIdsMigration));
             $this->wp->deleteOption('jigoshop_attributes_anti_duplicate');
             Migration::saveLog(__('Migration products END.', 'jigoshop'));
         }
         echo json_encode($ajax_response);
     } catch (Exception $e) {
         if (WP_DEBUG) {
             \Monolog\Registry::getInstance(JIGOSHOP_LOGGER)->addDebug($e);
         }
         echo json_encode(array('success' => false));
         Migration::saveLog(__('Migration products end with error: ', 'jigoshop') . $e);
     }
     exit;
 }
Ejemplo n.º 9
0
 public function ajaxMigrationEmails()
 {
     try {
         //			1 - if first time ajax request
         if ($_POST['msgLog'] == 1) {
             Migration::saveLog(__('Migration emails START.', 'jigoshop'), true);
         }
         $wpdb = $this->wp->getWPDB();
         $query = $wpdb->prepare("\n\t\t\t\tSELECT DISTINCT p.ID, pm.* FROM {$wpdb->posts} p\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID\n\t\t\t\t\tWHERE p.post_type IN (%s) AND p.post_status <> %s", array('shop_email', 'auto-draft'));
         $emails = $wpdb->get_results($query);
         $joinEmails = array();
         $emailsIdsMigration = array();
         for ($aa = 0; $aa < count($emails); $aa++) {
             $joinEmails[$emails[$aa]->ID][$emails[$aa]->meta_id] = new \stdClass();
             foreach ($emails[$aa] as $k => $v) {
                 $joinEmails[$emails[$aa]->ID][$emails[$aa]->meta_id]->{$k} = $v;
                 $emailsIdsMigration[] = $emails[$aa]->ID;
             }
         }
         $emailsIdsMigration = array_unique($emailsIdsMigration);
         $countAll = count($emailsIdsMigration);
         if (($TMP_emailsIdsMigration = $this->wp->getOption('jigoshop_emails_migrate_id')) !== false) {
             $emailsIdsMigration = unserialize($TMP_emailsIdsMigration);
         }
         $singleEmailsId = array_shift($emailsIdsMigration);
         $countRemain = count($emailsIdsMigration);
         sort($joinEmails[$singleEmailsId]);
         $ajax_response = array('success' => true, 'percent' => floor(($countAll - $countRemain) / $countAll * 100), 'processed' => $countAll - $countRemain, 'remain' => $countRemain, 'total' => $countAll);
         if ($singleEmailsId) {
             if ($this->migrate($joinEmails[$singleEmailsId])) {
                 $this->wp->updateOption('jigoshop_emails_migrate_id', serialize($emailsIdsMigration));
             } else {
                 $ajax_response['success'] = false;
                 Migration::saveLog(__('Migration emails end with error.', 'jigoshop'));
             }
         } elseif ($countRemain == 0) {
             $this->wp->updateOption('jigoshop_emails_migrate_id', serialize($emailsIdsMigration));
             Migration::saveLog(__('Migration emails END.', 'jigoshop'));
         }
         echo json_encode($ajax_response);
     } catch (Exception $e) {
         if (WP_DEBUG) {
             \Monolog\Registry::getInstance(JIGOSHOP_LOGGER)->addDebug($e);
         }
         echo json_encode(array('success' => false));
         Migration::saveLog(__('Migration emails end with error: ', 'jigoshop') . $e);
     }
     exit;
 }
Ejemplo n.º 10
0
 public function initialize(Wordpress $wp)
 {
     $wpdb = $wp->getWPDB();
     $wpdb->hide_errors();
     $collate = '';
     if ($wpdb->has_cap('collation')) {
         if (!empty($wpdb->charset)) {
             $collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
         }
         if (!empty($wpdb->collate)) {
             $collate .= " COLLATE {$wpdb->collate}";
         }
     }
     $query = "\n\t\t\tCREATE TABLE IF NOT EXISTS {$wpdb->prefix}jigoshop_product_variation_attribute (\n\t\t\t\tvariation_id BIGINT UNSIGNED NOT NULL,\n\t\t\t\tattribute_id INT(9) NOT NULL,\n\t\t\t\tvalue VARCHAR(255),\n\t\t\t\tPRIMARY KEY id (variation_id, attribute_id),\n\t\t\t\tFOREIGN KEY variation (variation_id) REFERENCES {$wpdb->posts} (ID) ON DELETE CASCADE,\n\t\t\t\tFOREIGN KEY attribute (attribute_id) REFERENCES {$wpdb->prefix}jigoshop_attribute (id) ON DELETE CASCADE\n\t\t\t) {$collate};\n\t\t";
     $wpdb->query($query);
     $wpdb->show_errors();
 }
Ejemplo n.º 11
0
 /**
  * @return array
  */
 public function getTemplates()
 {
     if (count($this->templates) == 0) {
         $wpdb = $this->wp->getWPDB();
         $templates = $wpdb->get_results($wpdb->prepare("\nSELECT posts.ID as id, meta.meta_value as actions FROM {$wpdb->posts} as posts\nJOIN {$wpdb->postmeta} as meta ON (meta.post_id = posts.ID AND meta.meta_key = %s)\nWHERE posts.post_type = %s", 'actions', Types\Email::NAME), ARRAY_A);
         foreach ($templates as $template) {
             $actions = maybe_unserialize($template['actions']);
             if (is_array($actions) && count($actions)) {
                 foreach ($actions as $action) {
                     if (!isset($this->templates[$action])) {
                         $this->templates[$action] = [$template['id']];
                     } else {
                         $this->templates[$action][] = $template['id'];
                     }
                 }
             }
         }
     }
     return $this->templates;
 }
Ejemplo n.º 12
0
 public function displayColumn($column)
 {
     $post = $this->wp->getGlobalPost();
     if ($post === null) {
         return;
     }
     /** @var Entity $order */
     $order = $this->orderService->findForPost($post);
     switch ($column) {
         case 'status':
             OrderHelper::renderStatus($order);
             break;
         case 'customer':
             echo OrderHelper::getUserLink($order->getCustomer());
             break;
         case 'billing_address':
             Render::output('admin/orders/billingAddress', array('order' => $order));
             break;
         case 'shipping_address':
             Render::output('admin/orders/shippingAddress', array('order' => $order));
             break;
         case 'shipping_payment':
             Render::output('admin/orders/shippingPayment', array('order' => $order));
             break;
         case 'total':
             Render::output('admin/orders/totals', array('order' => $order, 'getTaxLabel' => function ($taxClass) use($order) {
                 return Tax::getLabel($taxClass, $order);
             }));
             break;
         case 'products':
             $wpdb = $this->wp->getWPDB();
             $products = $wpdb->get_results("SELECT product_id, title FROM " . $wpdb->prefix . "jigoshop_order_item WHERE order_id = " . $order->getId());
             Render::output('admin/orders/products', array('products' => $products));
             break;
     }
 }
Ejemplo n.º 13
0
 public function getOrderReportData($query)
 {
     return $this->wp->getWPDB()->get_results($query);
 }
Ejemplo n.º 14
0
 /**
  * Finds and returns number of products of specified type.
  *
  * @param $type Types\Product\Type Type class.
  *
  * @return int Count of the products.
  */
 private function getTypeCount($type)
 {
     $wpdb = $this->wp->getWPDB();
     return $wpdb->get_var($wpdb->prepare("\n\t\t\tSELECT COUNT(*) FROM {$wpdb->posts} p\n\t\t\t\tLEFT JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID\n\t\t\t\tWHERE pm.meta_key = %s AND pm.meta_value = %s\n\t\t", array('type', $type->getId())));
 }
Ejemplo n.º 15
0
 /**
  * @param $product Product
  *
  * @return int
  */
 private function removeVariablePost($product)
 {
     $wpdb = $this->wp->getWPDB();
     $wpdb->delete($wpdb->posts, array('ID' => $product->getId()));
 }
Ejemplo n.º 16
0
 /**
  * @param string $when Base query.
  *
  * @return string Query for orders older than 30 days.
  * @internal
  */
 public function ordersFilter($when = '')
 {
     return $when . $this->wp->getWPDB()->prepare(' AND post_date < %s', date('Y-m-d', time() - 30 * 24 * 3600));
 }
Ejemplo n.º 17
0
 /**
  * Removes attribute from database.
  *
  * @param int $id Attribute ID.
  */
 public function removeAttribute($id)
 {
     $wpdb = $this->wp->getWPDB();
     $wpdb->delete($wpdb->prefix . 'jigoshop_attribute', array('id' => $id));
 }
Ejemplo n.º 18
0
 public function getAttachments($productId)
 {
     $wpdb = $this->wp->getWPDB();
     $query = $wpdb->prepare("SELECT attachment_id as id, type as type FROM {$wpdb->prefix}jigoshop_product_attachment WHERE product_id = %d", array($productId));
     return $wpdb->get_results($query, ARRAY_A);
 }
Ejemplo n.º 19
0
 /**
  * Migrates data from old format to new one.
  * @param mixed $options
  * @return bool migration options status: success or not
  */
 public function migrate($options = null)
 {
     $wpdb = $this->wp->getWPDB();
     //		Open transaction for save migration emails
     $var_autocommit_sql = $wpdb->get_var("SELECT @@AUTOCOMMIT");
     try {
         $this->checkSql();
         $wpdb->query("SET AUTOCOMMIT=0");
         $this->checkSql();
         $wpdb->query("START TRANSACTION");
         $this->checkSql();
         $options = $this->wp->getOption('jigoshop_options');
         $this->checkSql();
         $transformations = $this->_getTransformations();
         $transformations = $this->_addShippingTransformations($transformations);
         $transformations = $this->_addPaymentTransformations($transformations);
         foreach ($transformations as $old => $new) {
             if (array_key_exists($old, $options)) {
                 $value = $this->_transform($old, $options[$old]);
                 if ($old == 'jigoshop_default_country') {
                     $tmp = explode(':', $value);
                     if (count($tmp) > 1) {
                         $this->options->update('general.state', $tmp[1]);
                         $this->checkSql();
                         $value = $tmp[0];
                     }
                 }
             }
             if ($value !== null) {
                 $this->options->update($new, $value);
                 $this->checkSql();
             }
         }
         // Migrate tax rates
         if (!is_array($options['jigoshop_tax_rates'])) {
             $options['jigoshop_tax_rates'] = array();
         }
         $options['jigoshop_tax_rates'] = array_values($options['jigoshop_tax_rates']);
         for ($i = 0, $endI = count($options['jigoshop_tax_rates']); $i < $endI;) {
             $tax = $options['jigoshop_tax_rates'][$i];
             $rateDate = array('id' => '', 'rate' => $tax['rate'], 'label' => empty($tax['label']) ? __('Tax', 'jigoshop') : $tax['label'], 'class' => $tax['class'] == '*' ? 'standard' : $tax['class'], 'country' => $tax['country'], 'states' => isset($tax['is_all_states']) && $tax['is_all_states'] ? '' : $tax['state'], 'is_compound' => $tax['compound'] == 'yes' ? 1 : 0, 'postcodes' => '');
             $i++;
             $tax = isset($options['jigoshop_tax_rates'][$i]) ? $options['jigoshop_tax_rates'][$i] : '';
             while ($i < $endI && $tax['rate'] == $rateDate['rate'] && $tax['country'] == $rateDate['country']) {
                 if (isset($tax['is_all_states']) && $tax['is_all_states']) {
                     $rateDate['states'] = '';
                 } else {
                     $rateDate['states'] .= empty($tax['state']) ? '' : ',' . $options['jigoshop_tax_rates'][$i]['state'];
                 }
                 $i++;
                 $tax = isset($options['jigoshop_tax_rates'][$i]) ? $options['jigoshop_tax_rates'][$i] : '';
             }
             $this->taxService->save($rateDate);
             $this->checkSql();
         }
         $this->options->saveOptions();
         $this->checkSql();
         //			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 options end with error: ', 'jigoshop') . $e);
         return false;
     }
 }