Completed
Push — fix/normalize-www-in-site-url-... ( e67e76 )
by
unknown
13:13 queued 02:59
created

...s.wpcom-json-api-get-autosave-v1-1-endpoint.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
class WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
3
	function __construct( $args ) {
4
		parent::__construct( $args );
5
	}
6
7
	// /sites/%s/posts/%d/autosave -> $blog_id, $post_id
8
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
9
10
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
0 ignored issues
show
Consider using a different name than the parameter $blog_id. This often makes code more readable.
Loading history...
11
		if ( is_wp_error( $blog_id ) ) {
12
			return $blog_id;
13
		}
14
15
		$post = get_post( $post_id );
16
17
		if ( ! $post || is_wp_error( $post ) ) {
18
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
19
		}
20
21
		if ( ! current_user_can( 'edit_post', $post->ID ) ) {
22
			return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
23
		}
24
25
		$autosave = wp_get_post_autosave( $post->ID );
26
27
		if ( $autosave ) {
28
			$preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) );
29
			$nonce = wp_create_nonce( 'post_preview_' . $post->ID );
30
			$preview_url = add_query_arg( array( 'preview_id' => $auto_ID, 'preview_nonce' => $nonce ), $preview_url );
31
32
			return array(
33
				'ID'          => $autosave->ID,
34
				'author_ID'   => $autosave->post_author,
35
				'post_ID'     => $autosave->post_parent,
36
				'title'       => $autosave->post_title,
37
				'content'     => $autosave->post_content,
38
				'excerpt'     => $autosave->post_excerpt,
39
				'preview_URL' => $preview_url,
40
				'modified'    => $this->format_date( $autosave->post_modified )
41
			);
42
		} else {
43
			return new WP_Error( 'not_found', 'No autosaves exist for this post', 404 );
44
		}
45
	}
46
}
47