generatedLabelsDataStorage = new GeneratedLabelsData(); $this->profiles = new Profiles(); } public function delete() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $id = isset($post['ids']) ? uslp_a4bRecursiveSanitizeTextField($post['ids']) : ''; $data = Validator::create(array('ids' => $id), array('ids' => 'array|required'), true)->validate(); $deleteResult = $this->generatedLabelsDataStorage->deleteByIdsAndUserId(array( 'ids' => array_map('intval', $data['ids']), 'user_id' => get_current_user_id(), )); if (false === $deleteResult) { $result['error'][] = esc_html__('Generated label delete error', 'a4-barcode-generator'); } else { $result['uslpLabels'] = $this->generatedLabelsDataStorage->get(get_current_user_id()); } uslp_a4bJsonResponse($result); } public function clear() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $deleteResult = $this->generatedLabelsDataStorage->delete(array('user_id' => get_current_user_id())); if (false === $deleteResult) { $result['error'][] = esc_html__('Clear labels error', 'a4-barcode-generator'); } else { $result['uslpLabels'] = $this->generatedLabelsDataStorage->get(get_current_user_id()); } return $result; } public function copy() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); foreach (array('id', 'count', 'position') as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $data = Validator::create($data, array( 'id' => 'numeric|required', 'count' => 'numeric|required', 'position' => 'string|required', ), true)->validate(); for ($i = 0; $i < $data['count']; $i++) { $copyResult = $this->generatedLabelsDataStorage->copy($data['id'], get_current_user_id(), $data['position']); if (false === $copyResult) { $result['error'][] = esc_html__('Copy label error', 'a4-barcode-generator'); } else { $result['uslpLabels'] = $this->generatedLabelsDataStorage->get(get_current_user_id()); } } uslp_a4bJsonResponse($result); } public function recreate($post = null) { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); if (empty($post)) { $post = Request::getData(); } $data = array(); foreach (array( 'ids', 'replacements', 'approxImageWidth', ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } foreach (array( 'profileId', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'ids' => 'array|required', 'replacements' => 'array|required', 'profileId' => 'numeric', 'approxImageWidth' => 'array', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $labels = $this->generatedLabelsDataStorage->get(get_current_user_id(), $data['ids']); if (empty($labels)) { $result['error'][] = esc_html__("Labels not found or you don't have rights to change them.", 'a4-barcode-generator'); } else { foreach ($labels as $label) { if (!class_exists($label->maker_class)) { continue; } $dataForMaker = array(); $dataForMaker['notSave'] = true; $dataForMaker['profileId'] = !empty($data['profileId']) && !empty(intval($data['profileId'])) ? intval($data['profileId']) : $label->profile_id; $dataForMaker['format'] = $label->barcode_type; $dataForMaker['approxImageWidth'] = !empty($data['approxImageWidth']) ? $data['approxImageWidth'] : array(); if (!empty($label->order_item_id) && !empty(intval($label->order_item_id))) { $label->maker_class = WoocommerceOrderItems::class; $dataForMaker[WoocommerceOrderItems::IMPORT_ITEMS_IDS_PARAM] = array($label->order_item_id); } else { $dataForMaker[constant($label->maker_class . '::IMPORT_ITEMS_IDS_PARAM')] = array($label->item_id); } $labelMaker = new $label->maker_class($dataForMaker); $makeResult = $labelMaker->make(); if (!empty($makeResult['error'])) { $result['error'] = array_merge($result['errors'], $makeResult['error']); continue; } if ( empty($makeResult['uslpLabels']) || empty($makeResult['uslpLabels'][0]) || empty($makeResult['uslpLabels'][0]['replacements']) ) { continue; } unset($label->replacements['barcode_error_message']); foreach ($makeResult['uslpLabels'][0]['replacements'] as $newReplacementKey => $newReplacementValue) { $label->replacements[$newReplacementKey] = $newReplacementValue; } $this->generatedLabelsDataStorage->update($label->id, (array) $label); } $result['uslpLabels'] = $this->generatedLabelsDataStorage->get(get_current_user_id()); $lastLabelProfile = $this->profiles->getProfile($dataForMaker['profileId']); if ( !empty($lastLabelProfile) && property_exists($lastLabelProfile, 'sortAlphabetically') && '1' === $lastLabelProfile->sortAlphabetically && !empty($labelMaker) ) { $result['uslpLabels'] = $labelMaker->sortByTitle($result['uslpLabels']); } } return $result; } public function sort() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); if (isset($post['ids'])) { $data['ids'] = uslp_a4bRecursiveSanitizeTextField($post['ids']); } $data = Validator::create($data, array('ids' => 'required|array'), true)->validate(); foreach ($data['ids'] as $index => $id) { $this->generatedLabelsDataStorage->update($id, array('order_number' => $index)); } $result['uslpLabels'] = $this->generatedLabelsDataStorage->get(get_current_user_id()); uslp_a4bJsonResponse($result); } }