2026-02-01 15:31:21 +01:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Main plugin class.
|
|
|
|
|
*
|
|
|
|
|
* @package WP_Prometheus
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace Magdev\WpPrometheus;
|
|
|
|
|
|
|
|
|
|
use Magdev\WpPrometheus\Admin\Settings;
|
|
|
|
|
use Magdev\WpPrometheus\Endpoint\MetricsEndpoint;
|
|
|
|
|
use Magdev\WpPrometheus\License\Manager as LicenseManager;
|
|
|
|
|
use Magdev\WpPrometheus\Metrics\Collector;
|
2026-02-02 14:24:05 +01:00
|
|
|
use Magdev\WpPrometheus\Metrics\RuntimeCollector;
|
2026-02-01 15:31:21 +01:00
|
|
|
|
|
|
|
|
// Prevent direct file access.
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Plugin singleton class.
|
|
|
|
|
*
|
|
|
|
|
* Initializes and manages all plugin components.
|
|
|
|
|
*/
|
|
|
|
|
final class Plugin {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Singleton instance.
|
|
|
|
|
*
|
|
|
|
|
* @var Plugin|null
|
|
|
|
|
*/
|
|
|
|
|
private static ?Plugin $instance = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Metrics collector instance.
|
|
|
|
|
*
|
|
|
|
|
* @var Collector|null
|
|
|
|
|
*/
|
|
|
|
|
private ?Collector $collector = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get singleton instance.
|
|
|
|
|
*
|
|
|
|
|
* @return Plugin
|
|
|
|
|
*/
|
|
|
|
|
public static function get_instance(): Plugin {
|
|
|
|
|
if ( null === self::$instance ) {
|
|
|
|
|
self::$instance = new self();
|
|
|
|
|
}
|
|
|
|
|
return self::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Private constructor to enforce singleton pattern.
|
|
|
|
|
*/
|
|
|
|
|
private function __construct() {
|
|
|
|
|
$this->init_components();
|
|
|
|
|
$this->init_hooks();
|
|
|
|
|
$this->load_textdomain();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prevent cloning.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function __clone() {}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prevent unserialization.
|
|
|
|
|
*
|
|
|
|
|
* @throws \Exception Always throws to prevent unserialization.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __wakeup(): void {
|
|
|
|
|
throw new \Exception( 'Cannot unserialize singleton' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize plugin components.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function init_components(): void {
|
|
|
|
|
// Initialize license manager.
|
|
|
|
|
LicenseManager::get_instance();
|
|
|
|
|
|
|
|
|
|
// Initialize admin settings (always needed).
|
|
|
|
|
if ( is_admin() ) {
|
|
|
|
|
new Settings();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-02 14:24:05 +01:00
|
|
|
// Initialize runtime collector for request metrics (always runs to collect data).
|
|
|
|
|
// Only collect if at least one runtime metric is enabled.
|
|
|
|
|
$enabled_metrics = get_option( 'wp_prometheus_enabled_metrics', array() );
|
|
|
|
|
$runtime_metrics = array(
|
|
|
|
|
'wordpress_http_requests_total',
|
|
|
|
|
'wordpress_http_request_duration_seconds',
|
|
|
|
|
'wordpress_db_queries_total',
|
|
|
|
|
);
|
|
|
|
|
$has_runtime_metrics = ! empty( array_intersect( $runtime_metrics, $enabled_metrics ) );
|
|
|
|
|
|
|
|
|
|
if ( $has_runtime_metrics && LicenseManager::is_license_valid() ) {
|
|
|
|
|
RuntimeCollector::get_instance();
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-01 15:31:21 +01:00
|
|
|
// Initialize metrics endpoint (only if licensed).
|
|
|
|
|
if ( LicenseManager::is_license_valid() ) {
|
|
|
|
|
$this->collector = new Collector();
|
|
|
|
|
new MetricsEndpoint( $this->collector );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize WordPress hooks.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function init_hooks(): void {
|
|
|
|
|
// Add settings link to plugins page.
|
|
|
|
|
add_filter( 'plugin_action_links_' . WP_PROMETHEUS_BASENAME, array( $this, 'add_plugin_action_links' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add action links to the plugins page.
|
|
|
|
|
*
|
|
|
|
|
* @param array $links Existing action links.
|
|
|
|
|
* @return array Modified action links.
|
|
|
|
|
*/
|
|
|
|
|
public function add_plugin_action_links( array $links ): array {
|
|
|
|
|
$settings_link = sprintf(
|
|
|
|
|
'<a href="%s">%s</a>',
|
|
|
|
|
esc_url( admin_url( 'options-general.php?page=wp-prometheus' ) ),
|
|
|
|
|
esc_html__( 'Settings', 'wp-prometheus' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Add our link at the beginning.
|
|
|
|
|
array_unshift( $links, $settings_link );
|
|
|
|
|
|
|
|
|
|
return $links;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load plugin textdomain.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function load_textdomain(): void {
|
|
|
|
|
load_plugin_textdomain(
|
|
|
|
|
'wp-prometheus',
|
|
|
|
|
false,
|
|
|
|
|
dirname( WP_PROMETHEUS_BASENAME ) . '/languages'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the metrics collector.
|
|
|
|
|
*
|
|
|
|
|
* @return Collector|null
|
|
|
|
|
*/
|
|
|
|
|
public function get_collector(): ?Collector {
|
|
|
|
|
return $this->collector;
|
|
|
|
|
}
|
|
|
|
|
}
|