Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |
0.00% |
0 / 1 |
| callback | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit( 0 ); |
| 10 | } |
| 11 | |
| 12 | new WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint( |
| 13 | array( |
| 14 | 'description' => 'Get the most recent autosave for a post.', |
| 15 | 'group' => '__do_not_document', |
| 16 | 'stat' => 'posts:autosave', |
| 17 | 'min_version' => '1.1', |
| 18 | 'method' => 'GET', |
| 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 | 'response_format' => array( |
| 25 | 'ID' => '(int) autodraft post ID', |
| 26 | 'post_ID' => '(int) post ID', |
| 27 | 'author_ID' => '(int) author ID', |
| 28 | 'title' => '(HTML) The post title.', |
| 29 | 'content' => '(HTML) The post content.', |
| 30 | 'excerpt' => '(HTML) The post excerpt.', |
| 31 | 'preview_URL' => '(string) preview URL for the post', |
| 32 | 'modified' => '(ISO 8601 datetime) modified time', |
| 33 | ), |
| 34 | |
| 35 | 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/posts/1/autosave', |
| 36 | ) |
| 37 | ); |
| 38 | |
| 39 | // phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid |
| 40 | /** |
| 41 | * Class WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint |
| 42 | * |
| 43 | * @phan-constructor-used-for-side-effects |
| 44 | */ |
| 45 | class WPCOM_JSON_API_Get_Autosave_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { |
| 46 | /** |
| 47 | * Get Autosave callback |
| 48 | * /sites/%s/posts/%d/autosave -> $blog_id, $post_id |
| 49 | * |
| 50 | * @param string $path Path. |
| 51 | * @param int $blog_id Blog ID. |
| 52 | * @param int $post_id Post ID. |
| 53 | * |
| 54 | * @return array|int|mixed|WP_Error |
| 55 | */ |
| 56 | public function callback( $path = '', $blog_id = 0, $post_id = 0 ) { |
| 57 | |
| 58 | $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); |
| 59 | if ( is_wp_error( $blog_id ) ) { |
| 60 | return $blog_id; |
| 61 | } |
| 62 | |
| 63 | $post = get_post( $post_id ); |
| 64 | |
| 65 | if ( ! $post || is_wp_error( $post ) ) { |
| 66 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
| 67 | } |
| 68 | |
| 69 | if ( ! current_user_can( 'edit_post', $post->ID ) ) { |
| 70 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
| 71 | } |
| 72 | |
| 73 | $autosave = wp_get_post_autosave( $post->ID ); |
| 74 | |
| 75 | if ( $autosave ) { |
| 76 | $preview_url = add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ); |
| 77 | $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); |
| 78 | $preview_url = add_query_arg( |
| 79 | array( |
| 80 | 'preview_id' => $post->ID, |
| 81 | 'preview_nonce' => $nonce, |
| 82 | ), |
| 83 | $preview_url |
| 84 | ); |
| 85 | |
| 86 | return array( |
| 87 | 'ID' => $autosave->ID, |
| 88 | 'author_ID' => $autosave->post_author, |
| 89 | 'post_ID' => $autosave->post_parent, |
| 90 | 'title' => $autosave->post_title, |
| 91 | 'content' => $autosave->post_content, |
| 92 | 'excerpt' => $autosave->post_excerpt, |
| 93 | 'preview_URL' => $preview_url, |
| 94 | 'modified' => $this->format_date( $autosave->post_modified_gmt, $autosave->post_modified ), |
| 95 | ); |
| 96 | } else { |
| 97 | return new WP_Error( 'not_found', 'No autosaves exist for this post', 404 ); |
| 98 | } |
| 99 | } |
| 100 | } |