← Back
Editing: class-site-title-reporter.php
<?php namespace WPaaS; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Durable, coalescing MHP title outbox. No Site Designer dependency. * * A single option row is compared and swapped in SQL: a stale worker can neither * acknowledge nor overwrite a newer observation. Requests enqueue only; WP-Cron * performs bounded network calls. Quiet sites need platform cron for delivery. */ final class Site_Title_Reporter { const OPTION = 'wpaas_site_title_report'; const HOOK = 'wpaas_report_site_title'; const RECONCILE_SECONDS = 3600; public function __construct() { add_action( 'init', [ $this, 'ensure_scheduled' ] ); add_action( 'update_option_blogname', [ $this, 'reconcile' ], PHP_INT_MAX, 0 ); add_action( self::HOOK, [ $this, 'deliver' ] ); } /** Bootstrap cron without uncached title/outbox reads on every page request. */ public function ensure_scheduled() { if ( Plugin::is_mhp() && ! wp_next_scheduled( self::HOOK ) ) { $this->schedule( time() ); } } /** Observe the database value, including CLI/import updates, without writing blogname. */ public function reconcile() { if ( ! Plugin::is_mhp() ) { return false; } $retry = get_transient( 'wpaas_site_title_storage_retry' ); if ( $retry && $retry > time() ) { $this->schedule( $retry ); return false; } for ( $attempt = 0; $attempt < 5; $attempt++ ) { $snapshot = $this->read(); $old = $snapshot['state']; // Read uncached so concurrent requests cannot enqueue an older cached blogname. global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", 'blogname' ) ); if ( null === $row ) { $this->schedule( time() + self::RECONCILE_SECONDS ); return false; } $title = html_entity_decode( (string) $row->option_value, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); if ( strlen( $title ) > 1024 ) { if ( ! get_transient( 'wpaas_site_title_oversized' ) ) { set_transient( 'wpaas_site_title_oversized', true, self::RECONCILE_SECONDS ); error_log( 'WPaaS: site title report skipped because the decoded title exceeds 1024 bytes.' ); } $this->schedule( time() + self::RECONCILE_SECONDS ); return false; // Never truncate the authoritative setting into a different title. } if ( $old && $old['title'] === $title && ( ! $old['delivered'] || $old['checked_at'] + self::RECONCILE_SECONDS > time() ) ) { $this->schedule( $old['delivered'] ? $old['checked_at'] + self::RECONCILE_SECONDS : $old['next_attempt'] ); return true; } $next = [ 'title' => $title, // Local identity protects A-to-B-to-A changes from an in-flight acknowledgement. 'token' => wp_generate_uuid4(), 'delivered' => false, 'attempts' => 0, 'next_attempt' => time(), 'checked_at' => time(), ]; if ( $this->swap( $snapshot['raw'], $next ) ) { $this->schedule( time() + 1 ); return true; } if ( $this->read()['raw'] === $snapshot['raw'] ) { $this->storage_failure(); return false; } } $this->schedule( time() + 30 ); return false; } /** Cron delivery, with durable bounded exponential backoff and exact acknowledgement. */ public function deliver() { if ( ! Plugin::is_mhp() ) { return; } if ( ! $this->reconcile() ) { return; } $snapshot = $this->read(); $old = $snapshot['state']; if ( ! $old || $old['delivered'] || $old['next_attempt'] > time() ) { return; } $next = $old; $result = Hosting_API::report_site_title( $old['title'] ); if ( true === $result ) { $next['delivered'] = true; $next['checked_at'] = time(); $next['next_attempt'] = time() + self::RECONCILE_SECONDS; } else { $next['attempts'] = min( 8, $old['attempts'] + 1 ); $next['next_attempt'] = time() + min( self::RECONCILE_SECONDS, 30 * ( 2 ** ( $next['attempts'] - 1 ) ) ); } $written = $this->swap( $snapshot['raw'], $next ); $current_snapshot = $this->read(); if ( ! $written && $current_snapshot['raw'] === $snapshot['raw'] ) { $this->storage_failure(); return; } $current = $current_snapshot['state']; if ( $current ) { $this->schedule( $current['next_attempt'] ); } } /** Read bytes as hex so connection charset conversion cannot change the CAS identity. */ private function read() { global $wpdb; $value = $wpdb->get_var( $wpdb->prepare( "SELECT HEX(option_value) FROM {$wpdb->options} WHERE option_name = %s", self::OPTION ) ); $state = null === $value ? null : json_decode( hex2bin( $value ), true ); $valid = is_array( $state ) && isset( $state['title'], $state['delivered'], $state['attempts'], $state['next_attempt'], $state['checked_at'] ) && is_string( $state['title'] ) && strlen( $state['title'] ) <= 1024 && is_bool( $state['delivered'] ) && is_int( $state['attempts'] ) && $state['attempts'] >= 0 && $state['attempts'] <= 8 && is_int( $state['next_attempt'] ) && $state['next_attempt'] >= 0 && is_int( $state['checked_at'] ) && $state['checked_at'] >= 0 && isset( $state['token'] ) && is_string( $state['token'] ) && '' !== $state['token']; // Keep the original byte identity even for legacy serialized or corrupt state. return [ 'raw' => $value, 'state' => $valid ? $state : null ]; } /** Atomic acknowledgement/observation update; never replace another worker's state. */ private function swap( $raw, array $next ) { global $wpdb; // JSON defaults escape Unicode: the outbox is ASCII even on legacy option tables. if ( null === $raw ) { $result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", self::OPTION, wp_json_encode( $next ) ) ); } else { $result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = %s WHERE option_name = %s AND HEX(option_value) = %s", wp_json_encode( $next ), self::OPTION, $raw ) ); } if ( 1 === $result ) { wp_cache_delete( self::OPTION, 'options' ); wp_cache_delete( 'notoptions', 'options' ); } return 1 === $result; } /** Unchanged storage after a failed CAS is not contention. Back off and diagnose without title data. */ private function storage_failure() { $retry = time() + self::RECONCILE_SECONDS; if ( ! get_transient( 'wpaas_site_title_storage_retry' ) ) { error_log( 'WPaaS: site title report storage failed; retrying hourly.' ); } set_transient( 'wpaas_site_title_storage_retry', $retry, self::RECONCILE_SECONDS ); // Replace an already-due delivery event; otherwise schedule() would retain the hot retry. wp_clear_scheduled_hook( self::HOOK ); $this->schedule( $retry ); } private function schedule( $when ) { $when = max( time() + 1, $when ); $existing = wp_next_scheduled( self::HOOK ); if ( $existing && $existing <= $when ) { return; } if ( $existing ) { wp_unschedule_event( $existing, self::HOOK ); } wp_schedule_single_event( $when, self::HOOK ); } }
Save File
Cancel