Files
2meet-data-optimizer-hivepr…/includes/hivepress/adapters/class-tmdo-hivepress-statistics-adapter.php
T
wpdev b4400a68e5 chore: initial snapshot of 2meet-data-optimizer-hivepress-addon v0.1.0
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
2026-07-31 05:06:36 +08:00

131 lines
3.3 KiB
PHP

<?php
/**
* Statistics adapter — `hivepress-statistics` (DETECT-ONLY in v3.0.0).
*
* The statistics addon tracks listing view counts, click-throughs, and other
* counter-style metrics. The natural zone for these is WARM:
*
* - Increment-heavy writes (every page view → counter++).
* - Read-rarely (only the vendor dashboard cares).
* - Tolerable to lose seconds of writes (TTL-style flush).
*
* Mapped meta keys (best-effort from addon docs):
*
* hp_view_count → warm (cron flush hourly)
* hp_click_count → warm (cron flush hourly)
*
* Detect-only: not installed in dev environment.
*
* @package WP_Data_Optimizer
* @since 3.0.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'TMDO_HivePress_Statistics_Adapter' ) ) {
/**
* Statistics addon adapter (detect-only, warm zone).
*/
final class TMDO_HivePress_Statistics_Adapter implements TMDO_HivePress_Adapter {
use TMDO_HivePress_Adapter_Trait;
/**
* Constructor — bind register hooks via inherited trait.
*/
public function __construct() {
$this->bind_anti_eav_hooks();
}
/** Adapter slug (matches wp.org plugin slug). */
public function plugin_slug(): string {
return 'hivepress-statistics';
}
/** Class probed for addon presence. */
public function detection_class(): string {
return 'HivePress\\Statistics\\Plugin';
}
/** Version constant probed for addon presence. */
public function detection_const(): string {
return 'HIVEPRESS_STATISTICS_VERSION';
}
/** Minimum addon version supported. */
public function minimum_addon_version(): string {
return '1.0.0';
}
/**
* Register counter fields to warm zone.
*
* @param TMDO_Schema_Registry $registry Schema registry singleton.
*/
public function on_register_fields( TMDO_Schema_Registry $registry ): void {
$registry->register_many(
$this->plugin_slug(),
array(
array(
'post_type' => 'hp_listing',
'meta_key' => 'hp_view_count',
'zone' => 'warm',
'ttl' => null,
),
array(
'post_type' => 'hp_listing',
'meta_key' => 'hp_click_count',
'zone' => 'warm',
'ttl' => null,
),
)
);
}
/**
* Doctor probe — warm zone uses shared wp_wpdo_warm KV table.
*
* @return array{ok:bool, message:string}
*/
public function doctor_check(): array {
global $wpdb;
if ( ! isset( $wpdb ) || ! is_object( $wpdb ) ) {
return array(
'ok' => false,
'message' => 'wpdb global not available',
);
}
$table = $wpdb->prefix . 'wpdo_warm';
try {
$exists = (bool) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
return array(
'ok' => $exists,
'message' => $exists
? 'hivepress-statistics: warm zone (wpdo_warm) reachable'
: 'hivepress-statistics: wpdo_warm missing',
);
} catch ( \Throwable $e ) {
return array(
'ok' => false,
'message' => sprintf( 'hivepress-statistics doctor failed: %s', $e->getMessage() ),
);
}
}
/**
* 8-D self-score — D8 reduced (warm zone is best-effort until addon installs).
*/
public function suitability_score(): array {
return $this->compose_score(
array(
'd8_perf' => 0.9,
)
);
}
}
} // end if ( ! class_exists )