0 ? floor(log($bytes, 1024)) : 0; return number_format($bytes / pow(1024, $power), $decimals) . ' ' . $units[$power]; } } if (!function_exists('path_is_absolute')) { function path_is_absolute(string $path): bool { return str_starts_with($path, '/') || (bool) preg_match('#^[a-zA-Z]:\\\\#', $path); } } // -- Hook functions (no-ops) -- if (!function_exists('add_action')) { function add_action(string $hook, $callback, int $priority = 10, int $accepted_args = 1): bool { return true; } } if (!function_exists('add_filter')) { function add_filter(string $hook, $callback, int $priority = 10, int $accepted_args = 1): bool { return true; } } if (!function_exists('remove_all_filters')) { function remove_all_filters(string $hook, $priority = false): bool { return true; } } if (!function_exists('do_action')) { function do_action(string $hook, ...$args): void { } } if (!function_exists('apply_filters')) { function apply_filters(string $hook, $value, ...$args) { return $value; } } // -- Option functions (controllable via GlobalFunctionState) -- if (!function_exists('get_option')) { function get_option(string $option, $default = false) { if (array_key_exists($option, GlobalFunctionState::$options)) { return GlobalFunctionState::$options[$option]; } return $default; } } if (!function_exists('update_option')) { function update_option(string $option, $value, $autoload = null): bool { GlobalFunctionState::recordCall('update_option', $option, $value); GlobalFunctionState::$options[$option] = $value; return true; } } if (!function_exists('delete_option')) { function delete_option(string $option): bool { GlobalFunctionState::recordCall('delete_option', $option); unset(GlobalFunctionState::$options[$option]); return true; } } if (!function_exists('delete_transient')) { function delete_transient(string $transient): bool { GlobalFunctionState::recordCall('delete_transient', $transient); return true; } } if (!function_exists('flush_rewrite_rules')) { function flush_rewrite_rules(bool $hard = true): void { GlobalFunctionState::recordCall('flush_rewrite_rules'); } } // -- URL functions -- if (!function_exists('home_url')) { function home_url(string $path = ''): string { return 'https://example.com' . $path; } } if (!function_exists('admin_url')) { function admin_url(string $path = ''): string { return 'https://example.com/wp-admin/' . $path; } } // -- Conditional functions (controllable via GlobalFunctionState) -- if (!function_exists('is_admin')) { function is_admin(): bool { return GlobalFunctionState::$options['__is_admin'] ?? false; } } if (!function_exists('wp_doing_ajax')) { function wp_doing_ajax(): bool { return GlobalFunctionState::$options['__wp_doing_ajax'] ?? false; } } if (!function_exists('wp_doing_cron')) { function wp_doing_cron(): bool { return GlobalFunctionState::$options['__wp_doing_cron'] ?? false; } } if (!function_exists('is_multisite')) { function is_multisite(): bool { return false; } } // -- Plugin functions -- if (!function_exists('load_plugin_textdomain')) { function load_plugin_textdomain(string $domain, $deprecated = false, string $path = ''): bool { return true; } } if (!function_exists('register_activation_hook')) { function register_activation_hook(string $file, $callback): void { } } if (!function_exists('register_deactivation_hook')) { function register_deactivation_hook(string $file, $callback): void { } } // -- Rewrite functions -- if (!function_exists('add_rewrite_rule')) { function add_rewrite_rule(string $regex, string $redirect, string $after = 'bottom'): void { GlobalFunctionState::recordCall('add_rewrite_rule', $regex, $redirect, $after); } } if (!function_exists('add_rewrite_tag')) { function add_rewrite_tag(string $tag, string $regex, string $query = ''): void { GlobalFunctionState::recordCall('add_rewrite_tag', $tag, $regex, $query); } } // -- HTTP functions -- if (!function_exists('status_header')) { function status_header(int $code, string $description = ''): void { } } if (!function_exists('hash_equals')) { // hash_equals is a PHP built-in, but define stub just in case. } if (!function_exists('wp_rand')) { function wp_rand(int $min = 0, int $max = 0): int { return random_int($min, max($min, $max ?: PHP_INT_MAX >> 1)); } } if (!function_exists('get_bloginfo')) { function get_bloginfo(string $show = '', bool $filter = true): string { return match ($show) { 'version' => '6.7', 'language' => 'en-US', 'name' => 'Test Site', default => '', }; } } if (!function_exists('current_user_can')) { function current_user_can(string $capability, ...$args): bool { return true; } } if (!function_exists('deactivate_plugins')) { function deactivate_plugins($plugins, bool $silent = false, $network_wide = null): void { } } if (!function_exists('wp_die')) { function wp_die($message = '', $title = '', $args = []): void { throw new \RuntimeException((string) $message); } } // -- Plugin global authentication functions (from wp-prometheus.php) -- // Cannot include wp-prometheus.php directly due to constant definitions // and side effects. These mirror the production code for testing. if (!function_exists('wp_prometheus_get_authorization_header')) { function wp_prometheus_get_authorization_header(): string { if (isset($_SERVER['HTTP_AUTHORIZATION'])) { return sanitize_text_field(wp_unslash($_SERVER['HTTP_AUTHORIZATION'])); } if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])) { return sanitize_text_field(wp_unslash($_SERVER['REDIRECT_HTTP_AUTHORIZATION'])); } if (function_exists('apache_request_headers')) { $headers = apache_request_headers(); if (isset($headers['Authorization'])) { return sanitize_text_field($headers['Authorization']); } } return ''; } } // -- WordPress core class stubs -- if (!class_exists('WP')) { class WP { public array $query_vars = []; } } if (!function_exists('wp_prometheus_authenticate_request')) { function wp_prometheus_authenticate_request(): bool { $auth_token = get_option('wp_prometheus_auth_token', ''); // If no token is set, deny access. if (empty($auth_token)) { return false; } // Check for Bearer token in Authorization header. $auth_header = wp_prometheus_get_authorization_header(); if (!empty($auth_header) && preg_match('/Bearer\s+(.*)$/i', $auth_header, $matches)) { return hash_equals($auth_token, $matches[1]); } // Check for token in query parameter. if (isset($_GET['token']) && hash_equals($auth_token, sanitize_text_field(wp_unslash($_GET['token'])))) { return true; } return false; } }