| @@ 97-142 (lines=46) @@ | ||
| 94 | return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 ); |
|
| 95 | } |
|
| 96 | } |
|
| 97 | } else { |
|
| 98 | $input = $this->input( false ); |
|
| 99 | ||
| 100 | if ( !is_array( $input ) || !$input ) { |
|
| 101 | return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); |
|
| 102 | } |
|
| 103 | ||
| 104 | if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) { |
|
| 105 | return new WP_Error( 'unauthorized', 'User cannot delete post', 403 ); |
|
| 106 | } |
|
| 107 | ||
| 108 | // 'future' is an alias for 'publish' for now |
|
| 109 | if ( isset( $input['status'] ) && 'future' === $input['status'] ) { |
|
| 110 | $input['status'] = 'publish'; |
|
| 111 | } |
|
| 112 | ||
| 113 | $post = get_post( $post_id ); |
|
| 114 | $_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type; |
|
| 115 | $post_type = get_post_type_object( $_post_type ); |
|
| 116 | if ( !$post || is_wp_error( $post ) ) { |
|
| 117 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
|
| 118 | } |
|
| 119 | ||
| 120 | if ( !current_user_can( 'edit_post', $post->ID ) ) { |
|
| 121 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
|
| 122 | } |
|
| 123 | ||
| 124 | if ( ! empty( $input['author'] ) ) { |
|
| 125 | $author_id = $this->parse_and_set_author( $input['author'], $_post_type ); |
|
| 126 | unset( $input['author'] ); |
|
| 127 | if ( is_wp_error( $author_id ) ) |
|
| 128 | return $author_id; |
|
| 129 | } |
|
| 130 | ||
| 131 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) { |
|
| 132 | $input['status'] = 'pending'; |
|
| 133 | } |
|
| 134 | $last_status = $post->post_status; |
|
| 135 | $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; |
|
| 136 | ||
| 137 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
|
| 138 | $date_in_past = ( strtotime($post->post_date_gmt) < time() ); |
|
| 139 | if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) { |
|
| 140 | $input['date_gmt'] = gmdate( 'Y-m-d H:i:s' ); |
|
| 141 | } |
|
| 142 | } |
|
| 143 | ||
| 144 | if ( function_exists( 'wpcom_switch_to_blog_locale' ) ) { |
|
| 145 | // fixes calypso-pre-oss #12476: respect blog locale when creating the post slug |
|
| @@ 71-116 (lines=46) @@ | ||
| 68 | return new WP_Error( 'unauthorized', 'User cannot edit posts', 403 ); |
|
| 69 | } |
|
| 70 | } |
|
| 71 | } else { |
|
| 72 | $input = $this->input( false ); |
|
| 73 | ||
| 74 | if ( !is_array( $input ) || !$input ) { |
|
| 75 | return new WP_Error( 'invalid_input', 'Invalid request input', 400 ); |
|
| 76 | } |
|
| 77 | ||
| 78 | if ( isset( $input['status'] ) && 'trash' === $input['status'] && ! current_user_can( 'delete_post', $post_id ) ) { |
|
| 79 | return new WP_Error( 'unauthorized', 'User cannot delete post', 403 ); |
|
| 80 | } |
|
| 81 | ||
| 82 | // 'future' is an alias for 'publish' for now |
|
| 83 | if ( isset( $input['status'] ) && 'future' === $input['status'] ) { |
|
| 84 | $input['status'] = 'publish'; |
|
| 85 | } |
|
| 86 | ||
| 87 | $post = get_post( $post_id ); |
|
| 88 | $_post_type = ( ! empty( $input['type'] ) ) ? $input['type'] : $post->post_type; |
|
| 89 | $post_type = get_post_type_object( $_post_type ); |
|
| 90 | if ( !$post || is_wp_error( $post ) ) { |
|
| 91 | return new WP_Error( 'unknown_post', 'Unknown post', 404 ); |
|
| 92 | } |
|
| 93 | ||
| 94 | if ( !current_user_can( 'edit_post', $post->ID ) ) { |
|
| 95 | return new WP_Error( 'unauthorized', 'User cannot edit post', 403 ); |
|
| 96 | } |
|
| 97 | ||
| 98 | if ( ! empty( $input['author'] ) ) { |
|
| 99 | $author_id = parent::parse_and_set_author( $input['author'], $_post_type ); |
|
| 100 | unset( $input['author'] ); |
|
| 101 | if ( is_wp_error( $author_id ) ) |
|
| 102 | return $author_id; |
|
| 103 | } |
|
| 104 | ||
| 105 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && !current_user_can( 'publish_post', $post->ID ) ) { |
|
| 106 | $input['status'] = 'pending'; |
|
| 107 | } |
|
| 108 | $last_status = $post->post_status; |
|
| 109 | $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; |
|
| 110 | ||
| 111 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
|
| 112 | $date_in_past = ( strtotime($post->post_date_gmt) < time() ); |
|
| 113 | if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) { |
|
| 114 | $input['date_gmt'] = gmdate( 'Y-m-d H:i:s' ); |
|
| 115 | } |
|
| 116 | } |
|
| 117 | ||
| 118 | if ( function_exists( 'wpcom_switch_to_blog_locale' ) ) { |
|
| 119 | // fixes calypso-pre-oss #12476: respect blog locale when creating the post slug |
|