25 private static function getStyleClasses(
string $content): array
27 if (preg_match_all(
'/class="([^"]+)"/', $content, $matches))
30 foreach ($matches[1] as $classes)
33 $allClasses[] =
' ' . $classes .
' ';
45 private static function getThemeManifest(
string $tplCode): array
49 if (file_exists($path))
51 $manifest = include $path;
52 if (is_array($manifest))
67 private static function removeSiblingsClasses(
string $classString, array $targetClasses,
string $namespace): string
69 static $classesGroups = [];
71 $styleManifest = Block::getStyle();
74 if (!array_key_exists($namespace, $classesGroups))
76 $classesGroups[$namespace] = [];
78 isset($styleManifest[$namespace][
'style']) &&
79 is_array($styleManifest[$namespace][
'style'])
82 foreach ($styleManifest[$namespace][
'style'] as $style)
84 if (isset($style[
'items']) && is_array($style[
'items']))
87 foreach ($style[
'items'] as $item)
89 if (isset($item[
'value']) && is_string($item[
'value']))
91 $classesGroup[] = trim($item[
'value']);
96 $classesGroups[$namespace][] = $classesGroup;
103 $allClasses = $classesGroups[$namespace];
106 $findSiblings =
function($targetClass) use($allClasses)
108 $targetClass = trim($targetClass);
109 foreach ($allClasses as $classes)
111 if (in_array($targetClass, $classes))
121 foreach ($targetClasses as $targetClass)
124 foreach ($findSiblings($targetClass) as $classRemove)
126 $classString = str_replace(
' ' . $classRemove .
' ',
' ', $classString);
131 return trim($classString);
143 $blockMetadata = $block->
getMeta();
144 if (!$blockMetadata[
'SITE_TPL_CODE'])
149 $themeManifest = self::getThemeManifest($blockMetadata[
'SITE_TPL_CODE']);
151 isset($themeManifest[
'newBlockStyle']) &&
152 is_array($themeManifest[
'newBlockStyle'])
155 $themeManifest = $themeManifest[
'newBlockStyle'];
162 $contentWasChanged =
false;
164 $blockClasses = self::getStyleClasses($blockContent);
171 if (!isset($blockManifest[
'namespace']))
176 $blockNamespace = $blockManifest[
'namespace'];
177 $semanticManifest = Block::getSemantic();
178 if (isset($semanticManifest[$blockNamespace]))
180 $semanticManifest = $semanticManifest[$blockNamespace];
188 foreach ($themeManifest as $semanticCode => $needClasses)
190 if (!isset($semanticManifest[$semanticCode]))
194 if (!is_array($needClasses))
196 $needClasses = (array)$needClasses;
200 foreach ((array) $semanticManifest[$semanticCode] as $semanticClass)
202 $semanticClass =
' ' . $semanticClass .
' ';
203 foreach ($blockClasses as $classesString)
205 if (mb_strpos($classesString, $semanticClass) !==
false)
207 $contentWasChanged =
true;
208 $newClassString = self::removeSiblingsClasses(
213 $blockContent = str_replace(
214 'class="' . trim($classesString) .
'"',
215 'class="' . $newClassString .
' ' . implode(
' ', $needClasses) .
'"',
224 if ($contentWasChanged)
239 if (!array_key_exists($siteId, $sites))
241 $sites[$siteId] =
null;
242 $res = Site::getList([
250 if ($row = $res->fetch())
252 if (!$row[
'TPL_CODE'] && mb_strpos($row[
'XML_ID'],
'|'))
254 [, $row[
'TPL_CODE']] = explode(
'|', $row[
'XML_ID']);
256 if ($row[
'TPL_CODE'])
258 $manifest = self::getThemeManifest($row[
'TPL_CODE']);
260 isset($manifest[
'newPageTemplate'][0]) &&
261 is_string($manifest[
'newPageTemplate'][0])
264 $sites[$siteId] = $manifest[
'newPageTemplate'][0];
272 return $sites[$siteId];