Completed
Push — renovate/gridicons-3.x ( c004c1...f8ccd4 )
by
unknown
284:06 queued 275:32
created

....wpcom-json-api-autosave-post-v1-1-endpoint.php (4 issues)

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
3
new WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint( array(
4
	'description' => 'Create a post autosave.',
5
	'group'       => '__do_not_document',
6
	'stat'        => 'posts:autosave',
7
	'min_version' => '1.1',
8
	'method'      => 'POST',
9
	'path'        => '/sites/%s/posts/%d/autosave',
10
	'path_labels' => array(
11
		'$site'    => '(int|string) Site ID or domain',
12
		'$post_ID' => '(int) The post ID',
13
	),
14
	'request_format' => array(
15
		'content' => '(HTML) The post content.',
16
		'title'   => '(HTML) The post title.',
17
		'excerpt' => '(HTML) The post excerpt.',
18
	),
19
	'response_format' => array(
20
		'ID'          => '(int) autodraft post ID',
21
		'post_ID'     => '(int) post ID',
22
		'preview_URL' => '(string) preview URL for the post',
23
		'modified'    => '(ISO 8601 datetime) modified time',
24
	),
25
26
	'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/1/autosave',
27
28
	'example_request_data' => array(
29
		'headers' => array(
30
			'authorization' => 'Bearer YOUR_API_TOKEN'
31
		),
32
33
		'body' => array(
34
			'title'    => 'Howdy',
35
			'content'    => 'Hello. I am a test post. I was created by the API',
36
		)
37
	)
38
) );
39
40
class WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
41
	function __construct( $args ) {
42
		parent::__construct( $args );
43
	}
44
45
	// /sites/%s/posts/%d/autosave -> $blog_id, $post_id
46
	function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
47
48
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
49
		if ( is_wp_error( $blog_id ) ) {
50
			return $blog_id;
51
		}
52
53
		$args = $this->query_args();
54
55
		$input = $this->input( false );
56
57
		if ( ! is_array( $input ) || ! $input ) {
58
			return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'invalid_input'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
59
		}
60
61
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
62
			// Make sure Custom Post Types, etc. get registered.
63
			$this->load_theme_functions();
64
		}
65
66
		$post = get_post( $post_id );
67
68
		if ( ! $post || is_wp_error( $post ) ) {
69
			return new WP_Error( 'unknown_post', 'Unknown post', 404 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unknown_post'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
70
		}
71
72
		if ( ! current_user_can( 'edit_post', $post->ID ) ) {
73
			return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
74
		}
75
76
		$post_data = array (
77
			'post_ID'      => $post_id,
78
			'post_type'    => $post->post_type,
79
			'post_title'   => $input['title'],
80
			'post_content' => $input['content'],
81
			'post_excerpt' => $input['excerpt'],
82
		);
83
84
		$preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) );
85
86
		if ( ! wp_check_post_lock( $post->ID ) &&
87
			 get_current_user_id() == $post->post_author &&
88
			 ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status )
89
		) {
90
			// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
91
			$auto_ID = edit_post( wp_slash( $post_data ) );
92
		} else {
93
			// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
94
			$auto_ID = wp_create_post_autosave( wp_slash( $post_data ) );
95
			$nonce = wp_create_nonce( 'post_preview_' . $post->ID );
96
			$preview_url = add_query_arg( array( 'preview_id' => $auto_ID, 'preview_nonce' => $nonce ), $preview_url );
97
		}
98
99
		$updated_post = get_post( $auto_ID );
100
101
		if ( $updated_post && $updated_post->ID && $updated_post->post_modified ) {
102
			return array(
103
				'ID'          => $auto_ID,
104
				'post_ID'     => $post->ID,
105
				'modified'    => $this->format_date( $updated_post->post_modified ),
106
				'preview_URL' => $preview_url
107
			);
108
		} else {
109
			return new WP_Error( 'autosave_error', __( 'Autosave encountered an unexpected error', 'jetpack' ), 500 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'autosave_error'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
110
		}
111
	}
112
}
113