$value) { $valueStr = self::toString($value); if ($valueStr !== '') { $parts[] = $key . $keyValueSeparator . $valueStr; } } return implode($separator, $parts); } /** * Format an indexed array as a comma-separated list * * @param array $array Indexed array to format * @param string $separator Separator between items (default: ', ') * @return string Comma-separated string */ public static function formatList(array $array, string $separator = ', '): string { if (empty($array)) { return ''; } $stringValues = array_map(function($value) { return self::toString($value); }, $array); $stringValues = array_filter($stringValues, function($value) { return $value !== ''; }); return implode($separator, $stringValues); } /** * Batch convert an array of variables for str_replace() or similar operations * * Takes an array of [placeholder => value] and returns [placeholder => safeString] * * @param array $variables Map of placeholder => mixed value * @param string $emptyDefault Default for empty values * @return array Map of placeholder => string value */ public static function convertVariables(array $variables, string $emptyDefault = ''): array { $converted = []; foreach ($variables as $placeholder => $value) { $converted[$placeholder] = self::toString($value, $emptyDefault); } return $converted; } /** * Check if an array is associative (has string keys) * * @param array $array Array to check * @return bool True if associative, false if indexed */ private static function isAssociativeArray(array $array): bool { if (empty($array)) { return false; } return array_keys($array) !== range(0, count($array) - 1); } /** * Format product attributes specifically * * Helper method for WooCommerce product attributes * * @param mixed $attributes Product attributes (array or string) * @return string Formatted attributes string */ public static function formatProductAttributes($attributes): string { if (is_string($attributes)) { return $attributes; } if (!is_array($attributes) || empty($attributes)) { return ''; } $formatted = []; foreach ($attributes as $key => $value) { if (is_array($value)) { $valueStr = implode(', ', $value); } else { $valueStr = (string) $value; } if ($valueStr !== '') { $formatted[] = $key . ': ' . $valueStr; } } return implode(', ', $formatted); } /** * Format custom variables from ACF, post meta, etc. * * @param mixed $value Custom field value (could be anything) * @param string $fieldType Optional field type hint for better formatting * @return string Formatted value */ public static function formatCustomField($value, string $fieldType = ''): string { switch ($fieldType) { case 'date': case 'datetime': if (is_numeric($value)) { return gmdate('Y-m-d', $value); } break; case 'wysiwyg': case 'textarea': return wp_strip_all_tags($value); case 'select': case 'checkbox': case 'radio': if (is_array($value)) { return self::formatList($value); } break; } return self::toString($value); } }