PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.8.4
Jetpack – WP Security, Backup, Speed, & Growth v11.8.4
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 in Jetpack – WP Security, Backup, Speed, & Growth 11.8.4, at modules/copy-post.php

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