| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
/** |
| 4 |
* Get post backup endpoint class. |
| 5 |
* |
| 6 |
* /sites/%s/posts/%d/backup -> $blog_id, $post_id |
| 7 |
*/ |
| 8 |
class Jetpack_JSON_API_Get_Post_Backup_Endpoint extends Jetpack_JSON_API_Endpoint { |
| 9 |
|
| 10 |
/** |
| 11 |
* Needed capabilities. |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
protected $needed_capabilities = array(); // This endpoint is only accessible using a site token |
| 16 |
|
| 17 |
/** |
| 18 |
* The post ID. |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $post_id; |
| 23 |
|
| 24 |
/** |
| 25 |
* Validate the input. |
| 26 |
* |
| 27 |
* @param int $post_id - the post ID. |
| 28 |
*/ |
| 29 |
public function validate_input( $post_id ) { |
| 30 |
if ( empty( $post_id ) || ! is_numeric( $post_id ) ) { |
| 31 |
return new WP_Error( 'post_id_not_specified', __( 'You must specify a Post ID', 'jetpack' ), 400 ); |
| 32 |
} |
| 33 |
|
| 34 |
$this->post_id = (int) $post_id; |
| 35 |
|
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* The result. |
| 41 |
* |
| 42 |
* @return array|WP_Error |
| 43 |
*/ |
| 44 |
protected function result() { |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
// Disable Sync as this is a read-only operation and triggered by sync activity. |
| 48 |
\Automattic\Jetpack\Sync\Actions::mark_sync_read_only(); |
| 49 |
|
| 50 |
$post = get_post( $this->post_id ); |
| 51 |
if ( empty( $post ) ) { |
| 52 |
return new WP_Error( 'post_not_found', __( 'Post not found', 'jetpack' ), 404 ); |
| 53 |
} |
| 54 |
|
| 55 |
// Fetch terms associated with this post object |
| 56 |
$terms = $wpdb->get_results( |
| 57 |
$wpdb->prepare( |
| 58 |
"SELECT term_taxonomy_id, term_order FROM {$wpdb->term_relationships} WHERE object_id = %d;", |
| 59 |
$post->ID |
| 60 |
) |
| 61 |
); |
| 62 |
|
| 63 |
return array( |
| 64 |
'post' => (array) $post, |
| 65 |
'meta' => get_post_meta( $post->ID ), |
| 66 |
'terms' => (array) $terms, |
| 67 |
); |
| 68 |
} |
| 69 |
} |
| 70 |
|