settings = SettingsManager::get_instance(); $this->init_hooks(); } private function get_setting($key, $default = '') { return $this->settings->get('code_snippets', $key, $default); } private function init_hooks() { add_action('wp_head', [$this, 'print_header_code']); add_action('wp_footer', [$this, 'print_footer_code']); add_action('admin_head', [$this, 'print_admin_css']); } public function print_header_code() { $code = $this->get_setting('header_code'); if (!empty($code)) { // This field allows raw HTML/JS by design for features like Google Analytics. // We trust that only admins with proper capabilities have saved this. // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $code; } $css = $this->get_setting('custom_css'); if (!empty($css)) { // Sanitize CSS to remove HTML tags, but allow CSS syntax $css = wp_strip_all_tags($css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo ''; } } public function print_footer_code() { $code = $this->get_setting('footer_code'); if (!empty($code)) { // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $code; } $js = $this->get_setting('custom_js'); if (!empty($js)) { // We cannot strictly sanitize JS without breaking functionality, // but we can ensure it doesn't break out of the script tag easily if we wrapped it. // However, users might paste existing '; } } public function print_admin_css() { $css = $this->get_setting('admin_css'); if (!empty($css)) { $css = wp_strip_all_tags($css); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo ''; } } public function render_settings() { $settings = [ 'header_code' => ['type' => 'textarea', 'label' => __('Header Code (HTML/JS)', 'admin-menu-optimizer'), 'desc' => 'Code to be added to the <head> section.'], 'footer_code' => ['type' => 'textarea', 'label' => __('Footer Code (HTML/JS)', 'admin-menu-optimizer'), 'desc' => 'Code to be added before </body>.'], 'custom_css' => ['type' => 'textarea', 'label' => __('Custom CSS (Frontend)', 'admin-menu-optimizer'), 'desc' => 'CSS to be added to the frontend.'], 'custom_js' => ['type' => 'textarea', 'label' => __('Custom JS (Frontend)', 'admin-menu-optimizer'), 'desc' => 'JS to be added to the frontend footer.'], 'admin_css' => ['type' => 'textarea', 'label' => __('Admin Custom CSS', 'admin-menu-optimizer'), 'desc' => 'CSS to be added to the admin dashboard.'], ]; echo '
' . esc_html( __('Add custom code to your site. Use responsibly.', 'admin-menu-optimizer') ) . '
'; echo '| ' . esc_html($field['label']) . ' | '; echo '';
echo '';
if (isset($field['desc'])) {
echo ' ' . esc_html($field['desc']) . ' '; } echo ' | ';
echo '
|---|