class-wcs-upgrade-logger.php
1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* Class for logging data during the upgrade process
*
* @author Prospress
* @category Admin
* @package WooCommerce Subscriptions/Admin/Upgrades
* @version 2.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class WCS_Upgrade_Logger {
/** @var WC_Logger instance */
protected static $log = false;
/** @var string File handle */
public static $handle = 'wcs-upgrade';
/** @var string File handle */
public static $weeks_until_cleanup = 8;
public static function init() {
add_action( 'woocommerce_subscriptions_upgraded', __CLASS__ . '::schedule_cleanup' );
add_action( 'woocommerce_subscriptions_clear_upgrade_log', __CLASS__ . '::clear' );
}
/**
* Add an entry to the log
*
* @param string $message
*/
public static function add( $message ) {
if ( empty( self::$log ) ) {
self::$log = new WC_Logger(); // can't use __get() no a static property unfortunately
}
self::$log->add( self::$handle, $message );
}
/**
* Clear entries from the upgrade log.
*/
public static function clear() {
if ( empty( self::$log ) ) {
self::$log = new WC_Logger();
}
self::$log->clear( self::$handle );
}
/**
* Schedule a hook to automatically clear the log after 8 weeks
*/
public static function schedule_cleanup() {
$time_to_cleanup = gmdate( 'U' ) + self::$weeks_until_cleanup * WEEK_IN_SECONDS;
self::add( sprintf( 'Upgrade complete. Scheduling log cleanup for %s GMT/UTC', date( 'Y-m-d H:i:s', $time_to_cleanup ) ) );
wc_schedule_single_action( $time_to_cleanup, 'woocommerce_subscriptions_clear_upgrade_log' );
}
}
WCS_Upgrade_Logger::init();