diff --git a/plugi-core/src/Core/helpers.php b/plugi-core/src/Core/helpers.php index 8b2ff94..98ba975 100644 --- a/plugi-core/src/Core/helpers.php +++ b/plugi-core/src/Core/helpers.php @@ -7,13 +7,13 @@ if (! function_exists('phpb_e')) { /** * Encode HTML special characters in a string. * - * @param string $value + * @param string|null $value * @param bool $doubleEncode * @return string */ - function phpb_e(string $value, bool $doubleEncode = true): string + function phpb_e(?string $value, bool $doubleEncode = true): string { - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode); + return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8', $doubleEncode); } } @@ -641,27 +641,17 @@ if (! function_exists('phpb_registered_assets')) { */ function phpb_registered_assets(string $location = 'header'): void { - $assets = []; - if($location === 'header'){ - $assets = Extensions::getHeaderAssets(); - }elseif($location === 'footer'){ - $assets = Extensions::getFooterAssets(); - }elseif($location === 'admin-header'){ - $assets = Extensions::getAdminHeaderAssets(); - }elseif($location === 'admin-footer'){ - $assets = Extensions::getAdminFooterAssets(); - } - + $assets = Extensions::getAssetsArray($location); foreach ($assets as $asset) { $attributes = ''; foreach ($asset['attributes'] as $key => $value) { - $attributes .= ' ' . $key . '="' . $value . '"'; + $attributes .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"'; } - if ($asset['type'] == 'style') { - echo ''; + if ($asset['type'] === 'style') { + echo ''; } else { - echo ''; + echo ''; } } } diff --git a/plugi-core/src/Extensions.php b/plugi-core/src/Extensions.php index e66764a..5939cf8 100644 --- a/plugi-core/src/Extensions.php +++ b/plugi-core/src/Extensions.php @@ -241,7 +241,7 @@ class Extensions } /** - * Get all header assets. + * Get all html blocks for the given location. */ public static function getHtmlBlocks(string $location = 'head'): string { @@ -249,31 +249,63 @@ class Extensions } /** - * Get all header assets. + * Render assets as HTML string. */ - public static function getHeaderAssets(): array + protected static function renderAssets(array $assets): string { - return self::$assets['header']; + $html = ''; + foreach ($assets as $asset) { + $attributes = ''; + foreach ($asset['attributes'] as $key => $value) { + $attributes .= ' ' . htmlspecialchars($key, ENT_QUOTES, 'UTF-8') . '="' . htmlspecialchars($value, ENT_QUOTES, 'UTF-8') . '"'; + } + + if ($asset['type'] === 'style') { + $html .= ''; + } else { + $html .= ''; + } + } + return $html; } /** - * Get all footer assets. + * Get all header assets as HTML string. */ - public static function getFooterAssets() { - return self::$assets['footer']; + public static function getHeaderAssets(): string + { + return self::renderAssets(self::$assets['header']); } /** - * Get all admin header assets. + * Get all footer assets as HTML string. */ - public static function getAdminHeaderAssets() { - return self::$assets['admin-header']; + public static function getFooterAssets(): string + { + return self::renderAssets(self::$assets['footer']); } /** - * Get all admin footer assets. + * Get all admin header assets as HTML string. */ - public static function getAdminFooterAssets() { - return self::$assets['admin-footer']; + public static function getAdminHeaderAssets(): string + { + return self::renderAssets(self::$assets['admin-header']); + } + + /** + * Get all admin footer assets as HTML string. + */ + public static function getAdminFooterAssets(): string + { + return self::renderAssets(self::$assets['admin-footer']); + } + + /** + * Get raw assets array for internal use by phpb_registered_assets(). + */ + public static function getAssetsArray(string $location): array + { + return self::$assets[$location] ?? []; } }