Completed
Push — try/wp-client-i18n ( 5ca50e...f7e161 )
by Jeremy
26:36 queued 16:52
created

Jetpack_Copy_Post::update_featured_image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
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'] ) && 'post-new.php' === $GLOBALS['pagenow'] ) {
34
			add_action( 'wp_insert_post', array( $this, 'update_post_data' ), 10, 3 );
35
		}
36
	}
37
38
	/**
39
	 * Update the new (target) post data with the source post data.
40
	 *
41
	 * @param int     $target_post_id Target post ID.
42
	 * @param WP_Post $post           Target post object (not used).
43
	 * @param bool    $update         Whether this is an existing post being updated or not.
44
	 * @return void
45
	 */
46
	public function update_post_data( $target_post_id, $post, $update ) {
47
		// This `$update` check avoids infinite loops of trying to update our updated post.
48
		if ( $update ) {
49
			return;
50
		}
51
52
		$source_post = get_post( $_GET['jetpack-copy'] );
53
		if ( ! $source_post instanceof WP_Post ||
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
54
			! $this->user_can_access_post( $source_post->ID ) ||
55
			! $this->validate_post_type( $source_post ) ) {
56
			return;
57
		}
58
59
		$update_results = array(
60
			'update_content'         => $this->update_content( $source_post, $target_post_id ),
61
			'update_featured_image'  => $this->update_featured_image( $source_post, $target_post_id ),
62
			'update_post_format'     => $this->update_post_format( $source_post, $target_post_id ),
63
			'update_likes_sharing'   => $this->update_likes_sharing( $source_post, $target_post_id ),
64
			'update_post_type_terms' => $this->update_post_type_terms( $source_post, $target_post_id ),
65
		);
66
67
		// Required to satify get_default_post_to_edit(), which has these filters after post creation.
68
		add_filter( 'default_title', array( $this, 'filter_title' ), 10, 2 );
69
		add_filter( 'default_content', array( $this, 'filter_content' ), 10, 2 );
70
		add_filter( 'default_excerpt', array( $this, 'filter_excerpt' ), 10, 2 );
71
72
		/**
73
		 * Fires after all updates have been performed, and default content filters have been added.
74
		 * Allows for any cleanup or post operations, and default content filters can be removed or modified.
75
		 *
76
		 * @module copy-post
77
		 *
78
		 * @since 7.0.0
79
		 *
80
		 * @param WP_Post $source_post Post object that was copied.
81
		 * @param int     $target_post_id Target post ID.
82
		 * @param array   $update_results Results of all update operations, allowing action to be taken.
83
		 */
84
		do_action( 'jetpack_copy_post', $source_post, $target_post_id, $update_results );
85
	}
86
87
	/**
88
	 * Determine if the current user has access to the source post.
89
	 *
90
	 * @param int $post_id Source post ID (the post being copied).
91
	 * @return bool True if user has the meta cap of `read_post` for the given post ID, false otherwise.
92
	 */
93
	protected function user_can_access_post( $post_id ) {
94
		return current_user_can( 'read_post', $post_id );
95
	}
96
97
	/**
98
	 * Update the target post's title, content, excerpt, categories, and tags.
99
	 *
100
	 * @param WP_Post $source_post Post object to be copied.
101
	 * @param int     $target_post_id Target post ID.
102
	 * @return int    0 on failure, or the updated post ID on success.
103
	 */
104
	protected function update_content( $source_post, $target_post_id ) {
105
		$data = array(
106
			'ID'             => $target_post_id,
107
			'post_title'     => $source_post->post_title,
108
			'post_content'   => $source_post->post_content,
109
			'post_excerpt'   => $source_post->post_excerpt,
110
			'comment_status' => $source_post->comment_status,
111
			'ping_status'    => $source_post->ping_status,
112
			'post_category'  => $source_post->post_category,
113
			'tags_input'     => $source_post->tags_input,
114
		);
115
116
		/**
117
		 * Fires just before the target post is updated with its new data.
118
		 * Allows for final data adjustments before updating the target post.
119
		 *
120
		 * @module copy-post
121
		 *
122
		 * @since 7.0.0
123
		 *
124
		 * @param array $data Post data with which to update the target (new) post.
125
		 * @param WP_Post $source_post Post object being copied.
126
		 * @param int     $target_post_id Target post ID.
127
		 */
128
		$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
129
		return wp_update_post( $data );
130
	}
