fix: Localhost license bypass, rewrite rules flush, WooCommerce orders (v0.2.2)
All checks were successful
Create Release Package / build-release (push) Successful in 56s

- 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 <noreply@anthropic.com>
This commit is contained in:
2026-02-02 14:57:43 +01:00
parent 3eb66b0ebe
commit da6d5081f7
4 changed files with 73 additions and 6 deletions

View File

@@ -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/), 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). 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 ## [0.2.0] - 2026-02-02
### Added ### Added

View File

@@ -302,10 +302,47 @@ final class Manager {
* @return bool * @return bool
*/ */
public static function is_license_valid(): 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' ); $status = get_option( self::OPTION_LICENSE_STATUS, 'unchecked' );
return 'valid' === $status; 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. * Get the license key.
* *
@@ -396,9 +433,16 @@ final class Manager {
* @return void * @return void
*/ */
private function update_cached_status( string $status, array $data = array() ): 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_STATUS, $status );
update_option( self::OPTION_LICENSE_DATA, $data ); update_option( self::OPTION_LICENSE_DATA, $data );
update_option( self::OPTION_LAST_CHECK, time() ); 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() ); update_option( self::OPTION_LICENSE_DATA, array() );
delete_transient( self::TRANSIENT_LICENSE_CHECK ); delete_transient( self::TRANSIENT_LICENSE_CHECK );
// Flush rewrite rules to remove the /metrics endpoint.
flush_rewrite_rules();
wp_send_json_success( array( wp_send_json_success( array(
'success' => true, 'success' => true,
'message' => __( 'License deactivated.', 'wp-prometheus' ), 'message' => __( 'License deactivated.', 'wp-prometheus' ),

View File

@@ -466,11 +466,14 @@ class Collector {
array( 'status' ) array( 'status' )
); );
// Use WooCommerce's built-in order count function. // Get all registered order statuses and count each.
$order_counts = wc_orders_count(); $statuses = wc_get_order_statuses();
foreach ( $order_counts as $status => $count ) { foreach ( array_keys( $statuses ) as $status ) {
$gauge->set( (int) $count, array( $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 ) );
} }
} }

View File

@@ -3,7 +3,7 @@
* Plugin Name: WP Prometheus * Plugin Name: WP Prometheus
* Plugin URI: https://src.bundespruefstelle.ch/magdev/wp-prometheus * Plugin URI: https://src.bundespruefstelle.ch/magdev/wp-prometheus
* Description: Prometheus metrics endpoint for WordPress with extensible hooks for custom metrics. * 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 at least: 6.4
* Requires PHP: 8.3 * Requires PHP: 8.3
* Author: Marco Graetsch * Author: Marco Graetsch
@@ -26,7 +26,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* *
* @var string * @var string
*/ */
define( 'WP_PROMETHEUS_VERSION', '0.2.0' ); define( 'WP_PROMETHEUS_VERSION', '0.2.2' );
/** /**
* Plugin file path. * Plugin file path.