-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrvy_init-functions.php
1590 lines (1273 loc) · 57.8 KB
/
rvy_init-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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (isset($_SERVER['SCRIPT_FILENAME']) && basename(__FILE__) == basename(esc_url_raw($_SERVER['SCRIPT_FILENAME'])) )
die();
require_once( dirname(__FILE__).'/lib/agapetry_wp_core_lib.php');
if (defined('REVISIONARY_PRO_FILE')) {
$filepath = REVISIONARY_PRO_FILE;
} else {
$filepath = defined('REVISIONARY_FILE') ? REVISIONARY_FILE : '';
}
if ($filepath && !defined('RVY_NETWORK')) {
define('RVY_NETWORK', awp_is_mu() && rvy_plugin_active_for_network(plugin_basename($filepath)));
}
require_once(dirname(__FILE__).'/functions.php');
require_once(dirname(__FILE__).'/utils.php');
require_once(dirname(__FILE__).'/defaults_rvy.php');
function _rvy_delete_revision($revision_id, $published_post_id) {
revisionary_refresh_revision_flags($published_post_id, ['ignore_revision_ids' => $revision_id]);
}
function _rvy_rest_prepare($response, $post, $request) {
if (!rvy_in_revision_workflow($post)) {
return $response;
}
if ($type_obj = get_post_type_object($post->post_type)) {
$rest_base = ! empty( $type_obj->rest_base ) ? $type_obj->rest_base : $type_obj->name;
$namespace = ! empty( $type_obj->rest_namespace ) ? $type_obj->rest_namespace : 'wp/v2';
$base = sprintf( '%s/%s', $namespace, $rest_base );
$href = rest_url( trailingslashit( $base ) . $post->ID );
$response->add_link('https://api.w.org/action-publish', $href);
}
return $response;
}
function rvy_mail_check_buffer($new_msg = [], $args = []) {
if (empty($args['log_only'])) {
if (!$use_buffer = rvy_get_option('use_notification_buffer')) {
return (defined('REVISIONARY_DISABLE_MAIL_LOG'))
? array_fill_keys(['buffer', 'sent_mail', 'send_limits', 'sent_counts', 'new_msg_buffered'], [])
: [];
}
}
require_once( dirname(__FILE__).'/mail-buffer_rvy.php');
return _rvy_mail_check_buffer($new_msg, $args);
}
function rvy_send_buffered_mail() {
require_once( dirname(__FILE__).'/mail-buffer_rvy.php');
_rvy_send_buffered_mail();
}
function rvy_set_notification_buffer_cron() {
$cron_timestamp = wp_next_scheduled( 'rvy_mail_buffer_hook' );
if (rvy_get_option('use_notification_buffer')) {
if (!$cron_timestamp) {
wp_schedule_event(time(), 'two_minutes', 'rvy_mail_buffer_hook');
}
} else {
wp_unschedule_event($cron_timestamp, 'rvy_mail_buffer_hook');
}
}
function rvy_mail_buffer_cron_interval( $schedules ) {
$schedules['two_minutes'] = array(
'interval' => 120,
'display' => esc_html__( 'Every 2 Minutes', 'revisionary' ),
);
return $schedules;
}
function _revisionary_publish_scheduled_cron($revision_id) {
if (is_array($revision_id) && isset($revision_id['revision_id'])) {
$revision_id = $revision_id['revision_id'];
}
if (rvy_get_option('scheduled_revisions') && rvy_get_option('scheduled_publish_cron')) {
revisionary_publish_scheduled(compact('revision_id'));
}
}
/*=================== End WP-Cron implementation ====================*/
function _rvy_existing_schedules_to_cron($prev_use_cron, $use_cron) {
if ($use_cron && !$prev_use_cron) {
global $wpdb;
$time_gmt = current_time('mysql', 1);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM $wpdb->posts WHERE post_type != 'revision' AND post_status != 'inherit' AND post_mime_type = 'future-revision' AND post_date_gmt > %s ORDER BY post_date_gmt DESC",
$time_gmt
)
);
foreach($results as $revision) {
if (!wp_get_scheduled_event('publish_revision_rvy', ['revision_id' => $revision->ID])) {
wp_schedule_single_event(strtotime($revision->post_date_gmt), 'publish_revision_rvy', ['revision_id' => $revision->ID]);
}
}
}
if (!$use_cron && $prev_use_cron) {
require_once( dirname(__FILE__).'/admin/revision-action_rvy.php');
rvy_update_next_publish_date();
}
}
/*
* Revision previews: prevent redirect for non-standard post url
*/
function _rvy_no_redirect_filter($redirect, $orig) {
global $current_user, $wpdb;
if (!empty($current_user->ID) && (empty($wpdb) || empty($wpdb->is_404))) {
$redirect = $orig;
}
return $redirect;
}
function rvy_ajax_handler() {
global $current_user, $wpdb;
if (!empty($_REQUEST['rvy_ajax_field']) && !empty($_REQUEST['rvy_ajax_value'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if ($post_id = intval($_REQUEST['rvy_ajax_value'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
switch ($_REQUEST['rvy_ajax_field']) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
case 'create_revision':
if (current_user_can('copy_post', $post_id)) {
$time_gmt = (!empty($_REQUEST['rvy_date_selection'])) ? intval($_REQUEST['rvy_date_selection']) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
require_once( dirname(REVISIONARY_FILE).'/revision-creation_rvy.php' );
$rvy_creation = new PublishPress\Revisions\RevisionCreation();
$revision_status = (rvy_get_option('auto_submit_revisions') && current_user_can('edit_post', $post_id)) ? 'pending-revision' : 'draft-revision';
$rvy_creation->createRevision($post_id, $revision_status, compact('time_gmt'));
}
exit;
case 'submit_revision':
// capability check is applied within function to support batch execution without redundant checks
require_once( dirname(__FILE__).'/admin/revision-action_rvy.php');
rvy_revision_submit($post_id);
$check_autosave = true;
break;
case 'create_scheduled_revision':
if (!empty($_REQUEST['rvy_date_selection'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$time_gmt = intval($_REQUEST['rvy_date_selection']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (current_user_can('edit_post', $post_id)) {
require_once( dirname(REVISIONARY_FILE).'/revision-creation_rvy.php' );
$rvy_creation = new PublishPress\Revisions\RevisionCreation();
$rvy_creation->createRevision($post_id, 'future-revision', compact('time_gmt'));
}
}
break;
case 'author_select':
if (!empty($_REQUEST['rvy_selection'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (current_user_can('edit_post', $post_id)) {
update_post_meta($post_id, '_rvy_author_selection', (int) $_REQUEST['rvy_selection']); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
}
}
break;
default:
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (('submit_revision' != $_REQUEST['rvy_ajax_field']) && !empty($check_autosave) && !defined('REVISIONARY_IGNORE_REVISION_AUTOSAVE')) {
if ($autosave_post = PublishPress\Revisions\Utils::get_post_autosave($post_id, $current_user->ID)) {
$main_post = get_post($post_id);
// If revision autosave is newer than revision post_updated date, copy over post data
if (strtotime($autosave_post->post_modified_gmt) > strtotime($main_post->post_modified_gmt)) {
$set_post_properties = [
'post_content',
'post_content_filtered',
'post_title',
'post_excerpt',
];
foreach($set_post_properties as $prop) {
if (!empty($autosave_post->$prop)) {
$update_data[$prop] = $autosave_post->$prop;
}
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->update($wpdb->posts, $update_data, ['ID' => $post_id]);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->delete($wpdb->posts, ['ID' => $autosave_post->ID]);
}
}
}
exit;
}
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
if (defined('DOING_AJAX') && DOING_AJAX && isset($_REQUEST['action']) && ('get-revision-diffs' == $_REQUEST['action'])) {
require_once( dirname(__FILE__).'/admin/history_rvy.php' );
new RevisionaryHistory();
}
}
function rvy_get_post_meta($post_id, $meta_key, $unused = false) {
return get_post_meta($post_id, $meta_key, true);
}
function rvy_update_post_meta($post_id, $meta_key, $meta_value) {
global $wpdb, $revisionary;
if (!empty($revisionary)) {
$revisionary->internal_meta_update = true;
}
update_post_meta($post_id, $meta_key, $meta_value);
if (!empty($revisionary)) {
$revisionary->internal_meta_update = true;
}
}
function rvy_delete_post_meta($post_id, $meta_key) {
delete_post_meta($post_id, $meta_key);
}
function rvy_status_registrations() {
$block_editor = \PublishPress\Revisions\Utils::isBlockEditorActive();
$labels = apply_filters('revisionary_status_labels',
rvy_get_option('revision_statuses_noun_labels') ?
[
'draft-revision' => [
'name' => esc_html__('Working Copy', 'revisionary'),
'submit' => esc_html__('Create Working Copy', 'revisionary'),
'submit_short' => esc_html__('Copy', 'revisionary'),
'submitting' => esc_html__('Creating Working Copy...', 'revisionary'),
'submitted' => esc_html__('Working Copy ready', 'revisionary'),
'approve' => esc_html__('Approve Changes', 'revisionary'),
'approve_short' => esc_html__('Approve', 'revisionary'),
'approving' => esc_html__('Approving Changes...', 'revisionary'),
'publish' => esc_html__('Publish Changes', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Working Copies', 'revisionary'),
'short' => esc_html__('Working Copy', 'revisionary'),
'count' => _n_noop('Working Copies <span class="count">(%d)</span>', 'Working Copies <span class="count">(%d)</span>', 'revisionary'), // @todo: confirm API will support a fixed string
'basic' => 'Copy',
],
'pending-revision' => [
'name' => esc_html__('Change Request', 'revisionary'),
'submit' => esc_html__('Submit Change Request', 'revisionary'),
'submit_short' => esc_html__('Submit', 'revisionary'),
'submitting' => esc_html__('Submitting Changes...', 'revisionary'),
'submitted' => esc_html__('Changes Submitted', 'revisionary'),
'approve' => esc_html__('Approve Changes', 'revisionary'),
'approve_short' => esc_html__('Approve', 'revisionary'),
'approving' => esc_html__('Approving Changes...', 'revisionary'),
'publish' => esc_html__('Publish Changes', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Change Requests', 'revisionary'),
'short' => esc_html__('Change Request', 'revisionary'),
'count' => _n_noop('Change Requests <span class="count">(%d)</span>', 'Change Requests <span class="count">(%d)</span>', 'revisionary'),
'enable' => esc_html__('Enable Change Requests', 'revisionary'),
'basic' => 'Change Request',
],
'future-revision' => [
'name' => esc_html__('Scheduled Change', 'revisionary'),
'submit' => esc_html__('Schedule Changes', 'revisionary'),
'submit_short' => esc_html__('Schedule Changes', 'revisionary'),
'submitting' => esc_html__('Scheduling Changes...', 'revisionary'),
'submitted' => esc_html__('Changes are Scheduled.', 'revisionary'),
'approve' => esc_html__('Schedule Changes', 'revisionary'),
'approve_short' => esc_html__('Schedule Changes', 'revisionary'),
'publish' => esc_html__('Publish Changes', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Scheduled Changes', 'revisionary'),
'short' => esc_html__('Scheduled Change', 'revisionary'),
'count' => _n_noop('Scheduled <span class="count">(%d)</span>', 'Scheduled <span class="count">(%d)</span>', 'revisionary'),
'basic' => 'Scheduled Change',
],
]
:
[
'draft-revision' => [
'name' => esc_html__('Unsubmitted Revision', 'revisionary'),
'submit' => esc_html__('New Revision', 'revisionary'),
'submit_short' => esc_html__('New Revision', 'revisionary'),
'submitting' => esc_html__('Creating Revision...', 'revisionary'),
'submitted' => ($block_editor) ? esc_html__('The Revision is ready to edit.', 'revisionary') : esc_html__('Revision ready to edit.', 'revisionary'),
'approve' => esc_html__('Approve Revision', 'revisionary'),
'approve_short' => esc_html__('Approve', 'revisionary'),
'publish' => esc_html__('Publish Revision', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Unsubmitted Revisions', 'revisionary'),
'short' => esc_html__('Not Submitted', 'revisionary'),
'count' => _n_noop('Not Submitted <span class="count">(%s)</span>', 'Not Submitted <span class="count">(%s)</span>', 'revisionary'), // @todo: confirm API will support a fixed string
'basic' => 'Revision',
],
'pending-revision' => [
'name' => esc_html__('Submitted Revision', 'revisionary'),
'submit' => esc_html__('Submit Revision', 'revisionary'),
'submit_short' => esc_html__('Submit', 'revisionary'),
'submitting' => esc_html__('Submitting Revision...', 'revisionary'),
'submitted' => ($block_editor) ? esc_html__('The Revision is Submitted', 'revisionary') : esc_html__('Revision Submitted', 'revisionary'),
'approve' => esc_html__('Approve Revision', 'revisionary'),
'approve_short' => esc_html__('Approve', 'revisionary'),
'publish' => esc_html__('Publish Revision', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Submitted Revisions', 'revisionary'),
'short' => esc_html__('Submitted', 'revisionary'),
'count' => _n_noop('Submitted <span class="count">(%s)</span>', 'Submitted <span class="count">(%s)</span>', 'revisionary'),
'basic' => 'Revision',
],
'future-revision' => [
'name' => esc_html__('Scheduled Revision', 'revisionary'),
'submit' => esc_html__('Schedule Revision', 'revisionary'),
'submit_short' => esc_html__('Schedule Revision', 'revisionary'),
'submitting' => esc_html__('Scheduling Revision...', 'revisionary'),
'submitted' => ($block_editor) ? esc_html__('The Revision is Scheduled', 'revisionary') : esc_html__('Revision Scheduled', 'revisionary'),
'approve' => esc_html__('Approve Revision', 'revisionary'),
'approve_short' => esc_html__('Approve', 'revisionary'),
'publish' => esc_html__('Publish Revision', 'revisionary'),
'save' => esc_html__('Save Revision', 'revisionary'),
'update' => esc_html__('Update Revision', 'revisionary'),
'plural' => esc_html__('Scheduled Revisions', 'revisionary'),
'short' => esc_html__('Scheduled', 'revisionary'),
'count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>', 'revisionary'),
'basic' => 'Scheduled Revision',
],
]
);
register_post_status('draft-revision', array(
'label' => $labels['draft-revision']['name'],
'labels' => (object) $labels['draft-revision'],
'protected' => true,
'internal' => true,
'label_count' => $labels['draft-revision']['count'],
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
));
register_post_status('pending-revision', array(
'label' => $labels['pending-revision']['name'],
'labels' => (object) $labels['pending-revision'],
'protected' => true,
'internal' => true,
'label_count' => $labels['pending-revision']['count'],
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
));
register_post_status('future-revision', array(
'label' => $labels['future-revision']['name'],
'labels' => (object) $labels['future-revision'],
'protected' => true,
'internal' => true,
'label_count' => $labels['future-revision']['count'],
'exclude_from_search' => false,
'show_in_admin_all_list' => false,
'show_in_admin_status_list' => false,
));
foreach(rvy_get_manageable_types() as $post_type) {
add_filter("rest_{$post_type}_collection_params", function($query_params, $post_type = '')
{
$query_params['status']['items']['enum'] []= 'draft-revision';
$query_params['status']['items']['enum'] []= 'pending-revision';
$query_params['status']['items']['enum'] []= 'future-revision';
return $query_params;
}, 999, 2
);
}
// WP > 5.3: Don't allow revision statuses to be blocked at the REST API level. Our own filters are sufficient to regulate their usage.
add_action( 'rest_api_init', function() {
global $wp_post_statuses;
foreach(rvy_revision_statuses() as $status) {
if (isset($wp_post_statuses[$status])) {
$wp_post_statuses[$status]->internal = false;
}
}
}, 97
);
add_action( 'rest_api_init', function() {
global $wp_post_statuses;
foreach(rvy_revision_statuses() as $status) {
if (isset($wp_post_statuses[$status])) {
$wp_post_statuses[$status]->internal = true;
}
}
}, 99
);
}
function pp_revisions_status_label($status_name, $label_property) {
global $wp_post_statuses;
if (!empty($wp_post_statuses[$status_name]) && !empty($wp_post_statuses[$status_name]->labels->$label_property)) {
return $wp_post_statuses[$status_name]->labels->$label_property;
} elseif (!empty($wp_post_statuses[$status_name]) && !empty($wp_post_statuses[$status_name]->label)) {
return $wp_post_statuses[$status_name]->label;
} else {
return '';
}
}
function pp_revisions_label($label_name) {
static $labels;
if (empty($labels)) {
$labels = apply_filters('revisionary_labels',
[
'my_revisions' => (rvy_get_option('revision_statuses_noun_labels'))
? _n_noop('%sMy Copies & Changes%s(%s)</span>', '%sMy Copies & Changes%s(%s)</span>', 'revisionary')
: _n_noop('%sMy Revisions%s(%s)</span>', '%sMy Revisions%s(%s)</span>', 'revisionary'),
'my_published_posts' => _n_noop('%sRevisions to My Posts%s(%s)</span>', '%sRevisions to My Posts%s(%s)', 'revisionary'),
'queue_col_revision' => esc_html__('Revision', 'revisionary'),
'queue_col_revised_by' => esc_html__('Revised By', 'revisionary'),
'queue_col_revision_date' => esc_html__('Revision Date', 'revisionary'),
'queue_col_post_author' => esc_html__('Post Author', 'revisionary'),
'queue_col_published_post' => esc_html__('Published Post', 'revisionary'),
'update_revision' => esc_html__('Update Revision', 'revisionary'),
'submit_revision' => (rvy_get_option('revision_statuses_noun_labels'))
? esc_html__('Submit Revision', 'revisionary')
: esc_html__('Submit Changes', 'revisionary')
]);
}
return (isset($labels[$label_name])) ? $labels[$label_name] : '';
}
// WP function is_plugin_active_for_network() is defined in admin
function rvy_plugin_active_for_network( $plugin ) {
if ( ! is_multisite() ) {
return false;
}
$plugins = get_site_option( 'active_sitewide_plugins' );
if ( isset( $plugins[ $plugin ] ) ) {
return true;
}
return false;
}
function rvy_is_plugin_active($check_plugin_file) {
$plugins = (array)get_option('active_plugins');
foreach ($plugins as $plugin_file) {
if (false !== strpos($plugin_file, $check_plugin_file)) {
return $plugin_file;
}
}
if (is_multisite()) {
$plugins = (array)get_site_option('active_sitewide_plugins');
// network activated plugin names are array keys
foreach (array_keys($plugins) as $plugin_file) {
if (false !== strpos($plugin_file, $check_plugin_file)) {
return $plugin_file;
}
}
}
}
function rvy_configuration_late_init() {
global $revisionary;
if (!empty($revisionary)) {
$revisionary->configurationLateInit();
}
}
// auto-define the Revisor role to include custom post type capabilities equivalent to those added for post, page in rvy_add_revisor_role()
function rvy_add_revisor_custom_caps() {
if ( ! rvy_get_option( 'revisor_role_add_custom_rolecaps' ) )
return;
global $wp_roles, $revisionary;
if (empty($revisionary)) {
return;
}
$custom_types = array_intersect_key(
get_post_types(['_builtin' => false], 'object'),
$revisionary->enabled_post_types
);
if ( isset( $wp_roles->roles['revisor'] ) ) {
if ($custom_types) {
foreach( $custom_types as $post_type => $type_obj ) {
$cap = $type_obj->cap;
$custom_caps = array_fill_keys( array( $cap->read_private_posts, $cap->edit_posts, $cap->edit_others_posts, "delete_{$post_type}s" ), true );
if (!empty($type_obj->cap->edit_published_posts)) {
$list_published_cap = str_replace('edit_', 'list_', $type_obj->cap->edit_published_posts);
$custom_caps[$list_published_cap] = true;
}
if (!empty($type_obj->cap->edit_private_posts)) {
$list_private_cap = str_replace('edit_', 'list_', $type_obj->cap->edit_private_posts);
$custom_caps[$list_private_cap] = true;
}
$wp_roles->roles['revisor']['capabilities'] = array_merge( $wp_roles->roles['revisor']['capabilities'], $custom_caps );
$wp_roles->role_objects['revisor']->capabilities = array_merge( $wp_roles->role_objects['revisor']->capabilities, $custom_caps );
}
}
}
if ( isset( $wp_roles->roles['contributor'] ) ) {
if ($custom_types) {
foreach( $custom_types as $post_type => $type_obj ) {
$cap = $type_obj->cap;
$custom_caps = [];
if (!empty($type_obj->cap->edit_published_posts)) {
$list_published_cap = str_replace('edit_', 'list_', $type_obj->cap->edit_published_posts);
$custom_caps[$list_published_cap] = true;
}
if (!empty($type_obj->cap->edit_private_posts)) {
$list_private_cap = str_replace('edit_', 'list_', $type_obj->cap->edit_private_posts);
$custom_caps[$list_private_cap] = true;
}
$wp_roles->roles['contributor']['capabilities'] = array_merge( $wp_roles->roles['contributor']['capabilities'], $custom_caps );
$wp_roles->role_objects['contributor']->capabilities = array_merge( $wp_roles->role_objects['contributor']->capabilities, $custom_caps );
}
}
}
global $current_user;
foreach(['contributor', 'revisor'] as $role_name) {
if (in_array($role_name, $current_user->roles)) {
$current_user->allcaps = array_merge($current_user->allcaps, $wp_roles->role_objects[$role_name]->capabilities);
}
}
if (function_exists('presspermit')) {
$user = presspermit()->getUser();
$user->allcaps = $current_user->allcaps;
}
}
function rvy_detect_post_type() {
global $revisionary;
if ( isset($revisionary) && $revisionary->doing_rest && $revisionary->rest->is_posts_request )
return $revisionary->rest->post_type;
else
return awp_post_type_from_uri();
}
function rvy_detect_post_id() {
global $revisionary;
if ( isset($revisionary) && $revisionary->doing_rest && $revisionary->rest->is_posts_request ) {
$post_id = $revisionary->rest->post_id;
} elseif ( ! empty( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_GET['post']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_POST['post_ID'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_POST['post_ID']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_REQUEST['post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_REQUEST['post_id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_GET['p'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_GET['p']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_GET['id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_GET['id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_REQUEST['fl_builder_data'] ) && is_array( $_REQUEST['fl_builder_data'] ) && ! empty( $_REQUEST['fl_builder_data']['post_id'] ) ) {
$post_id = (int) $_REQUEST['fl_builder_data']['post_id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif ( ! empty( $_GET['page_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
$post_id = (int) $_GET['page_id']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
// phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif (defined('REST_REQUEST') && REST_REQUEST && isset($_SERVER['REQUEST_URI']) && strpos(esc_url_raw($_SERVER['REQUEST_URI']), 'autosaves')) {
require_once( dirname(__FILE__).'/rest_rvy.php' );
$post_id = Revisionary_REST::get_id_element(esc_url_raw($_SERVER['REQUEST_URI']), 1); // phpcs:ignore WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing
} elseif (defined('DOING_AJAX') && DOING_AJAX) {
$post_id = apply_filters('revisionary_detect_id', 0, ['is_ajax' => true]);
} else {
$post_id = 0;
}
return $post_id;
}
function rvy_add_revisor_role( $requested_blog_id = '' ) {
global $wp_roles;
if (defined('REVISIONARY_NO_REVISOR_ROLE')) {
return;
}
$wp_role_caps = array(
'read' => true,
'read_private_posts' => true,
'read_private_pages' => true,
'edit_posts' => true,
'delete_posts' => true,
'edit_others_posts' => true,
'edit_pages' => true,
'delete_pages' => true,
'edit_others_pages' => true,
'list_published_posts' => true,
'list_published_pages' => true,
'list_private_posts' => true,
'list_private_pages' => true,
'upload_files' => true,
'level_3' => true,
'level_2' => true,
'level_1' => true,
'level_0' => true
);
$wp_roles->add_role( 'revisor', esc_html__( 'Revisor', 'revisionary' ), $wp_role_caps );
}
function rvy_apply_role_translation($translations, $text, $context, $domain) {
if (('User role' === $context) && ('Revisor' == $text) && ($domain !== 'revisionary')) {
return translate_with_gettext_context($text, $context, 'revisionary');
}
return $translations;
}
function rvy_role_translation_support() {
_x('Revisor', 'User role', 'revisionary');
add_filter('gettext_with_context', 'rvy_apply_role_translation', 10, 4);
}
// wrapper function for use with wp_cron hook
function revisionary_publish_scheduled($args = []) {
require_once( dirname(__FILE__).'/admin/revision-action_rvy.php');
rvy_publish_scheduled_revisions($args);
}
function revisionary_refresh_postmeta($post_id, $args = []) {
global $wpdb;
$ignore_revisions = (!empty($args['ignore_revisions'])) ? $args['ignore_revisions'] : [];
$ignore_clause = ($ignore_revisions) ? " AND ID NOT IN (" . implode(",", array_map('intval', $ignore_revisions)) . ")" : '';
if (defined('REVISIONARY_LIMIT_IGNORE_UNSUBMITTED')) {
$ignore_clause .= " AND post_mime_type != 'draft-revision'";
}
$revision_status_csv = implode("','", array_map('sanitize_key', rvy_revision_statuses()));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$has_revisions = $wpdb->get_var(
// account for post deletion
$wpdb->prepare(
"SELECT ID FROM $wpdb->posts WHERE post_mime_type IN ('$revision_status_csv') $ignore_clause AND post_status != 'trash' AND comment_count = %d LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$post_id
)
);
$set_value = !empty($has_revisions);
$_post = get_post($post_id);
if ($set_value && (empty($_post) || empty($_post->post_mime_type) || !in_array($_post->post_mime_type, ['draft-revision', 'pending-revision', 'future-revision']))) {
rvy_update_post_meta($post_id, '_rvy_has_revisions', $set_value);
} elseif (empty($args['insert_only'])) { // avoid redundant deletions
delete_post_meta($post_id, '_rvy_has_revisions');
}
}
function rvy_post_revision_supported($post, $args = []) {
if ($post) {
if (rvy_post_revision_blocked($post, $args)) {
return false;
}
}
return true;
}
function rvy_post_revision_blocked($post, $args = []) {
static $unfiltered_html;
$revision_unfiltered_html_check = rvy_get_option('revision_unfiltered_html_check');
if ($revision_unfiltered_html_check && !isset($unfiltered_html)) {
$unfiltered_html = current_user_can('unfiltered_html');
}
$post_id = (is_scalar($post)) ? $post : $post->ID;
if (1 === intval(rvy_get_option('revision_limit_per_post'))) {
if (rvy_get_post_meta($post_id, '_rvy_has_revisions')) {
return [
'code' => 'blocked_revision_limit',
'description' => __('The post already has a revision in process.', 'revisionary')
];
}
}
if ($revision_unfiltered_html_check && !$unfiltered_html && (empty($args['context']) || ('admin_posts' != $args['context']))) {
if (is_scalar($post)) {
$post = get_post($post);
}
if (!empty($post) && is_object($post) && !empty($post->post_content && (wp_filter_post_kses($post->post_content) != $post->post_content))) {
return [
'code' => 'blocked_unfiltered',
'description' => __('The unfiltered_html capability is required to create a revision of this post.', 'revisionary')
];
}
}
return false;
}
if (!empty($_REQUEST['rvy_flush_flags'])) {
check_admin_referer('flush-flags');
revisionary_refresh_revision_flags();
}
function revisionary_refresh_revision_flags($published_post_id = 0, $args = []) {
global $wpdb;
$ignore_revision_ids = (!empty($args['ignore_revision_ids'])) ? (array) $args['ignore_revision_ids'] : [];
$status_csv = implode("','", array_map('sanitize_key', rvy_filtered_statuses()));
$revision_base_status_csv = implode("','", array_map('sanitize_key', rvy_revision_base_statuses()));
$revision_statuses = rvy_revision_statuses();
if (defined('REVISIONARY_LIMIT_IGNORE_UNSUBMITTED')) {
$revision_statuses = array_diff($revision_statuses, ['draft-revision']);
}
$revision_status_csv = implode("','", array_map('sanitize_key', $revision_statuses));
$query = "SELECT r.comment_count FROM $wpdb->posts r INNER JOIN $wpdb->posts p ON r.comment_count = p.ID"
. " WHERE p.post_status IN ('$status_csv') AND r.post_status IN ('$revision_base_status_csv')"
. " AND r.post_mime_type IN ('$revision_status_csv') AND p.post_mime_type NOT IN ('$revision_status_csv')";
if ($published_post_id) {
$query = $wpdb->prepare("$query AND p.ID = %d", $published_post_id); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
if ($ignore_revision_ids) {
$ignore_revisions_csv = implode("','", array_map('sanitize_key', $ignore_revision_ids));
$query .= " AND r.ID NOT IN ('$ignore_revisions_csv')";
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
$arr_have_revisions = $wpdb->get_col($query);
$have_revisions = implode("','", array_map('intval', array_unique($arr_have_revisions)));
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared
if ($ids = $wpdb->get_col("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_rvy_has_revisions' AND post_id NOT IN ('$have_revisions')")) {
foreach ($ids as $post_id) {
rvy_delete_post_meta($post_id, '_rvy_has_revisions');
}
}
$query = "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_rvy_has_revisions'";
if ($published_post_id) {
$query = $wpdb->prepare("$query AND post_id = %d", $published_post_id); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
$have_flag_ids = $wpdb->get_col($query);
if ($posts_missing_flag = array_diff($arr_have_revisions, $have_flag_ids)) {
foreach($posts_missing_flag as $post_id) {
rvy_update_post_meta($post_id, '_rvy_has_revisions', true);
}
}
if ($posts_invalid_flag = array_diff($have_flag_ids, $arr_have_revisions)) {
foreach($posts_missing_flag as $post_id) {
rvy_delete_post_meta($post_id, '_rvy_has_revisions');
}
}
}
function rvy_refresh_options() {
rvy_retrieve_options(true);
rvy_retrieve_options(false);
rvy_refresh_default_options();
rvy_refresh_options_sitewide();
}
function rvy_refresh_options_sitewide() {
if ( ! RVY_NETWORK )
return;
global $rvy_options_sitewide;
$rvy_options_sitewide = apply_filters( 'options_sitewide_rvy', rvy_default_options_sitewide() ); // establishes which options are set site-wide
if ( $options_sitewide_reviewed = rvy_get_option( 'options_sitewide_reviewed', true ) ) {
$custom_options_sitewide = (array) rvy_get_option( 'options_sitewide', true );
$unreviewed_default_sitewide = array_diff( array_keys($rvy_options_sitewide), $options_sitewide_reviewed );
$rvy_options_sitewide = array_fill_keys( array_merge( $custom_options_sitewide, $unreviewed_default_sitewide ), true );
}
$rvy_options_sitewide = array_filter( $rvy_options_sitewide );
}
function rvy_refresh_default_options() {
global $rvy_default_options;
$rvy_default_options = apply_filters( 'default_options_rvy', rvy_default_options() );
if ( RVY_NETWORK )
rvy_apply_custom_default_options();
}
function rvy_apply_custom_default_options() {
global $wpdb, $rvy_default_options, $rvy_options_sitewide;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $results = $wpdb->get_results(
$wpdb->prepare(
"SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key LIKE 'rvy_default_%'",
$wpdb->siteid
)
) ) {
foreach ( $results as $row ) {
$option_basename = str_replace( 'rvy_default_', '', $row->meta_key );
if ( ! empty( $rvy_options_sitewide[$option_basename] ) )
continue; // custom defaults are only for site-specific options
if( isset( $rvy_default_options[$option_basename] ) )
$rvy_default_options[$option_basename] = maybe_unserialize( $row->meta_value );
}
}
}
function rvy_delete_option( $option_basename, $sitewide = -1 ) {
// allow explicit selection of sitewide / non-sitewide scope for better performance and update security
if ( -1 === $sitewide ) {
global $rvy_options_sitewide;
$sitewide = isset( $rvy_options_sitewide ) && ! empty( $rvy_options_sitewide[$option_basename] );
}
if ( $sitewide ) {
global $wpdb;
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->prepare(
"DELETE FROM {$wpdb->sitemeta} WHERE site_id = %s AND meta_key = %s",
$wpdb->siteid,
"rvy_$option_basename"
)
);
} else
delete_option( "rvy_$option_basename" );
}
function rvy_update_option( $option_basename, $option_val, $sitewide = -1 ) {
// allow explicit selection of sitewide / non-sitewide scope for better performance and update security
if ( -1 === $sitewide ) {
global $rvy_options_sitewide;
$sitewide = isset( $rvy_options_sitewide ) && ! empty( $rvy_options_sitewide[$option_basename] );
}
if ($sitewide) {
update_site_option("rvy_$option_basename", $option_val);
} else {
update_option("rvy_$option_basename", $option_val);
}
}
function rvy_retrieve_options( $sitewide = false ) {
global $wpdb;
if ( $sitewide ) {
if ( ! RVY_NETWORK )
return;
global $rvy_site_options;
$rvy_site_options = array();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $results = $wpdb->get_results(
$wpdb->prepare(
"SELECT meta_key, meta_value FROM $wpdb->sitemeta WHERE site_id = %d AND meta_key LIKE 'rvy_%'",
$wpdb->siteid
)
) ) {
foreach ( $results as $row ) {
$rvy_site_options[$row->meta_key] = $row->meta_value;
}
}
$rvy_site_options = apply_filters( 'site_options_rvy', $rvy_site_options );
return $rvy_site_options;
} else {
global $rvy_blog_options;
$rvy_blog_options = array();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
if ( $results = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE 'rvy_%'") ) {
foreach ( $results as $row ) {
$rvy_blog_options[$row->option_name] = $row->option_value;
}
}
$rvy_blog_options = apply_filters( 'options_rvy', $rvy_blog_options );
return $rvy_blog_options;
}
}
function rvy_filter_option($option_basename, $args) {
return apply_filters("pp_revisions_option_{$option_basename}", rvy_get_option($option_basename), $args);
}
function rvy_get_option($option_basename, $sitewide = -1, $get_default = false, $args = []) {
if (('async_scheduled_publish' == $option_basename) && function_exists('relevanssi_query')) {
return false;
}
if (('scheduled_revisions' == $option_basename) && empty($args['bypass_condition_check'])
&& defined('DISABLE_WP_CRON') && DISABLE_WP_CRON && rvy_get_option('scheduled_publish_cron') && !rvy_get_option('wp_cron_usage_detected') && apply_filters('revisionary_wp_cron_disabled', true)
) {
return false;
}
if ( ! $get_default ) {
// allow explicit selection of sitewide / non-sitewide scope for better performance and update security
if ( -1 === $sitewide ) {
global $rvy_options_sitewide;
$sitewide = isset( $rvy_options_sitewide ) && ! empty( $rvy_options_sitewide[$option_basename] );
}
if ( $sitewide ) {