Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
Ensure and test that trashed posts can have empty content
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Jun 1, 2016
1 parent 21b5965 commit 3f55558
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
8 changes: 3 additions & 5 deletions php/class-wp-customize-post-setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ public function sanitize( $post_data ) {
$post_data = sanitize_post( $post_data, 'db' );
$initial_sanitized_post_data = $post_data;

$maybe_empty = 'trash' !== $post_data['post_status'] && 'attachment' !== $this->post_type
$maybe_empty = 'attachment' !== $this->post_type
&& empty( $post_data['post_content'] ) && empty( $post_data['post_title'] ) && empty( $post_data['post_excerpt'] )
&& post_type_supports( $this->post_type, 'editor' )
&& post_type_supports( $this->post_type, 'title' )
&& post_type_supports( $this->post_type, 'excerpt' );

/** This filter is documented in wp-includes/post.php */
if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $post_data ) ) {
if ( 'trash' !== $post_data['post_status'] && apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $post_data ) ) {
return $has_setting_validation ? new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.', 'customize-posts' ) ) : null;
}

Expand Down Expand Up @@ -514,10 +514,8 @@ protected function update( $data ) {
$r = wp_update_post( wp_slash( $data ), true );
$result = ! is_wp_error( $r );

if ( $result && $is_trashed ) {
$result = wp_trash_post( $this->post_id );
}
if ( $is_trashed ) {
$result = wp_trash_post( $this->post_id );
remove_filter( 'wp_insert_post_empty_content', '__return_false', 100 );
}
return $result;
Expand Down
16 changes: 13 additions & 3 deletions tests/php/test-class-wp-customize-post-setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,29 @@ public function test_get_post_data() {
public function test_sanitize_empty_content() {
$has_setting_validation = method_exists( 'WP_Customize_Setting', 'validate' );
$setting = $this->create_post_setting();
$error = $setting->sanitize( array( 'post_title' => '', 'post_content' => '' ), true );
$error = $setting->sanitize( array( 'post_title' => '', 'post_content' => '' ) );
if ( $has_setting_validation ) {
$this->assertInstanceOf( 'WP_Error', $error );
$this->assertEquals( 'empty_content', $error->get_error_code() );
} else {
$this->assertNull( $error );
}
add_filter( 'wp_insert_post_empty_content', '__return_false' );
$data = $setting->sanitize( array( 'post_title' => '', 'post_content' => '' ), true );
$data = $setting->sanitize( array( 'post_title' => '', 'post_content' => '' ) );
$this->assertInternalType( 'array', $data );
}

/**
* Test validate trashing empty content.
*
* @see WP_Customize_Post_Setting::sanitize()
*/
public function test_sanitize_trashed_empty_content() {
$setting = $this->create_post_setting();
$result = $setting->sanitize( array( 'post_title' => '', 'post_content' => '', 'post_status' => 'trash' ) );
$this->assertInternalType( 'array', $result );
}

/**
* Test sanitize().
*
Expand Down Expand Up @@ -610,5 +621,4 @@ function test_save_trash() {
function handle_action_trashed_post( $post_id ) {
$this->trashed_post_id = $post_id;
}

}

0 comments on commit 3f55558

Please sign in to comment.