render(); exit; } /** * Authenticate a metrics request using Bearer token or query parameter. * * Shared authentication logic used by both the MetricsEndpoint class * and the isolated mode handler to avoid code duplication. * * @return bool True if authenticated, false otherwise. */ 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 (less secure but useful for testing). // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Auth token check. if ( isset( $_GET['token'] ) && hash_equals( $auth_token, sanitize_text_field( wp_unslash( $_GET['token'] ) ) ) ) { return true; } return false; } /** * Get the Authorization header from the request. * * Checks multiple sources for the Authorization header to support * different server configurations (Apache, nginx, CGI, etc.). * * @return string The Authorization header value, or empty string if not found. */ 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 ''; } // Try early metrics handling before full plugin initialization. wp_prometheus_early_metrics_check(); /** * Plugin version. * * @var string */ define( 'WP_PROMETHEUS_VERSION', '0.5.1' ); /** * Plugin file path. * * @var string */ define( 'WP_PROMETHEUS_FILE', __FILE__ ); /** * Plugin directory path. * * @var string */ define( 'WP_PROMETHEUS_PATH', plugin_dir_path( __FILE__ ) ); /** * Plugin directory URL. * * @var string */ define( 'WP_PROMETHEUS_URL', plugin_dir_url( __FILE__ ) ); /** * Plugin basename. * * @var string */ define( 'WP_PROMETHEUS_BASENAME', plugin_basename( __FILE__ ) ); /** * Minimum WordPress version required. * * @var string */ define( 'WP_PROMETHEUS_MIN_WP_VERSION', '6.4' ); /** * Minimum PHP version required. * * @var string */ define( 'WP_PROMETHEUS_MIN_PHP_VERSION', '8.3' ); /** * Check requirements and bootstrap the plugin. * * @return void */ function wp_prometheus_init(): void { // Check PHP version. if ( version_compare( PHP_VERSION, WP_PROMETHEUS_MIN_PHP_VERSION, '<' ) ) { add_action( 'admin_notices', 'wp_prometheus_php_version_notice' ); return; } // Check WordPress version. if ( version_compare( get_bloginfo( 'version' ), WP_PROMETHEUS_MIN_WP_VERSION, '<' ) ) { add_action( 'admin_notices', 'wp_prometheus_wp_version_notice' ); return; } // Check if Composer autoloader exists. $autoloader = WP_PROMETHEUS_PATH . 'vendor/autoload.php'; if ( ! file_exists( $autoloader ) ) { add_action( 'admin_notices', 'wp_prometheus_autoloader_notice' ); return; } require_once $autoloader; // Initialize the plugin. \Magdev\WpPrometheus\Plugin::get_instance(); } /** * Display PHP version notice. * * @return void */ function wp_prometheus_php_version_notice(): void { $message = sprintf( /* translators: 1: Required PHP version, 2: Current PHP version */ __( 'WP Prometheus requires PHP version %1$s or higher. You are running PHP %2$s.', 'wp-prometheus' ), WP_PROMETHEUS_MIN_PHP_VERSION, PHP_VERSION ); printf( '
%s
%s
%s