From da6d5081f732758130588b31fd519ad649bd7acb Mon Sep 17 00:00:00 2001 From: magdev Date: Mon, 2 Feb 2026 14:57:43 +0100 Subject: [PATCH] fix: Localhost license bypass, rewrite rules flush, WooCommerce orders (v0.2.2) - Add localhost license bypass for development environments - Flush rewrite rules when license status changes to fix 404 on /metrics - Fix wc_orders_count() missing required status parameter Co-Authored-By: Claude Opus 4.5 --- CHANGELOG.md | 17 ++++++++++++++ src/License/Manager.php | 47 +++++++++++++++++++++++++++++++++++++++ src/Metrics/Collector.php | 11 +++++---- wp-prometheus.php | 4 ++-- 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 196a0a0..419cea5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.2] - 2026-02-02 + +### Fixed + +- Fixed `wc_orders_count()` call missing required status parameter in WooCommerce orders metrics + +## [0.2.1] - 2026-02-02 + +### Added + +- Localhost license bypass for development environments (localhost, 127.0.0.1, ::1, \*.localhost, \*.local) +- Automatic rewrite rules flush when license status changes + +### Fixed + +- Fixed 404 error on `/metrics` endpoint when license becomes valid after plugin activation + ## [0.2.0] - 2026-02-02 ### Added diff --git a/src/License/Manager.php b/src/License/Manager.php index c387dd5..1462838 100644 --- a/src/License/Manager.php +++ b/src/License/Manager.php @@ -302,10 +302,47 @@ final class Manager { * @return bool */ public static function is_license_valid(): bool { + // Bypass license check on localhost for development. + if ( self::is_localhost() ) { + return true; + } + $status = get_option( self::OPTION_LICENSE_STATUS, 'unchecked' ); return 'valid' === $status; } + /** + * Check if the current site is running on localhost. + * + * @return bool + */ + public static function is_localhost(): bool { + $host = wp_parse_url( home_url(), PHP_URL_HOST ); + + $localhost_patterns = array( + 'localhost', + '127.0.0.1', + '::1', + ); + + // Check exact matches. + if ( in_array( $host, $localhost_patterns, true ) ) { + return true; + } + + // Check .localhost TLD (e.g., mysite.localhost). + if ( str_ends_with( $host, '.localhost' ) ) { + return true; + } + + // Check .local TLD (common for local development). + if ( str_ends_with( $host, '.local' ) ) { + return true; + } + + return false; + } + /** * Get the license key. * @@ -396,9 +433,16 @@ final class Manager { * @return void */ private function update_cached_status( string $status, array $data = array() ): void { + $previous_status = get_option( self::OPTION_LICENSE_STATUS, 'unchecked' ); + update_option( self::OPTION_LICENSE_STATUS, $status ); update_option( self::OPTION_LICENSE_DATA, $data ); update_option( self::OPTION_LAST_CHECK, time() ); + + // Flush rewrite rules when license becomes valid to register the /metrics endpoint. + if ( 'valid' === $status && 'valid' !== $previous_status ) { + flush_rewrite_rules(); + } } /** @@ -465,6 +509,9 @@ final class Manager { update_option( self::OPTION_LICENSE_DATA, array() ); delete_transient( self::TRANSIENT_LICENSE_CHECK ); + // Flush rewrite rules to remove the /metrics endpoint. + flush_rewrite_rules(); + wp_send_json_success( array( 'success' => true, 'message' => __( 'License deactivated.', 'wp-prometheus' ), diff --git a/src/Metrics/Collector.php b/src/Metrics/Collector.php index 4a7eda2..c5ef065 100644 --- a/src/Metrics/Collector.php +++ b/src/Metrics/Collector.php @@ -466,11 +466,14 @@ class Collector { array( 'status' ) ); - // Use WooCommerce's built-in order count function. - $order_counts = wc_orders_count(); + // Get all registered order statuses and count each. + $statuses = wc_get_order_statuses(); - foreach ( $order_counts as $status => $count ) { - $gauge->set( (int) $count, array( $status ) ); + foreach ( array_keys( $statuses ) as $status ) { + // Remove 'wc-' prefix for the label. + $status_label = str_replace( 'wc-', '', $status ); + $count = wc_orders_count( $status ); + $gauge->set( (int) $count, array( $status_label ) ); } } diff --git a/wp-prometheus.php b/wp-prometheus.php index d51c2e8..7793983 100644 --- a/wp-prometheus.php +++ b/wp-prometheus.php @@ -3,7 +3,7 @@ * Plugin Name: WP Prometheus * Plugin URI: https://src.bundespruefstelle.ch/magdev/wp-prometheus * Description: Prometheus metrics endpoint for WordPress with extensible hooks for custom metrics. - * Version: 0.2.0 + * Version: 0.2.2 * Requires at least: 6.4 * Requires PHP: 8.3 * Author: Marco Graetsch @@ -26,7 +26,7 @@ if ( ! defined( 'ABSPATH' ) ) { * * @var string */ -define( 'WP_PROMETHEUS_VERSION', '0.2.0' ); +define( 'WP_PROMETHEUS_VERSION', '0.2.2' ); /** * Plugin file path.