forked from humanmade/Unpublish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunpublish.php
401 lines (344 loc) · 10.8 KB
/
unpublish.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
<?php
/*
Plugin Name: Unpublish
Version: 1.3
Description: Unpublish your content
Author: Human Made Ltd
Author URI: http://hmn.md/
Plugin URI: http://hmn.md/
Text Domain: unpublish
Domain Path: /languages
*/
class Unpublish {
public static $supports_key = 'unpublish';
public static $deprecated_cron_key = 'unpublish_cron';
public static $cron_key = 'unpublish_post_cron';
public static $post_meta_key = 'unpublish_timestamp';
protected static $instance;
public static function get_instance() {
if ( empty( self::$instance ) ) {
self::$instance = new Unpublish;
// Standard setup methods
foreach ( array( 'setup_variables', 'includes', 'setup_actions' ) as $method ) {
if ( method_exists( self::$instance, $method ) ) {
self::$instance->$method();
}
}
}
return self::$instance;
}
private function __construct() {
/** Prevent the class from being loaded more than once **/
}
/**
* Set up variables associated with the plugin
*/
private function setup_variables() {
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url( $this->file );
$this->cron_frequency = 'twicedaily';
}
/**
* Set up action associated with the plugin
*/
private function setup_actions() {
add_action( 'load-post.php', array( self::$instance, 'action_load_customizations' ) );
add_action( 'load-post-new.php', array( self::$instance, 'action_load_customizations' ) );
add_action( 'added_post_meta', array( self::$instance, 'update_schedule' ), 10, 4 );
add_action( 'updated_post_meta', array( self::$instance, 'update_schedule' ), 10, 4 );
add_action( 'deleted_post_meta', array( self::$instance, 'remove_schedule' ), 10, 3 );
add_action( 'trashed_post', array( self::$instance, 'unschedule_unpublish' ) );
add_action( 'untrashed_post', array( self::$instance, 'reschedule_unpublish' ) );
add_action( self::$cron_key, array( self::$instance, 'unpublish_post' ) );
add_filter( 'is_protected_meta', array( self::$instance, 'protect_meta_key' ), 10, 3 );
if ( wp_next_scheduled( self::$deprecated_cron_key ) ) {
add_action( self::$deprecated_cron_key, array( self::$instance, 'unpublish_content' ) );
}
}
/**
* Load any / all customizations to the admin
*/
public function action_load_customizations() {
$post_type = get_current_screen()->post_type;
if ( post_type_supports( $post_type, self::$supports_key ) ) {
add_action( 'post_submitbox_misc_actions', array( self::$instance, 'render_unpublish_ui' ), 1 );
add_action( 'admin_enqueue_scripts', array( self::$instance, 'enqueue_scripts_styles' ) );
add_action( 'save_post_' . $post_type, array( self::$instance, 'action_save_unpublish_timestamp' ) );
}
}
/**
* Get month names
*
* global WP_Locale $wp_locale
*
* @return array Array of month names.
*/
protected function get_month_names() {
global $wp_locale;
$month_names = [];
for ( $i = 1; $i < 13; $i = $i + 1 ) {
$month_num = zeroise( $i, 2 );
$month_text = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
$month_names[] = array(
'value' => $month_num,
'text' => $month_text,
'label' => sprintf( _x( '%1$s-%2$s', 'month number-name', 'unpublish' ), $month_num, $month_text ),
);
}
return $month_names;
}
/**
* Get post unpublish timestamp
*
* @param int $post_id Post ID.
* @return string Timestamp.
*/
private function get_unpublish_timestamp( $post_id ) {
return get_post_meta( $post_id, self::$post_meta_key, true );
}
/**
* Render the UI for changing the unpublish time of a post
*/
public function render_unpublish_ui() {
$unpublish_timestamp = $this->get_unpublish_timestamp( get_the_ID() );
if ( ! empty( $unpublish_timestamp ) ) {
$local_timestamp = strtotime( get_date_from_gmt( date( 'Y-m-d H:i:s', $unpublish_timestamp ) ) );
/* translators: Unpublish box date format, see https://secure.php.net/date */
$datetime_format = __( 'M j, Y @ H:i', 'unpublish' );
$unpublish_date = date_i18n( $datetime_format, $local_timestamp );
$date_parts = array(
'jj' => date( 'd', $local_timestamp ),
'mm' => date( 'm', $local_timestamp ),
'aa' => date( 'Y', $local_timestamp ),
'hh' => date( 'H', $local_timestamp ),
'mn' => date( 'i', $local_timestamp ),
);
} else {
$unpublish_date = '—';
$date_parts = array(
'jj' => '',
'mm' => '',
'aa' => '',
'hh' => '',
'mn' => '',
);
}
$vars = array(
'unpublish_date' => $unpublish_date,
'month_names' => $this->get_month_names(),
'date_parts' => $date_parts,
'date_units' => array( 'aa', 'mm', 'jj', 'hh', 'mn' ),
);
echo $this->get_view( 'unpublish-ui', $vars ); // xss ok
}
/**
* Enqueue scripts & styles
*/
public function enqueue_scripts_styles() {
wp_enqueue_style( 'unpublish', plugins_url( 'css/unpublish.css', __FILE__ ), array(), '0.1-alpha' );
wp_enqueue_script( 'unpublish', plugins_url( 'js/unpublish.js', __FILE__ ), array( 'jquery' ), '0.1-alpha', true );
wp_localize_script( 'unpublish', 'unpublish', array(
/* translators: 1: month, 2: day, 3: year, 4: hour, 5: minute */
'dateFormat' => __( '%1$s %2$s, %3$s @ %4$s:%5$s', 'unpublish' ),
) );
}
/**
* Add schedule
*
* @param int $meta_id ID of updated metadata entry.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param mixed $meta_value Meta value.
*/
public function update_schedule( $meta_id, $object_id, $meta_key, $meta_value ) {
if ( self::$post_meta_key !== $meta_key ) {
return;
}
if ( $meta_value ) {
$this->schedule_unpublish( $object_id, $meta_value );
} else {
$this->unschedule_unpublish( $object_id );
}
}
/**
* Remove schedule
*
* @param array $meta_ids An array of deleted metadata entry IDs.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
*/
public function remove_schedule( $meta_ids, $object_id, $meta_key ) {
if ( self::$post_meta_key === $meta_key ) {
$this->unschedule_unpublish( $object_id );
}
}
/**
* Save the unpublish time for a given post
*/
public function action_save_unpublish_timestamp( $post_id ) {
if ( ! isset( $_POST['unpublish-nonce'] ) || ! wp_verify_nonce( $_POST['unpublish-nonce'], 'unpublish' ) ) {
return;
}
if ( ! post_type_supports( get_post_type( $post_id ), self::$supports_key ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
$units = array( 'aa', 'mm', 'jj', 'hh', 'mn' );
$units_count = count( $units );
$date_parts = [];
foreach ( $units as $unit ) {
$key = sprintf( 'unpublish-%s', $unit );
$date_parts[ $unit ] = $_POST[ $key ];
}
$date_parts = array_filter( $date_parts );
// The unpublish date has just been cleared.
if ( empty( $date_parts ) ) {
delete_post_meta( $post_id, self::$post_meta_key );
return;
}
// Bail if one of the fields is empty.
if ( count( $date_parts ) !== $units_count ) {
return;
}
$unpublish_date = vsprintf( '%04d-%02d-%02d %02d:%02d:00', $date_parts );
$valid_date = wp_checkdate( $date_parts['mm'], $date_parts['jj'], $date_parts['aa'], $unpublish_date );
if ( ! $valid_date ) {
return;
}
$timestamp = strtotime( get_gmt_from_date( $unpublish_date ) );
update_post_meta( $post_id, self::$post_meta_key, $timestamp );
}
/**
* Unpublish post
*
* Invoked by cron 'unpublish_post_cron' event.
*
* @param int $post_id Post ID.
*/
public function unpublish_post( $post_id ) {
$unpublish_timestamp = (int) $this->get_unpublish_timestamp( $post_id );
if ( $unpublish_timestamp > time() ) {
$this->schedule_unpublish( $post_id, $unpublish_timestamp );
return;
}
wp_trash_post( $post_id );
}
/**
* Unschedule unpublishing post
*
* @param int $post_id Post ID.
*/
public function unschedule_unpublish( $post_id ) {
wp_clear_scheduled_hook( self::$cron_key, array( $post_id ) );
}
/**
* Schedule unpublishing post
*
* @param int $post_id Post ID.
* @param int $timestamp Timestamp.
*/
public function schedule_unpublish( $post_id, $timestamp ) {
$this->unschedule_unpublish( $post_id );
if ( $timestamp > current_time( 'timestamp', true ) ) {
wp_schedule_single_event( $timestamp, self::$cron_key, array( $post_id ) );
}
}
/**
* Reschedule unpublishing post
*
* @param int $post_id Post ID.
*/
public function reschedule_unpublish( $post_id ) {
$timestamp = $this->get_unpublish_timestamp( $post_id );
if ( $timestamp ) {
$this->schedule_unpublish( $post_id, $timestamp );
}
}
/**
* Unpublish any content that needs unpublishing
*/
public function unpublish_content() {
global $_wp_post_type_features;
$post_types = array();
foreach ( $_wp_post_type_features as $post_type => $features ) {
if ( ! empty( $features[ self::$supports_key ] ) ) {
$post_types[] = $post_type;
}
}
$args = array(
'fields' => 'ids',
'post_type' => $post_types,
'post_status' => 'any',
'posts_per_page' => 40,
'meta_query' => array(
array(
'meta_key' => self::$post_meta_key,
'meta_value' => current_time( 'timestamp' ),
'compare' => '<',
'type' => 'NUMERIC',
),
array(
'meta_key' => self::$post_meta_key,
'meta_value' => current_time( 'timestamp' ),
'compare' => 'EXISTS',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
foreach ( $query->posts as $post_id ) {
wp_trash_post( $post_id );
}
} else {
// There are no posts scheduled to unpublish, we can safely remove the old cron.
wp_clear_scheduled_hook( self::$deprecated_cron_key );
}
}
/**
* Protect meta key so it doesn't show up on Custom Fields meta box
*
* @param bool $protected Whether the key is protected. Default false.
* @param string $meta_key Meta key.
* @param string $meta_type Meta type.
*
* @return bool
*/
public function protect_meta_key( $protected, $meta_key, $meta_type ) {
if ( $meta_key === self::$post_meta_key && 'post' === $meta_type ) {
$protected = true;
}
return $protected;
}
/**
* Get a given view (if it exists)
*
* @param string $view The slug of the view
* @return string
*/
public function get_view( $view, $vars = array() ) {
if ( isset( $this->template_dir ) ) {
$template_dir = $this->template_dir;
} else {
$template_dir = $this->plugin_dir . '/inc/templates/';
}
$view_file = $template_dir . $view . '.tpl.php';
if ( ! file_exists( $view_file ) ) {
return '';
}
extract( $vars, EXTR_SKIP );
ob_start();
include $view_file;
return ob_get_clean();
}
}
/**
* Load the plugin
*/
function unpublish() {
return Unpublish::get_instance();
}
add_action( 'plugins_loaded', 'unpublish' );