1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Module Name: Copy Post |
4
|
|
|
* Module Description: Copy an existing post's content into a new draft post |
5
|
|
|
* Jumpstart Description: Copy an existing post's content into a new draft post |
6
|
|
|
* Sort Order: 15 |
7
|
|
|
* First Introduced: 7.0 |
8
|
|
|
* Requires Connection: No |
9
|
|
|
* Auto Activate: No |
10
|
|
|
* Module Tags: Writing |
11
|
|
|
* Feature: Writing |
12
|
|
|
* Additional Search Queries: copy, duplicate |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Copy Post class. |
17
|
|
|
*/ |
18
|
|
|
class Jetpack_Copy_Post { |
19
|
|
|
/** |
20
|
|
|
* Jetpack_Copy_Post_By_Param constructor. |
21
|
|
|
* Add row actions to post/page/CPT listing screens. |
22
|
|
|
* Process any `?copy` param if on a create new post/page/CPT screen. |
23
|
|
|
* |
24
|
|
|
* @return void |
|
|
|
|
25
|
|
|
*/ |
26
|
|
|
public function __construct() { |
27
|
|
|
if ( 'edit.php' === $GLOBALS['pagenow'] ) { |
28
|
|
|
add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
29
|
|
|
add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 ); |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { |
34
|
|
|
add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 ); |
35
|
|
|
add_filter( 'pre_option_default_post_format', '__return_empty_string' ); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Update the new (target) post data with the source post data. |
41
|
|
|
* |
42
|
|
|
* @param int $target_post_id Target post ID. |
43
|
|
|
* @param WP_Post $post Target post object (not used). |
44
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function update_post_data( $target_post_id, $post, $update ) { |
48
|
|
|
// This `$update` check avoids infinite loops of trying to update our updated post. |
49
|
|
|
if ( $update ) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$source_post = get_post( $_GET['jetpack-copy'] ); |
54
|
|
|
if ( ! $source_post instanceof WP_Post || |
|
|
|
|
55
|
|
|
! $this->user_can_access_post( $source_post->ID ) || |
56
|
|
|
! $this->validate_post_type( $source_post ) ) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$update_results = array( |
61
|
|
|
'update_content' => $this->update_content( $source_post, $target_post_id ), |
62
|
|
|
'update_featured_image' => $this->update_featured_image( $source_post, $target_post_id ), |
63
|
|
|
'update_post_format' => $this->update_post_format( $source_post, $target_post_id ), |
64
|
|
|
'update_likes_sharing' => $this->update_likes_sharing( $source_post, $target_post_id ), |
65
|
|
|
'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ), |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
// Required to satisfy get_default_post_to_edit(), which has these filters after post creation. |
69
|
|
|
add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 ); |
70
|
|
|
add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 ); |
71
|
|
|
add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 ); |
72
|
|
|
|
73
|
|
|
// Required to avoid the block editor from adding default blocks according to post format. |
74
|
|
|
add_filter( 'block_editor_settings', array( $this, 'remove_post_format_template' ) ); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Fires after all updates have been performed, and default content filters have been added. |
78
|
|
|
* Allows for any cleanup or post operations, and default content filters can be removed or modified. |
79
|
|
|
* |
80
|
|
|
* @module copy-post |
81
|
|
|
* |
82
|
|
|
* @since 7.0.0 |
83
|
|
|
* |
84
|
|
|
* @param WP_Post $source_post Post object that was copied. |
85
|
|
|
* @param int $target_post_id Target post ID. |
86
|
|
|
* @param array $update_results Results of all update operations, allowing action to be taken. |
87
|
|
|
*/ |
88
|
|
|
do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Determine if the current user has edit access to the source post. |
93
|
|
|
* |
94
|
|
|
* @param int $post_id Source post ID (the post being copied). |
95
|
|
|
* @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise. |
96
|
|
|
*/ |
97
|
|
|
protected function user_can_access_post( $post_id ) { |
98
|
|
|
return current_user_can( 'edit_post', $post_id ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Update the target post's title, content, excerpt, categories, and tags. |
103
|
|
|
* |
104
|
|
|
* @param WP_Post $source_post Post object to be copied. |
105
|
|
|
* @param int $target_post_id Target post ID. |
106
|
|
|
* @return int 0 on failure, or the updated post ID on success. |
107
|
|
|
*/ |
108
|
|
|
protected function update_content( $source_post, $target_post_id ) { |
109
|
|
|
$data = array( |
110
|
|
|
'ID' => $target_post_id, |
111
|
|
|
'post_title' => $source_post->post_title, |
112
|
|
|
'post_content' => $source_post->post_content, |
113
|
|
|
'post_excerpt' => $source_post->post_excerpt, |
114
|
|
|
'comment_status' => $source_post->comment_status, |
115
|
|
|
'ping_status' => $source_post->ping_status, |
116
|
|
|
'post_category' => $source_post->post_category, |
117
|
|
|
'post_password' => $source_post->post_password, |
118
|
|
|
'tags_input' => $source_post->tags_input, |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Fires just before the target post is updated with its new data. |
123
|
|
|
* Allows for final data adjustments before updating the target post. |
124
|
|
|
* |
125
|
|
|
* @module copy-post |
126
|
|
|
* |
127
|
|
|
* @since 7.0.0 |
128
|
|
|
* |
129
|
|
|
* @param array $data Post data with which to update the target (new) post. |
130
|
|
|
* @param WP_Post $source_post Post object being copied. |
131
|
|
|
* @param int $target_post_id Target post ID. |
132
|
|
|
*/ |
133
|
|
|
$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id ); |
134
|
|
|
return wp_update_post( $data ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Update terms for post types. |
139
|
|
|
* |
140
|
|
|
* @param WP_Post $source_post Post object to be copied. |
141
|
|
|
* @param int $target_post_id Target post ID. |
142
|
|
|
* @return array Results of attempts to set each term to the target (new) post. |
143
|
|
|
*/ |
144
|
|
|
protected function update_post_type_terms( $source_post, $target_post_id ) { |
145
|
|
|
$results = array(); |
146
|
|
|
|
147
|
|
|
$bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id ); |
148
|
|
|
if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) { |
149
|
|
|
return $results; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$taxonomies = get_object_taxonomies( $source_post, 'objects' ); |
153
|
|
|
foreach ( $taxonomies as $taxonomy ) { |
154
|
|
|
$terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) ); |
155
|
|
|
$results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name ); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $results; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Update the target post's featured image. |
163
|
|
|
* |
164
|
|
|
* @param WP_Post $source_post Post object to be copied. |
165
|
|
|
* @param int $target_post_id Target post ID. |
166
|
|
|
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
167
|
|
|
*/ |
168
|
|
|
protected function update_featured_image( $source_post, $target_post_id ) { |
169
|
|
|
$featured_image_id = get_post_thumbnail_id( $source_post ); |
170
|
|
|
return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Update the target post's post format. |
175
|
|
|
* |
176
|
|
|
* @param WP_Post $source_post Post object to be copied. |
177
|
|
|
* @param int $target_post_id Target post ID. |
178
|
|
|
* @return array|WP_Error|false WP_Error on error, array of affected term IDs on success. |
179
|
|
|
*/ |
180
|
|
|
protected function update_post_format( $source_post, $target_post_id ) { |
181
|
|
|
$post_format = get_post_format( $source_post ); |
182
|
|
|
return set_post_format( $target_post_id, $post_format ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Ensure the block editor doesn't modify the source post content for non-standard post formats. |
187
|
|
|
* |
188
|
|
|
* @param array $settings Settings to be passed into the block editor. |
189
|
|
|
* @return array Settings with any `template` key removed. |
190
|
|
|
*/ |
191
|
|
|
public function remove_post_format_template( $settings ) { |
192
|
|
|
unset( $settings['template'] ); |
193
|
|
|
return $settings; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Update the target post's Likes and Sharing statuses. |
198
|
|
|
* |
199
|
|
|
* @param WP_Post $source_post Post object to be copied. |
200
|
|
|
* @param int $target_post_id Target post ID. |
201
|
|
|
* @return array Array with the results of each update action. |
202
|
|
|
*/ |
203
|
|
|
protected function update_likes_sharing( $source_post, $target_post_id ) { |
204
|
|
|
$likes = get_post_meta( $source_post->ID, 'switch_like_status', true ); |
205
|
|
|
$sharing = get_post_meta( $source_post->ID, 'sharing_disabled', false ); |
206
|
|
|
$likes_result = update_post_meta( $target_post_id, 'switch_like_status', $likes ); |
207
|
|
|
$sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing ); |
208
|
|
|
return array( |
209
|
|
|
'likes' => $likes_result, |
210
|
|
|
'sharing' => $sharing_result, |
211
|
|
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Update the target post's title. |
216
|
|
|
* |
217
|
|
|
* @param string $post_title Post title determined by `get_default_post_to_edit()`. |
218
|
|
|
* @param WP_Post $post Post object of newly-inserted post. |
219
|
|
|
* @return string Updated post title from source post. |
220
|
|
|
*/ |
221
|
|
|
public function filter_title( $post_title, $post ) { |
222
|
|
|
return $post->post_title; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Update the target post's content (`post_content`). |
227
|
|
|
* |
228
|
|
|
* @param string $post_content Post content determined by `get_default_post_to_edit()`. |
229
|
|
|
* @param WP_Post $post Post object of newly-inserted post. |
230
|
|
|
* @return string Updated post content from source post. |
231
|
|
|
*/ |
232
|
|
|
public function filter_content( $post_content, $post ) { |
233
|
|
|
return $post->post_content; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Update the target post's excerpt. |
238
|
|
|
* |
239
|
|
|
* @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`. |
240
|
|
|
* @param WP_Post $post Post object of newly-inserted post. |
241
|
|
|
* @return string Updated post excerpt from source post. |
242
|
|
|
*/ |
243
|
|
|
public function filter_excerpt( $post_excerpt, $post ) { |
244
|
|
|
return $post->post_excerpt; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Validate the post type to be used for the target post. |
249
|
|
|
* |
250
|
|
|
* @param WP_Post $post Post object of current post in listing. |
251
|
|
|
* @return bool True if the post type is in a list of supported psot types; false otherwise. |
252
|
|
|
*/ |
253
|
|
|
protected function validate_post_type( $post ) { |
254
|
|
|
/** |
255
|
|
|
* Fires when determining if the "Copy" row action should be made available. |
256
|
|
|
* Allows overriding supported post types. |
257
|
|
|
* |
258
|
|
|
* @module copy-post |
259
|
|
|
* |
260
|
|
|
* @since 7.0.0 |
261
|
|
|
* |
262
|
|
|
* @param array Post types supported by default. |
263
|
|
|
* @param WP_Post $post Post object of current post in listing. |
264
|
|
|
*/ |
265
|
|
|
$valid_post_types = apply_filters( |
266
|
|
|
'jetpack_copy_post_post_types', |
267
|
|
|
array( |
268
|
|
|
'post', |
269
|
|
|
'page', |
270
|
|
|
'jetpack-testimonial', |
271
|
|
|
'jetpack-portfolio', |
272
|
|
|
), |
273
|
|
|
$post |
274
|
|
|
); |
275
|
|
|
return in_array( $post->post_type, $valid_post_types, true ); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Add a "Copy" row action to supported posts/pages/CPTs on list views. |
280
|
|
|
* |
281
|
|
|
* @param array $actions Existing actions. |
282
|
|
|
* @param WP_Post $post Post object of current post in list. |
283
|
|
|
* @return array Array of updated row actions. |
284
|
|
|
*/ |
285
|
|
|
public function add_row_action( $actions, $post ) { |
286
|
|
|
if ( ! $this->user_can_access_post( $post->ID ) || |
287
|
|
|
! $post instanceof WP_Post || |
|
|
|
|
288
|
|
|
! $this->validate_post_type( $post ) ) { |
289
|
|
|
return $actions; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
$edit_url = add_query_arg( |
293
|
|
|
array( |
294
|
|
|
'post_type' => $post->post_type, |
295
|
|
|
'jetpack-copy' => $post->ID, |
296
|
|
|
), |
297
|
|
|
admin_url( 'post-new.php' ) |
298
|
|
|
); |
299
|
|
|
$edit_action = array( |
300
|
|
|
'jetpack-copy' => sprintf( |
301
|
|
|
'<a href="%s" aria-label="%s">%s</a>', |
302
|
|
|
esc_url( $edit_url ), |
303
|
|
|
esc_attr__( 'Copy this post.', 'jetpack' ), |
304
|
|
|
esc_html__( 'Copy', 'jetpack' ) |
305
|
|
|
), |
306
|
|
|
); |
307
|
|
|
|
308
|
|
|
// Insert the Copy action before the Trash action. |
309
|
|
|
$edit_offset = array_search( 'trash', array_keys( $actions ), true ); |
310
|
|
|
$updated_actions = array_merge( |
311
|
|
|
array_slice( $actions, 0, $edit_offset ), |
312
|
|
|
$edit_action, |
313
|
|
|
array_slice( $actions, $edit_offset ) |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Fires after the new Copy action has been added to the row actions. |
318
|
|
|
* Allows changes to the action presentation, or other final checks. |
319
|
|
|
* |
320
|
|
|
* @module copy-post |
321
|
|
|
* |
322
|
|
|
* @since 7.0.0 |
323
|
|
|
* |
324
|
|
|
* @param array $updated_actions Updated row actions with the Copy Post action. |
325
|
|
|
* @param array $actions Original row actions passed to this filter. |
326
|
|
|
* @param WP_Post $post Post object of current post in listing. |
327
|
|
|
*/ |
328
|
|
|
return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post ); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook. |
334
|
|
|
*/ |
335
|
|
|
function jetpack_copy_post_init() { |
336
|
|
|
new Jetpack_Copy_Post(); |
337
|
|
|
} |
338
|
|
|
add_action( 'admin_init', 'jetpack_copy_post_init' ); |
339
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.