forked from joshuadavidnelson/archived-post-status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archived-post-status.php
386 lines (286 loc) · 8.32 KB
/
archived-post-status.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
<?php
/**
* Plugin Name: Archived Post Status
* Description: Allows posts and pages to be archived so you can unpublish content without having to trash it.
* Version: 0.3.3
* Author: Frankie Jarrett
* Author URI: http://frankiejarrett.com
* License: GPLv2+
* Text Domain: archived-post-status
*/
/**
* Define plugin constants
*/
define( 'ARCHIVED_POST_STATUS_VERSION', '0.3.3' );
define( 'ARCHIVED_POST_STATUS_PLUGIN', plugin_basename( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_DIR', plugin_dir_path( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_URL', plugin_dir_url( __FILE__ ) );
define( 'ARCHIVED_POST_STATUS_LANG_PATH', dirname( ARCHIVED_POST_STATUS_PLUGIN ) . '/languages' );
/**
* Load languages
*
* @action plugins_loaded
*/
function aps_i18n() {
load_plugin_textdomain( 'archived-post-status', false, ARCHIVED_POST_STATUS_LANG_PATH );
}
add_action( 'plugins_loaded', 'aps_i18n' );
/**
* Translations strings placeholder function
*
* Translation strings that are not used elsewhere but Plugin Title and Description
* are helt here to be picked up by Poedit. Keep these in sync with the actual plugin's
* title and description.
*/
function aps_i18n_strings() {
__( 'Archived Post Status', 'archived-post-status' );
__( 'Allows posts and pages to be archived so you can unpublish content without having to trash it.', 'archived-post-status' );
}
/**
* Register a custom post status for Archived
*
* @action init
*/
function aps_register_archive_post_status() {
$args = array(
'label' => __( 'Archived', 'archived-post-status' ),
'public' => (bool) apply_filters( 'aps_status_arg_public', aps_current_user_can_view() ),
'private' => (bool) apply_filters( 'aps_status_arg_private', true ),
'exclude_from_search' => (bool) apply_filters( 'aps_status_arg_exclude_from_search', ! aps_current_user_can_view() ),
'show_in_admin_all_list' => (bool) apply_filters( 'aps_status_arg_show_in_admin_all_list', aps_current_user_can_view() ),
'show_in_admin_status_list' => (bool) apply_filters( 'aps_status_arg_show_in_admin_status_list', aps_current_user_can_view() ),
'label_count' => _n_noop( 'Archived <span class="count">(%s)</span>', 'Archived <span class="count">(%s)</span>', 'archived-post-status' ),
);
register_post_status( 'archive', $args );
}
add_action( 'init', 'aps_register_archive_post_status' );
/**
* Returns TRUE if on the frontend, otherwise FALSE
*
* @filter aps_status_arg_exclude_from_search
*
* @return bool
*/
function aps_is_frontend() {
return ! is_admin();
}
add_filter( 'aps_status_arg_exclude_from_search', 'aps_is_frontend' );
/**
* Returns TRUE if current user can view, otherwise FALSE
*
* @return bool
*/
function aps_current_user_can_view() {
/**
* Default capability to grant ability to view Archived content
*
* @since 0.3.0
*
* @return string
*/
$capability = (string) apply_filters( 'aps_default_read_capability', 'read_private_posts' );
return current_user_can( $capability );
}
/**
* Filter archived post titles on the frontend
*
* @param string $title
* @param int $post_id (optional)
*
* @return string
*/
function aps_the_title( $title, $post_id = null ) {
$post = get_post( $post_id );
if (
! is_admin()
&&
isset( $post->post_status )
&&
'archive' === $post->post_status
) {
$title = sprintf( '%s: %s', __( 'Archived', 'archived-post-status' ), $title );
}
return $title;
}
add_filter( 'the_title', 'aps_the_title', 10, 2 );
/**
* Check if a post type should NOT be using the Archived status
*
* @param string $post_type
*
* @return bool
*/
function aps_is_excluded_post_type( $post_type ) {
/**
* Prevent the Archived status from being used on these post types
*
* @since 0.1.0
*
* @return array
*/
$excluded = (array) apply_filters( 'aps_excluded_post_types', array( 'attachment' ) );
if ( in_array( $post_type, $excluded ) ) {
return true;
}
return false;
}
/**
* Modify the DOM on post screens
*
* @action admin_footer-post.php
*/
function aps_post_screen_js() {
global $post;
if ( aps_is_excluded_post_type( $post->post_type ) ) {
return;
}
if ( 'draft' !== $post->post_status && 'pending' !== $post->post_status ) {
?>
<script>
jQuery( document ).ready( function( $ ) {
$( '#post_status' ).append( '<option value="archive"><?php esc_html_e( 'Archived', 'archived-post-status' ) ?></option>' );
});
</script>
<?php
}
}
add_action( 'admin_footer-post.php', 'aps_post_screen_js' );
/**
* Modify the DOM on edit screens
*
* @action admin_footer-edit.php
*/
function aps_edit_screen_js() {
global $typenow;
if ( aps_is_excluded_post_type( $typenow ) ) {
return;
}
?>
<script>
jQuery( document ).ready( function( $ ) {
$rows = $( '#the-list tr.status-archive' );
$.each( $rows, function() {
disallowEditing( $( this ) );
});
$( 'select[name="_status"]' ).append( '<option value="archive"><?php esc_html_e( 'Archived', 'archived-post-status' ) ?></option>' );
$( '.editinline' ).on( 'click', function() {
var $row = $( this ).closest( 'tr' ),
$option = $( '.inline-edit-row' ).find( 'select[name="_status"] option[value="archive"]' ),
is_archived = $row.hasClass( 'status-archive' );
$option.prop( 'selected', is_archived );
});
$( '.inline-edit-row' ).on( 'remove', function() {
var id = $( this ).prop( 'id' ).replace( 'edit-', '' ),
$row = $( '#post-' + id );
if ( $row.hasClass( 'status-archive' ) ) {
disallowEditing( $row );
}
});
function disallowEditing( $row ) {
var title = $row.find( '.column-title a.row-title' ).text();
$row.find( '.column-title a.row-title' ).remove();
$row.find( '.column-title strong' ).prepend( title );
$row.find( '.row-actions .edit' ).remove();
}
});
</script>
<?php
}
add_action( 'admin_footer-edit.php', 'aps_edit_screen_js' );
/**
* Prevent archived content from being edited
*
* @action load-post.php
*/
function aps_load_post_screen() {
$post_id = (int) filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
$post = get_post( $post_id );
if (
is_null( $post )
||
aps_is_excluded_post_type( $post->post_type )
||
'archive' !== $post->post_status
) {
return;
}
$action = filter_input( INPUT_GET, 'action', FILTER_SANITIZE_STRING );
$message = (int) filter_input( INPUT_GET, 'message', FILTER_SANITIZE_NUMBER_INT );
// Redirect to list table after saving as Archived
if ( 'edit' === $action && 1 === $message ) {
wp_safe_redirect(
add_query_arg(
array(
'post_type' => $post->post_type,
),
admin_url( 'edit.php' )
),
302
);
exit;
}
wp_die(
__( "You can't edit this item because it has been Archived. Please change the post status and try again.", 'archived-post-status' ),
translate( 'WordPress › Error' )
);
}
add_action( 'load-post.php', 'aps_load_post_screen' );
/**
* Display custom post state text next to post titles that are Archived
*
* @filter display_post_states
*
* @param array $post_states An array of post display states
* @param object $post WP_Post
*
* @return array
*/
function aps_display_post_states( $post_states, $post ) {
if (
aps_is_excluded_post_type( $post->post_type )
||
'archive' !== $post->post_status
||
'archive' === get_query_var( 'post_status' )
) {
return $post_states;
}
return array_merge(
$post_states,
array(
'archive' => __( 'Archived', 'archived-post-status' ),
)
);
}
add_filter( 'display_post_states', 'aps_display_post_states', 10, 2 );
/**
* Close comments and pings when content is archived
*
* @action save_post
*
* @param int $post_id Post ID
* @param object $post WP_Post
* @param bool $update Whether this is an existing post being updated or not
*/
function aps_save_post( $post_id, $post, $update ) {
if (
aps_is_excluded_post_type( $post->post_type )
||
wp_is_post_revision( $post )
) {
return;
}
if ( 'archive' === $post->post_status ) {
// Unhook to prevent infinite loop
remove_action( 'save_post', __FUNCTION__ );
$args = array(
'ID' => $post->ID,
'comment_status' => 'closed',
'ping_status' => 'closed',
);
wp_update_post( $args );
// Add hook back again
add_action( 'save_post', __FUNCTION__, 10, 3 );
}
}
add_action( 'save_post', 'aps_save_post', 10, 3 );