search = new Search(); } public function getBarcodes() { Request::checkNonce(); if (empty(wp_get_current_user()->allcaps['read_product'])) { wp_die(); } $post = Request::getData(); $data = array(); foreach (array( 'isImportSingleVariation', 'profileId', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } foreach (array( WoocommerceLabelMaker::IMPORT_ITEMS_IDS_PARAM, 'approxImageWidth' ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } $validationRules = array( WoocommerceLabelMaker::IMPORT_ITEMS_IDS_PARAM => 'required|array|bail', 'isImportSingleVariation' => 'string', 'profileId' => 'numeric', 'approxImageWidth' => 'array', ); $data = Validator::create($data, $validationRules, true)->validate(); $data[WoocommerceLabelMaker::IMPORT_ITEMS_IDS_PARAM] = uslp_filter_by_capability('read_product', $data[WoocommerceLabelMaker::IMPORT_ITEMS_IDS_PARAM]); $postsBarcodesGenerator = new WoocommerceLabelMaker($data); $result = $postsBarcodesGenerator->make(); uslp_a4bJsonResponseStream($result); wp_die(); } public function getBarcodesByOrders() { Request::checkNonce(); if (empty(wp_get_current_user()->allcaps['read_shop_order'])) { wp_die(); } $post = Request::getData(); $data = array(); foreach (array( 'profileId', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } foreach (array( 'ordersCategories', WoocommerceOrdersLabelMaker::IMPORT_ITEMS_IDS_PARAM, 'approxImageWidth', ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } $validationRules = array( 'ordersCategories' => 'requiredItem:if_empty,'.WoocommerceOrdersLabelMaker::IMPORT_ITEMS_IDS_PARAM.'|array|bail', WoocommerceOrdersLabelMaker::IMPORT_ITEMS_IDS_PARAM => 'requiredItem:if_empty,ordersCategories|array|bail', 'profileId' => 'numeric', 'approxImageWidth' => 'array', ); $data = Validator::create($data, $validationRules, true)->validate(); $data[WoocommerceOrdersLabelMaker::IMPORT_ITEMS_IDS_PARAM] = uslp_filter_by_capability('read_shop_order', $data[WoocommerceOrdersLabelMaker::IMPORT_ITEMS_IDS_PARAM]); $postsBarcodesGenerator = new WoocommerceOrdersLabelMaker($data); $result = $postsBarcodesGenerator->make(); uslp_a4bJsonResponse($result); } public function getBarcodesByOrderProducts() { Request::checkNonce(); if (empty(wp_get_current_user()->allcaps['read_shop_order'])) { wp_die(); } $post = Request::getData(); $data = array(); foreach (array('profileId') as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } foreach (array( WoocommerceOrderProductsLabelMaker::IMPORT_ITEMS_IDS_PARAM, 'approxImageWidth', ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } $validationRules = array( WoocommerceOrderProductsLabelMaker::IMPORT_ITEMS_IDS_PARAM => 'required|array', 'profileId' => 'numeric', 'approxImageWidth' => 'array', ); $data = Validator::create($data, $validationRules, true)->validate(); $data[WoocommerceOrderProductsLabelMaker::IMPORT_ITEMS_IDS_PARAM] = uslp_filter_by_capability('read_shop_order', $data[WoocommerceOrderProductsLabelMaker::IMPORT_ITEMS_IDS_PARAM]); $postsBarcodesGenerator = new WoocommerceOrderProductsLabelMaker($data); $result = $postsBarcodesGenerator->make(); uslp_a4bJsonResponse($result); } public function getBarcodesByOrderItems() { Request::checkNonce(); if (empty(wp_get_current_user()->allcaps['read_shop_order'])) { wp_die(); } $post = Request::getData(); $data = array(); foreach (array('profileId') as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } foreach (array( WoocommerceOrderItems::IMPORT_ITEMS_IDS_PARAM, 'approxImageWidth' ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } $validationRules = array( WoocommerceOrderItems::IMPORT_ITEMS_IDS_PARAM => 'required|array', 'profileId' => 'numeric', 'approxImageWidth' => 'array', ); $data = Validator::create($data, $validationRules, true)->validate(); $validatedItemIds = array(); foreach ($data[WoocommerceOrderItems::IMPORT_ITEMS_IDS_PARAM] as $itemId) { $orderId = wc_get_order_id_by_order_item_id($itemId); if (current_user_can('read_shop_order', $orderId)) { $validatedItemIds[] = $itemId; } } $data[WoocommerceOrderItems::IMPORT_ITEMS_IDS_PARAM] = $validatedItemIds; $postsBarcodesGenerator = new WoocommerceOrderItems($data); $result = $postsBarcodesGenerator->make(); uslp_a4bJsonResponse($result); } public function countProductsByCustomField() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $post = Request::getData(); $data = array(); foreach (array('field', 'postType') as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'field' => 'required|string', 'postType' => 'string', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $fields = array_map('trim', explode(',', $data['field'])); $postType = isset($data['postType']) ? $data['postType'] : ""; $counters = array(); foreach ($fields as $field) { $fieldKey = $field; if (empty($field)) { continue; } elseif (0 === strpos($field, 'product.')) { $field = substr($field, 8); } elseif (0 === strpos($field, 'variation.')) { $field = substr($field, 10); } if ($postType === "import-orders") { $postTypes = SupportedPostTypes::getCommaSeparatedString(SupportedPostTypes::WC_ORDERS); } elseif ($postType === "import-cf-messages") { $postTypes = SupportedPostTypes::getCommaSeparatedString(SupportedPostTypes::CF7); } elseif ($postType === "import-atum-po") { $postTypes = SupportedPostTypes::getCommaSeparatedString(SupportedPostTypes::ATUM); } else { $postTypes = SupportedPostTypes::getCommaSeparatedString(SupportedPostTypes::ALL); } $count = $this->search->getCustomFieldCount($field, $postTypes); if ( $postType === "import-cf-messages" && $count === '0' ) { $count = $this->search->getCustomFieldCount('_field_' . $field, $postTypes); } $counters[$fieldKey] = $count; } return array('counters' => $counters); } }