1, 'replacements' => $this->getTemplateReplacements($itemData, $this->templateShortcodesArgs), ); } protected function getCodeField($post) { if ($this->activeTemplate->code_match) { return $this->getCodeValue($post, $this->activeTemplate->single_product_code); } else { return isset($this->data['lineBarcode']) ? $this->getField($post, $this->data['lineBarcode']) : ''; } } protected function getCodeValue($post, $template) { $this->extractTemplateShortcodes($template, $this->barcodeTemplateShortcodesArgs); $shortCodesReplacements = $this->getTemplateReplacements($post, $this->barcodeTemplateShortcodesArgs); $html = $template; foreach ($shortCodesReplacements as $shortCode => $replacement) { $html = str_replace($shortCode, $replacement, $html); } return $html; } protected function getField($post, &$field) { $this->registerShortcodeClass($field['type']); $value = ''; $field['profile'] = $this->profile; $value = apply_filters('uslp_get_shortcode_value_hook', $value, $field['type'], $post, $field); $fieldName = apply_filters('uslp_generator_get_shortcode_name_hook', $value, $field['type'], $post, $field); $value = $this->prepareFieldValue($fieldName, $value, $field); return (string) $value; } public function getTemplateReplacements($post, $shortcodesArgs) { $replacements = new \ArrayObject(); foreach ($shortcodesArgs as $shortCode => $args) { try { $replacements[$shortCode] = $this->getShortcodeFieldValue($post, $args); } catch (LabelGenerationError $e) { $replacements[$shortCode] = ''; $replacements['barcode_error_message'] = $e->getMessage(); } } return $replacements; } protected function getShortcodeFieldValue($post, $args) { $result = ''; $args['value'] = isset($args['value']) ? $args['value'] : ''; $argsValues = 'static' !== $args['type'] ? explode('|', $args['value']) : array($args['value']); foreach ($argsValues as $argsValue) { if (0 === strpos($argsValue, 'parent.')) { $post = !empty($post->post_parent) ? get_post($post->post_parent) : $post; $argsValue = str_replace('parent.', '', $argsValue); } $field = array('type' => $args['type'], 'value' => $argsValue, 'args' => $args); switch ($args['type']) { default: $text = $this->getField($post, $field); break; } $text = $this->round($text, $field); if (!empty($text)) { $texts[] = $text; $result = implode(' ', $texts); if (!empty($args['before'])) { $result = $args['before'] . $result; } if (!empty($args['after'])) { $result = $result . $args['after']; } break; } } $result = $this->convertValueToBarcodeImageUrlIfNeed($field, $result); return $result; } public function prepareFieldValue($fieldName, $value, $field) { if (!empty($field['args']['barcode-image-url']) || !empty($field['args']['barcode-image'])) { return $value; } if (empty($field['args']['isMatching'])) { return $value; } if ($this->profile->displayFieldName) { $value = ''.$value.''; if (!empty($fieldName) && !empty($value)) { $value = '' . $fieldName . ': ' . $value; } } return $value; } public function round($value, &$field) { if ( is_plugin_active('woocommerce/woocommerce.php') && false !== strpos($value, wc_get_price_decimal_separator()) ) { $tmpValue = str_replace(wc_get_price_decimal_separator(), '.', $value); } else { $tmpValue = $value; } if (isset($field['args']['round-number']) && 'true' === $field['args']['round-number'] && is_numeric($tmpValue)) { unset($field['args']['round-number']); $result = round((float)$tmpValue); } else { $result = $value; } return $result; } }