class-learndash-import-export-logger.php
3.59 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* This class provides an easy way to import and export processes.
*
* @since 4.5.0
*
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Learndash_Import_Export_Logger' ) && class_exists( 'Learndash_Logger' ) ) {
/**
* Import/Export logger class.
*
* @since 4.5.0
*/
class Learndash_Import_Export_Logger extends Learndash_Logger {
/**
* Log type import.
*
* @since 4.5.0
*
* @var string
*/
public static $log_type_import = 'importing';
/**
* Log type export.
*
* @since 4.5.0
*
* @var string
*/
public static $log_type_export = 'exporting';
/**
* Log type. Should be one of the LOG_TYPE_* constants.
*
* @since 4.5.0
*
* @var string
*/
private $log_type;
/**
* Logger constructor.
*
* @since 4.5.0
*
* @param string $log_type Log type. Should be one of the LOG_TYPE_* constants.
*
* @throws InvalidArgumentException If log type is invalid.
*
* @return void
*/
public function __construct( string $log_type ) {
if ( ! in_array( $log_type, array( self::$log_type_import, self::$log_type_export ), true ) ) {
throw new InvalidArgumentException( 'Invalid log type.' );
}
$this->log_type = $log_type;
}
/**
* Returns the label.
*
* @since 4.5.0
*
* @return string
*/
public function get_label(): string {
switch ( $this->log_type ) {
case self::$log_type_import:
return esc_html__( 'Data Importing', 'learndash' );
case self::$log_type_export:
return esc_html__( 'Data Exporting ', 'learndash' );
default:
return '';
}
}
/**
* Returns the name.
*
* @since 4.5.0
*
* @return string
*/
public function get_name(): string {
return 'import_export_' . $this->log_type;
}
/**
* Maps options and writes to the log file.
*
* @since 4.5.0
*
* @param array<string,mixed> $options Options.
*
* @return void
*/
public function log_options( array $options ): void {
$lines = array(
'Options:',
);
foreach ( $options as $key => $values ) {
$lines[] = "$key: " . wp_json_encode( $values );
}
$this->info(
implode( PHP_EOL, $lines )
);
}
/**
* Maps class name with object vars and writes to the log file.
*
* @since 4.5.0
*
* @param string $classname Class name.
* @param array<string,mixed> $object_vars Object properties.
* @param string $message_before Message before.
* @param string $message_after Message after.
*
* @return void
*/
public function log_object(
string $classname,
array $object_vars,
string $message_before = '',
string $message_after = ''
): void {
$object_vars = array_filter(
$object_vars,
function ( $value, $key ) {
return ! is_object( $value ) && ! in_array(
$key,
array( 'offset_rows', 'offset_media' ),
true
);
},
ARRAY_FILTER_USE_BOTH
);
$additional_info = '';
if ( ! empty( $object_vars ) ) {
$additional_info .= ' with properties' . PHP_EOL;
$additional_info .= implode(
' and ',
array_map(
function ( string $key, $value ) {
return "$key: " . wp_json_encode( $value );
},
array_keys( $object_vars ),
array_values( $object_vars )
)
);
}
if ( ! empty( $message_before ) ) {
$message_before = $message_before . PHP_EOL;
}
if ( ! empty( $message_after ) ) {
$message_after = PHP_EOL . $message_after;
}
$this->info( $message_before . $classname . $additional_info . $message_after );
}
}
}