Skip to content

Commit

Permalink
Refactored getting public ID
Browse files Browse the repository at this point in the history
  • Loading branch information
ihorvansach committed Jun 27, 2023
1 parent 0587b9a commit d35571d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Controller/Adminhtml/Container/Generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function execute()
'rm' => true
];
$this->fileFactory->create(
sprintf($this->config->getPublicId() . '_%s.json', $this->dateTime->date('Y-m-d_H-i-s')),
sprintf('GTM' . '_%s.json', $this->dateTime->date('Y-m-d_H-i-s')),
$fileContent,
DirectoryList::MEDIA,
'application/json'
Expand Down
21 changes: 16 additions & 5 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ public function __construct(
*/
public function isEnabled(string $storeId = null): bool
{
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId) && (
($this->getPublicId($storeId) && 'use_public_id' === $this->getInstallGtm($storeId))
|| ($this->getGtmScript($storeId) && 'use_head_and_body_script' === $this->getInstallGtm($storeId))
);
return (bool)$this->getConfig(self::XML_PATH_EXTENSION_ENABLED, $storeId);
}

/**
Expand Down Expand Up @@ -117,7 +114,21 @@ public function getContainerId(string $storeId = null): string
*/
public function getPublicId(string $storeId = null): string
{
return trim((string)$this->getConfig(self::XML_PATH_WEB_PUBLIC_ID, $storeId));
$result = trim((string)$this->getConfig(self::XML_PATH_WEB_PUBLIC_ID, $storeId));

if (!$result) {
if ($gtmScript = $this->getGtmScript($storeId)) {
$pattern = '/GTM-[A-Z0-9]+/';
$matches = [];
if (preg_match($pattern, $gtmScript, $matches)) {
if (isset($matches[0])) {
return (string)$matches[0];
}
}
}
}

return $result;
}

/**
Expand Down

0 comments on commit d35571d

Please sign in to comment.