Code Duplication    Length = 55-55 lines in 2 locations

json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php 1 location

@@ 283-337 (lines=55) @@
280
					return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
281
				}
282
			}
283
		} else {
284
			$input = $this->input( false );
285
286
			if ( !is_array( $input ) || !$input ) {
287
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
288
			}
289
290
			if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) {
291
				return new WP_Error( 'unauthorized', 'User cannot delete post', 403 );
292
			}
293
294
			// 'future' is an alias for 'publish' for now
295
			if ( isset( $input['status'] ) && 'future' === $input['status'] ) {
296
				$input['status'] = 'publish';
297
			}
298
299
			$post = get_post( $post_id );
300
			$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
301
			$post_type = get_post_type_object( $_post_type );
302
			if ( !$post || is_wp_error( $post ) ) {
303
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
304
			}
305
306
			if ( !current_user_can( 'edit_post', $post->ID ) ) {
307
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
308
			}
309
310
			if ( ! empty( $input['author'] ) ) {
311
				$author_id = $this->parse_and_set_author( $input['author'], $_post_type );
312
				unset( $input['author'] );
313
				if ( is_wp_error( $author_id ) )
314
					return $author_id;
315
			}
316
317
			if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
318
				$input['status'] = 'pending';
319
			}
320
			$last_status = $post->post_status;
321
			$new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
322
323
			// Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
324
			// Similarly, scheduled posts that are manually published before their scheduled date should have the date reset.
325
			$date_in_past = ( strtotime($post->post_date_gmt) < time() );
326
			$reset_draft_date     = 'publish' === $new_status && 'draft'  === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past;
327
			$reset_scheduled_date = 'publish' === $new_status && 'future' === $last_status && ! isset( $input['date_gmt'] ) && ! $date_in_past;
328
329
			if ( $reset_draft_date || $reset_scheduled_date ) {
330
				$input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
331
			}
332
333
			// Untrash a post so that the proper hooks get called as well as the comments get untrashed.
334
			if ( $this->should_untrash_post( $last_status, $new_status, $post ) ) {
335
				$input = $this->untrash_post( $post, $input );
336
			}
337
		}
338
339
		if ( function_exists( 'wpcom_switch_to_blog_locale' ) ) {
340
			// fixes calypso-pre-oss #12476: respect blog locale when creating the post slug

json-endpoints/class.wpcom-json-api-update-post-v1-2-endpoint.php 1 location

@@ 227-281 (lines=55) @@
224
					return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 );
225
				}
226
			}
227
		} else {
228
			$input = $this->input( false );
229
230
			if ( !is_array( $input ) || !$input ) {
231
				return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
232
			}
233
234
			if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) {
235
				return new WP_Error( 'unauthorized', 'User cannot delete post', 403 );
236
			}
237
238
			// 'future' is an alias for 'publish' for now
239
			if ( isset( $input['status'] ) && 'future' === $input['status'] ) {
240
				$input['status'] = 'publish';
241
			}
242
243
			$post = get_post( $post_id );
244
			$_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type;
245
			$post_type = get_post_type_object( $_post_type );
246
			if ( !$post || is_wp_error( $post ) ) {
247
				return new WP_Error( 'unknown_post', 'Unknown post', 404 );
248
			}
249
250
			if ( !current_user_can( 'edit_post', $post->ID ) ) {
251
				return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
252
			}
253
254
			if ( ! empty( $input['author'] ) ) {
255
				$author_id = parent::parse_and_set_author( $input['author'], $_post_type );
256
				unset( $input['author'] );
257
				if ( is_wp_error( $author_id ) )
258
					return $author_id;
259
			}
260
261
			if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) {
262
				$input['status'] = 'pending';
263
			}
264
			$last_status = $post->post_status;
265
			$new_status = isset( $input['status'] ) ? $input['status'] : $last_status;
266
267
			// Make sure that drafts get the current date when transitioning to publish if not supplied in the post.
268
			// Similarly, scheduled posts that are manually published before their scheduled date should have the date reset.
269
			$date_in_past = ( strtotime($post->post_date_gmt) < time() );
270
			$reset_draft_date     = 'publish' === $new_status && 'draft'  === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past;
271
			$reset_scheduled_date = 'publish' === $new_status && 'future' === $last_status && ! isset( $input['date_gmt'] ) && ! $date_in_past;
272
273
			if ( $reset_draft_date || $reset_scheduled_date ) {
274
				$input['date_gmt'] = gmdate( 'Y-m-d H:i:s' );
275
			}
276
277
			// Untrash a post so that the proper hooks get called as well as the comments get untrashed.
278
			if ( $this->should_untrash_post( $last_status, $new_status, $post ) ) {
279
				$input = $this->untrash_post( $post, $input );
280
			}
281
		}
282
283
		if ( function_exists( 'wpcom_switch_to_blog_locale' ) ) {
284
			// fixes calypso-pre-oss #12476: respect blog locale when creating the post slug