fix(2meet-data-optimizer): default misc bucket and garbage filter to off

- Both term/comment filters now default to disabled for new installs, so
  activating the plugin no longer reroutes or drops other plugins' meta
- One-time maybe_upgrade() migration keeps the misc bucket on for sites
  that already hold misc rows and never set the toggle (data stays readable)
- Admin toggles show the same defaults as the filters

Verify: phpunit-integration --filter 'defaults_false|preserve_default';
  unit 600 OK, integration 420 OK
PR: PR-006
This commit is contained in:
2026-09-25 21:39:38 +08:00
parent d3b72bd1af
commit 151aadd661
6 changed files with 89 additions and 9 deletions
+2 -2
View File
@@ -3490,7 +3490,7 @@ wpdo.getListings({ per_page: 3 }).then(r => console.log(r));'
<?php <?php
// v2.12.1: Term + Comment garbage write-time filter toggle + status. // v2.12.1: Term + Comment garbage write-time filter toggle + status.
$tcg_enabled = (bool) get_option( 'wpdo_term_comment_garbage_filter_enabled', '1' ); $tcg_enabled = (bool) get_option( 'wpdo_term_comment_garbage_filter_enabled', '0' );
$tcg_drops_24h = class_exists( 'TMDO_Term_Comment_Garbage_Filter' ) $tcg_drops_24h = class_exists( 'TMDO_Term_Comment_Garbage_Filter' )
? TMDO_Term_Comment_Garbage_Filter::get_drop_count_24h() ? TMDO_Term_Comment_Garbage_Filter::get_drop_count_24h()
: 0; : 0;
@@ -3653,7 +3653,7 @@ wpdo.getListings({ per_page: 3 }).then(r => console.log(r));'
<?php <?php
// v2.12.4: Term + Comment misc bucket toggle + status. // v2.12.4: Term + Comment misc bucket toggle + status.
$misc_enabled = (bool) get_option( 'wpdo_term_comment_misc_bucket_enabled', '1' ); $misc_enabled = (bool) get_option( 'wpdo_term_comment_misc_bucket_enabled', '0' );
$term_misc_rows = class_exists( 'TMDO_Term_Comment_Misc_Bucket' ) $term_misc_rows = class_exists( 'TMDO_Term_Comment_Misc_Bucket' )
? TMDO_Term_Comment_Misc_Bucket::count_rows( 'term' ) ? TMDO_Term_Comment_Misc_Bucket::count_rows( 'term' )
: 0; : 0;
+31
View File
@@ -40,6 +40,9 @@ class TMDO_Installer {
*/ */
private const SCHEMA_VERSION = '2.1.0'; private const SCHEMA_VERSION = '2.1.0';
/** Set once maybe_preserve_misc_bucket_default() has run for this site. */
public const MISC_BUCKET_DEFAULT_FLAG = 'wpdo_misc_bucket_default_v2';
/** Allowed SQL base types for hot-zone column definitions. */ /** Allowed SQL base types for hot-zone column definitions. */
private const ALLOWED_COL_BASE_TYPES = array( private const ALLOWED_COL_BASE_TYPES = array(
'bigint', 'bigint',
@@ -299,6 +302,34 @@ class TMDO_Installer {
if ( get_option( 'wpdo_db_version' ) !== self::SCHEMA_VERSION ) { if ( get_option( 'wpdo_db_version' ) !== self::SCHEMA_VERSION ) {
self::install(); self::install();
} }
self::maybe_preserve_misc_bucket_default();
}
/**
* One-time migration for the misc bucket default flip (on → off).
*
* The bucket used to default to enabled; its rows are only readable while it
* is on. Sites that already hold misc rows and never set the toggle get '1'
* persisted so turning the default off cannot hide their data. Runs from
* maybe_upgrade() rather than install() because install() only runs when
* SCHEMA_VERSION changes; the flag is autoloaded since it is read per request.
*
* @return void
*/
public static function maybe_preserve_misc_bucket_default(): void {
if ( get_option( self::MISC_BUCKET_DEFAULT_FLAG ) || ! class_exists( 'TMDO_Term_Comment_Misc_Bucket' ) ) {
return;
}
$unset = false === get_option( TMDO_Term_Comment_Misc_Bucket::OPT_ENABLED );
$has_rows = TMDO_Term_Comment_Misc_Bucket::count_rows( 'term' ) > 0
|| TMDO_Term_Comment_Misc_Bucket::count_rows( 'comment' ) > 0;
if ( $unset && $has_rows ) {
update_option( TMDO_Term_Comment_Misc_Bucket::OPT_ENABLED, '1', false );
}
update_option( self::MISC_BUCKET_DEFAULT_FLAG, '1', true );
} }
// ── v2.0.0 entity + audit + shadow_diffs schema (PR-2) ───────────────── // ── v2.0.0 entity + audit + shadow_diffs schema (PR-2) ─────────────────
@@ -109,12 +109,12 @@ final class TMDO_Term_Comment_Garbage_Filter {
} }
/** /**
* Check the admin toggle. Defaults to enabled. * Check the admin toggle. Defaults to disabled.
* *
* @return bool * @return bool
*/ */
public static function is_enabled(): bool { public static function is_enabled(): bool {
return (bool) get_option( self::OPT_ENABLED, '1' ); return (bool) get_option( self::OPT_ENABLED, '0' );
} }
/** /**
@@ -95,12 +95,13 @@ final class TMDO_Term_Comment_Misc_Bucket {
} }
/** /**
* Check the admin toggle. Defaults to enabled. * Check the admin toggle. Defaults to disabled; sites that already hold misc
* rows keep it on via TMDO_Installer::maybe_preserve_misc_bucket_default().
* *
* @return bool * @return bool
*/ */
public static function is_enabled(): bool { public static function is_enabled(): bool {
return (bool) get_option( self::OPT_ENABLED, '1' ); return (bool) get_option( self::OPT_ENABLED, '0' );
} }
/** /**
@@ -152,9 +152,9 @@ class TermCommentGarbageFilterTest extends TestCase {
// ── is_enabled toggle ─────────────────────────────────────────────────── // ── is_enabled toggle ───────────────────────────────────────────────────
public function test_is_enabled_defaults_true(): void { public function test_is_enabled_defaults_false(): void {
delete_option( WPDO_Term_Comment_Garbage_Filter::OPT_ENABLED ); delete_option( WPDO_Term_Comment_Garbage_Filter::OPT_ENABLED );
$this->assertTrue( WPDO_Term_Comment_Garbage_Filter::is_enabled() ); $this->assertFalse( WPDO_Term_Comment_Garbage_Filter::is_enabled() );
} }
public function test_is_enabled_respects_zero_value(): void { public function test_is_enabled_respects_zero_value(): void {
@@ -189,11 +189,59 @@ class TermCommentMiscBucketTest extends TestCase {
// ── is_enabled toggle ─────────────────────────────────────────────────── // ── is_enabled toggle ───────────────────────────────────────────────────
public function test_is_enabled_defaults_true(): void { public function test_is_enabled_defaults_false(): void {
delete_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED ); delete_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED );
$this->assertFalse( WPDO_Term_Comment_Misc_Bucket::is_enabled() );
}
// ── one-time default migration for sites that already use the bucket ─────
public function test_preserve_default_keeps_bucket_on_when_misc_has_rows(): void {
$this->reset_preserve_state();
WPDO_Term_Comment_Misc_Bucket::on_term_update( null, 1, 'a', 'x', '' );
TMDO_Installer::maybe_preserve_misc_bucket_default();
$this->assertSame( '1', get_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED ) );
$this->assertTrue( WPDO_Term_Comment_Misc_Bucket::is_enabled() ); $this->assertTrue( WPDO_Term_Comment_Misc_Bucket::is_enabled() );
} }
public function test_preserve_default_leaves_bucket_off_when_misc_empty(): void {
$this->reset_preserve_state();
TMDO_Installer::maybe_preserve_misc_bucket_default();
$this->assertFalse( get_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED ) );
$this->assertFalse( WPDO_Term_Comment_Misc_Bucket::is_enabled() );
$this->assertNotEmpty( get_option( TMDO_Installer::MISC_BUCKET_DEFAULT_FLAG ) );
}
public function test_preserve_default_respects_explicit_off(): void {
$this->reset_preserve_state();
update_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED, '0' );
WPDO_Term_Comment_Misc_Bucket::on_term_update( null, 1, 'a', 'x', '' );
TMDO_Installer::maybe_preserve_misc_bucket_default();
$this->assertSame( '0', get_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED ) );
delete_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED );
}
public function test_preserve_default_runs_only_once(): void {
$this->reset_preserve_state();
TMDO_Installer::maybe_preserve_misc_bucket_default(); // Empty tables: stays off, flag set.
WPDO_Term_Comment_Misc_Bucket::on_term_update( null, 1, 'a', 'x', '' );
TMDO_Installer::maybe_preserve_misc_bucket_default();
$this->assertFalse( get_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED ) );
}
private function reset_preserve_state(): void {
delete_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED );
delete_option( TMDO_Installer::MISC_BUCKET_DEFAULT_FLAG );
}
public function test_is_enabled_respects_zero_value(): void { public function test_is_enabled_respects_zero_value(): void {
update_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED, '0' ); update_option( WPDO_Term_Comment_Misc_Bucket::OPT_ENABLED, '0' );
$this->assertFalse( WPDO_Term_Comment_Misc_Bucket::is_enabled() ); $this->assertFalse( WPDO_Term_Comment_Misc_Bucket::is_enabled() );