|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* |
|
4
|
|
|
* |
|
5
|
|
|
* inspired by https://github.com/adamsilverstein/wp-post-meta-revisions/blob/master/wp-post-meta-revisions.php |
|
6
|
|
|
* many thx @adamsilverstein |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace KMM\Timeshift; |
|
11
|
|
|
|
|
12
|
|
|
class Core { |
|
13
|
|
|
private $plugin_dir; |
|
14
|
|
|
private $last_author = false; |
|
15
|
|
|
private $timeshift_cached_meta; |
|
16
|
|
|
|
|
17
|
16 |
|
public function __construct($i18n) { |
|
18
|
|
|
global $wpdb; |
|
19
|
16 |
|
$this->i18n = $i18n; |
|
|
|
|
|
|
20
|
16 |
|
$this->wpdb = $wpdb; |
|
|
|
|
|
|
21
|
16 |
|
$this->plugin_dir = plugin_dir_url(__FILE__) . '../'; |
|
22
|
16 |
|
$this->timeshift_posts_per_page = 5; |
|
|
|
|
|
|
23
|
16 |
|
$this->pagination_ajax_action = 'pagination_timeshift'; |
|
|
|
|
|
|
24
|
16 |
|
$this->add_filters(); |
|
25
|
16 |
|
$this->add_actions(); |
|
26
|
16 |
|
$this->add_metabox(); |
|
27
|
|
|
// Disable WP's own revision system |
|
28
|
16 |
|
remove_post_type_support('post', 'revisions'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
2 |
|
public function hasTimeshifts($post_id) { |
|
32
|
2 |
|
$post_type = get_post_type($post_id); |
|
33
|
2 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
34
|
2 |
|
$this->checkTable($post_type); |
|
35
|
2 |
|
$sql = "select count(1) as amount from $table_name where post_id=" . $post_id; |
|
36
|
2 |
|
$r = $this->wpdb->get_results($sql); |
|
37
|
|
|
|
|
38
|
2 |
|
if ($r && 1 == count($r)) { |
|
39
|
1 |
|
if (intval($r[0]->amount) > 0) { |
|
40
|
1 |
|
return true; |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
return false; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
16 |
|
public function timeshiftVisible() { |
|
48
|
16 |
|
$check = apply_filters('krn_timeshift_visible', true); |
|
49
|
|
|
|
|
50
|
16 |
|
return $check; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
16 |
|
public function add_metabox() { |
|
54
|
16 |
|
$cl = $this; |
|
55
|
16 |
|
if (! $this->timeshiftVisible()) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
// Keep adding the metabox even if no timeshifts available, i.e. will render timeshift box with only live version |
|
59
|
16 |
|
if (! isset($_GET['post'])) { |
|
60
|
16 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
add_action('add_meta_boxes', function () use ($cl) { |
|
63
|
|
|
add_meta_box('krn-timeshift', __('Timeshift', 'kmm-timeshift'), [$cl, 'timeshift_metabox'], null, 'normal', 'core'); |
|
64
|
|
|
}); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function timeshift_metabox() { |
|
68
|
1 |
|
if (! isset($_GET['post'])) { |
|
69
|
1 |
|
return; |
|
70
|
|
|
} |
|
71
|
|
|
$prod_post = get_post($_GET['post']); |
|
72
|
|
|
|
|
73
|
|
|
$start = 0; |
|
74
|
|
|
$timeshift_page = 1; |
|
75
|
|
|
if (isset($_GET['timeshift_page'])) { |
|
76
|
|
|
$start = ($_GET['timeshift_page'] - 1) * $this->timeshift_posts_per_page; |
|
77
|
|
|
$timeshift_page = $_GET['timeshift_page']; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// pagination |
|
81
|
|
|
$pagination = $this->get_paginated_links($prod_post, $timeshift_page); |
|
82
|
|
|
echo $pagination; |
|
83
|
|
|
|
|
84
|
|
|
// load first few & render |
|
85
|
|
|
$rows = $this->get_next_rows($prod_post, $start); |
|
86
|
|
|
$timeshift_table = $this->render_metabox_table($prod_post, $rows); |
|
87
|
|
|
echo $timeshift_table; |
|
88
|
|
|
|
|
89
|
|
|
if (isset($_GET['action']) && $_GET['action'] == $this->pagination_ajax_action) { |
|
90
|
|
|
wp_die(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
16 |
|
public function add_filters() { |
|
95
|
|
|
// When revisioned post meta has changed, trigger a revision save. |
|
96
|
|
|
//add_filter('wp_save_post_revision_post_has_changed', [$this, '_wp_check_revisioned_meta_fields_have_changed'], 10, 3); |
|
97
|
|
|
|
|
98
|
16 |
|
add_filter('get_post_metadata', [$this, 'inject_metadata_timeshift'], 1, 4); |
|
99
|
16 |
|
add_filter('update_post_metadata', [$this, 'update_post_metadata'], 1, 5); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
public function update_post_metadata($check, int $object_id, string $meta_key, $meta_value, $prev_value) { |
|
|
|
|
|
|
103
|
4 |
|
if ('_edit_last' == $meta_key) { |
|
104
|
4 |
|
$lo = get_post_meta($object_id, '_edit_last', true); |
|
105
|
4 |
|
$this->last_author = $lo; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
4 |
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
5 |
|
public function inject_metadata_timeshift($value, $post_id, $key, $single) { |
|
|
|
|
|
|
112
|
5 |
|
if (! isset($_GET['timeshift'])) { |
|
113
|
5 |
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
// Load timeshift |
|
116
|
|
|
if (! $this->timeshift_cached_meta) { |
|
117
|
|
|
$post_type = get_post_type($post_id); |
|
118
|
|
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
119
|
|
|
$sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
120
|
|
|
$r = $this->wpdb->get_results($sql); |
|
121
|
|
View Code Duplication |
if ($r && 1 == count($r)) { |
|
|
|
|
|
|
122
|
|
|
$payload = unserialize($r[0]->post_payload); |
|
123
|
|
|
$this->timeshift_cached_meta = $payload->meta; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
// is the requested meta data in the stored snapshot |
|
127
|
|
|
if ($this->timeshift_cached_meta && isset($this->timeshift_cached_meta[$key])) { |
|
128
|
|
|
return $this->timeshift_cached_meta[$key]; |
|
129
|
|
|
} else { |
|
130
|
|
|
// Otherwise return default value, like acf core fields. |
|
131
|
|
|
return $value; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
2 |
|
public function inject_timeshift($p) { |
|
|
|
|
|
|
136
|
|
|
global $post; |
|
137
|
2 |
|
if (! isset($_GET['timeshift'])) { |
|
138
|
1 |
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
// Load timeshift |
|
141
|
1 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
142
|
1 |
|
$sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
143
|
1 |
|
$r = $this->wpdb->get_results($sql); |
|
144
|
1 |
View Code Duplication |
if ($r && 1 == count($r)) { |
|
|
|
|
|
|
145
|
|
|
$payload = unserialize($r[0]->post_payload); |
|
146
|
|
|
$post = $payload->post; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
16 |
|
public function add_actions() { |
|
151
|
16 |
|
add_action('edit_form_top', [$this, 'inject_timeshift'], 1, 1); |
|
152
|
16 |
|
add_action('pre_post_update', [$this, 'pre_post_update'], 2, 1); |
|
153
|
16 |
|
add_action('add_attachment', [$this, 'add_attachment'], 1, 1); |
|
154
|
16 |
|
add_action('admin_notices', [$this, 'admin_notice']); |
|
155
|
16 |
|
add_action('krn_timeshift_create_snapshot', [$this, 'create_snapshot'], 1, 1); |
|
156
|
16 |
|
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']); |
|
157
|
16 |
|
add_action('wp_ajax_pagination_timeshift', [$this, 'timeshift_metabox']); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function admin_notice() { |
|
161
|
|
|
if (isset($_GET['timeshift']) && $_GET['timeshift']) { |
|
162
|
|
|
echo '<div class="notice notice-warning is-dismissible">'; |
|
163
|
|
|
echo '<p style="font-weight: 800; color: red">' . __('You are editing a historical version! if you save or publish, this will replace the current live one', 'kmm-timeshift') . '</p>'; |
|
164
|
|
|
echo '</div>'; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
public function enqueue_scripts() { |
|
169
|
|
|
wp_enqueue_script('krn-timeshift-pagination-ajax', plugin_dir_url(__FILE__) . '../assets/js/pagination-ajax.js', ['jquery']); |
|
170
|
|
|
wp_localize_script('krn-timeshift-pagination-ajax', 'krn_timeshift', [ |
|
171
|
|
|
'action' => $this->pagination_ajax_action, |
|
172
|
|
|
'post' => isset($_GET['post']) ? $_GET['post'] : false, |
|
173
|
|
|
'timeshift' => isset($_GET['timeshift']) ? $_GET['timeshift'] : false, |
|
174
|
|
|
]); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
6 |
|
public function checkTable($postType) { |
|
178
|
6 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $postType; |
|
179
|
|
|
|
|
180
|
6 |
|
$charset_collate = $this->wpdb->get_charset_collate(); |
|
181
|
|
|
|
|
182
|
6 |
|
$sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
|
183
|
|
|
id int(12) NOT NULL AUTO_INCREMENT, |
|
184
|
|
|
post_id int(12) NOT NULL, |
|
185
|
|
|
create_date datetime default CURRENT_TIMESTAMP, |
|
186
|
|
|
post_payload LONGTEXT, |
|
187
|
|
|
PRIMARY KEY (id) |
|
188
|
6 |
|
) $charset_collate;"; |
|
189
|
|
|
|
|
190
|
6 |
|
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
191
|
6 |
|
$a = dbDelta($sql); |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
6 |
|
return true; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
1 |
|
public function storeTimeshift($timeshift) { |
|
197
|
1 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $timeshift->post->post_type; |
|
198
|
1 |
|
$sql = "insert into $table_name (post_id, post_payload) VALUES(%d, '%s')"; |
|
199
|
1 |
|
$query = $this->wpdb->prepare($sql, $timeshift->post->ID, serialize($timeshift)); |
|
200
|
1 |
|
$this->wpdb->query($query); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
public function create_snapshot($postID) { |
|
204
|
|
|
$this->pre_post_update($postID); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
3 |
|
public function add_attachment($postID) { |
|
208
|
3 |
|
$post = get_post($postID); |
|
209
|
3 |
|
update_post_meta($postID, '_edit_last', $post->post_author); |
|
210
|
3 |
|
$this->pre_post_update($postID); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
3 |
|
public function pre_post_update(int $post_ID, array $data = null) { |
|
|
|
|
|
|
214
|
3 |
|
if (wp_is_post_autosave($post_ID)) { |
|
215
|
|
|
return; |
|
216
|
|
|
} |
|
217
|
3 |
|
if ('auto-draft' == get_post_status($post_ID)) { |
|
218
|
|
|
return; |
|
219
|
|
|
} |
|
220
|
3 |
|
$post_type = get_post_type($post_ID); |
|
221
|
3 |
|
$this->checkTable($post_type); |
|
222
|
|
|
|
|
223
|
3 |
|
$mdata = get_metadata('post', $post_ID); |
|
224
|
3 |
|
$post = get_post($post_ID); |
|
225
|
|
|
|
|
226
|
3 |
|
if ($this->last_author) { |
|
227
|
|
|
$mdata['_edit_last'][0] = $this->last_author; |
|
228
|
|
|
} else { |
|
229
|
|
|
// For unknown last author, clear it. It is a current user now |
|
230
|
3 |
|
$mdata['_edit_last'][0] = ''; |
|
231
|
|
|
} |
|
232
|
3 |
|
unset($mdata['_edit_lock']); |
|
233
|
|
|
|
|
234
|
|
|
// Don't save timeshift when the media was just uploaded, i.e. the post was just created |
|
235
|
3 |
|
if (count($mdata) > 2) { |
|
236
|
1 |
|
$timeshift = (object) ['post' => $post, 'meta' => $mdata]; |
|
237
|
1 |
|
$this->storeTimeshift($timeshift); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
public function get_paginated_links($prod_post, $paged = 1) { |
|
242
|
|
|
if (is_null($prod_post)) { |
|
243
|
|
|
return; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
// count timeshift-versions |
|
247
|
|
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $prod_post->post_type; |
|
248
|
|
|
$sql = "select count(1) as cnt from $table_name where post_id=" . $prod_post->ID; |
|
249
|
|
|
$maxrows = $this->wpdb->get_results($sql); |
|
250
|
|
|
$allrows = (int) $maxrows[0]->{'cnt'}; |
|
251
|
|
|
|
|
252
|
|
|
// max. number of pages |
|
253
|
|
|
$max_page = ceil($allrows / $this->timeshift_posts_per_page); |
|
254
|
|
|
|
|
255
|
|
|
// create pagination links |
|
256
|
|
|
$output = paginate_links([ |
|
257
|
|
|
'current' => max(1, $paged), |
|
258
|
|
|
'total' => $max_page, |
|
259
|
|
|
'mid_size' => 1, |
|
260
|
|
|
'prev_text' => __('«'), |
|
261
|
|
|
'next_text' => __('»'), |
|
262
|
|
|
]); |
|
263
|
|
|
|
|
264
|
|
|
return $output; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
public function get_next_rows($prod_post, $start = 0) { |
|
268
|
|
|
if (! isset($prod_post)) { |
|
269
|
|
|
return; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $prod_post->post_type; |
|
273
|
|
|
$sql = "select * from $table_name where post_id=" . $prod_post->ID . ' order by create_date desc limit ' . $start . ', ' . $this->timeshift_posts_per_page; |
|
274
|
|
|
$rows = $this->wpdb->get_results($sql); |
|
275
|
|
|
|
|
276
|
|
|
return $rows; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
public function render_metabox_table($prod_post, $rows = []) { |
|
280
|
|
|
if (! isset($prod_post)) { |
|
281
|
|
|
return; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
// get last editor |
|
285
|
|
|
$table_postmeta = $this->wpdb->prefix . 'postmeta'; |
|
286
|
|
|
$sql_last_editor = 'select meta_value from ' . $table_postmeta . ' where post_id=' . $prod_post->ID . " AND meta_key='_edit_last'"; |
|
287
|
|
|
$last_editor = $this->wpdb->get_var($sql_last_editor); |
|
288
|
|
|
|
|
289
|
|
|
$output = '<table class="widefat fixed">'; |
|
290
|
|
|
$output .= '<thead>'; |
|
291
|
|
|
$output .= '<tr>'; |
|
292
|
|
|
$output .= '<th width=30></th>'; |
|
293
|
|
|
$output .= '<th width="40%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Title', 'kmm-timeshift') . '</th>'; |
|
294
|
|
|
$output .= '<th width="30%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Snapshot Date', 'kmm-timeshift') . '</th>'; |
|
295
|
|
|
$output .= '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Author', 'kmm-timeshift') . '</th>'; |
|
296
|
|
|
$output .= '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Actions', 'kmm-timeshift') . '</th>'; |
|
297
|
|
|
$output .= '</tr>'; |
|
298
|
|
|
$output .= '</thead>'; |
|
299
|
|
|
$output .= '<tbody>'; |
|
300
|
|
|
|
|
301
|
|
|
// live-version |
|
302
|
|
|
$output .= '<tr style="font-weight: 800;">'; |
|
303
|
|
|
$output .= '<td>' . get_avatar($last_editor, 30) . '</td>'; |
|
304
|
|
|
$output .= '<td>' . $prod_post->post_title . '</td>'; |
|
305
|
|
|
$output .= '<td>' . $prod_post->post_modified . '</td>'; |
|
306
|
|
|
$output .= '<td>' . get_the_author_meta('display_name', $last_editor) . '</td>'; |
|
307
|
|
|
$output .= '<td><a href="post.php?post=' . $_GET['post'] . '&action=edit"><span class="dashicons dashicons-admin-site"></span></A></td>'; |
|
308
|
|
|
$output .= '</tr>'; |
|
309
|
|
|
|
|
310
|
|
|
foreach ($rows as $rev) { |
|
311
|
|
|
$timeshift = unserialize($rev->post_payload); |
|
312
|
|
|
$style = ''; |
|
313
|
|
|
|
|
314
|
|
|
// highlight currently loaded version |
|
315
|
|
|
if (isset($_GET['timeshift']) && $_GET['timeshift'] == $rev->id) { |
|
316
|
|
|
$style = 'style="font-style:italic;background-color: lightblue;"'; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
// some images dont have _edit_last field |
|
320
|
|
|
if (! isset($timeshift->meta['_edit_last']) || is_null($timeshift->meta['_edit_last'])) { |
|
321
|
|
|
$timeshift->meta['_edit_last'] = 0; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
$output .= '<tr ' . $style . '>'; |
|
325
|
|
|
$output .= '<td>' . get_avatar($timeshift->meta['_edit_last'][0], 30) . '</td>'; |
|
326
|
|
|
$output .= '<td>' . $timeshift->post->post_title . '</td>'; |
|
327
|
|
|
$output .= '<td>' . $timeshift->post->post_modified . '</td>'; |
|
328
|
|
|
$output .= '<td>' . get_the_author_meta('display_name', $timeshift->meta['_edit_last'][0]) . '</td>'; |
|
329
|
|
|
$output .= '<td><a href="post.php?post=' . $_GET['post'] . '&action=edit×hift=' . $rev->id . '"><span class="dashicons dashicons-backup"></span></a></td>'; |
|
330
|
|
|
$output .= '</tr>'; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
$output .= '</tbody>'; |
|
334
|
|
|
$output .= '</table>'; |
|
335
|
|
|
|
|
336
|
|
|
return $output; |
|
337
|
|
|
} |
|
338
|
|
|
} |
|
339
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: