-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfunctions.php
451 lines (362 loc) · 18.6 KB
/
functions.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
function revisionary() {
return \PublishPress\Revisions::instance();
}
function revisionary_unrevisioned_postmeta() {
$exclude = array_fill_keys( array( '_rvy_base_post_id', '_rvy_has_revisions', '_rvy_published_gmt', '_pp_is_autodraft', '_pp_last_parent', '_edit_lock', '_edit_last', '_wp_old_slug', '_wp_attached_file', '_menu_item_classes', '_menu_item_menu_item_parent', '_menu_item_object', '_menu_item_object_id', '_menu_item_target', '_menu_item_type', '_menu_item_url', '_menu_item_xfn', '_rs_file_key', '_scoper_custom', '_scoper_last_parent', '_wp_attachment_backup_sizes', '_wp_attachment_metadata', '_wp_trash_meta_status', '_wp_trash_meta_time', '_last_attachment_ids', '_last_category_ids', '_encloseme', '_pingme' ), true );
$exclude = apply_filters( 'revisionary_unrevisioned_postmeta', $exclude );
$exclude = array_merge(
$exclude,
array_fill_keys( // These cannot be revisioned without breaking plugin functionality
['_rvy_base_post_id', '_rvy_has_revisions', '_rvy_published_gmt', '_pp_is_autodraft'],
true
)
);
return array_keys(array_filter($exclude));
}
/**
* Sanitizes a string entry
*
* Keys are used as internal identifiers. Uppercase or lowercase alphanumeric characters,
* spaces, periods, commas, plusses, asterisks, colons, pipes, parentheses, dashes and underscores are allowed.
*
* @param string $entry String entry
* @return string Sanitized entry
*/
function pp_revisions_sanitize_entry( $entry ) {
$entry = preg_replace( '/[^a-zA-Z0-9 \.\,\+\*\:\|\(\)_\-]/', '', $entry );
return $entry;
}
/*
* Same as sanitize_key(), but without applying filters
*/
function pp_revisions_sanitize_key( $key ) {
$raw_key = $key;
$key = strtolower( $key );
$key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
return $key;
}
/**
* Copies the taxonomies of a post to another post.
* Based on Yoast Duplicate Post
*
* @param \WP_Post $from_post The source post object.
* @param int $target_id Target post ID.
* @param array $args The options array.
*
* @return void
*/
function revisionary_copy_terms($from_post, $target_id, $args = []) {
global $wpdb;
$defaults = ['empty_target_only' => false];
$args = array_merge($defaults, $args);
foreach (array_keys($defaults) as $var) {
$$var = $args[$var];
}
if ( isset( $wpdb->terms ) ) {
if (is_scalar($from_post)) {
$from_post = get_post($from_post);
}
if (empty($from_post) || empty($from_post->post_type)) {
return;
}
// Clear default category (added by wp_insert_post).
if (!$empty_target_only || !wp_get_object_terms($target_id, 'category', ['fields' => 'ids'])) {
wp_set_object_terms( $target_id, null, 'category' );
}
$post_taxonomies = get_object_taxonomies( $from_post->post_type );
// Several plugins just add support to post-formats but don't register post_format taxonomy.
if (!in_array('post_format', $post_taxonomies, true) && post_type_supports($from_post->post_type, 'post-formats')) {
$post_taxonomies[] = 'post_format';
}
/**
* Filters the taxonomy excludelist when copying a post.
*
* @param array $taxonomies_blacklist The taxonomy excludelist from the options.
*
* @return array
*/
$taxonomies_blacklist = [];
$taxonomies_blacklist = apply_filters('revisionary_skip_taxonomies', $taxonomies_blacklist);
if (defined('POLYLANG_VERSION')) {
if (!empty($args['applying_revision'])) {
$taxonomies_blacklist = array_merge($taxonomies_blacklist, ['language', 'post_translations', 'term_language', 'term_translations', '']);
}
}
foreach (array_diff($post_taxonomies, $taxonomies_blacklist) as $taxonomy) {
if ($empty_target_only) {
$target_terms = wp_get_object_terms($target_id, $taxonomy, ['fields' => 'ids']);
if (!empty($target_terms)) {
continue;
}
}
$post_term_slugs = wp_get_object_terms($from_post->ID, $taxonomy, ['fields' => 'slugs', 'orderby' => 'term_order']);
wp_set_object_terms($target_id, $post_term_slugs, $taxonomy);
}
}
}
/**
* Copies the meta information of a post to another post.
* Based on Yoast Duplicate Post
*
* @param \WP_Post $from_post The source post object.
* @param int $target_id Target post ID.
* @param array $args The options array.
*
* @return void
*/
function revisionary_copy_postmeta($from_post, $to_post_id, $args = []) {
$defaults = ['empty_target_only' => false, 'apply_deletions' => false];
$args = array_merge($defaults, $args);
foreach (array_keys($defaults) as $var) {
$$var = $args[$var];
}
if (is_scalar($from_post)) {
$from_post = get_post($from_post);
}
$source_meta_keys = \get_post_custom_keys( $from_post->ID );
if ( empty( $source_meta_keys ) ) {
return;
}
$meta_excludelist = revisionary_unrevisioned_postmeta();
$meta_excludelist_string = '(' . implode( ')|(', $meta_excludelist ) . ')';
if ( strpos( $meta_excludelist_string, '*' ) !== false ) {
$meta_excludelist_string = str_replace( [ '*' ], [ '[a-zA-Z0-9_]*' ], $meta_excludelist_string );
$meta_keys = [];
foreach ( $source_meta_keys as $meta_key ) {
if (!in_array($meta_key, $meta_excludelist)
&& !preg_match( '#^' . $meta_excludelist_string . '$#', $meta_key )
) {
$meta_keys[] = $meta_key;
}
}
} else {
$meta_keys = array_diff( $source_meta_keys, $meta_excludelist );
}
$target_meta_keys = (array) \get_post_custom_keys( $to_post_id );
$meta_keys = apply_filters(
'revisionary_create_revision_meta_keys', // Bypass problematic Link Whisper plugin postmeta by default
array_diff($meta_keys, ['wpil_links_outbound_external_count_data', 'wpil_links_outbound_internal_count_data', 'wpil_links_outbound_external_count'])
);
foreach ( $meta_keys as $meta_key ) {
if ($empty_target_only && !empty($target_meta_keys) && is_array($target_meta_keys)) {
if (in_array($meta_key, $target_meta_keys)) {
continue;
}
}
$meta_values = \get_post_custom_values( $meta_key, $from_post->ID );
if (!empty($meta_values)) {
if (count($meta_values) > 1) {
delete_post_meta($to_post_id, $meta_key);
foreach ( $meta_values as $meta_value ) {
$meta_value = maybe_unserialize( $meta_value );
add_post_meta( $to_post_id, $meta_key, \PublishPress\Revisions\Utils::recursively_slash_strings( $meta_value ) );
}
} else {
foreach ( $meta_values as $meta_value ) {
$meta_value = maybe_unserialize( $meta_value );
update_post_meta( $to_post_id, $meta_key, \PublishPress\Revisions\Utils::recursively_slash_strings( $meta_value ) );
}
}
}
}
if (!$empty_target_only && !empty($target_meta_keys) && is_array($target_meta_keys)) {
if ($delete_meta_keys = array_diff($target_meta_keys, $meta_keys, revisionary_unrevisioned_postmeta())) {
$deletable_keys = apply_filters('revisionary_deletable_postmeta_keys', ['_links_to', '_links_to_target']);
}
foreach($delete_meta_keys as $meta_key) {
if (in_array($meta_key, $deletable_keys) || !empty($args['apply_deletions']) || defined('PP_REVISIONS_APPLY_POSTMETA_DELETION')) {
delete_post_meta($to_post_id, $meta_key);
}
}
}
$args = array_merge(
$args,
compact('meta_keys', 'source_meta_keys')
);
do_action('revisionary_copy_postmeta', $from_post, $to_post_id, $args);
}
function rvy_revision_base_statuses($args = []) {
$defaults = ['output' => 'names', 'return' => 'array'];
$args = array_merge($defaults, $args);
foreach (array_keys($defaults) as $var) {
$$var = $args[$var];
}
$arr = array_map('sanitize_key', (array) apply_filters('rvy_revision_base_statuses', ['draft', 'pending', 'future']));
if ('object' == $output) {
$status_keys = array_values($arr);
$arr = [];
foreach($status_keys as $k) {
$arr[$k] = get_post_status_object($k);
}
}
return ('csv' == $return) ? "'" . implode("','", $arr) . "'" : $arr;
}
function rvy_revision_statuses($args = []) {
$defaults = ['output' => 'names', 'return' => 'array'];
$args = array_merge($defaults, $args);
foreach (array_keys($defaults) as $var) {
$$var = $args[$var];
}
$arr = array_map('sanitize_key', (array) apply_filters('rvy_revision_statuses', ['draft-revision', 'pending-revision', 'future-revision']));
if ('object' == $output) {
$status_keys = array_values($arr);
$arr = [];
foreach($status_keys as $k) {
$arr[$k] = get_post_status_object($k);
}
}
return ('csv' == $return) ? "'" . implode("','", $arr) . "'" : $arr;
}
function rvy_is_revision_status($post_status) {
return in_array($post_status, rvy_revision_statuses());
}
function rvy_in_revision_workflow($post, $args = []) {
if (!empty($post) && is_numeric($post)) {
$post = get_post($post);
}
if (empty($post) || empty($post->post_mime_type)) {
return false;
}
$base_statuses = rvy_revision_base_statuses();
if (!empty($args['include_trash'])) {
$base_statuses []= 'trash';
}
return rvy_is_revision_status($post->post_mime_type) && in_array($post->post_status, $base_statuses) ? $post->post_mime_type : false;
}
function rvy_post_id($revision_id) {
if ($_post = get_post($revision_id)) {
// if ID passed in is not a revision, return it as is
if (('revision' != $_post->post_type) && !rvy_in_revision_workflow($_post)) {
return $revision_id;
} elseif ('revision' == $_post->post_type) {
return $_post->post_parent;
} else {
if (!$_post->comment_count) {
static $busy;
if (!empty($busy)) {
return;
}
$busy = true;
$published_id = rvy_get_post_meta( $revision_id, '_rvy_base_post_id', true );
$busy = false;
if ($published_id) {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->update($wpdb->posts, ['comment_count' => $published_id], ['ID' => $revision_id]);
}
} else {
$published_id = $_post->comment_count;
}
}
}
return (!empty($published_id)) ? $published_id : 0;
}
// Append a random argument for cache busting
function rvy_nc_url($url, $args = []) {
$nc = (!empty($args['nc'])) ? $args['nc'] : substr(md5(wp_rand(1, 99999999)), 1, 8);
return add_query_arg('nc', $nc, $url);
}
// Complete an admin URL, appending a random argument for cache busting
function rvy_admin_url($partial_admin_url) {
return rvy_nc_url( admin_url($partial_admin_url) );
}
function pp_revisions_plugin_updated($current_version) {
global $wpdb;
$last_ver = get_option('revisionary_last_version');
if (version_compare($last_ver, '3.0.12-rc4', '<')) {
global $wp_version;
if (class_exists('WpeCommon') || version_compare($wp_version, '5.9', '>=')) {
update_option('rvy_scheduled_publish_cron', 1); // trigger generation of cron schedules for existing scheduled revisions
}
}
if (version_compare($last_ver, '3.0.5-beta', '<')) {
if ($role = @get_role('revisor')) {
$role->add_cap('list_others_posts');
$role->add_cap('list_others_pages');
$role->add_cap('list_published_posts');
$role->add_cap('list_published_pages');
$role->add_cap('list_private_posts');
$role->add_cap('list_private_pages');
}
}
if (version_compare($last_ver, '3.0.1', '<')) {
// convert pending / scheduled revisions to v3.0 format
$revision_status_csv = implode("','", array_map('sanitize_key', rvy_revision_statuses()));
$wpdb->query("UPDATE $wpdb->posts SET post_mime_type = post_status WHERE post_status IN ('$revision_status_csv')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'draft', post_mime_type = 'draft-revision' WHERE post_status IN ('draft-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'pending', post_mime_type = 'pending-revision' WHERE post_status IN ('pending-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'pending', post_mime_type = 'future-revision' WHERE post_status IN ('future-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
if (version_compare($last_ver, '3.0.7-rc4', '<') && !defined('PRESSPERMIT_DEBUG')) {
// delete revisions that were erroneously trashed instead of deleted
$wpdb->query("DELETE FROM $wpdb->posts WHERE post_mime_type IN ('draft-revision', 'pending-revision', 'future-revision') AND post_status = 'trash'"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
if (version_compare($last_ver, '3.0-rc7', '<')) {
if ($role = @get_role('administrator')) {
$role->add_cap('manage_unsubmitted_revisions');
}
if ($role = @get_role('revisor')) {
$role->add_cap('upload_files');
}
}
if ($current_version != $last_ver) {
require_once( dirname(__FILE__).'/lib/agapetry_wp_core_lib.php');
require_once(dirname(__FILE__).'/rvy_init.php');
revisionary_refresh_revision_flags();
// mirror to REVISIONARY_VERSION
update_option('revisionary_last_version', $current_version);
delete_option('revisionary_sent_mail');
}
}
function pp_revisions_plugin_activation() {
// force this timestamp to be regenerated, in case something went wrong before
delete_option( 'rvy_next_rev_publish_gmt' );
if (!class_exists('RevisionaryActivation')) {
require_once(dirname(__FILE__).'/activation_rvy.php');
}
require_once(dirname(__FILE__).'/functions.php');
// import from Revisionary 1.x
new RevisionaryActivation(['import_legacy' => true]);
// convert pending / scheduled revisions to v3.0 format
global $wpdb;
$revision_status_csv = implode("','", array_map('sanitize_key', rvy_revision_statuses()));
if (!defined('REVISIONARY_DISABLE_ACTIVATION_TRASH_QUERY')) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_results("SELECT ID, comment_count FROM $wpdb->posts WHERE post_mime_type IN ('draft-revision', 'pending-revision', 'future-revision') AND post_status = 'trash'");
$trashed_ids = [];
foreach ($results as $row) {
$trashed_ids[$row->comment_count] = $row->ID;
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$revision_post_ids = $wpdb->get_col("SELECT comment_count FROM $wpdb->posts WHERE post_mime_type IN ('draft-revision', 'pending-revision', 'future-revision')");
$id_csv = implode("','", $revision_post_ids);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared
$deleted_ids = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE post_id NOT IN ('" . $id_csv . "') AND meta_key = '_rvy_base_post_id'");
foreach (array_merge($trashed_ids, $deleted_ids) as $revision_id) {
delete_post_meta($revision_id, '_rvy_base_post_id', true);
}
if (get_option('rvy_revision_limit_per_post')) {
foreach (array_keys($trashed_ids) as $post_id) {
if ($post_id) {
revisionary_refresh_postmeta($post_id);
}
}
}
}
$wpdb->query("UPDATE $wpdb->posts SET post_mime_type = post_status WHERE post_status IN ('$revision_status_csv')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'draft', post_mime_type = 'draft-revision' WHERE post_status IN ('draft-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'pending', post_mime_type = 'pending-revision' WHERE post_status IN ('pending-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query("UPDATE $wpdb->posts SET post_status = 'pending', post_mime_type = 'future-revision' WHERE post_status IN ('future-revision')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
}
function pp_revisions_plugin_deactivation() {
global $wpdb;
require_once(dirname(__FILE__).'/functions.php');
// Prevents pending / scheduled revisions from being listed as regular drafts / pending posts after plugin is deactivated
$revision_status_csv = implode("','", array_map('sanitize_key', rvy_revision_statuses()));
$wpdb->query("UPDATE $wpdb->posts SET post_status = post_mime_type WHERE post_mime_type IN ('$revision_status_csv')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$wpdb->query("UPDATE $wpdb->posts SET post_mime_type = '' WHERE post_mime_type IN ('$revision_status_csv')"); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ($timestamp = wp_next_scheduled('rvy_mail_buffer_hook')) {
wp_unschedule_event($timestamp,'rvy_mail_buffer_hook');
}
}