|
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
|
|
|
{ |
|
14
|
|
|
private $plugin_dir; |
|
15
|
|
|
private $last_author = false; |
|
16
|
|
|
private $timeshift_cached_meta; |
|
17
|
|
|
|
|
18
|
12 |
|
public function __construct($i18n) |
|
19
|
|
|
{ |
|
20
|
|
|
global $wpdb; |
|
|
|
|
|
|
21
|
12 |
|
$this->i18n = $i18n; |
|
|
|
|
|
|
22
|
12 |
|
$this->wpdb = $wpdb; |
|
|
|
|
|
|
23
|
12 |
|
$this->plugin_dir = plugin_dir_url(__FILE__) . '../'; |
|
24
|
12 |
|
$this->add_filters(); |
|
25
|
12 |
|
$this->add_actions(); |
|
26
|
12 |
|
$this->add_metabox(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
2 |
|
public function hasTimeshifts($post_id) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$post_type = get_post_type($post_id); |
|
32
|
2 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
33
|
2 |
|
$this->checkTable($post_type); |
|
34
|
2 |
|
$sql = "select count(1) as amount from $table_name where post_id=" . $post_id; |
|
35
|
2 |
|
$r = $this->wpdb->get_results($sql); |
|
36
|
|
|
|
|
37
|
2 |
|
if ($r && count($r) == 1) { |
|
38
|
1 |
|
if (intval($r[0]->amount) > 0) { |
|
39
|
1 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
12 |
|
public function timeshiftVisible() |
|
47
|
|
|
{ |
|
48
|
12 |
|
$check = apply_filters('krn_timeshift_visible', true); |
|
49
|
|
|
|
|
50
|
12 |
|
return $check; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
12 |
|
public function add_metabox() |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
12 |
|
$cl = $this; |
|
56
|
12 |
|
if (! $this->timeshiftVisible()) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
12 |
|
if (! isset($_GET['post']) || ! $this->hasTimeshifts($_GET['post'])) { |
|
60
|
12 |
|
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
|
2 |
|
public function timeshift_metabox() |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
global $post; |
|
|
|
|
|
|
70
|
2 |
|
if (! isset($_GET['post'])) { |
|
71
|
1 |
|
return; |
|
72
|
|
|
} |
|
73
|
1 |
|
$prod_post = get_post($_GET['post']); |
|
74
|
1 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
75
|
1 |
|
$sql = "select * from $table_name where post_id=" . $post->ID . ' order by create_date desc'; |
|
76
|
|
|
|
|
77
|
1 |
|
$last_editor = get_post_meta($prod_post->ID, '_edit_last', true); |
|
78
|
1 |
|
$row = $this->wpdb->get_results($sql); |
|
79
|
1 |
|
echo '<table class="widefat fixed">'; |
|
80
|
1 |
|
echo '<thead>'; |
|
81
|
1 |
|
echo '<tr>'; |
|
82
|
1 |
|
echo '<th width=30></th>'; |
|
83
|
1 |
|
echo '<th width="40%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Title', 'kmm-timeshift') . '</th>'; |
|
84
|
1 |
|
echo '<th width="30%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Snapshot Date', 'kmm-timeshift') . '</th>'; |
|
85
|
1 |
|
echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Author', 'kmm-timeshift') . '</th>'; |
|
86
|
1 |
|
echo '<th width="10%" id="columnname" class="manage-column column-columnname" scope="col">' . __('Actions', 'kmm-timeshift') . '</th>'; |
|
87
|
1 |
|
echo '</tr>'; |
|
88
|
1 |
|
echo ' </thead>'; |
|
89
|
1 |
|
echo '<tbody>'; |
|
90
|
1 |
|
echo '<tr style="font-weight: 800;">'; |
|
91
|
1 |
|
echo '<td>' . get_avatar($last_editor, 30) . '</td>'; |
|
92
|
1 |
|
echo '<td>' . $prod_post->post_title . '</td>'; |
|
93
|
1 |
|
echo '<td>' . $prod_post->post_date . '</td>'; |
|
94
|
1 |
|
echo '<td>' . get_the_author_meta('display_name', $last_editor) . '</td>'; |
|
95
|
1 |
|
echo "<td><a href='post.php?post=" . $_GET['post'] . "&action=edit'><span class='dashicons dashicons-admin-site'></span></A></td>"; |
|
96
|
1 |
|
echo '</tr>'; |
|
97
|
|
|
|
|
98
|
1 |
|
foreach ($row as $rev) { |
|
99
|
|
|
$timeshift = unserialize($rev->post_payload); |
|
100
|
|
|
$style = ''; |
|
101
|
|
|
if (isset($_GET['timeshift']) && $_GET['timeshift'] == $rev->id) { |
|
102
|
|
|
$style = 'style="font-style:italic;background-color: lightblue;"'; |
|
103
|
|
|
} |
|
104
|
|
|
echo '<tr ' . $style . '>'; |
|
105
|
|
|
echo '<td>' . get_avatar($timeshift->meta['_edit_last'][0], 30) . '</td>'; |
|
106
|
|
|
echo '<td>' . $timeshift->post->post_title . '</td>'; |
|
107
|
|
|
echo '<td>' . $rev->create_date . '</td>'; |
|
108
|
|
|
echo '<td>' . get_the_author_meta('display_name', $timeshift->meta['_edit_last'][0]) . '</td>'; |
|
109
|
|
|
echo "<td><a href='post.php?post=" . $_GET['post'] . '&action=edit×hift=' . $rev->id . "'><span class='dashicons dashicons-backup'></span></a></td>"; |
|
110
|
|
|
echo '</tr>'; |
|
111
|
|
|
} |
|
112
|
1 |
|
echo '</tbody>'; |
|
113
|
1 |
|
echo '</table>'; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
12 |
|
public function add_filters() |
|
117
|
|
|
{ |
|
118
|
|
|
// When revisioned post meta has changed, trigger a revision save. |
|
119
|
|
|
//add_filter('wp_save_post_revision_post_has_changed', [$this, '_wp_check_revisioned_meta_fields_have_changed'], 10, 3); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
12 |
|
add_filter('get_post_metadata', [$this, 'inject_metadata_timeshift'], 1, 4); |
|
122
|
12 |
|
add_filter('update_post_metadata', [$this, 'update_post_metadata'], 1, 5); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
public function update_post_metadata($check, int $object_id, string $meta_key, $meta_value, $prev_value) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
1 |
|
if ($meta_key == '_edit_last') { |
|
128
|
1 |
|
$lo = get_post_meta($object_id, '_edit_last', true); |
|
129
|
1 |
|
$this->last_author = $lo; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
return null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
public function inject_metadata_timeshift($value, $post_id, $key, $single) |
|
|
|
|
|
|
136
|
|
|
{ |
|
137
|
3 |
|
if (! isset($_GET['timeshift'])) { |
|
138
|
3 |
|
return; |
|
139
|
|
|
} |
|
140
|
|
|
//Load timeshift |
|
141
|
|
|
if (! $this->timeshift_cached_meta) { |
|
142
|
|
|
$post_type = get_post_type($post_id); |
|
143
|
|
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post_type; |
|
144
|
|
|
$sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
145
|
|
|
$r = $this->wpdb->get_results($sql); |
|
146
|
|
View Code Duplication |
if ($r && count($r) == 1) { |
|
|
|
|
|
|
147
|
|
|
$payload = unserialize($r[0]->post_payload); |
|
148
|
|
|
$this->timeshift_cached_meta = $payload->meta; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
if ($this->timeshift_cached_meta && isset($this->timeshift_cached_meta[$key])) { |
|
152
|
|
|
return $this->timeshift_cached_meta[$key]; |
|
153
|
|
|
} |
|
154
|
|
|
if ($single) { |
|
155
|
|
|
return null; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return []; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
2 |
|
public function inject_timeshift($p) |
|
|
|
|
|
|
162
|
|
|
{ |
|
163
|
|
|
global $post; |
|
|
|
|
|
|
164
|
2 |
|
if (! isset($_GET['timeshift'])) { |
|
165
|
1 |
|
return; |
|
166
|
|
|
} |
|
167
|
|
|
//Load timeshift |
|
168
|
1 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $post->post_type; |
|
169
|
1 |
|
$sql = "select * from $table_name where id=" . intval($_GET['timeshift']); |
|
170
|
1 |
|
$r = $this->wpdb->get_results($sql); |
|
171
|
1 |
View Code Duplication |
if ($r && count($r) == 1) { |
|
|
|
|
|
|
172
|
|
|
$payload = unserialize($r[0]->post_payload); |
|
173
|
|
|
$post = $payload->post; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
12 |
|
public function add_actions() |
|
|
|
|
|
|
178
|
|
|
{ |
|
179
|
12 |
|
add_action('edit_form_top', [$this, 'inject_timeshift'], 1, 1); |
|
180
|
12 |
|
add_action('pre_post_update', [$this, 'pre_post_update'], 2, 1); |
|
181
|
12 |
|
add_action('admin_notices', function () { |
|
182
|
|
|
if (isset($_GET['timeshift']) && $_GET['timeshift']) { |
|
183
|
|
|
echo '<div class="notice notice-warning is-dismissible"> |
|
184
|
|
|
<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> |
|
185
|
|
|
</div>'; |
|
186
|
|
|
} |
|
187
|
12 |
|
}); |
|
188
|
12 |
|
add_action('krn_timeshift_create_snapshot', [$this, 'create_snapshot'], 1, 1); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
4 |
|
public function checkTable($postType) |
|
192
|
|
|
{ |
|
193
|
4 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $postType; |
|
194
|
|
|
|
|
195
|
4 |
|
$charset_collate = $this->wpdb->get_charset_collate(); |
|
196
|
|
|
|
|
197
|
4 |
|
$sql = "CREATE TABLE IF NOT EXISTS $table_name ( |
|
198
|
|
|
id int(12) NOT NULL AUTO_INCREMENT, |
|
199
|
|
|
post_id int(12) NOT NULL, |
|
200
|
|
|
create_date datetime default CURRENT_TIMESTAMP, |
|
201
|
|
|
post_payload TEXT |
|
202
|
|
|
,PRIMARY KEY (id) |
|
203
|
4 |
|
) $charset_collate;"; |
|
204
|
|
|
|
|
205
|
4 |
|
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
|
206
|
4 |
|
$a = dbDelta($sql); |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
4 |
|
return true; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
2 |
|
public function storeTimeshift($timeshift) |
|
212
|
|
|
{ |
|
213
|
2 |
|
$table_name = $this->wpdb->prefix . 'timeshift_' . $timeshift->post->post_type; |
|
214
|
2 |
|
$sql = "insert into $table_name (post_id, post_payload) VALUES(%d, '%s')"; |
|
215
|
2 |
|
$query = $this->wpdb->prepare($sql, $timeshift->post->ID, serialize($timeshift)); |
|
216
|
2 |
|
$this->wpdb->query($query); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function create_snapshot($postID) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->pre_post_update($postID); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
1 |
|
public function pre_post_update(int $post_ID, array $data = null) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
1 |
|
if (wp_is_post_autosave($post_ID)) { |
|
227
|
|
|
return; |
|
228
|
|
|
} |
|
229
|
1 |
|
if (get_post_status($post_ID) == 'auto-draft') { |
|
230
|
|
|
return; |
|
231
|
|
|
} |
|
232
|
1 |
|
$post_type = get_post_type($post_ID); |
|
233
|
1 |
|
$this->checkTable($post_type); |
|
234
|
|
|
|
|
235
|
1 |
|
$mdata = get_metadata('post', $post_ID); |
|
236
|
1 |
|
$post = get_post($post_ID); |
|
237
|
|
|
|
|
238
|
1 |
|
if ($this->last_author) { |
|
239
|
|
|
$mdata['_edit_last'][0] = $this->last_author; |
|
240
|
|
|
} |
|
241
|
1 |
|
unset($mdata['_edit_lock']); |
|
242
|
|
|
|
|
243
|
1 |
|
$timeshift = (object) ['post' => $post, 'meta' => $mdata]; |
|
244
|
1 |
|
$this->storeTimeshift($timeshift); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state