Files
wp-prometheus/src/Installer.php

105 lines
2.7 KiB
PHP
Raw Normal View History

<?php
/**
* Plugin installer class.
*
* @package WP_Prometheus
*/
namespace Magdev\WpPrometheus;
// Prevent direct file access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Installer class.
*
* Handles plugin activation, deactivation, and uninstallation.
*/
final class Installer {
/**
* Plugin activation.
*
* @return void
*/
public static function activate(): void {
// Set default options.
self::set_default_options();
// Flush rewrite rules for the metrics endpoint.
flush_rewrite_rules();
// Store activation time for reference.
update_option( 'wp_prometheus_activated', time() );
}
/**
* Plugin deactivation.
*
* @return void
*/
public static function deactivate(): void {
// Flush rewrite rules.
flush_rewrite_rules();
}
/**
* Plugin uninstallation.
*
* @return void
*/
public static function uninstall(): void {
// Remove all plugin options.
$options = array(
'wp_prometheus_activated',
'wp_prometheus_license_key',
'wp_prometheus_license_server_url',
'wp_prometheus_license_server_secret',
'wp_prometheus_license_status',
'wp_prometheus_license_data',
'wp_prometheus_license_last_check',
'wp_prometheus_auth_token',
'wp_prometheus_enable_default_metrics',
'wp_prometheus_enabled_metrics',
'wp_prometheus_runtime_metrics',
);
foreach ( $options as $option ) {
delete_option( $option );
}
// Remove transients.
delete_transient( 'wp_prometheus_license_check' );
}
/**
* Set default plugin options.
*
* @return void
*/
private static function set_default_options(): void {
// Generate a random auth token if not set.
if ( ! get_option( 'wp_prometheus_auth_token' ) ) {
update_option( 'wp_prometheus_auth_token', wp_generate_password( 32, false ) );
}
// Enable default metrics by default.
if ( false === get_option( 'wp_prometheus_enable_default_metrics' ) ) {
update_option( 'wp_prometheus_enable_default_metrics', 1 );
}
// Default enabled metrics.
if ( false === get_option( 'wp_prometheus_enabled_metrics' ) ) {
update_option( 'wp_prometheus_enabled_metrics', array(
'wordpress_info',
'wordpress_users_total',
'wordpress_posts_total',
'wordpress_comments_total',
'wordpress_plugins_total',
) );
}
}
}