Changeset 52184
- Timestamp:
- 11/16/2021 05:07:43 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/nav-menu.php
r52079 r52184 407 407 * Save the properties of a menu item or create a new one. 408 408 * 409 * The menu-item-title, menu-item-description , menu-item-attr-title, and menu-item-contentare expected409 * The menu-item-title, menu-item-description and menu-item-attr-title are expected 410 410 * to be pre-slashed since they are passed directly to APIs that expect slashed data. 411 411 * 412 412 * @since 3.0.0 413 * @since 5.9.0 Added the menu-item-content parameter.414 413 * 415 414 * @param int $menu_id The ID of the menu. Required. If "0", makes the menu item a draft orphan. … … 450 449 'menu-item-target' => '', 451 450 'menu-item-classes' => '', 452 'menu-item-content' => '',453 451 'menu-item-xfn' => '', 454 452 'menu-item-status' => '', … … 575 573 update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] ); 576 574 update_post_meta( $menu_item_db_id, '_menu_item_url', esc_url_raw( $args['menu-item-url'] ) ); 577 update_post_meta( $menu_item_db_id, '_menu_item_content', $args['menu-item-content'] );578 575 579 576 if ( 0 == $menu_id ) { … … 914 911 $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title; 915 912 916 } elseif ( 'block' === $menu_item->type ) {917 $menu_item->type_label = __( 'Block' );918 $menu_item->title = $menu_item->post_title;919 $menu_item->menu_item_content = ! isset( $menu_item->menu_item_content ) ? get_post_meta( $menu_item->ID, '_menu_item_content', true ) : $menu_item->menu_item_content;920 913 } else { 921 914 $menu_item->type_label = __( 'Custom Link' ); -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php
r52079 r52184 340 340 'menu-item-url' => $menu_item_obj->url, 341 341 'menu-item-description' => $menu_item_obj->description, 342 'menu-item-content' => $menu_item_obj->menu_item_content,343 342 'menu-item-attr-title' => $menu_item_obj->attr_title, 344 343 'menu-item-target' => $menu_item_obj->target, … … 361 360 'menu-item-url' => '', 362 361 'menu-item-description' => '', 363 'menu-item-content' => '',364 362 'menu-item-attr-title' => '', 365 363 'menu-item-target' => '', … … 407 405 } elseif ( ! empty( $request['title']['raw'] ) ) { 408 406 $prepared_nav_item['menu-item-title'] = $request['title']['raw']; 409 }410 }411 412 // Nav menu content.413 if ( ! empty( $schema['properties']['content'] ) && isset( $request['content'] ) ) {414 if ( is_string( $request['content'] ) ) {415 $prepared_nav_item['menu-item-content'] = $request['content'];416 } elseif ( isset( $request['content']['raw'] ) ) {417 $prepared_nav_item['menu-item-content'] = $request['content']['raw'];418 407 } 419 408 } … … 461 450 } 462 451 463 // If menu item is type block, then content is required.464 if ( 'block' === $prepared_nav_item['menu-item-type'] && empty( $prepared_nav_item['menu-item-content'] ) ) {465 $error->add( 'rest_content_required', __( 'The content is required when using a block menu item type.' ), array( 'status' => 400 ) );466 }467 468 452 if ( $error->has_errors() ) { 469 453 return $error; … … 565 549 // It is stored as a string, but should be exposed as an integer. 566 550 $data['object_id'] = absint( $menu_item->object_id ); 567 }568 569 if ( rest_is_field_included( 'content', $fields ) ) {570 $data['content'] = array();571 }572 573 if ( rest_is_field_included( 'content.raw', $fields ) ) {574 $data['content']['raw'] = $menu_item->menu_item_content;575 }576 577 if ( rest_is_field_included( 'content.rendered', $fields ) ) {578 /** This filter is documented in wp-includes/post-template.php */579 $data['content']['rendered'] = apply_filters( 'the_content', $menu_item->menu_item_content );580 }581 582 if ( rest_is_field_included( 'content.block_version', $fields ) ) {583 $data['content']['block_version'] = block_version( $menu_item->menu_item_content );584 551 } 585 552 … … 782 749 'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".' ), 783 750 'type' => 'string', 784 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom' , 'block'),751 'enum' => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom' ), 785 752 'context' => array( 'view', 'edit', 'embed' ), 786 753 'default' => 'custom', … … 858 825 'minimum' => 0, 859 826 'default' => 0, 860 );861 862 $schema['properties']['content'] = array(863 'description' => __( 'HTML content to display for this block menu item.' ),864 'context' => array( 'view', 'edit', 'embed' ),865 'type' => array( 'string', 'object' ),866 'properties' => array(867 'raw' => array(868 'description' => __( 'HTML content, as it exists in the database.' ),869 'type' => 'string',870 'context' => array( 'edit' ),871 ),872 'rendered' => array(873 'description' => __( 'HTML content, transformed for display.' ),874 'type' => 'string',875 'context' => array( 'view', 'edit' ),876 'readonly' => true,877 ),878 'block_version' => array(879 'description' => __( 'Version of the block format used in the HTML content.' ),880 'type' => 'integer',881 'context' => array( 'edit' ),882 'readonly' => true,883 ),884 ),885 827 ); 886 828 -
trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php
r52079 r52184 609 609 610 610 /** 611 * Tests that a block menu item can be created.612 *613 * @ticket 40878614 * @covers ::create_item615 */616 public function test_create_item_block() {617 wp_set_current_user( self::$admin_id );618 $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );619 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );620 $params = $this->set_menu_item_data(621 array(622 'type' => 'block',623 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',624 )625 );626 $request->set_body_params( $params );627 $response = rest_get_server()->dispatch( $request );628 629 $this->check_create_menu_item_response( $response );630 }631 632 /**633 * Tests that a block menu item can be created.634 *635 * @ticket 40878636 * @covers ::create_item637 */638 public function test_create_item_invalid_block_content() {639 wp_set_current_user( self::$admin_id );640 $request = new WP_REST_Request( 'POST', '/wp/v2/menu-items' );641 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );642 $params = $this->set_menu_item_data(643 array(644 'type' => 'block',645 )646 );647 $request->set_body_params( $params );648 $response = rest_get_server()->dispatch( $request );649 $this->assertErrorResponse( 'rest_content_required', $response, 400 );650 }651 652 /**653 611 * @ticket 40878 654 612 * @covers ::update_item … … 684 642 * @covers ::update_item 685 643 */ 686 public function test_update_item_preserves_type() {687 wp_set_current_user( self::$admin_id );688 689 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );690 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );691 $params = array(692 'status' => 'draft',693 'type' => 'block',694 'title' => 'TEST',695 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',696 );697 $request->set_body_params( $params );698 $response = rest_get_server()->dispatch( $request );699 $this->check_update_menu_item_response( $response );700 $new_data = $response->get_data();701 $this->assertSame( 'block', $new_data['type'] );702 703 $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/menu-items/%d', $this->menu_item_id ) );704 $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );705 $params = array(706 'status' => 'draft',707 'title' => 'TEST2',708 'content' => '<!-- wp:paragraph --><p>Block content</p><!-- /wp:paragraph -->',709 );710 $request->set_body_params( $params );711 $response = rest_get_server()->dispatch( $request );712 $this->check_update_menu_item_response( $response );713 $new_data = $response->get_data();714 715 // The type shouldn't change just because it was missing from request args.716 $this->assertSame( 'block', $new_data['type'] );717 }718 719 /**720 * @ticket 40878721 * @covers ::update_item722 */723 644 public function test_update_item_clean_xfn() { 724 645 wp_set_current_user( self::$admin_id ); … … 826 747 $data = $response->get_data(); 827 748 $properties = $data['schema']['properties']; 828 $this->assertSame( 1 9, count( $properties ) );749 $this->assertSame( 18, count( $properties ) ); 829 750 $this->assertArrayHasKey( 'type_label', $properties ); 830 751 $this->assertArrayHasKey( 'attr_title', $properties ); 831 752 $this->assertArrayHasKey( 'classes', $properties ); 832 753 $this->assertArrayHasKey( 'description', $properties ); 833 $this->assertArrayHasKey( 'content', $properties );834 754 $this->assertArrayHasKey( 'id', $properties ); 835 755 $this->assertArrayHasKey( 'url', $properties ); … … 958 878 } 959 879 960 // Check content.961 if ( 'block' === $data['type'] ) {962 $menu_item_content = get_post_meta( $post->ID, '_menu_item_content', true );963 $this->assertSame( apply_filters( 'the_content', $menu_item_content ), $data['content']['rendered'] );964 if ( 'edit' === $context ) {965 $this->assertSame( $menu_item_content, $data['content']['raw'] );966 } else {967 $this->assertFalse( isset( $data['title']['raw'] ) );968 }969 $this->assertSame( 1, $data['content']['block_version'] );970 } else {971 $this->assertEmpty( $data['content']['rendered'] );972 }973 974 880 // post_parent. 975 881 $this->assertArrayHasKey( 'parent', $data ); -
trunk/tests/qunit/fixtures/wp-api-generated.js
r52145 r52184 3706 3706 "post_type", 3707 3707 "post_type_archive", 3708 "custom", 3709 "block" 3708 "custom" 3710 3709 ], 3711 3710 "required": false … … 3766 3765 "type": "integer", 3767 3766 "minimum": 0, 3768 "required": false3769 },3770 "content": {3771 "description": "HTML content to display for this block menu item.",3772 "type": [3773 "string",3774 "object"3775 ],3776 "properties": {3777 "raw": {3778 "description": "HTML content, as it exists in the database.",3779 "type": "string",3780 "context": [3781 "edit"3782 ]3783 },3784 "rendered": {3785 "description": "HTML content, transformed for display.",3786 "type": "string",3787 "context": [3788 "view",3789 "edit"3790 ],3791 "readonly": true3792 },3793 "block_version": {3794 "description": "Version of the block format used in the HTML content.",3795 "type": "integer",3796 "context": [3797 "edit"3798 ],3799 "readonly": true3800 }3801 },3802 3767 "required": false 3803 3768 }, … … 3932 3897 "post_type", 3933 3898 "post_type_archive", 3934 "custom", 3935 "block" 3899 "custom" 3936 3900 ], 3937 3901 "required": false … … 3988 3952 "type": "integer", 3989 3953 "minimum": 0, 3990 "required": false3991 },3992 "content": {3993 "description": "HTML content to display for this block menu item.",3994 "type": [3995 "string",3996 "object"3997 ],3998 "properties": {3999 "raw": {4000 "description": "HTML content, as it exists in the database.",4001 "type": "string",4002 "context": [4003 "edit"4004 ]4005 },4006 "rendered": {4007 "description": "HTML content, transformed for display.",4008 "type": "string",4009 "context": [4010 "view",4011 "edit"4012 ],4013 "readonly": true4014 },4015 "block_version": {4016 "description": "Version of the block format used in the HTML content.",4017 "type": "integer",4018 "context": [4019 "edit"4020 ],4021 "readonly": true4022 }4023 },4024 3954 "required": false 4025 3955 }, … … 4158 4088 "post_type", 4159 4089 "post_type_archive", 4160 "custom", 4161 "block" 4090 "custom" 4162 4091 ], 4163 4092 "required": false … … 4208 4137 "type": "integer", 4209 4138 "minimum": 0, 4210 "required": false4211 },4212 "content": {4213 "description": "HTML content to display for this block menu item.",4214 "type": [4215 "string",4216 "object"4217 ],4218 "properties": {4219 "raw": {4220 "description": "HTML content, as it exists in the database.",4221 "type": "string",4222 "context": [4223 "edit"4224 ]4225 },4226 "rendered": {4227 "description": "HTML content, transformed for display.",4228 "type": "string",4229 "context": [4230 "view",4231 "edit"4232 ],4233 "readonly": true4234 },4235 "block_version": {4236 "description": "Version of the block format used in the HTML content.",4237 "type": "integer",4238 "context": [4239 "edit"4240 ],4241 "readonly": true4242 }4243 },4244 4139 "required": false 4245 4140 },
Note: See TracChangeset
for help on using the changeset viewer.