wp_privacy_delete_old_export_files()
云策文档标注
概述
wp_privacy_delete_old_export_files() 函数用于自动清理超过指定寿命的个人数据导出文件,以增强数据保护。它通过 cron 作业定期运行,默认删除超过3天的文件。
关键要点
- 函数清理存储在 wp-content/uploads 目录中的个人数据导出文件,这些文件因存储位置而可能公开访问。
- 默认文件寿命为3天,可通过 wp_privacy_export_expiration 过滤器自定义(以秒为单位)。
- 使用 list_files() 函数列出导出目录中的文件,并基于文件修改时间判断是否过期。
- 作为 WordPress 隐私工具的一部分,自版本4.9.6引入,旨在遵循数据保护法规。
代码示例
function wp_privacy_delete_old_export_files() {
$exports_dir = wp_privacy_exports_dir();
if ( ! is_dir( $exports_dir ) ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
$export_files = list_files( $exports_dir, 100, array( 'index.php' ) );
$expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
foreach ( (array) $export_files as $export_file ) {
$file_age_in_seconds = time() - filemtime( $export_file );
if ( $expiration < $file_age_in_seconds ) {
@unlink( $export_file );
}
}
}注意事项
导出文件文件名附加了 CSPRN 以降低未授权下载风险,但自动删除提供了额外保护层。确保 cron 作业正确配置以定期执行此函数。
原文内容
Cleans up export files older than three days old.
Description
The export files are stored in wp-content/uploads, and are therefore publicly accessible. A CSPRN is appended to the filename to mitigate the risk of an unauthorized person downloading the file, but it is still possible. Deleting the file after the data subject has had a chance to delete it adds an additional layer of protection.
Source
function wp_privacy_delete_old_export_files() {
$exports_dir = wp_privacy_exports_dir();
if ( ! is_dir( $exports_dir ) ) {
return;
}
require_once ABSPATH . 'wp-admin/includes/file.php';
$export_files = list_files( $exports_dir, 100, array( 'index.php' ) );
/**
* Filters the lifetime, in seconds, of a personal data export file.
*
* By default, the lifetime is 3 days. Once the file reaches that age, it will automatically
* be deleted by a cron job.
*
* @since 4.9.6
*
* @param int $expiration The expiration age of the export, in seconds.
*/
$expiration = apply_filters( 'wp_privacy_export_expiration', 3 * DAY_IN_SECONDS );
foreach ( (array) $export_files as $export_file ) {
$file_age_in_seconds = time() - filemtime( $export_file );
if ( $expiration < $file_age_in_seconds ) {
unlink( $export_file );
}
}
}
Hooks
- apply_filters( ‘wp_privacy_export_expiration’, int $expiration )
-
Filters the lifetime, in seconds, of a personal data export file.
Changelog
| Version | Description |
|---|---|
| 4.9.6 | Introduced. |