profiles = new Profiles(); $this->profilePermissions = new ProfilesPermissions(); $this->userSettings = new UserSettings(); $this->integrationButtons = IntegrationButtons::getInstance(); } public function create() { try { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); foreach (array( 'profileName', 'btn_slug', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'profileName' => 'string|required', 'btn_slug' => 'string|required', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $newProfileId = $this->profiles->createProfile(array_merge(array( 'name' => $data['profileName'], 'author_id' => get_current_user_id(), ), Profiles::PROFILE_DEFAULT_DATA)); if (empty($newProfileId)) { $result['error'][] = esc_html__('Profile not created', 'a4-barcode-generator'); $result['success'] = false; } else { $buttonsToAddNewProfilePermissions = $this->integrationButtons->getButtonsIncludingSameIntegrationTypesBySlug($data['btn_slug']); foreach ($buttonsToAddNewProfilePermissions as $button) { $this->profilePermissions->add($newProfileId, null, $button->getSlug()); } $result['profile'] = $this->profiles->getProfile($newProfileId); $result['profiles'] = $this->profiles->getProfilesList(); } } catch (LabelPrintingError $e) { $result['error'][] = esc_html__('Creating profile error.', 'a4-barcode-generator'); $result['success'] = false; } return $result; } public function save($post = null) { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } if (empty($post)) { $post = Request::getData(); } $data = array(); foreach (array( 'id', 'name', 'templateId', 'author_id', 'parent_id', 'paperId', 'sheetId', 'labelWidth', 'labelHeight', 'orientation', 'fontSize', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft', 'withVariations', 'onlyParent', 'displayPriceWithCurrency', 'printGridOnPages', 'sortAlphabetically', 'attributeIsntSpec', 'matching', 'displayFieldName', ) as $key) { if (isset($post[$key])) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($post[$key]); } } foreach ($post as $key => $value) { if (preg_match('/^labelOptionsValueTPL\d+$/', $key)) { $data[$key] = uslp_a4bRecursiveSanitizeTextField($value); } } if (!isset($post['author_id'])) { $data['author_id'] = get_current_user_id(); } $validationOptions = array( 'id' => 'numeric', 'name' => 'required|string', 'templateId' => 'required|numeric', 'author_id' => 'numeric', 'parent_id' => 'numeric', 'paperId' => 'string', 'sheetId' => 'string', 'labelWidth' => 'string', 'labelHeight' => 'string', 'orientation' => 'string', 'fontSize' => 'string', 'paddingTop' => 'string', 'paddingRight' => 'string', 'paddingBottom' => 'string', 'paddingLeft' => 'string', 'withVariations' => 'string', 'onlyParent' => 'string', 'displayFieldName' => 'string', 'displayPriceWithCurrency' => 'string', 'printGridOnPages' => 'string', 'sortAlphabetically' => 'string', 'attributeIsntSpec' => 'string', 'matching' => 'array', ); foreach ($post as $key => $value) { if (preg_match('/^labelOptionsValueTPL\d+$/', $key)) { $validationOptions[$key] = 'array'; } } $result = array(); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } if (isset($post["update"])) { $postUpdate = $post["update"]; unset($post["update"]); } $profile = isset($data['id']) ? $this->profiles->getProfile($data['id']) : null; if (isset($postUpdate) && !$postUpdate) { if ($profile) { return array( "need_approving" => 1, "error" => "Profile exists", ); } else { $newProfileId = $this->profiles->createProfile($data); $savedProfile = !empty($newProfileId) ? $this->profiles->getProfile($newProfileId) : null; } } else { if ($profile) { if ( current_user_can('manage_options') || $profile->author_id == get_current_user_id() ) { $this->profiles->update($profile->id, $data); $savedProfile = $this->profiles->getProfile($profile->id); } else { $savedProfile = $profile; } } else { $newProfileId = $this->profiles->createProfile($data); $savedProfile = !empty($newProfileId) ? $this->profiles->getProfile($newProfileId) : null; } } $result['profile'] = $savedProfile; return $result; } public function rename() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); foreach (array( 'id', 'name', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'id' => 'required|numeric', 'name' => 'required|string', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $profile = $this->profiles->getProfile($data['id']); if (empty($profile)) { $result['error'][] = esc_html__('Profile not found', 'a4-barcode-generator'); $result['success'] = false; } if ( !current_user_can('manage_options') && (get_current_user_id() != intval($profile->author_id)) ) { $result['error'][] = esc_html__('No rights to edit this profile', 'a4-barcode-generator'); $result['success'] = false; return $result; } if ($profile) { $this->profiles->update($profile->id, $data); $result['profile'] = $this->profiles->getProfile($profile->id); $result['profiles'] = $this->profiles->getProfilesList(); } return $result; } public function copy() { try { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); if (isset($post['id'])) { $data['id'] = sanitize_key($post['id']); } if (isset($post['profileName'])) { $data['profileName'] = sanitize_text_field($post['profileName']); } $validationOptions = array( 'id' => 'numeric|required', 'profileName' => 'string|required', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $profileToCopy = (array)$this->profiles->getProfile($data['id']); if (empty($profileToCopy)) { $result['error'][] = esc_html__('Profile not found', 'a4-barcode-generator'); $result['success'] = false; } else { unset($profileToCopy['id']); $profileToCopy['name'] = $data['profileName']; $newProfileId = $this->profiles->createProfile($profileToCopy); if (empty($newProfileId)) { $result['error'][] = esc_html__('Profile not created', 'a4-barcode-generator'); $result['success'] = false; } else { $profilePermissionsData = $this->profilePermissions->getByProfileId($data['id']); foreach ($profilePermissionsData as $profilePermissionData) { $this->profilePermissions->add($newProfileId, $profilePermissionData['role'], $profilePermissionData['button_slug']); } $result['profile'] = $this->profiles->getProfile($newProfileId); $result['profiles'] = $this->profiles->getProfilesList(); } } } catch (LabelPrintingError $e) { $result['error'][] = esc_html__('Copy profile error.', 'a4-barcode-generator'); $result['success'] = false; } return $result; } public function delete() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); if (isset($post['id'])) { $data['id'] = sanitize_key($post['id']); } $validationOptions = array( 'id' => 'numeric|required', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $profile = $this->profiles->getProfile($data['id']); if ( !current_user_can('manage_options') && (get_current_user_id() != intval($profile->author_id)) ) { $result['error'][] = esc_html__('No rights to delete this profile', 'a4-barcode-generator'); $result['success'] = false; return $result; } $deleteResult = $this->profiles->delete($data['id']); if (!$deleteResult) { $result['error'][] = esc_html__('Profile not deleted', 'a4-barcode-generator'); $result['success'] = false; } else { $result['profile'] = $profile; $result['profiles'] = $this->profiles->getProfilesList(); } return $result; } public function setDefault() { Request::checkNonce(); if (!current_user_can('read')) { wp_die(); } $result = array( 'success' => true, 'error' => array(), ); $post = Request::getData(); $data = array(); foreach (array( 'id', 'btn_slug', ) as $key) { if (isset($post[$key])) { $data[$key] = sanitize_text_field($post[$key]); } } $validationOptions = array( 'id' => 'numeric|required', 'btn_slug' => 'string|required', ); $validator = Validator::create($data, $validationOptions); $data = $validator->validate(); if (false === $data) { $result['success'] = false; $result['error'] = $validator->getErrors(); return $result; } $profile = $this->profiles->getProfile($data['id']); if (empty($profile)) { $result['error'][] = esc_html__('Profile not found', 'a4-barcode-generator'); $result['success'] = false; } else { $this->userSettings->save("button-{$data['btn_slug']}", $data['id'], get_current_user_id()); } return $result; } }