, native_polluted:array} */ public static function run( string $entity_type, bool $apply ): array { global $wpdb; $report = array( 'found' => 0, 'repairable' => 0, 'repaired' => 0, 'unrepairable' => array(), 'native_polluted' => array(), ); $adapter = TMDO_Entity_Registry::get_adapter( $entity_type ); if ( ! $adapter ) { return $report; } $meta_table = $adapter->get_native_meta_table(); $id_col = $adapter->get_entity_id_column(); $meta_pk = 'user' === $entity_type ? 'umeta_id' : 'meta_id'; foreach ( TMDO_Entity_Registry::get_groups_for_type( $entity_type ) as $group ) { $table = TMDO_Schema_Manager::get_table_name( $entity_type, $group ); if ( ! TMDO_Schema_Manager::table_exists( $table ) ) { continue; } foreach ( TMDO_Entity_Registry::get_group_fields( $entity_type, $group ) as $field ) { if ( ! in_array( $field['type'] ?? 'text', array( 'text', 'textarea' ), true ) ) { continue; } $key = (string) $field['key']; $col = TMDO_Schema_Manager::sanitize_column_name( $key ); $ids = $wpdb->get_col( $wpdb->prepare( "SELECT `{$id_col}` FROM `{$table}` WHERE `{$col}` = %s", self::LITERAL ) ); foreach ( $ids as $id ) { $id = (int) $id; ++$report['found']; $native = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM `{$meta_table}` WHERE `{$id_col}` = %d AND meta_key = %s ORDER BY `{$meta_pk}` LIMIT 1", $id, $key ) ); $reason = null === $native ? 'no_native_row' : ( self::LITERAL === $native ? 'native_is_array_literal' : '' ); if ( '' !== $reason ) { $report['unrepairable'][] = array( 'group' => $group, 'key' => $key, 'id' => $id, 'reason' => $reason, ); continue; } ++$report['repairable']; if ( $apply ) { $value = TMDO_Type_Caster::to_db( TMDO_Safe_Unserialize::run( $native ), $field + array( 'group' => $group ) ); $wpdb->update( $table, array( $col => $value ), array( $id_col => $id ) ); ++$report['repaired']; } } $polluted = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT `{$id_col}` FROM `{$meta_table}` WHERE meta_key = %s AND meta_value = %s", $key, self::LITERAL ) ); foreach ( $polluted as $id ) { $report['native_polluted'][] = array( 'group' => $group, 'key' => $key, 'id' => (int) $id, ); } } } if ( $report['repaired'] > 0 ) { TMDO_Cache_Orchestrator::flush_entity( $entity_type ); } return $report; } }