maker = $maker; } else { throw new \Exception("Label data maker class not provided"); } self::$instancesTags[$cls] = array(); } if (empty($tag)) { $tag = self::$instances[$cls]->getShortcodeTag(); } if (!in_array($tag, self::$instancesTags[$cls])) { if (self::$instances[$cls]->isEnabled()) { self::$instancesTags[$cls][] = $tag; uslp_add_custom_shortcode($tag, function($value, $shortcode, $item, $field) use ($cls) { return self::$instances[$cls]->convertValueToBarcodeImageUrlIfNeed( $field, self::$instances[$cls]->isItemValid($item) ? self::$instances[$cls]->getValue($item, $field) : '' ); }); uslp_add_custom_shortcode_name($tag, function($value, $shortcode, $item, $field) use ($cls) { return self::$instances[$cls]->getName($field); }); return true; } else { unset(self::$instances[$cls]); return false; } } else { return true; } } public static function getInstance() { $cls = static::class; if (!isset(self::$instances[$cls])) { $cls::register(); } return self::$instances[$cls]; } public static function fullIntegrationSlug() { return static::INTEGRATION . '.' . static::INTEGRATION_TYPE; } protected function getShortcodeTag() { $ref = new \ReflectionClass(self::$instances[static::class]); return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $ref->getShortName())); } abstract public function getValue($post, $field); protected function isItemValid($item) { return true; } public function convertValueToBarcodeImageUrlIfNeed($field, $value) { return $value; } protected function getBarcodeImageUrl($value, $barcodeType) { $barcodeType = strtoupper($barcodeType); switch ($barcodeType) { case 'CODE128': $barcodeType = 'C128'; break; case 'CODE39': $barcodeType = 'C39'; break; } $validationResult = $this->maker->a4barcodes->validateBarcode($value, $barcodeType, $this->maker->data); if ($validationResult['is_valid']) { $imageUrl = $this->maker->a4barcodes->generateXml(array( 'lineBarcode' => $value, 'algorithm' => $barcodeType, )); $imageUrl = preg_replace('/.*<\/desc>/ms', '', $imageUrl); $imageUrl = "data:image/svg+xml;base64," . base64_encode($imageUrl); } else { $imageUrl = ''; } return $imageUrl; } public function getName($field) { return preg_replace('/\s\(.*\)/', '', $this->display_name); } public function addInfo(&$shortcodesInfoArr) { $shortcode = self::$instances[static::class]; $shortcodeTag = $this->getShortcodeTag(); $shortcodesInfoArr[$shortcodeTag] = array( 'integration' => $shortcode::INTEGRATION, 'integration_name' => $shortcode::INTEGRATION_NAME, 'integration_type' => $shortcode::INTEGRATION_TYPE, 'integration_type_name' => $shortcode::INTEGRATION_TYPE_NAME, 'label' => $shortcode->display_name, 'shortcode' => "[$shortcodeTag]", 'show_in_matching' => $shortcode::SHOW_IN_MATCHING, 'show_in_doc' => $shortcode::SHOW_IN_DOC, 'have_variants' => $shortcode::HAVE_VARIANTS, 'variants' => $shortcode->getVariants(), 'order' => $shortcode::ORDER, 'doc_tab_name_slug' => $shortcode::DOC_TAB_NAME_SLUG, 'doc_tab_name_title' => $shortcode::DOC_TAB_NAME_TITLE, 'doc_tab_content_slug' => $shortcode::DOC_TAB_CONTENT_SLUG, 'doc_section_name' => $shortcode::DOC_SECTION_NAME, 'doc_lines' => $shortcode::DOC_LINES, ); } public function isEnabled() { return true; } public function getVariants() { return array(); } public function update($subject) { } }