diff --git a/src/wp-includes/class-wp-xmlrpc-server.php b/src/wp-includes/class-wp-xmlrpc-server.php index c5a4eba66e858..4a18ffbdd925a 100644 --- a/src/wp-includes/class-wp-xmlrpc-server.php +++ b/src/wp-includes/class-wp-xmlrpc-server.php @@ -1540,6 +1540,22 @@ protected function _insert_post( $user, $content_struct ) { $post_data['edit_date'] = true; } + /** + * Filter XML-RPC data to be added/updated via XML-RPC before any DB insertion + * + * @since 4.8 + * + * @param array $post_data Parsed array of post data. + * @param array $content_struct Post data array. + * @param WP_User $user WP_User object. + * + * @return array|IXR_Error + */ + $post_data = apply_filters( 'xmlrpc_before_insert_post', $post_data, $content_struct, $user ); + if ( $post_data instanceof IXR_Error ) { + return $post_data; + } + if ( ! isset( $post_data['ID'] ) ) { $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID; } diff --git a/tests/phpunit/tests/xmlrpc/wp/editPost.php b/tests/phpunit/tests/xmlrpc/wp/editPost.php index b5f27b85488d8..428cc753b4d99 100644 --- a/tests/phpunit/tests/xmlrpc/wp/editPost.php +++ b/tests/phpunit/tests/xmlrpc/wp/editPost.php @@ -528,4 +528,41 @@ public function test_draft_not_assigned_published_date() { $after = get_post( $post_id ); $this->assertSame( '0000-00-00 00:00:00', $after->post_date_gmt ); } + + /** + * @ticket 39699 + */ + public function test_xmlrpc_before_insert_post() { + $this->make_user_by_role( 'editor' ); + + $post_id = self::factory()->post->create( + array( + 'post_title' => 'Post title', + 'post_content' => 'Post edited', + 'post_status' => 'draft', + 'post_type' => 'post', + ) + ); + + // Add filter + add_filter( 'xmlrpc_before_insert_post', array( $this, 'filter_xmlrpc_before_insert_post' ), 10, 3 ); + + $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, array( 'post_title' => 'Too short' ) ) ); + $this->assertInstanceOf( 'IXR_Error', $result ); + $this->assertEquals( 500, $result->code ); + $this->assertEquals( 'Post title too short.', $result->message ); + + $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, array( 'post_title' => 'Right title' ) ) ); + $fetched_post = get_post( $post_id ); + $this->assertNotInstanceOf( 'IXR_Error', $result ); + $this->assertTrue( $result ); + $this->assertEquals( 'Right title', $fetched_post->post_title ); + } + + public function filter_xmlrpc_before_insert_post( $post_data, $content_struct, $user ) { + if ( strlen( $post_data['post_title'] ) < 10 ) { + return new \IXR_Error( 500, 'Post title too short.' ); + } + return $post_data; + } } diff --git a/tests/phpunit/tests/xmlrpc/wp/newPost.php b/tests/phpunit/tests/xmlrpc/wp/newPost.php index b98b06c01d55b..dfa4151afa6d8 100644 --- a/tests/phpunit/tests/xmlrpc/wp/newPost.php +++ b/tests/phpunit/tests/xmlrpc/wp/newPost.php @@ -445,4 +445,62 @@ public function test_valid_IXR_post_date_gmt() { $this->assertStringMatchesFormat( '%d', $result ); $this->assertSame( $date_string, $fetched_post->post_date_gmt ); } + + /** + * @ticket 39699 + */ + public function test_xmlrpc_before_insert_post() { + $this->make_user_by_role( 'editor' ); + + // Add filter + add_filter( 'xmlrpc_before_insert_post', array( $this, 'filter_xmlrpc_before_insert_post' ), 10, 3 ); + + $result = $this->myxmlrpcserver->wp_newPost( + array( + 1, + 'editor', + 'editor', + array( + 'post_title' => 'Too short', + 'custom_fields' => array( + array( + 'key' => 'custom_field_to_create', + 'value' => '123456789', + ), + ), + ), + ) + ); + $this->assertInstanceOf( 'IXR_Error', $result ); + $this->assertEquals( 500, $result->code ); + $this->assertEquals( 'Post title too short.', $result->message ); + + $result = $this->myxmlrpcserver->wp_newPost( + array( + 1, + 'editor', + 'editor', + array( + 'post_title' => 'Right title', + 'custom_fields' => array( + array( + 'key' => 'custom_field_to_create', + 'value' => '123456789', + ), + ), + ), + ) + ); + $fetched_post = get_post( $result ); + $this->assertStringMatchesFormat( '%d', $result ); + $this->assertEquals( 'Right title', $fetched_post->post_title ); + } + + public function filter_xmlrpc_before_insert_post( $post_data, $content_struct, $user ) { + + if ( strlen( $post_data['post_title'] ) < 10 ) { + return new \IXR_Error( 500, 'Post title too short.' ); + } + return $post_data; + } }