Completed
Push — add/copy-a-post ( de5838...bbbb7b )
by Kirk
06:46
created

Jetpack_Copy_Post::update_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * Module Name: Copy Post
4
 * Module Description: Copy an existing post's content into a new post.
5
 * Jumpstart Description: Copy an existing post's content into a new 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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
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'] ) &&
34
			wp_verify_nonce( $_GET['_wpnonce'], 'jetpack-copy-post' ) &&
35
			'post-new.php' === $GLOBALS['pagenow'] ) {
36
			add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
37
		}
38
	}
39
40
	/**
41
	 * Update the new (target) post data with the source post data.
42
	 *
43
	 * @param int     $target_post_id Target post ID.
44
	 * @param WP_Post $post           Target post object (not used).
45
	 * @param bool    $update         Whether this is an existing post being updated or not.
46
	 * @return void
47
	 */
48
	public function update_post_data( $target_post_id, $post, $update ) {
49
		// This `$update` check avoids infinite loops of trying to update our updated post.
50
		if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'jetpack-copy-post' ) || $update ) {
51
			return;
52
		}
53
54
		$source_post = get_post( $_GET['jetpack-copy'] );
55
		if ( ! $source_post || ! $this->user_can_edit_post( $source_post ) ) {
56
			return;
57
		}
58
59
		$update_content        = $this->update_content( $source_post, $target_post_id );
60
		$update_featured_image = $this->update_featured_image( $source_post, $target_post_id );
61
		$update_post_format    = $this->update_post_format( $source_post, $target_post_id );
62
63
		// Required to satify get_default_post_to_edit(), which has these filters after post creation.
64
		add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
65
		add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
66
		add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
67
68
		do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_content, $update_featured_image, $update_post_format );
69
	}
70
71
	/**
72
	 * Determine if the current user has access to the source post.
73
	 *
74
	 * @param WP_Post $post Source post object (the post being copied).
75
	 * @return bool         True if current user is the post author, or has permissions for `edit_others_posts`; false otherwise.
76
	 */
77
	protected function user_can_edit_post( $post ) {
78
		return get_current_user_id() === (int) $post->post_author || current_user_can( 'edit_others_posts' );
79
	}
80
81
	/**
82
	 * Update the target post's title, content, excerpt, categories, and tags.
83
	 *
84
	 * @param WP_Post $source_post Post object to be copied.
85
	 * @param int     $target_post_id Target post ID.
86
	 * @return int    0 on failure, or the updated post ID on success.
87
	 */
88
	protected function update_content( $source_post, $target_post_id ) {
89
		$data = array(
90
			'ID'             => $target_post_id,
91
			'post_title'     => $source_post->post_title,
92
			'post_content'   => $source_post->post_content,
93
			'post_excerpt'   => $source_post->post_excerpt,
94
			'comment_status' => $source_post->comment_status,
95
			'ping_status'    => $source_post->ping_status,
96
			'post_category'  => $source_post->post_category,
97
			'tags_input'     => $source_post->tags_input,
98
		);
99
		$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
100
		return wp_update_post( $data );
101
	}
102
103
	/**
104
	 * Update the target post's featured image.
105
	 *
106
	 * @param WP_Post $source_post Post object to be copied.
107
	 * @param int     $target_post_id Target post ID.
108
	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
109
	 */
110
	protected function update_featured_image( $source_post, $target_post_id ) {
111
		$featured_image_id = get_post_thumbnail_id( $source_post );
112
		return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
113
	}
114
115
	/**
116
	 * Update the target post's post format.
117
	 *
118
	 * @param WP_Post $source_post Post object to be copied.
119
	 * @param int     $target_post_id Target post ID.
120
	 * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success.
121
	 */
122
	protected function update_post_format( $source_post, $target_post_id ) {
123
		$post_format = get_post_format( $source_post );
124
		return set_post_format( $target_post_id, $post_format );
125
	}
126
127
	/**
128
	 * Update the target post's title.
129
	 *
130
	 * @param string  $post_title Post title determined by `get_default_post_to_edit()`.
131
	 * @param WP_Post $post       Post object of newly-inserted post.
132
	 * @return string             Updated post title from source post.
133
	 */
134
	public function filter_title( $post_title, $post ) {
135
		return $post->post_title;
136
	}
137
138
	/**
139
	 * Update the target post's content (`post_content`).
140
	 *
141
	 * @param string  $post_content Post content determined by `get_default_post_to_edit()`.
142
	 * @param WP_Post $post         Post object of newly-inserted post.
143
	 * @return string               Updated post content from source post.
144
	 */
145
	public function filter_content( $post_content, $post ) {
146
		return $post->post_content;
147
	}
148
149
	/**
150
	 * Update the target post's excerpt.
151
	 *
152
	 * @param string  $post_excerpt Post excerpt determined by `get_default_post_to_edit()`.
153
	 * @param WP_Post $post         Post object of newly-inserted post.
154
	 * @return string               Updated post excerpt from source post.
155
	 */
156
	public function filter_excerpt( $post_excerpt, $post ) {
157
		return $post->post_excerpt;
158
	}
159
160
	/**
161
	 * Add a "Copy" row action to posts/pages/CPTs on list views.
162
	 *
163
	 * @param array   $actions Existing actions.
164
	 * @param WP_Post $post    Post object of current post in list.
165
	 * @return array           Array of updated row actions.
166
	 */
167
	public function add_row_action( $actions, $post ) {
168
		$edit_url    = add_query_arg(
169
			array(
170
				'post_type'    => $post->post_type,
171
				'jetpack-copy' => $post->ID,
172
				'_wpnonce'     => wp_create_nonce( 'jetpack-copy-post' ),
173
			),
174
			admin_url( 'post-new.php' )
175
		);
176
		$edit_action = array(
177
			'jetpack-copy' => sprintf(
178
				'<a href="%s" aria-label="%s">%s</a>',
179
				esc_url( $edit_url ),
180
				esc_attr__( 'Copy this post.', 'jetpack' ),
181
				esc_html__( 'Copy', 'jetpack' )
182
			),
183
		);
184
185
		// Insert the Copy action before the Trash action.
186
		$edit_offset = array_search( 'trash', array_keys( $actions ), true );
187
		$actions     = array_merge(
188
			array_slice( $actions, 0, $edit_offset ),
189
			$edit_action,
190
			array_slice( $actions, $edit_offset )
191
		);
192
193
		return $actions;
194
	}
195
}
196
197
/**
198
 * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook.
199
 */
200
function jetpack_copy_post_init() {
201
	new Jetpack_Copy_Post();
202
}
203
add_action( 'admin_init', 'jetpack_copy_post_init' );
204