Files
wordpress-theme-aksrt/inc/classes/frontend/class-aksrt-custom-code-renderer.php
T

65 lines
2.0 KiB
PHP

<?php
/**
* AKSRT Custom Code Renderer
*
* Outputs user-defined custom CSS/JS (header + footer) into the page.
* Extracted from AKSRT_Scripts to reduce that class's line count.
*
* @package AKSRT
* @since 3.4.0
*/
if (!defined('ABSPATH')) {
exit;
}
class AKSRT_Custom_Code_Renderer {
public function output_header(): void {
$custom_css = class_exists('AKSRT_Settings')
? AKSRT_Settings::custom_css()
: aksrt_get_setting('custom_css', '');
$head_code = class_exists('AKSRT_Settings')
? AKSRT_Settings::custom_head_code()
: aksrt_get_setting('custom_head_code', get_option('aksrt_custom_head_code', ''));
$trackcode = class_exists('AKSRT_Settings')
? AKSRT_Settings::trackcode()
: '';
echo $this->format((string) $custom_css, 'css', 'aksrt-custom-css');
if ($head_code) {
echo $this->format((string) $head_code, 'js', 'aksrt-custom-js-header');
}
if ($trackcode) {
echo $this->format((string) $trackcode, 'js', 'aksrt-trackcode');
}
}
public function output_footer(): void {
$footer_code = class_exists('AKSRT_Settings')
? AKSRT_Settings::custom_footer_code()
: aksrt_get_setting('custom_footer_code', get_option('aksrt_custom_footer_code', ''));
if ($footer_code) {
echo $this->format((string) $footer_code, 'js', 'aksrt-custom-js-footer');
}
}
private function format(string $code, string $type, string $id): string {
$code = trim($code);
if ($code === '') {
return '';
}
if (preg_match('/<\s*(script|style|meta|link|noscript)\b/i', $code)) {
return $code . "\n";
}
if ($type === 'css') {
return '<style id="' . esc_attr($id) . '">' . "\n" . $code . "\n</style>\n";
}
return '<script id="' . esc_attr($id) . '">' . "\n" . $code . "\n</script>\n";
}
}