-
Notifications
You must be signed in to change notification settings - Fork 2
/
Log.php
249 lines (219 loc) · 5.96 KB
/
Log.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* The Log class file.
*/
namespace Geniem\Oopi;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Log
*
* This class controls logging into the custom database table.
*
* @package Geniem\Oopi
*/
class Log {
/**
* Log id.
*
* @var int
*/
protected $id;
/**
* The importer id of the logged item.
*
* @var string
*/
protected $oopi_id;
/**
* The WordPress item id of the logged item.
*
* @var int
*/
protected $wp_id;
/**
* The gmt timestamp of the log.
*
* @var string
*/
protected $import_date_gmt;
/**
* Importer post data.
*
* @var object|string
*/
protected $data;
/**
* Import status.
*
* @var string
*/
protected $status;
/**
* Get the id.
*
* @return int
*/
public function get_id() {
return $this->id;
}
/**
* Get the importer id.
*
* @return string
*/
public function get_oopi_id() {
return $this->oopi_id;
}
/**
* Get the WP item id.
*
* @return int
*/
public function get_wp_id() {
return $this->wp_id;
}
/**
* Get the log timestamp.
*
* @return mixed
*/
public function get_import_date_gmt() {
return $this->import_date_gmt;
}
/**
* Get importer post data.
*
* @return object
*/
public function get_data() {
return $this->data;
}
/**
* Get import status.
*
* @return string
*/
public function get_status() {
return $this->status;
}
/**
* Log constructor.
*
* An instance can be made out of a Importer Post object or from a log entry data.
* If the $data is a Post instance, a new log entry is saved automatically.
*
* @param mixed $data The data from which the log instance is parsed.
*/
public function __construct( $data ) {
// This is an importer Post object. Save the log entry.
if ( $data instanceof Post ) {
// Get status texts.
$ok_status = Settings::get( 'log_status_ok' );
$fail_status = Settings::get( 'log_status_fail' );
// Data for the log entry.
$this->oopi_id = $data->get_oopi_id();
$this->wp_id = $data->get_wp_id();
$this->import_date_gmt = \current_time( 'mysql', true );
$this->data = $data->to_json();
$this->status = empty( $data->get_errors() ) ? $ok_status : $fail_status;
}
// This is fetch.
else {
$this->oopi_id = isset( $data->oopi_id ) ? $data->oopi_id : null;
$this->wp_id = isset( $data->wp_id ) ? (int) $data->wp_id : null;
$this->import_date_gmt = isset( $data->import_date_gmt ) ? $data->import_date_gmt : null;
$this->data = isset( $data->data ) ? $data->data : null;
$this->status = isset( $data->status ) ? $data->status : null;
// Data might not be decoded yet.
$this->data = Util::is_json( $this->data ) ? json_decode( $this->data ) : $this->data;
}
}
/**
* Save the log entry into the database.
*
* @return int|false The number of rows inserted, or false on error.
*/
public function save() {
global $wpdb;
// Insert into database.
$table = $wpdb->prefix . Settings::get( 'table_name' );
return $wpdb->insert( // phpcs:ignore
$table,
[
'oopi_id' => $this->oopi_id,
'wp_id' => $this->wp_id,
'import_date_gmt' => $this->import_date_gmt,
'data' => $this->data,
'status' => $this->status,
],
[
'%s',
'%d',
'%s',
'%s',
'%s',
]
);
}
/**
* Fetches the last successful import from the database for a given post id.
*
* @param integer $post_id A WP post id.
*
* @return Log|null
*/
public static function get_last_successful_import( $post_id ) : ?Log {
global $wpdb;
$table_name = static::get_table_name();
$ok_status = Settings::get( 'log_status_ok' );
// phpcs:disable
$row = $wpdb->get_row( $wpdb->prepare(
"
SELECT * FROM $table_name
WHERE wp_id = %d
AND status = %s
ORDER BY import_date_gmt DESC;
",
$post_id,
$ok_status
) );
// phpcs:enable
if ( $row !== null ) {
// Make things visible and help IDEs to interpret the object.
return new Log( $row );
}
return null;
}
/**
* Get the logger database table name.
*
* @return string
*/
public static function get_table_name() : string {
global $wpdb;
return $wpdb->prefix . Settings::get( 'table_name' );
}
/**
* Create the logger database table on install.
*/
public static function install() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = static::get_table_name();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
oopi_id VARCHAR(255) NOT NULL,
wp_id BIGINT(20) UNSIGNED,
import_date_gmt DATETIME NULL,
data LONGTEXT NOT NULL,
status VARCHAR(10) NOT NULL,
PRIMARY KEY (id),
INDEX oopi_id (oopi_id(255)),
INDEX postid_date (wp_id, import_date_gmt, status(10))
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$response = dbDelta( $sql );
}
}