131
132
	/**
133
	 * Update terms for post types.
134
	 *
135
	 * @param WP_Post $source_post Post object to be copied.
136
	 * @param int     $target_post_id Target post ID.
137
	 * @return array Results of attempts to set each term to the target (new) post.
138
	 */
139
	protected function update_post_type_terms( $source_post, $target_post_id ) {
140
		$results = array();
141
142
		$bypassed_post_types = apply_filters( 'jetpack_copy_post_bypassed_post_types', array( 'post', 'page' ), $source_post, $target_post_id );
143
		if ( in_array( $source_post->post_type, $bypassed_post_types, true ) ) {
144
			return $results;
145
		}
146
147
		$taxonomies = get_object_taxonomies( $source_post, 'objects' );
148
		foreach ( $taxonomies as $taxonomy ) {
149
			$terms     = wp_get_post_terms( $source_post->ID, $taxonomy->name, array( 'fields' => 'ids' ) );
150
			$results[] = wp_set_post_terms( $target_post_id, $terms, $taxonomy->name );
151
		}
152
153
		return $results;
154
	}
155
156
	/**
157
	 * Update the target post's featured image.
158
	 *
159
	 * @param WP_Post $source_post Post object to be copied.
160
	 * @param int     $target_post_id Target post ID.
161
	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
162
	 */
163
	protected function update_featured_image( $source_post, $target_post_id ) {
164
		$featured_image_id = get_post_thumbnail_id( $source_post );
165
		return update_post_meta( $target_post_id, '_thumbnail_id', $featured_image_id );
166
	}
167
168
	/**
169
	 * Update the target post's post format.
170
	 *
171
	 * @param WP_Post $source_post Post object to be copied.
172
	 * @param int     $target_post_id Target post ID.
173
	 * @return array|WP_Error|false WP_Error on error, array of affected term IDs on success.
174
	 */
175
	protected function update_post_format( $source_post, $target_post_id ) {
176
		$post_format = get_post_format( $source_post );
177
		return set_post_format( $target_post_id, $post_format );
178
	}
179
180
	/**
181
	 * Update the target post's Likes and Sharing statuses.
182
	 *
183
	 * @param WP_Post $source_post Post object to be copied.
184
	 * @param int     $target_post_id Target post ID.
185
	 * @return array Array with the results of each update action.
186
	 */
187
	protected function update_likes_sharing( $source_post, $target_post_id ) {
188
		$likes          = get_post_meta( $source_post->ID, 'switch_like_status', true );
189
		$sharing        = get_post_meta( $source_post->ID, 'sharing_disabled', false );
190
		$likes_result   = update_post_meta( $target_post_id, 'switch_like_status', $likes );
191
		$sharing_result = update_post_meta( $target_post_id, 'sharing_disabled', $sharing );
192
		return array(
193
			'likes'   => $likes_result,
194
			'sharing' => $sharing_result,
195
		);
196
	}
197
198
	/**
199
	 * Update the target post's title.
200
	 *
201
	 * @param string  $post_title Post title determined by `get_default_post_to_edit()`.
202
	 * @param WP_Post $post       Post object of newly-inserted post.
203
	 * @return string             Updated post title from source post.
204
	 */
205
	public function filter_title( $post_title, $post ) {
206
		return $post->post_title;
207
	}
208
209
	/**
210
	 * Update the target post's content (`post_content`).
211
	 *
212
	 * @param string  $post_content Post content determined by `get_default_post_to_edit()`.
213
	 * @param WP_Post $post         Post object of newly-inserted post.
214
	 * @return string               Updated post content from source post.
215
	 */
