PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
← All changes | json-endpoints/class.wpcom-json-api-autosave-post-v1-1-endpoint.php +141 -0 16.2-beta → 16.3-a.5 View file →
@@ -1,0 +1,141 @@
1 +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2 +/**
3 + * WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint
4 + *
5 + * @package automattic/jetpack
6 + */
7 +
8 +if ( ! defined( 'ABSPATH' ) ) {
9 + exit( 0 );
10 +}
11 +
12 +new WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint(
13 + array(
14 + 'description' => 'Create a post autosave.',
15 + 'group' => '__do_not_document',
16 + 'stat' => 'posts:autosave',
17 + 'min_version' => '1.1',
18 + 'method' => 'POST',
19 + 'path' => '/sites/%s/posts/%d/autosave',
20 + 'path_labels' => array(
21 + '$site' => '(int|string) Site ID or domain',
22 + '$post_ID' => '(int) The post ID',
23 + ),
24 + 'request_format' => array(
25 + 'content' => '(HTML) The post content.',
26 + 'title' => '(HTML) The post title.',
27 + 'excerpt' => '(HTML) The post excerpt.',
28 + ),
29 + 'response_format' => array(
30 + 'ID' => '(int) autodraft post ID',
31 + 'post_ID' => '(int) post ID',
32 + 'preview_URL' => '(string) preview URL for the post',
33 + 'modified' => '(ISO 8601 datetime) modified time',
34 + ),
35 +
36 + 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/1/autosave',
37 +
38 + 'example_request_data' => array(
39 + 'headers' => array(
40 + 'authorization' => 'Bearer YOUR_API_TOKEN',
41 + ),
42 +
43 + 'body' => array(
44 + 'title' => 'Howdy',
45 + 'content' => 'Hello. I am a test post. I was created by the API',
46 + ),
47 + ),
48 + )
49 +);
50 +
51 +// phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid
52 +
53 +/**
54 + * Class WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint
55 + *
56 + * @phan-constructor-used-for-side-effects
57 + */
58 +class WPCOM_JSON_API_Autosave_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint {
59 +
60 + /**
61 + * Autosave Post callback.
62 + * /sites/%s/posts/%d/autosave -> $blog_id, $post_id
63 + *
64 + * @param string $path Path.
65 + * @param int $blog_id Blog ID.
66 + * @param int $post_id Post ID.
67 + */
68 + public function callback( $path = '', $blog_id = 0, $post_id = 0 ) {
69 + if ( ! defined( 'DOING_AUTOSAVE' ) ) {
70 + define( 'DOING_AUTOSAVE', true );
71 + }
72 +
73 + $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
74 + if ( is_wp_error( $blog_id ) ) {
75 + return $blog_id;
76 + }
77 +
78 + $input = $this->input( false );
79 +
80 + if ( ! is_array( $input ) || ! $input ) {
81 + return new WP_Error( 'invalid_input', 'Invalid request input', 400 );
82 + }
83 +
84 + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
85 + // Make sure Custom Post Types, etc. get registered.
86 + $this->load_theme_functions();
87 + }
88 +
89 + $post = get_post( $post_id );
90 +
91 + if ( ! $post || is_wp_error( $post ) ) {
92 + return new WP_Error( 'unknown_post', 'Unknown post', 404 );
93 + }
94 +
95 + if ( ! current_user_can( 'edit_post', $post->ID ) ) {
96 + return new WP_Error( 'unauthorized', 'User cannot edit post', 403 );
97 + }
98 +
99 + $post_data = array(
100 + 'post_ID' => $post_id,
101 + 'post_type' => $post->post_type,
102 + 'post_title' => $input['title'],
103 + 'post_content' => $input['content'],
104 + 'post_excerpt' => $input['excerpt'],
105 + );
106 +
107 + $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) );
108 +
109 + if ( ! wp_check_post_lock( $post->ID ) &&
110 + get_current_user_id() === (int) $post->post_author &&
111 + ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status )
112 + ) {
113 + // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked.
114 + $auto_id = edit_post( wp_slash( $post_data ) );
115 + } else {
116 + // Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
117 + $auto_id = wp_create_post_autosave( wp_slash( $post_data ) );
118 + $nonce = wp_create_nonce( 'post_preview_' . $post->ID );
119 + $preview_url = add_query_arg(
120 + array(
121 + 'preview_id' => $post->ID,
122 + 'preview_nonce' => $nonce,
123 + ),
124 + $preview_url
125 + );
126 + }
127 +
128 + $updated_post = get_post( $auto_id );
129 +
130 + if ( $updated_post && $updated_post->ID && $updated_post->post_modified ) {
131 + return array(
132 + 'ID' => $auto_id,
133 + 'post_ID' => $post->ID,
134 + 'modified' => $this->format_date( $updated_post->post_modified_gmt, $updated_post->post_modified ),
135 + 'preview_URL' => $preview_url,
136 + );
137 + } else {
138 + return new WP_Error( 'autosave_error', __( 'Autosave encountered an unexpected error', 'jetpack' ), 500 );
139 + }
140 + }
141 +}