You've already forked wp-prometheus
151 lines
3.3 KiB
PHP
151 lines
3.3 KiB
PHP
|
|
<?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;
|
||
|
|
|
||
|
|
// 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();
|
||
|
|
}
|
||
|
|
|
||
|
|
// 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;
|
||
|
|
}
|
||
|
|
}
|