forked from humanmade/repeatable-posts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hm-post-repeat.php
433 lines (326 loc) · 12.9 KB
/
hm-post-repeat.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
<?php
/*
Plugin Name: Repeatable Posts
Description: Designate a post as repeatable and it'll be copied and re-published on your chosen interval.
Author: Human Made Limited
Author URI: http://hmn.md/
Version: 0.4
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
Text Domain: hm-post-repeat
Domain Path: /languages
*/
/*
Copyright Human Made Limited (email : hello@hmn.md)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace HM\Post_Repeat;
/**
* Setup the actions and filters required by this class.
*/
add_action( 'post_submitbox_misc_actions', __NAMESPACE__ . '\publish_box_ui' );
add_action( 'save_post', __NAMESPACE__ . '\save_post_repeating_status', 10 );
add_action( 'save_post', __NAMESPACE__ . '\create_next_repeat_post', 11 );
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_scripts' );
add_filter( 'display_post_states', __NAMESPACE__ . '\post_states', 10, 2 );
/**
* Enqueue the scripts and styles that are needed by this plugin.
*/
function enqueue_scripts( $hook ) {
// Ensure we only load them on the edit post and add new post admin screens
if ( ! in_array( $hook, array( 'post.php', 'post-new.php' ) ) ) {
return;
}
$plugin_data = get_plugin_data( __FILE__ );
$plugin_dir_url = plugin_dir_url( __FILE__ );
wp_enqueue_script( 'hm-post-repeat', $plugin_dir_url . 'hm-post-repeat.js', 'jquery', $plugin_data['Version'], true );
wp_enqueue_style( 'hm-post-repeat', $plugin_dir_url . 'hm-post-repeat.css', array(), $plugin_data['Version'] );
}
/**
* Output the Post Repeat UI that is shown in the Publish post meta box.
*
* The UI varies depending on whether the post is the original repeating post
* or itself a repeat.
*/
function publish_box_ui() {
if ( ! in_array( get_post_type(), repeating_post_types() ) ) {
return;
} ?>
<div class="misc-pub-section misc-pub-hm-post-repeat">
<span class="dashicons dashicons-controls-repeat"></span>
<?php esc_html_e( 'Repeat:', 'hm-post-repeat' ); ?>
<?php if ( is_repeat_post( get_the_id() ) ) : ?>
<strong><?php printf( esc_html__( 'Repeat of %s', 'hm-post-repeat' ), '<a href="' . esc_url( get_edit_post_link( get_post()->post_parent ) ) . '">' . esc_html( get_the_title( get_post_field( 'post_parent', get_the_id() ) ) ) . '</a>' ); ?></strong>
<?php else : ?>
<?php $repeating_schedule = get_repeating_schedule( get_the_id() ); ?>
<?php $is_repeating_post = is_repeating_post( get_the_id() ) && isset( $repeating_schedule ); ?>
<strong><?php echo ! $is_repeating_post ? esc_html__( 'No', 'hm-post-repeat' ) : esc_html( $repeating_schedule['display'] ); ?></strong>
<a href="#hm-post-repeat" class="edit-hm-post-repeat hide-if-no-js"><span aria-hidden="true"><?php esc_html_e( 'Edit', 'hm-post-repeat' ); ?></span> <span class="screen-reader-text"><?php esc_html_e( 'Edit Repeat Settings', 'hm-post-repeat' ); ?></span></a>
<span class="hide-if-js" id="hm-post-repeat">
<select name="hm-post-repeat">
<option<?php selected( ! $is_repeating_post ); ?> value="no"><?php esc_html_e( 'No', 'hm-post-repeat' ); ?></option>
<?php foreach ( get_repeating_schedules() as $schedule_slug => $schedule ) : ?>
<option<?php selected( $is_repeating_post && $schedule_slug === $repeating_schedule['slug'] ); ?> value="<?php echo esc_attr( $schedule_slug ); ?>"><?php echo esc_html( $schedule['display'] ); ?></option>
<?php endforeach; ?>
</select>
<a href="#hm-post-repeat" class="save-post-hm-post-repeat hide-if-no-js button"><?php esc_html_e( 'OK', 'hm-post-repeat' ); ?></a>
</span>
<?php endif; ?>
</div>
<?php }
/**
* Add some custom post states to cover repeat and repeating posts.
*
* By default post states are displayed on the Edit Post screen in bold after the post title.
*
* @param array $post_states The original array of post states.
* @param WP_Post $post The post object to get / return the states.
* @return array The array of post states with ours added.
*/
function post_states( $post_states, $post ) {
if ( is_repeating_post( $post->ID ) ) {
// If the schedule has been removed since publishing, let the user know.
if ( get_repeating_schedule( $post->ID ) ) {
$post_states['hm-post-repeat'] = __( 'Repeating', 'hm-post-repeat' );
} else {
$post_states['hm-post-repeat'] = __( 'Invalid Repeating Schedule', 'hm-post-repeat' );
}
}
if ( is_repeat_post( $post->ID ) ) {
$post_states['hm-post-repeat'] = __( 'Repeat', 'hm-post-repeat' );
}
return $post_states;
}
/**
* Save the repeating status to post meta.
*
* Hooked into `save_post`. When saving a post that has been set to repeat we save a post meta entry.
*
* @param int $post_id The ID of the post.
* @param string $post_repeat_setting Used to manually set the repeating schedule from tests.
*/
function save_post_repeating_status( $post_id = null, $post_repeat_setting = null ) {
if ( is_null( $post_repeat_setting ) ) {
$post_repeat_setting = isset( $_POST['hm-post-repeat'] ) ? sanitize_text_field( $_POST['hm-post-repeat'] ) : '';
}
if ( ! in_array( get_post_type( $post_id ), repeating_post_types() ) || empty( $post_repeat_setting ) ) {
return;
}
if ( 'no' === $post_repeat_setting ) {
delete_post_meta( $post_id, 'hm-post-repeat' );
}
// Make sure we have a valid schedule.
elseif ( in_array( $post_repeat_setting, array_keys( get_repeating_schedules() ) ) ) {
update_post_meta( $post_id, 'hm-post-repeat', $post_repeat_setting );
}
}
/**
* Create the next repeat post when the last one is published.
*
* When a repeat post (or the original) is published we copy and schedule a new post
* to publish on the correct interval. That way the next repeat post is always ready to go.
* This is hooked into publish_post so that the repeat post is only created when the original
* is published.
*
* @param int $post_id The ID of the post.
*/
function create_next_repeat_post( $post_id ) {
if ( ! in_array( get_post_type( $post_id ), repeating_post_types() ) ) {
return false;
}
if ( 'publish' !== get_post_status( $post_id ) ) {
return false;
}
$original_post_id = get_repeating_post( $post_id );
// Bail if we're not publishing a repeat(ing) post
if ( ! $original_post_id ) {
return false;
}
$original_post = get_post( $original_post_id, ARRAY_A );
// If there is already a repeat post scheduled don't create another one
if ( get_next_scheduled_repeat_post( $original_post['ID'] ) ) {
return false;
}
// Bail if the saved schedule doesn't exist
$repeating_schedule = get_repeating_schedule( $original_post['ID'] );
if ( ! $repeating_schedule ) {
return false;
}
// Bail if the original post isn't already published
if ( 'publish' !== $original_post['post_status'] ) {
return false;
}
$next_post = $original_post;
// Create the repeat post as a copy of the original, but ignore some fields
unset( $next_post['ID'] );
unset( $next_post['guid'] );
unset( $next_post['post_date_gmt'] );
unset( $next_post['post_modified'] );
unset( $next_post['post_modified_gmt'] );
// We set the post_parent to the original post_id, so they're related
$next_post['post_parent'] = $original_post['ID'];
// Set the next post to publish in the future
$next_post['post_status'] = 'future';
// Use the date of the current post being saved as the base
$next_post['post_date'] = date( 'Y-m-d H:i:s', strtotime( get_post_field( 'post_date', $post_id ) . ' + ' . $repeating_schedule['interval'] ) );
// Make sure the next post will be in the future
if ( strtotime( $next_post['post_date'] ) <= time() ) {
return false;
}
// All checks done, get that post scheduled!
$next_post_id = wp_insert_post( wp_slash( $next_post ), true );
if ( is_wp_error( $next_post_id ) ) {
return false;
}
// Mirror any post_meta
$post_meta = get_post_meta( $original_post['ID'] );
if ( $post_meta ) {
// Ignore some internal meta fields
unset( $post_meta['_edit_lock'] );
unset( $post_meta['_edit_last'] );
// Don't copy the post repeat meta as only the original post should have that
unset( $post_meta['hm-post-repeat'] );
foreach ( $post_meta as $key => $values ) {
foreach ( $values as $value ) {
add_post_meta( $next_post_id, $key, maybe_unserialize( $value ) );
}
}
}
// Mirror any term relationships
$taxonomies = get_object_taxonomies( $original_post['post_type'] );
foreach ( $taxonomies as $taxonomy ) {
wp_set_object_terms( $next_post_id, wp_list_pluck( wp_get_object_terms( $original_post['ID'], $taxonomy ), 'slug' ), $taxonomy );
}
return $next_post_id;
}
/**
* The post types the feature is enabled on
*
* By default only posts have the feature enabled but others can be added with the `hm_post_repeat_post_types` filter.
*
* @return array An array of post types
*/
function repeating_post_types() {
/**
* Enable support for additional post types.
*
* @param string[] $post_types Post type slugs.
*/
return apply_filters( 'hm_post_repeat_post_types', array( 'post' ) );
}
/**
* All available repeat schedules.
*
* @return array An array of all available repeat schedules
*/
function get_repeating_schedules() {
/**
* Enable support for additional schedules.
*
* @param array[] $schedules Schedule array items.
*/
$schedules = apply_filters( 'hm_post_repeat_schedules', array(
'daily' => array( 'interval' => '1 day', 'display' => __( 'Daily', 'hm-post-repeat' ) ),
'weekly' => array( 'interval' => '1 week', 'display' => __( 'Weekly', 'hm-post-repeat' ) ),
'monthly' => array( 'interval' => '1 month', 'display' => __( 'Monthly', 'hm-post-repeat' ) ),
) );
foreach ( $schedules as $slug => &$schedule ) {
$schedule['slug'] = $slug;
}
return $schedules;
}
/**
* Get the repeating schedule of the given post_id.
*
* @param int $post_id The id of the post you want to check.
* @return array|null The schedule to repeat by, or null if invalid.
*/
function get_repeating_schedule( $post_id ) {
if ( ! is_repeating_post( $post_id ) ) {
return;
}
$repeating_schedule = get_post_meta( $post_id, 'hm-post-repeat', true );
$schedules = get_repeating_schedules();
// Backwards compatibility with 0.3 when we only supported weekly
if ( '1' === $repeating_schedule ) {
$repeating_schedule = 'weekly';
}
if ( array_key_exists( $repeating_schedule, $schedules ) ) {
return $schedules[ $repeating_schedule ];
}
}
/**
* Check whether a given post_id is a repeating post.
*
* A repeating post is defined as the original post that was set to repeat.
*
* @param int $post_id The id of the post you want to check.
* @return bool Whether the passed post_id is a repeating post or not.
*/
function is_repeating_post( $post_id ) {
// We check $_POST data so that this function works inside a `save_post` hook when the post_meta hasn't yet been saved
if ( isset( $_POST['hm-post-repeat'] ) && isset( $_POST['ID'] ) && $_POST['ID'] === $post_id ) {
return true;
}
if ( get_post_meta( $post_id, 'hm-post-repeat', true ) ) {
return true;
}
return false;
}
/**
* Check whether a given post_id is a repeat post.
*
* A repeat post is defined as any post which is a repeat of the original repeating post.
*
* @param int $post_id The id of the post you want to check.
* @return bool Whether the passed post_id is a repeat post or not.
*/
function is_repeat_post( $post_id ) {
$post_parent = get_post_field( 'post_parent', $post_id );
if ( $post_parent && get_post_meta( $post_parent, 'hm-post-repeat', true ) ) {
return true;
}
return false;
}
/**
* Get the next scheduled repeat post
*
* @param int $post_id The id of a repeat or repeating post
* @return Int|Bool Return the ID of the next repeat post_id or false if it can't find one
*/
function get_next_scheduled_repeat_post( $post_id ) {
$post = get_post( get_repeating_post( $post_id ) );
$repeat_posts = get_posts( array( 'post_status' => 'future', 'post_parent' => $post->ID ) );
if ( isset( $repeat_posts[0] ) ) {
return $repeat_posts[0];
}
return false;
}
/**
* Get the next scheduled repeat post
*
* @param int $post_id The id of a repeat or repeating post
* @return Int|Bool Return the original repeating post_id or false if it can't find it
*/
function get_repeating_post( $post_id ) {
$original_post_id = false;
// Are we publishing a repeat post
if ( is_repeat_post( $post_id ) ) {
$original_post_id = get_post( $post_id )->post_parent;
}
// Or the original
elseif ( is_repeating_post( $post_id ) ) {
$original_post_id = $post_id;
}
return $original_post_id;
}