Skip to content

Developer notes

← Overview

app/addons/kv_data_retention/
addon.xml settings, 4 tables, 2 privileges
init.php / func.php hook registration, cron info, licence helpers
controllers/backend/kv_data_retention.php rules, preview, run, history, export, vendor policy, cron
controllers/backend/orders.post.php "anonymized" note on the order page
controllers/backend/kv_data_retention_license.php "Check now" and licence cron
schemas/menu/*.post.php Administration menu, vendor panel menu
schemas/permissions/*.post.php privileges, vendor policy mode, cron as trusted controller
Tygh/Addons/KvDataRetention/
Config.php settings access, live mode, licence gate
Period.php retention period arithmetic (shop time zone)
Rules.php rules, validation, review gate, starting set
Runner.php preview / live runs, batches, caps, lock, hooks
Audit.php runs, audit log, export, cleanup
Types.php handler registry (+ hook)
CoreGdpr.php integration with the core GDPR add-on
Policy.php vendor policy, cron output line
Db.php table / column presence
Handlers/ one class per data type (Handler.php = base)
License/, Upgrade/ licence client, self-healing upgrades
design/backend/templates/addons/kv_data_retention/ views, settings info, order note, licence UI
design/backend/css/addons/kv_data_retention/styles.less
var/langs/en/addons/kv_data_retention.po
Table Contents
?:kv_dret_rules One row per rule: type, name, status, period value and unit (d, m, y), action, options (JSON), company filter, review gate (reviewed_at, updated_at), last preview and last run
?:kv_dret_marks (object_type, object_id) of every record that was anonymized (so it is never anonymized twice). Order types share order; accounts use user.
?:kv_dret_runs One row per run: mode (preview, manual, cron), live flag, status, counts, per-rule JSON summary
?:kv_dret_audit One row per processed record (or per batch for carts and logs): run, rule, type, action, record id, count, time
?:kv_dret_migmap Temporary, only during the update from 1.0.0: old id -> new id of runs, audit rows and rules whose id was already taken in the new table. Dropped when the update has moved every old record.

Update from 1.0.0: the old tables ?:kv_dr_* are moved to ?:kv_dret_* (Upgrade\TableMigration). An old table is dropped only when a check proves that every row is in the new table. While an old table still holds records of this add-on, runs (cron, Run now, preview) and rule changes are refused and the admin panel shows a notice; opening Administration > Data retention retries the move.

Both are fired with fn_set_hook(); register them in your add-on with fn_register_hooks().

// Register an additional data type. Handlers extend Tygh\Addons\KvDataRetention\Handlers\Handler.
function fn_my_addon_kv_data_retention_collect_handlers(&$handlers)
{
$handlers['my_tickets'] = new \Tygh\Addons\MyAddon\RetentionTickets();
}
// Runs after a batch was anonymized or deleted (also after bulk batches, with an empty $ids).
function fn_my_addon_kv_data_retention_processed(&$type, &$action, &$ids, &$rule)
{
if ($type === 'guest_orders' && $action === 'anonymize') {
// clean your own copy of the order contact data for the order ids in $ids
}
}

A handler implements key(), count($rule, $cutoff) and process($rule, $ids, $action, $now), and usually ids($rule, $cutoff, $limit) (the next unprocessed records, in a stable order) and samples(). Bulk types return isBulk() === true and implement purge(). actions() lists what the type supports. Return only record ids and counts: they go into the audit log. A handler that changes more records than it was given (parent orders, orders of an account) appends them to its public $cascaded array as ['type' => '<audit type>', 'ids' => [...]] and may return an estimate from cascadeEstimate() so the preview total includes them. Failed records are returned by leaving them out of the array process() returns: the runner skips them for the rest of the run and reports the number. Language variables for a custom type: kv_data_retention.type_<key>, kv_data_retention.type_<key>_desc and kv_data_retention.field_period_hint_<key>.

fn_delete_user(), fn_delete_shipments(), fn_delete_return(), fn_get_simple_statuses(), the core delete_order hook (fired by the stock-safe order delete), template hook orders:details_sidebar, and the core GDPR services gdpr.user_data_collector, gdpr.anonymizer, gdpr.user_data_updater (4.17.2 and later) when you choose the core engine for inactive accounts. The core engine saves the account with fn_update_user() under the signed-in administrator’s session, so it is used for Run now only; the cron job always uses the own engine. All of them exist with the same signature in 4.17.2 and 4.21.2.

Runner::run() takes a database lock (GET_LOCK, name derived from the database and table prefix) for the whole run, so cron and the admin panel never run at once. A rule is processed in batches (batch_size) until nothing is left, the per-run cap (max_per_rule) or the time budget is reached. Anonymize types select records without a mark, so a batch never returns the same records twice; delete types select the next oldest records.

The add-on has no storefront output, no storefront hooks and writes nothing to ?:storage_data at runtime, so it needs no cache handling. Settings are read from the registry.

Config::isLicensed() wraps License\Client::isActive(). Only runs that can change data check it (Config::canChangeData()); previews, rules, history and the audit log work without a valid licence.