| @@ 641-677 (lines=37) @@ | ||
| 638 | } |
|
| 639 | ||
| 640 | // /sites/%s/posts/%d/delete -> $blog_id, $post_id |
|
| 641 | function delete_post( $path, $blog_id, $post_id ) { |
|
| 642 | $post = get_post( $post_id ); |
|
| 643 | if ( !$post || is_wp_error( $post ) ) { |
|
| 644 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
|
| 645 | } |
|
| 646 | ||
| 647 | if ( ! $this->is_post_type_allowed( $post->post_type ) ) { |
|
| 648 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
|
| 649 | } |
|
| 650 | ||
| 651 | if ( !current_user_can( 'delete_post', $post->ID ) ) { |
|
| 652 | return new WP_Error( 'unauthorized', 'User cannot delete posts', 403 ); |
|
| 653 | } |
|
| 654 | ||
| 655 | $args = $this->query_args(); |
|
| 656 | $return = $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
|
| 657 | if ( !$return || is_wp_error( $return ) ) { |
|
| 658 | return $return; |
|
| 659 | } |
|
| 660 | ||
| 661 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
|
| 662 | do_action( 'wpcom_json_api_objects', 'posts' ); |
|
| 663 | ||
| 664 | // we need to call wp_trash_post so that untrash will work correctly for all post types |
|
| 665 | if ( 'trash' === $post->post_status ) |
|
| 666 | wp_delete_post( $post->ID ); |
|
| 667 | else |
|
| 668 | wp_trash_post( $post->ID ); |
|
| 669 | ||
| 670 | $status = get_post_status( $post->ID ); |
|
| 671 | if ( false === $status ) { |
|
| 672 | $return['status'] = 'deleted'; |
|
| 673 | return $return; |
|
| 674 | } |
|
| 675 | ||
| 676 | return $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
|
| 677 | } |
|
| 678 | ||
| 679 | // /sites/%s/posts/%d/restore -> $blog_id, $post_id |
|
| 680 | function restore_post( $path, $blog_id, $post_id ) { |
|
| @@ 707-743 (lines=37) @@ | ||
| 704 | } |
|
| 705 | ||
| 706 | // /sites/%s/posts/%d/delete -> $blog_id, $post_id |
|
| 707 | function delete_post( $path, $blog_id, $post_id ) { |
|
| 708 | $post = get_post( $post_id ); |
|
| 709 | if ( !$post || is_wp_error( $post ) ) { |
|
| 710 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
|
| 711 | } |
|
| 712 | ||
| 713 | if ( ! $this->is_post_type_allowed( $post->post_type ) ) { |
|
| 714 | return new WP_Error( 'unknown_post_type', 'Unknown post type', 404 ); |
|
| 715 | } |
|
| 716 | ||
| 717 | if ( !current_user_can( 'delete_post', $post->ID ) ) { |
|
| 718 | return new WP_Error( 'unauthorized', 'User cannot delete posts', 403 ); |
|
| 719 | } |
|
| 720 | ||
| 721 | $args = $this->query_args(); |
|
| 722 | $return = $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
|
| 723 | if ( !$return || is_wp_error( $return ) ) { |
|
| 724 | return $return; |
|
| 725 | } |
|
| 726 | ||
| 727 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
|
| 728 | do_action( 'wpcom_json_api_objects', 'posts' ); |
|
| 729 | ||
| 730 | // we need to call wp_trash_post so that untrash will work correctly for all post types |
|
| 731 | if ( 'trash' === $post->post_status ) |
|
| 732 | wp_delete_post( $post->ID ); |
|
| 733 | else |
|
| 734 | wp_trash_post( $post->ID ); |
|
| 735 | ||
| 736 | $status = get_post_status( $post->ID ); |
|
| 737 | if ( false === $status ) { |
|
| 738 | $return['status'] = 'deleted'; |
|
| 739 | return $return; |
|
| 740 | } |
|
| 741 | ||
| 742 | return $this->get_post_by( 'ID', $post->ID, $args['context'] ); |
|
| 743 | } |
|
| 744 | ||
| 745 | // /sites/%s/posts/%d/restore -> $blog_id, $post_id |
|
| 746 | function restore_post( $path, $blog_id, $post_id ) { |
|