prefix . 'subtleforms_submissions'; $schemas = $wpdb->prefix . 'subtleforms_form_schemas'; $cursor = get_option( self::OPTION, '0' ); $result = array( 'scanned' => 0, 'cleaned' => 0, 'kept' => 0, 'done' => 'done' === $cursor, ); if ( $result['done'] ) { return $result; } $cursor = (int) $cursor; $limit = max( 1, $limit ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time background upgrade. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, form_id, schema_version, meta FROM %i WHERE id > %d AND meta LIKE %s ORDER BY id ASC LIMIT %d', $table, $cursor, '%' . $wpdb->esc_like( '"' . EntrySchema::META_KEY . '"' ) . '%', $limit ), ARRAY_A ); $versions = array(); foreach ( (array) $rows as $row ) { ++$result['scanned']; $cursor = max( $cursor, (int) $row['id'] ); $meta = json_decode( (string) $row['meta'], true ); if ( ! is_array( $meta ) || ! array_key_exists( EntrySchema::META_KEY, $meta ) ) { continue; } $version = null === $row['schema_version'] ? null : (int) $row['schema_version']; $key = (int) $row['form_id'] . ':' . (string) $version; if ( ! array_key_exists( $key, $versions ) ) { $versions[ $key ] = null !== $version && (bool) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time background upgrade. $wpdb->prepare( 'SELECT 1 FROM %i WHERE form_id = %d AND version = %d LIMIT 1', $schemas, (int) $row['form_id'], $version ) ); } if ( ! $versions[ $key ] ) { // No version to read the entry with: the copy stays. ++$result['kept']; continue; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- One-time background upgrade. $wpdb->update( $table, array( 'meta' => wp_json_encode( EntrySchema::stripCopy( $meta ) ) ), array( 'id' => (int) $row['id'] ), array( '%s' ), array( '%d' ) ); ++$result['cleaned']; } $result['done'] = count( (array) $rows ) < $limit; update_option( self::OPTION, $result['done'] ? 'done' : (string) $cursor, false ); if ( $result['done'] ) { Logger::info( 'Entry schema copies removed; entries reference their schema version.' ); } return $result; } }