From b3a07651a67fcba3068b1d8a8a31f43463ca3745 Mon Sep 17 00:00:00 2001 From: Luke Towers Date: Fri, 3 Jan 2025 23:43:35 -0600 Subject: [PATCH] Improves support for snippets defined in partials --- classes/Snippet.php | 20 ++++++++++++++------ classes/SnippetManager.php | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/classes/Snippet.php b/classes/Snippet.php index 9698bb2..90b5929 100644 --- a/classes/Snippet.php +++ b/classes/Snippet.php @@ -363,7 +363,7 @@ public static function processTemplateSettings($template) foreach ($parsedProperties as $index => &$property) { $property['id'] = $index; - if (isset($property['options'])) { + if (isset($property['options']) && is_array($property['options'])) { $property['options'] = self::dropDownOptionsToString($property['options']); } } @@ -408,18 +408,26 @@ protected static function preprocessPropertyValues($theme, $snippetCode, $compon /** * Converts a keyed object to an array, converting the index to the "property" value. - * @return array */ - protected static function parseIniProperties($properties) + protected static function parseIniProperties($properties): array { foreach ($properties as $index => $value) { - $properties[$index]['property'] = $index; + if (!is_array($value['options'])) { + $value['options'] = self::dropDownOptionsToArray($value['options']); + } + + if (is_int($index)) { + $properties[$value['property']] = $value; + unset($properties[$index]); + } elseif (is_string($index)) { + $properties[$index]['property'] = $index; + } } return array_values($properties); } - protected static function dropDownOptionsToArray($optionsString) + protected static function dropDownOptionsToArray(string $optionsString): array { $options = explode('|', $optionsString); @@ -447,7 +455,7 @@ protected static function dropDownOptionsToArray($optionsString) return $result; } - protected static function dropDownOptionsToString($optionsArray) + protected static function dropDownOptionsToString(array $optionsArray) { $result = []; $isAssoc = (bool) count(array_filter(array_keys($optionsArray), 'is_string')); diff --git a/classes/SnippetManager.php b/classes/SnippetManager.php index 1fa9a59..eccf014 100644 --- a/classes/SnippetManager.php +++ b/classes/SnippetManager.php @@ -91,7 +91,7 @@ public function removeSnippet(string $snippetCode) * @param string $code Specifies the snippet code. * @param string $$componentClass Specifies the snippet component class, if available. * @param boolean $allowCaching Specifies whether caching is allowed for the call. - * @return array Returns an array of Snippet objects. + * @return Snippet|null Returns the Snippet object if found */ public function findByCodeOrComponent($theme, $code, $componentClass, $allowCaching = false) { @@ -185,7 +185,7 @@ protected static function getPartialMapCacheKey($theme) /** * Returns a list of partial-based snippets and corresponding partial names. * @param \Cms\Classes\Theme $theme Specifies a parent theme. - * @return Returns an associative array with the snippet code in keys and partial file names in values. + * @return array Returns an associative array with the snippet code in keys and partial file names in values. */ public function getPartialSnippetMap($theme) {