summaryrefslogtreecommitdiff
blob: 1ae33ced956a4bf1d85660b578d2cb3b7e0c3c54 (plain)
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
<?php
/**
 * Module Name: Copy Post
 * Module Description: Copy an existing post's content into a new draft post
 * Jumpstart Description: Copy an existing post's content into a new draft post
 * Sort Order: 15
 * First Introduced: 7.0
 * Requires Connection: No
 * Auto Activate: No
 * Module Tags: Writing
 * Feature: Writing
 * Additional Search Queries: copy, duplicate
 */

/**
 * Copy Post class.
 */
class Jetpack_Copy_Post {
	/**
	 * Jetpack_Copy_Post_By_Param constructor.
	 * Add row actions to post/page/CPT listing screens.
	 * Process any `?copy` param if on a create new post/page/CPT screen.
	 *
	 * @return void
	 */
	public function __construct() {
		if ( 'edit.php' === $GLOBALS['pagenow'] ) {
			add_filter( 'post_row_actions', array( $this, 'add_row_action' ), 10, 2 );
			add_filter( 'page_row_actions', array( $this, 'add_row_action' ), 10, 2 );
			return;
		}

		if ( ! empty( $_GET['jetpack-copy'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
			add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
			add_filter( 'pre_option_default_post_format', '__return_empty_string' );
		}
	}

	/**
	 * Update the new (target) post data with the source post data.
	 *
	 * @param int     $target_post_id Target post ID.
	 * @param WP_Post $post           Target post object (not used).
	 * @param bool    $update         Whether this is an existing post being updated or not.
	 * @return void
	 */
	public function update_post_data( $target_post_id, $post, $update ) {
		// This `$update` check avoids infinite loops of trying to update our updated post.
		if ( $update ) {
			return;
		}

		$source_post = get_post( $_GET['jetpack-copy'] );
		if ( ! $source_post instanceof WP_Post ||
			! $this->user_can_access_post( $source_post->ID ) ||
			! $this->validate_post_type( $source_post ) ) {
			return;
		}

		$update_results = array(
			'update_content'         => $this->update_content( $source_post, $target_post_id ),
			'update_featured_image'  => $this->update_featured_image( $source_post, $target_post_id ),
			'update_post_format'     => $this->update_post_format( $source_post, $target_post_id ),
			'update_likes_sharing'   => $this->update_likes_sharing( $source_post, $target_post_id ),
			'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ),
		);

		// Required to satisfy get_default_post_to_edit(), which has these filters after post creation.
		add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
		add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
		add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );

		// Required to avoid the block editor from adding default blocks according to post format.
		add_filter( 'block_editor_settings', array( $this, 'remove_post_format_template' ) );

		/**
		 * Fires after all updates have been performed, and default content filters have been added.
		 * Allows for any cleanup or post operations, and default content filters can be removed or modified.
		 *
		 * @module copy-post
		 *
		 * @since 7.0.0
		 *
		 * @param WP_Post $source_post Post object that was copied.
		 * @param int     $target_post_id Target post ID.
		 * @param array   $update_results Results of all update operations, allowing action to be taken.
		 */
		do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results );
	}

	/**
	 * Determine if the current user has edit access to the source post.
	 *
	 * @param int $post_id Source post ID (the post being copied).
	 * @return bool True if user has the meta cap of `edit_post` for the given post ID, false otherwise.
	 */
	protected function user_can_access_post( $post_id ) {
		return current_user_can( 'edit_post', $post_id );
	}

	/**
	 * Update the target post's title, content, excerpt, categories, and tags.
	 *
	 * @param WP_Post $source_post Post object to be copied.
	 * @param int     $target_post_id Target post ID.
	 * @return int    0 on failure, or the updated post ID on success.
	 */
	protected function update_content( $source_post, $target_post_id ) {
		$data = array(
			'ID'             => $target_post_id,
			'post_title'     => $source_post->post_title,
			'post_content'   => $source_post->post_content,
			'post_excerpt'   => $source_post->post_excerpt,
			'comment_status' => $source_post->comment_status,
			'ping_status'    => $source_post->ping_status,
			'post_category'  => $source_post->post_category,
			'post_password'  => $source_post->post_password,
			'tags_input'     => $source_post->tags_input,
		);

		/**
		 * Fires just before the target post is updated with its new data.
		 * Allows for final data adjustments before updating the target post.
		 *
		 * @module copy-post
		 *
		 * @since 7.0.0
		 *
		 * @param array $data Post data with which to update the target (new) post.
		 * @param WP_Post $source_post Post object being copied.
		 * @param int     $target_post_id Target post ID.
		 */
		$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
		return wp_update_post( $data );
	}

