PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 14.8
Jetpack – WP Security, Backup, Speed, & Growth v14.8
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / copy-post.php
copy-post.php
384 lines 13.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 /**
3 * Module Name: Copy Post
4 * Module Description: Duplicate any post or page in one click to speed up content creation.
5 * Sort Order: 15
6 * First Introduced: 7.0
7 * Requires Connection: No
8 * Auto Activate: No
9 * Module Tags: Writing
10 * Feature: Writing
11 * Additional Search Queries: copy, duplicate
12 *
13 * @package automattic/jetpack
14 */
15
16 use Automattic\Jetpack\Assets\Logo;
17
18 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
19
20 /**
21 * Copy Post class.
22 */
23 class Jetpack_Copy_Post {
24 /**
25 * Jetpack_Copy_Post_By_Param constructor.
26 * Add row actions to post/page/CPT listing screens.
27 * Process any `?copy` param if on a create new post/page/CPT screen.
28 *
29 * @return void
30 */
31 public function __construct() {
32 if ( 'edit.php' === $GLOBALS['pagenow'] || ( 'admin-ajax.php' === $GLOBALS['pagenow'] && ! empty( $_POST['post_view'] ) && 'list' === $_POST['post_view'] && ! empty( $_POST['action'] ) && 'inline-save' === $_POST['action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- update_post_data() handles access check.
33 add_action( 'admin_head', array( $this, 'print_inline_styles' ) );
34 add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 );
35 add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 );
36 return;
37 }
38
39 if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- update_post_data() handles access check.
40 add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
41 add_filter( 'pre_option_default_post_format', '__return_empty_string' );
42 }
43 }
44
45 /**
46 * Echos inline styles for the Jetpack logo.
47 *
48 * @return void
49 */
50 public function print_inline_styles() {
51 echo '
52 <style id="jetpack-copy-post-styles">
53 #jetpack-logo__icon {
54 height: 14px;
55 width: 14px;
56 vertical-align: text-bottom;
57 }
58 #jetpack-logo__icon path {
59 fill: inherit;
60 }
61 </style>
62 ';
63 }
64
65 /**
66 * Update the new (target) post data with the source post data.
67 *
68 * @param int $target_post_id Target post ID.
69 * @param WP_Post $post Target post object (not used).
70 * @param bool $update Whether this is an existing post being updated or not.
71 * @return void
72 */
73 public function update_post_data( $target_post_id, $post, $update ) {
74 // This `$update` check avoids infinite loops of trying to update our updated post.
75 if ( $update ) {
76 return;
77 }
78
79 // Shouldn't happen, since this filter is only added when the value isn't empty, but check anyway.
80 if ( empty( $_GET['jetpack-copy'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
81 return;
82 }
83
84 $source_post = get_post( intval( $_GET['jetpack-copy'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
85 if ( ! $source_post instanceof WP_Post ||
86 ! $this->user_can_access_post( $source_post->ID ) ||
87 ! $this->validate_post_type( $source_post ) ) {
88 return;
89 }
90
91 $update_results = array(
92 'update_content' => $this->update_content( $source_post, $target_post_id ),
93 'update_featured_image' => $this->update_featured_image( $source_post, $target_post_id ),
94 'update_post_format' => $this->update_post_format( $source_post, $target_post_id ),
95 'update_likes_sharing' => $this->update_likes_sharing( $source_post, $target_post_id ),
96 'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ),
97 );
98
99 // Required to satisfy get_default_post_to_edit(), which has these filters after post creation.
100 add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
101 add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
102 add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
103
104 // Required to avoid the block editor from adding default blocks according to post format.
105 add_filter( 'block_editor_settings_all', array( $this, 'remove_post_format_template' ) );
106
107 /**
108 * Fires after all updates have been performed, and default content filters have been added.
109 * Allows for any cleanup or post operations, and default content filters can be removed or modified.
110 *
111 * @module copy-post
112 *
113 * @since 7.0.0
114 *
115 * @param WP_Post $source_post Post object that was copied.
116 * @param int $target_post_id Target post ID.
117 * @param array $update_results Results of all update operations, allowing action to be taken.
118 */
119 do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results );
120 }
121
122 /**
123 * Determine if the current user has edit access to the source post.
124 *
125 * @param int $post_id Source post ID (the post being copied).
126 * @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise.
127 */
128 protected function user_can_access_post( $post_id ) {
129 return current_user_can( 'edit_post', $post_id );
130 }
131
132 /**
133 * Update the target post's title, content, excerpt, categories, and tags.
134 *
135 * @param WP_Post $source_post Post object to be copied.
136 * @param int $target_post_id Target post ID.
137 * @return int 0 on failure, or the updated post ID on success.
138 */
139 protected function update_content( $source_post, $target_post_id ) {
140 $data = array(
141 'ID' => $target_post_id,
142 'post_title' => $source_post->post_title,
143 'post_content' => $source_post->post_content,
144 'post_excerpt' => $source_post->post_excerpt,
145 'comment_status' => $source_post->comment_status,
146 'ping_status' => $source_post->ping_status,
147 'post_category' => wp_get_post_categories( $source_post->ID ),
148 'post_password' => $source_post->post_password,
149 'tags_input' => $source_post->tags_input,
150 );
151
152 /**
153 * Fires just before the target post is updated with its new data.
154 * Allows for final data adjustments before updating the target post.
155 *
156 * @module copy-post
157 *
158 * @since 7.0.0
159 *
160 * @param array $data Post data with which to update the target (new) post.
161 * @param WP_Post $source_post Post object being copied.
162 * @param int $target_post_id Target post ID.
163 */
164 $data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
165 return wp_update_post( $data );
166 }
167
168 /**
169 * Update terms for post types.
170 *
171 * @param WP_Post $source_post Post object to be copied.
172 * @param int $target_post_id Target post ID.
173 * @return array Results of attempts to set each term to the target (new) post.
174 */
175 protected function update_post_type_terms( $source_post, $target_post_id ) {
176 $results = array();
177
178 $bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id );
179 if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) {
180 return $results;
181 }
182
183 $taxonomies = get_object_taxonomies( $source_post, 'objects' );
184 foreach ( $taxonomies as $taxonomy ) {
185 $terms = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
186 $results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name );
187 }
188
189 return $results;
190 }
191
192 /**
193 * Update the target post's featured image.
194 *
195 * @param WP_Post $source_post Post object to be copied.
196 * @param int $target_post_id Target post ID.
197 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
198 */
199 protected function update_featured_image( $source_post, $target_post_id ) {
200 $featured_image_id = get_post_thumbnail_id( $source_post );
201 return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
202 }
203
204 /**
205 * Update the target post's post format.
206 *
207 * @param WP_Post $source_post Post object to be copied.
208 * @param int $target_post_id Target post ID.
209 * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success.
210 */
211 protected function update_post_format( $source_post, $target_post_id ) {
212 $post_format = get_post_format( $source_post );
213 return set_post_format( $target_post_id, $post_format );
214 }
215
216 /**
217 * Ensure the block editor doesn't modify the source post content for non-standard post formats.
218 *
219 * @param array $settings Settings to be passed into the block editor.
220 * @return array Settings with any `template` key removed.
221 */
222 public function remove_post_format_template( $settings ) {
223 unset( $settings['template'] );
224 return $settings;
225 }
226
227 /**
228 * Update the target post's Likes and Sharing statuses.
229 *
230 * @param WP_Post $source_post Post object to be copied.
231 * @param int $target_post_id Target post ID.
232 * @return array Array with the results of each update action.
233 */
234 protected function update_likes_sharing( $source_post, $target_post_id ) {
235 $likes = get_post_meta( $source_post->ID, 'switch_like_status', true );
236 $sharing = get_post_meta( $source_post->ID, 'sharing_disabled', true );
237
238 if ( '' !== $likes ) {
239 $likes_result = update_post_meta( $target_post_id, 'switch_like_status', $likes );
240 } else {
241 $likes_result = null;
242 }
243
244 if ( '' !== $sharing ) {
245 $sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing );
246 } else {
247 $sharing_result = null;
248 }
249
250 return array(
251 'likes' => $likes_result,
252 'sharing' => $sharing_result,
253 );
254 }
255
256 /**
257 * Update the target post's title.
258 *
259 * @param string $post_title Post title determined by `get_default_post_to_edit()`.
260 * @param WP_Post $post Post object of newly-inserted post.
261 * @return string Updated post title from source post.
262 */
263 public function filter_title( $post_title, $post ) {
264 return $post->post_title;
265 }
266
267 /**
268 * Update the target post's content (`post_content`).
269 *
270 * @param string $post_content Post content determined by `get_default_post_to_edit()`.
271 * @param WP_Post $post Post object of newly-inserted post.
272 * @return string Updated post content from source post.
273 */
274 public function filter_content( $post_content, $post ) {
275 return $post->post_content;
276 }
277
278 /**
279 * Update the target post's excerpt.
280 *
281 * @param string $post_excerpt Post excerpt determined by `get_default_post_to_edit()`.
282 * @param WP_Post $post Post object of newly-inserted post.
283 * @return string Updated post excerpt from source post.
284 */
285 public function filter_excerpt( $post_excerpt, $post ) {
286 return $post->post_excerpt;
287 }
288
289 /**
290 * Validate the post type to be used for the target post.
291 *
292 * @param WP_Post $post Post object of current post in listing.
293 * @return bool True if the post type is in a list of supported psot types; false otherwise.
294 */
295 protected function validate_post_type( $post ) {
296 /**
297 * Fires when determining if the "Copy" row action should be made available.
298 * Allows overriding supported post types.
299 *
300 * @module copy-post
301 *
302 * @since 7.0.0
303 *
304 * @param array Post types supported by default.
305 * @param WP_Post $post Post object of current post in listing.
306 */
307 $valid_post_types = apply_filters(
308 'jetpack_copy_post_post_types',
309 array(
310 'post',
311 'page',
312 'jetpack-testimonial',
313 'jetpack-portfolio',
314 ),
315 $post
316 );
317 return in_array( $post->post_type, $valid_post_types, true );
318 }
319
320 /**
321 * Add a "Copy" row action to supported posts/pages/CPTs on list views.
322 *
323 * @param array $actions Existing actions.
324 * @param WP_Post $post Post object of current post in list.
325 * @return array Array of updated row actions.
326 */
327 public function add_row_action( $actions, $post ) {
328 if ( ! $this->user_can_access_post( $post->ID ) ||
329 ! $post instanceof WP_Post ||
330 ! $this->validate_post_type( $post ) ) {
331 return $actions;
332 }
333
334 $edit_url = add_query_arg(
335 array(
336 'post_type' => $post->post_type,
337 'jetpack-copy' => $post->ID,
338 ),
339 admin_url( 'post-new.php' )
340 );
341
342 $jetpack_logo = new Logo();
343 $edit_action = array(
344 'jetpack-copy' => sprintf(
345 '<a href="%1$s" aria-label="%2$s">%3$s %4$s</a>',
346 esc_url( $edit_url ),
347 esc_attr__( 'Duplicate this post with Jetpack.', 'jetpack' ),
348 esc_html__( 'Duplicate', 'jetpack' ),
349 $jetpack_logo->get_jp_emblem()
350 ),
351 );
352
353 // Insert the Copy action before the Trash action.
354 $edit_offset = array_search( 'trash', array_keys( $actions ), true );
355 $updated_actions = array_merge(
356 array_slice( $actions, 0, $edit_offset ),
357 $edit_action,
358 array_slice( $actions, $edit_offset )
359 );
360
361 /**
362 * Fires after the new Copy action has been added to the row actions.
363 * Allows changes to the action presentation, or other final checks.
364 *
365 * @module copy-post
366 *
367 * @since 7.0.0
368 *
369 * @param array $updated_actions Updated row actions with the Copy Post action.
370 * @param array $actions Original row actions passed to this filter.
371 * @param WP_Post $post Post object of current post in listing.
372 */
373 return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post );
374 }
375 }
376
377 /**
378 * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook.
379 */
380 function jetpack_copy_post_init() {
381 new Jetpack_Copy_Post();
382 }
383 add_action( 'admin_init', 'jetpack_copy_post_init' );
384