1, 'isDefault' => 0, 'paperId' => 1, 'sheetId' => 1, 'labelWidth' => 105, 'labelHeight' => 48, 'orientation' => 0, 'fontSize' => '', 'paddingTop' => 10, 'paddingRight' => 0, 'paddingBottom' => 10, 'paddingLeft' => 0, 'withVariations' => 0, 'onlyParent' => 0, 'displayFieldName' => 0, ); protected $profilesPermissions; protected $tbl; public function __construct() { global $wpdb; $this->profilesPermissions = new ProfilesPermissions(); $this->tbl = $wpdb->prefix . Database::$tableProfiles; add_action('init', array($this, 'registerProfileMetaFields')); } public function registerProfileMetaFields() { register_meta(Database::$profilesMetaType, 'matching', array( 'type' => 'array', 'single' => true, 'default' => array(), 'sanitize_callback' => 'USWBG_a4bRecursiveSanitizeTextField', 'auth_callback' => function() { return current_user_can('manage_options'); }, 'show_in_rest' => false, 'revisions_enabled' => false, )); } public function update($id, $data) { global $wpdb; $profileTableData = array(); foreach (self::PROFILE_TABLE_FIELDS as $profileTableField) { if (isset($data[$profileTableField])) { $profileTableData[$profileTableField] = $data[$profileTableField]; unset($data[$profileTableField]); } } if (!empty($profileTableData)) { $updateResult = $wpdb->update( $this->tbl, $profileTableData, array('ID' => $id) ); } foreach ($data as $key => $value) { update_metadata(Database::$profilesMetaType, $id, $key, $value); } } public function createProfile($data) { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $insertResult = $wpdb->insert( $tableProfile, array( 'name' => $data['name'], 'author_id' => get_current_user_id(), 'templateId' => $data['templateId'], 'isDefault' => isset($data['isDefault']) ? $data['isDefault'] : 0, 'parent_id' => isset($data['parent_id']) ? $data['parent_id'] : null, ), array('%s', '%d', '%d', '%d') ); if ($insertResult) { $profileId = $wpdb->insert_id; foreach (self::PROFILE_TABLE_FIELDS as $profileTableField) { if (isset($data[$profileTableField])) { unset($data[$profileTableField]); } } foreach ($data as $key => $value) { $res = add_metadata(Database::$profilesMetaType, $profileId, $key, $value, true); if ($res === false) { $wpdb->insert( $wpdb->prefix . Database::$tableProfileMeta, array( 'barcode_v4_profile_id' => $profileId, 'meta_key' => $key, 'meta_value' => is_array($value) ? serialize($value) : $value, ), array('%d', '%s', '%s') ); } } } else { $profileId = null; } return $profileId; } public function delete($id) { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $deleteResult = $wpdb->delete($tableProfile, array('id' => $id)); if ($deleteResult) { foreach (get_metadata(Database::$profilesMetaType, $id) as $key => $values) { delete_metadata(Database::$profilesMetaType, $id, $key); } $this->profilesPermissions->deleteProfilePermissions($id); return true; } else { return false; } } public function getProfile($id) { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $profileRow = $wpdb->get_row( $wpdb->prepare("SELECT * FROM `{$tableProfile}` WHERE `id` = %d;", $id), ARRAY_A ); return $this->convertFromDb($profileRow); } public function getProfileByAuthorIdAndParentId($authorId, $parentId) { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $profileRow = $wpdb->get_row( $wpdb->prepare("SELECT * FROM `{$tableProfile}` WHERE `author_id` = %d AND `parent_id` = %d;", $authorId, $parentId), ARRAY_A ); if (empty($profileRow)) { return null; } return $this->convertFromDb($profileRow); } public function getProfilesCount() { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $profileRowsCount = $wpdb->get_var( "SELECT COUNT(*) FROM `{$tableProfile}` " ); return null !== $profileRowsCount ? intval($profileRowsCount) : 0; } public function createUserDefaultProfileIfNotExists() { if (empty($this->getProfilesCount())) { $productsProfileParams = array( 'templateId' => 1, 'paperId' => 1, 'sheetId' => 1, 'matching' => array( '[barcode_img_url]' => "[product-sku before='' after='' barcode-image-url=C128]", '[value1]' => "[product-name-variation before='' after='']", '[value3]' => "[actual-price before='' after='']", '[value5]' => "[product-sku before='' after='']", '[value7]' => "[wc-category before='' after='']", ), ); $productsProfileId = $this->createProfile(array_merge( self::PROFILE_DEFAULT_DATA, array( 'name' => 'Products', 'author_id' => get_current_user_id(), 'isDefault' => 1, ), $productsProfileParams )); $this->profilesPermissions->add($productsProfileId, null, ProductsPageImportProductsBtn::SLUG); $this->profilesPermissions->add($productsProfileId, null, ProductsPageImportProductBtn::SLUG); $this->profilesPermissions->add($productsProfileId, null, ProductPageImportSingleBtn::SLUG); $this->profilesPermissions->add($productsProfileId, null, ProductPageImportVariationBtn::SLUG); $this->profilesPermissions->add($productsProfileId, null, OrdersPageImportProductsBtn::SLUG); $this->profilesPermissions->add($productsProfileId, null, OrderPageImportAllItemsBtn::SLUG); $ordersProfileParams = array( 'templateId' => 1, 'paperId' => 1, 'sheetId' => 1, ); $ordersProfileId = $this->createProfile(array_merge( self::PROFILE_DEFAULT_DATA, array( 'name' => 'Orders', 'author_id' => get_current_user_id(), 'isDefault' => 1, ), $ordersProfileParams )); $this->profilesPermissions->add($ordersProfileId, null, OrdersPageImportOrdersBtn::SLUG); $this->profilesPermissions->add($ordersProfileId, null, OrdersPageImportOrderBtn::SLUG); $this->profilesPermissions->add($ordersProfileId, null, OrderPageImportOrderBtn::SLUG); $usersProfileParams = array( 'templateId' => 1, 'paperId' => 1, 'sheetId' => 1, ); $usersProfileId = $this->createProfile(array_merge( self::PROFILE_DEFAULT_DATA, array( 'name' => 'Users', 'author_id' => get_current_user_id(), 'isDefault' => 1, ), $usersProfileParams )); $this->profilesPermissions->add($usersProfileId, null, UsersPageImportUsersBtn::SLUG); } } public function getProfilesList() { global $wpdb; $profilesList = $wpdb->get_results("SELECT * FROM {$this->tbl};", ARRAY_A); return array_map(array($this, 'convertFromDb'), $profilesList); } protected function addMetaToProfileRow($profileRow) { $profileMetaData = $this->unserializeMetaData(wp_list_pluck(get_metadata(Database::$profilesMetaType, $profileRow['id']), 0)); return $profileRow ? array_merge($profileRow, $profileMetaData) : $profileMetaData; } protected function unserializeMetaData($metaFields) { return array_map(function ($value) { return is_serialized($value) ? maybe_unserialize($value) : $value; }, $metaFields); } public function setDefault($id) { global $wpdb; $tableProfile = $wpdb->prefix . Database::$tableProfiles; $unsetAllResult = $wpdb->query($wpdb->prepare(" UPDATE $tableProfile SET `isDefault` = %d ", 0)); $setFlagResult = $wpdb->update( $tableProfile, array('isDefault' => 1), array('ID' => $id) ); return $setFlagResult; } public function getProfileByName($name) { global $wpdb; return $wpdb->get_row( $wpdb->prepare("SELECT * FROM `{$this->tbl}` WHERE `name` = %s;", $name) ); } public function updateMatchingForTemplate($profile, $templateId) { $isMatchingChanged = false; $template = (new Templates())->get($templateId); $templateXml = $template->template; $templateMatchingShortcodes = Templates::getMatchingShortcodes($templateXml); $profile->matchingExclude = array(); $matchingExclude = array(); if (!property_exists($profile, 'matching')) { $profile->matching = array(); $isMatchingChanged = true; } foreach ($templateMatchingShortcodes as $matchingShortcode) { if (!isset($profile->matching[$matchingShortcode])) { $profile->matching[$matchingShortcode] = ''; $isMatchingChanged = true; } } foreach ($profile->matching as $matchShortcode => &$match) { if ( $matchShortcode === '[barcode_img_url]' && preg_match('/barcode-image-url=["\']?([^"\'\]\s]+)["\']?/', $match, $matches) && is_array($template->default_barcode_type) && !in_array($matches[1], $template->default_barcode_type) ) { $match = str_replace($matches[1], $template->default_barcode_type[0], $match); } if ( !in_array($matchShortcode , $templateMatchingShortcodes) ) { $matchingExclude[] = $matchShortcode; $isMatchingChanged = true; } } if ($isMatchingChanged) { $profile->matchingExclude = $matchingExclude; } } public function copyDefaultProfilesToCurrentUser() { if (!is_admin() || !is_user_logged_in()) { return; } if (!UserSettingsHelper::getOption("showOnlyOwnedByUserProfiles", "0")) { return; } foreach(array(ProfileTypeEnum::PRODUCT, ProfileTypeEnum::ORDER, ProfileTypeEnum::USER) as $profileType) { $defaultProfileId = intval(UserSettingsHelper::getOption("default_{$profileType}_profile", 1)); if (!empty($defaultProfileId)) { $defaultProfile = $this->getProfile($defaultProfileId); if (intval($defaultProfile->author_id) !== get_current_user_id() && !empty($defaultProfile)) { $copiedProfile = $this->getProfileByAuthorIdAndParentId(get_current_user_id(), $defaultProfile->id); if (empty($copiedProfile)) { $defaultProfile->author_id = get_current_user_id(); $defaultProfile->parent_id = $defaultProfile->id; $createdProfileId = $this->createProfile((array)$defaultProfile); if (!empty($createdProfileId)) { if ($profileType === ProfileTypeEnum::PRODUCT) { $this->profilesPermissions->add($createdProfileId, null, ProductsPageImportProductsBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, ProductsPageImportProductBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, ProductPageImportSingleBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, ProductPageImportVariationBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, OrdersPageImportProductsBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, OrderPageImportAllItemsBtn::SLUG); } elseif ($profileType === ProfileTypeEnum::ORDER) { $this->profilesPermissions->add($createdProfileId, null, OrdersPageImportOrdersBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, OrdersPageImportOrderBtn::SLUG); $this->profilesPermissions->add($createdProfileId, null, OrderPageImportOrderBtn::SLUG); } elseif ($profileType === ProfileTypeEnum::USER) { $this->profilesPermissions->add($createdProfileId, null, UsersPageImportUsersBtn::SLUG); } } } } } } } public function convertFromDb($profileRow) { $profileRow = (object) $this->addMetaToProfileRow($profileRow); $this->updateMatchingForTemplate($profileRow, $profileRow->templateId); if (is_admin() && current_user_can('manage_options') && !empty($profileRow->author_id)) { $user = get_userdata($profileRow->author_id); $profileRow->author_name = $user->display_name; } else { $profileRow->author_name = ''; } return $profileRow; } }