['adghost_code_head', 'adghost_code_head_on', 'wp_head', 5], 'body' => ['adghost_code_body', 'adghost_code_body_on', 'wp_body_open', 1], 'footer' => ['adghost_code_footer', 'adghost_code_footer_on', 'wp_footer', 999], ]; const MAX_BYTES = 65536; public static function init() { foreach (self::SLOTS as $slot => [$code, $on, $hook, $priority]) { add_action($hook, function () use ($slot) { self::output($slot); }, $priority); } } public static function output($slot) { if (is_admin() || is_feed() || is_embed() || (defined('REST_REQUEST') && REST_REQUEST)) { return; } if (class_exists('AdGhost_AMP') && AdGhost_AMP::is_amp()) { return; } [$code_option, $on_option] = self::SLOTS[$slot]; if (!get_option($on_option, true)) { return; } $code = (string) get_option($code_option, ''); if (trim($code) === '') { return; } echo "\n\n" . $code . "\n\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } public static function save(array $data) { $touched = false; foreach (self::SLOTS as $slot => [$code_option, $on_option]) { if (array_key_exists('code_' . $slot . '_on', $data)) { update_option($on_option, filter_var($data['code_' . $slot . '_on'], FILTER_VALIDATE_BOOLEAN) ? 1 : 0); } if (!array_key_exists('code_' . $slot, $data)) { continue; } $code = (string) $data['code_' . $slot]; if ($code === (string) get_option($code_option, '')) { continue; } $touched = true; if (!current_user_can('unfiltered_html')) { return __('Your account is not allowed to add scripts to the site, so the header and footer code was not saved. Ask a user with the unfiltered_html capability (a super admin on multisite).', 'adghost-lite'); } if (strlen($code) > self::MAX_BYTES) { /* translators: %s: where the code goes (head, body or footer). */ return sprintf(__('The %s code is larger than 64 KB and was not saved.', 'adghost-lite'), $slot); } update_option($code_option, $code, false); } return null; } public static function all() { $out = []; foreach (self::SLOTS as $slot => [$code_option, $on_option]) { $out[$slot] = [ 'code' => (string) get_option($code_option, ''), 'on' => (bool) get_option($on_option, true), ]; } return $out; } }