/
wp-post-revision-delete-action.php
144 lines (123 loc) · 3.12 KB
/
wp-post-revision-delete-action.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
<?php
/*
* Plugin Name: Post Revision Delete Action
* Plugin URI: https://github.com/adamsilverstein/wp-post-meta-revisions
* Description: Adds delete links to the revision list on the post editing screen.
* Version: 0.0.1
* Author: Adam Silverstein
* License: GPLv2 or later
*/
class Wp_Post_Revision_Delete_Action {
/**
* Plugin setup.
*/
public function __construct() {
/**
* Only applies in the admin context.
*/
if ( ! is_admin() ) {
return;
}
/**
* Hook into the post editing screen revisions list meta
* box and add a link to delete individual revisions.
*/
add_filter( 'wp_post_revision_actions', array( $this, 'wp_post_revision_delete_action' ), 10, 3 );
/**
* Provide an ajax endpoint for the delete action.
*/
add_action( 'wp_ajax_delete_revision', array( $this, 'revision_delete_action' ) );
/**
* Add our confirmation messages to the post edit screen.
*/
add_filter( 'post_updated_messages', array( $this, 'add_revision_delete_confirmation_messages' ) );
}
/**
* Add our confirmation messages
*/
public function add_revision_delete_confirmation_messages( $messages ) {
$messages['post'][99] = sprintf(
__('Revision id #%d deleted'),
isset( $_GET['revision'] ) ? (int) $_GET['revision'] : 0
);
return $messages;
}
/**
* Add a link to delete a revision, via the `wp_post_revision_actions` filter.
*/
public function wp_post_revision_delete_action( $actions, $revision, $post ) {
$actions .= ' | ' .
sprintf(
'<a href="%s">%s</a>',
wp_nonce_url(
add_query_arg(
array(
'revision' => $revision->ID,
'diff' => false,
'action' => 'delete_revision',
),
admin_url( 'admin-ajax.php' )
),
"delete-revision_$post->ID|$revision->ID"
),
__( 'Delete' )
);
return $actions;
}
/**
* Ajax callback to handle deleting the revision, then redirecting
* back to the post edit page with a confirmation message.
*/
public function revision_delete_action() {
/**
* Bail if required values unset.
*/
if ( ! isset( $_GET['revision'] ) ) {
return;
}
$revision_id = sanitize_key( $_GET['revision'] );
/**
* Verify revision ID valud.
*/
if ( ! $revision = wp_get_post_revision( $revision_id ) ) {
break;
}
/**
* Verify parent post valid.
*/
if ( ! $post = get_post( $revision->post_parent ) ) {
break;
}
/**
* Verify current user can edit parent post.
*/
if ( ! current_user_can( 'edit_post', $post ) ) {
break;
}
/**
* Verify revisions not disabled and we're not looking at an autosave.
*/
if ( ! constant('WP_POST_REVISIONS') && ! wp_is_post_autosave( $revision ) ) {
break;
}
/**
* Check the nonce.
*/
check_admin_referer( "delete-revision_$post->ID|$revision->ID" );
/**
* Every checks out, delete the revision.
*/
wp_delete_post_revision( $revision->ID );
wp_redirect (
add_query_arg(
array(
'message' => 99,
'revision' => $revision->ID,
),
get_edit_post_link( $post->ID, 'url' )
)
);
exit();
}
}
$wp_post_revision_actions = new Wp_Post_Revision_Delete_Action();