Make WordPress Core

Ticket #39699: 39699.patch

File 39699.patch, 4.2 KB (added by enrico.sorcinelli, 8 years ago)
  • src/wp-includes/class-wp-xmlrpc-server.php

     
    13781378                        $post_data['edit_date'] = true;
    13791379                }
    13801380
     1381                /**
     1382                 * Filter XML-RPC data to be added/updated via XML-RPC before any DB insertion
     1383                 *
     1384                 * @since 4.8
     1385                 *
     1386                 * @param array   $post_data       Parsed array of post data.
     1387                 * @param array   $content_struct  Post data array.
     1388                 * @param WP_User $user            WP_User object.
     1389                 *
     1390                 * @return array|IXR_Error
     1391                 */
     1392                $post_data = apply_filters( 'xmlrpc_before_insert_post', $post_data, $content_struct, $user );
     1393                if ( $post_data instanceof IXR_Error ) {
     1394                        return $post_data;
     1395                }
     1396
    13811397                if ( ! isset( $post_data['ID'] ) )
    13821398                        $post_data['ID'] = get_default_post_to_edit( $post_data['post_type'], true )->ID;
    13831399                $post_ID = $post_data['ID'];
  • tests/phpunit/tests/xmlrpc/wp/newPost.php

     
    372372                $this->assertStringMatchesFormat( '%d', $result );
    373373                $this->assertEquals( $date_string , $fetched_post->post_date_gmt );
    374374        }
     375       
     376        /**
     377         * @ticket
     378         */
     379        function test_xmlrpc_before_insert_post () {
     380                $this->make_user_by_role( 'editor' );
    375381
     382                // Add filter
     383                add_filter( 'xmlrpc_before_insert_post', array( $this, 'filter_xmlrpc_before_insert_post' ), 10, 3 );
     384
     385                $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' ) ) ) ) );
     386                $this->assertInstanceOf( 'IXR_Error', $result );
     387                $this->assertEquals( 500, $result->code );
     388                $this->assertEquals( 'Post title too short.', $result->message );
     389
     390                $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' ) ) ) ) );
     391                $fetched_post = get_post( $result );
     392                $this->assertStringMatchesFormat( '%d', $result );
     393                $this->assertEquals( 'Right title', $fetched_post->post_title );
     394        }
     395
     396        function filter_xmlrpc_before_insert_post ( $post_data, $content_struct, $user  ) {
     397
     398                if ( strlen( $post_data['post_title'] ) < 10 ) {
     399                        return new \IXR_Error( 500, 'Post title too short.' );
     400                }
     401                return $post_data;
     402        }
     403
    376404}
  • tests/phpunit/tests/xmlrpc/wp/editPost.php

     
    437437                $future_date_string = strftime( '%Y-%m-%d %H:%M:%S', $future_time );
    438438                $this->assertEquals( $future_date_string, $after->post_date );
    439439        }
     440
     441        /**
     442         * @ticket
     443         */
     444        function test_xmlrpc_before_insert_post () {
     445                $this->make_user_by_role( 'editor' );
     446
     447                $post_id = self::factory()->post->create( array(
     448                        'post_title'   => 'Post title',
     449                        'post_content' => 'Post edited',
     450                        'post_status'  => 'draft',
     451                        'post_type'    => 'post'
     452                ) );
     453
     454                // Add filter
     455                add_filter( 'xmlrpc_before_insert_post', array( $this, 'filter_xmlrpc_before_insert_post' ), 10, 3 );
     456
     457                $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, array( 'post_title' => 'Too short' ) ) );
     458                $this->assertInstanceOf( 'IXR_Error', $result );
     459                $this->assertEquals( 500, $result->code );
     460                $this->assertEquals( 'Post title too short.', $result->message );
     461
     462                $result = $this->myxmlrpcserver->wp_editPost( array( 1, 'editor', 'editor', $post_id, array( 'post_title' => 'Right title' ) ) );
     463                $fetched_post = get_post( $post_id );
     464                $this->assertNotInstanceOf( 'IXR_Error', $result );
     465                $this->assertTrue($result);
     466                $this->assertEquals( 'Right title', $fetched_post->post_title );
     467        }
     468
     469        function filter_xmlrpc_before_insert_post ( $post_data, $content_struct, $user  ) {
     470                if ( strlen( $post_data['post_title'] ) < 10 ) {
     471                        return new \IXR_Error( 500, 'Post title too short.' );
     472                }
     473                return $post_data;
     474        }
    440475}