216
	public function filter_content( $post_content, $post ) {
217
		return $post->post_content;
218
	}
219
220
	/**
221
	 * Update the target post's excerpt.
222
	 *
223
	 * @param string  $post_excerpt Post excerpt determined by `get_default_post_to_edit()`.
224
	 * @param WP_Post $post         Post object of newly-inserted post.
225
	 * @return string               Updated post excerpt from source post.
226
	 */
227
	public function filter_excerpt( $post_excerpt, $post ) {
228
		return $post->post_excerpt;
229
	}
230
231
	/**
232
	 * Validate the post type to be used for the target post.
233
	 *
234
	 * @param WP_Post $post Post object of current post in listing.
235
	 * @return bool True if the post type is in a list of supported psot types; false otherwise.
236
	 */
237
	protected function validate_post_type( $post ) {
238
		/**
239
		 * Fires when determining if the "Copy" row action should be made available.
240
		 * Allows overriding supported post types.
241
		 *
242
		 * @module copy-post
243
		 *
244
		 * @since 7.0.0
245
		 *
246
		 * @param array   Post types supported by default.
247
		 * @param WP_Post $post Post object of current post in listing.
248
		 */
249
		$valid_post_types = apply_filters(
250
			'jetpack_copy_post_post_types',
251
			array(
252
				'post',
253
				'page',
254
				'jetpack-testimonial',
255
				'jetpack-portfolio',
256
			),
257
			$post
258
		);
259
		return in_array( $post->post_type, $valid_post_types, true );
260
	}
261
262
	/**
263
	 * Add a "Copy" row action to supported posts/pages/CPTs on list views.
264
	 *
265
	 * @param array   $actions Existing actions.
266
	 * @param WP_Post $post    Post object of current post in list.
267
	 * @return array           Array of updated row actions.
268
	 */
269
	public function add_row_action( $actions, $post ) {
270
		if ( ! $this->user_can_access_post( $post->ID ) ||
271
			! $post instanceof WP_Post ||
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
272
			! $this->validate_post_type( $post ) ) {
273
			return $actions;
274
		}
275
276
		$edit_url    = add_query_arg(
277
			array(
278
				'post_type'    => $post->post_type,
279
				'jetpack-copy' => $post->ID,
280
			),
281
			admin_url( 'post-new.php' )
282
		);
283
		$edit_action = array(
284
			'jetpack-copy' => sprintf(
285
				'<a href="%s" aria-label="%s">%s</a>',
286
				esc_url( $edit_url ),
287
				esc_attr__( 'Copy this post.', 'jetpack' ),
288
				esc_html__( 'Copy', 'jetpack' )
289
			),
290
		);
291
292
		// Insert the Copy action before the Trash action.
293
		$edit_offset = array_search( 'trash', array_keys( $actions ), true );
294
		$updated_actions     = array_merge(
295
			array_slice( $actions, 0, $edit_offset ),
296
			$edit_action,
297
			array_slice( $actions, $edit_offset )
298
		);
299
300
		/**
301
		 * Fires after the new Copy action has been added to the row actions.
302
		 * Allows changes to the action presentation, or other final checks.
303
		 *
304
		 * @module copy-post
305
		 *
306
		 * @since 7.0.0
307
		 *
308
		 * @param array   $updated_actions Updated row actions with the Copy Post action.
309
		 * @param array   $actions Original row actions passed to this filter.
310
		 * @param WP_Post $post Post object of current post in listing.
311
		 */
312
		return apply_filters( 'jetpack_copy_post_row_actions', $updated_actions, $actions, $post );
313
	}
314
}
315
316
/**
317
 * Instantiate an instance of Jetpack_Copy_Post on the `admin_init` hook.
318
 */
319
function jetpack_copy_post_init() {
320
	new Jetpack_Copy_Post();
321
}
322
add_action( 'admin_init', 'jetpack_copy_post_init' );
323