	/**
	 * Update terms for post types.
	 *
	 * @param WP_Post $source_post Post object to be copied.
	 * @param int     $target_post_id Target post ID.
	 * @return array Results of attempts to set each term to the target (new) post.
	 */
	protected function update_post_type_terms( $source_post, $target_post_id ) {
		$results = array();

		$bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id );
		if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) {
			return $results;
		}

		$taxonomies = get_object_taxonomies( $source_post, 'objects' );
		foreach ( $taxonomies as $taxonomy ) {
			$terms     = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
			$results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name );
		}

		return $results;
	}

	/**
	 * Update the target post's featured image.
	 *
	 * @param WP_Post $source_post Post object to be copied.
	 * @param int     $target_post_id Target post ID.
	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
	 */
	protected function update_featured_image( $source_post, $target_post_id ) {
		$featured_image_id = get_post_thumbnail_id( $source_post );
		return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
	}

	/**
	 * Update the target post's post format.
	 *
	 * @param WP_Post $source_post Post object to be copied.
	 * @param int     $target_post_id Target post ID.
	 * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success.
	 */
	protected function update_post_format( $source_post, $target_post_id ) {
		$post_format = get_post_format( $source_post );
		return set_post_format( $target_post_id, $post_format );
	}

	/**
	 * Ensure the block editor doesn't modify the source post content for non-standard post formats.
	 *
	 * @param array $settings Settings to be passed into the block editor.
	 * @return array Settings with any `template` key removed.
	 */
	public function remove_post_format_template( $settings ) {
		unset( $settings['template'] );
		return $settings;
	}

	/**
	 * Update the target post's Likes and Sharing statuses.
	 *
	 * @param WP_Post $source_post Post object to be copied.
	 * @param int     $target_post_id Target post ID.
	 * @return array Array with the results of each update action.
	 */
	protected function update_likes_sharing( $source_post, $target_post_id ) {
		$likes          = get_post_meta( $source_post->ID, 'switch_like_status', true );
		$sharing        = get_post_meta( $source_post->ID, 'sharing_disabled', false );
		$likes_result   = update_post_meta( $target_post_id, 'switch_like_status', $likes );
		$sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing );
		return array(
			'likes'   => $likes_result,
			'sharing' => $sharing_result,
		);
	}

	/**
	 * Update the target post's title.
	 *
	 * @param string  $post_title Post title determined by `get_default_post_to_edit()`.
	 * @param WP_Post $post       Post object of newly-inserted post.
	 * @return string             Updated post title from source post.
	 */
	public function filter_title( $post_title, $post ) {
		return $post->post_title;
	}

	/**
	 * Update the target post's content (`post_content`).
	 *
	 * @param string  $post_content Post content determined by `get_default_post_to_edit()`.
	 * @param WP_Post $post         Post object of newly-inserted post.
	 * @return string               Updated post content from source post.
	 */
	public function filter_content( $post_content, $post ) {
		return $post->post_content;
	}

	/**
	 * Update the target post's excerpt.
	 *
	 * @param string  $post_excerpt Post excerpt determined by `get_default_post_to_edit()`.
	 * @param WP_Post $post         Post object of newly-inserted post.
	 * @return string               Updated post excerpt from source post.
	 */
	public function filter_excerpt( $post_excerpt, $post ) {
		return $post->post_excerpt;
	}

	/**
	 * Validate the post type to be used for the target post.
	 *
	 * @param WP_Post $post Post object of current post in listing.
	 * @return bool True if the post type is in a list of supported psot types; false otherwise.
	 */
	protected function validate_post_type( $post ) {
		/**
		 * Fires when determining if the "Copy" row action should be made available.
		 * Allows overriding supported post types.
		 *
		 * @module copy-post
		 *
		 * @since 7.0.0
		 *
		 * @param array   Post types supported by default.
		 * @param WP_Post $post Post object of current post in listing.
		 */
		$valid_post_types = apply_filters(
			'jetpack_copy_post_post_types',
			array(
				'post',
				'page',
				'jetpack-testimonial',
				'jetpack-portfolio',
			),
			$post
		);
		return in_array( $post->post_type, $valid_post_types, true );
	}

	/**
	 * Add a "Copy" row action to supported posts/pages/CPTs on list views.
	 *
	 * @param array   $actions Existing actions.
	 * @param WP_Post $post    Post object of current post in list.
	 * @return array           Array of updated row actions.
	 */
	public function add_row_action( $actions, $post ) {
		if ( ! $this->user_can_access_post( $post->ID ) ||
			! $post instanceof WP_Post ||
			! $this->validate_post_type( $post ) ) {
			return $actions;
		}

		$edit_url    = add_query_arg(
			array(
				'post_type'    => $post->post_type,
				'jetpack-copy' => $post->ID,
			),
			admin_url( 'post-new.php' )
		);
		$edit_action = array(
			'jetpack-copy' => sprintf(
				'<a href="%s" aria-label="%s">%s</a>',
				esc_url( $edit_url ),
				esc_attr__( 'Copy this post.', 'jetpack' ),
				esc_html__( 'Copy', 'jetpack' )
			),
		);

		// Insert the Copy action before the Trash action.
		$edit_offset = array_search( 'trash', array_keys( $actions ), true );
		$updated_actions     = array_merge(
			array_slice( $actions, 0, $edit_offset ),
			$edit_action,
			array_slice( $actions, $edit_offset )
		);

		/**
		 * Fires after the new Copy action has been added to the row actions.
		 * Allows changes to the action presentation, or other final checks.
		 *
		 * @module copy-post
		 *
		 * @since 7.0.0
		 *
		 * @param array   $updated_actions Updated row actions with the Copy Post action.
		 * @param array   $actions Original row actions passed to this filter.
		 * @param WP_Post $post Post object of current post in listing.
		 */
		return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post );
	}
}

/**
 * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook.
 */
function jetpack_copy_post_init() {
	new Jetpack_Copy_Post();
}
add_action( 'admin_init', 'jetpack_copy_post_init' );