Make WordPress Core

Changeset 52184


Ignore:
Timestamp:
11/16/2021 05:07:43 PM (2 years ago)
Author:
spacedmonkey
Message:

REST API: Remove experimental block menu item types.

The menu items REST API controller was added in [52079]. This included functionality to add a "block" menu item type. This functionality is experimental and not currently used in WordPress core, so should be removed.

Props noisysocks.
See #40878.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/nav-menu.php

    r52079 r52184  
    407407 * Save the properties of a menu item or create a new one.
    408408 *
    409  * The menu-item-title, menu-item-description, menu-item-attr-title, and menu-item-content are expected
     409 * The menu-item-title, menu-item-description and menu-item-attr-title are expected
    410410 * to be pre-slashed since they are passed directly to APIs that expect slashed data.
    411411 *
    412412 * @since 3.0.0
    413  * @since 5.9.0 Added the menu-item-content parameter.
    414413 *
    415414 * @param int   $menu_id         The ID of the menu. Required. If "0", makes the menu item a draft orphan.
     
    450449        'menu-item-target'        => '',
    451450        'menu-item-classes'       => '',
    452         'menu-item-content'       => '',
    453451        'menu-item-xfn'           => '',
    454452        'menu-item-status'        => '',
     
    575573    update_post_meta( $menu_item_db_id, '_menu_item_xfn', $args['menu-item-xfn'] );
    576574    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'] );
    578575
    579576    if ( 0 == $menu_id ) {
     
    914911                $menu_item->title = ( '' === $menu_item->post_title ) ? $original_title : $menu_item->post_title;
    915912
    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;
    920913            } else {
    921914                $menu_item->type_label = __( 'Custom Link' );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-menu-items-controller.php

    r52079 r52184  
    340340                'menu-item-url'         => $menu_item_obj->url,
    341341                'menu-item-description' => $menu_item_obj->description,
    342                 'menu-item-content'     => $menu_item_obj->menu_item_content,
    343342                'menu-item-attr-title'  => $menu_item_obj->attr_title,
    344343                'menu-item-target'      => $menu_item_obj->target,
     
    361360                'menu-item-url'         => '',
    362361                'menu-item-description' => '',
    363                 'menu-item-content'     => '',
    364362                'menu-item-attr-title'  => '',
    365363                'menu-item-target'      => '',
     
    407405            } elseif ( ! empty( $request['title']['raw'] ) ) {
    408406                $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'];
    418407            }
    419408        }
     
    461450        }
    462451
    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 
    468452        if ( $error->has_errors() ) {
    469453            return $error;
     
    565549            // It is stored as a string, but should be exposed as an integer.
    566550            $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 );
    584551        }
    585552
     
    782749            'description' => __( 'The family of objects originally represented, such as "post_type" or "taxonomy".' ),
    783750            'type'        => 'string',
    784             'enum'        => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom', 'block' ),
     751            'enum'        => array( 'taxonomy', 'post_type', 'post_type_archive', 'custom' ),
    785752            'context'     => array( 'view', 'edit', 'embed' ),
    786753            'default'     => 'custom',
     
    858825            'minimum'     => 0,
    859826            '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             ),
    885827        );
    886828
  • trunk/tests/phpunit/tests/rest-api/wpRestMenuItemsController.php

    r52079 r52184  
    609609
    610610    /**
    611      * Tests that a block menu item can be created.
    612      *
    613      * @ticket 40878
    614      * @covers ::create_item
    615      */
    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 40878
    636      * @covers ::create_item
    637      */
    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     /**
    653611     * @ticket 40878
    654612     * @covers ::update_item
     
    684642     * @covers ::update_item
    685643     */
    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 40878
    721      * @covers ::update_item
    722      */
    723644    public function test_update_item_clean_xfn() {
    724645        wp_set_current_user( self::$admin_id );
     
    826747        $data       = $response->get_data();
    827748        $properties = $data['schema']['properties'];
    828         $this->assertSame( 19, count( $properties ) );
     749        $this->assertSame( 18, count( $properties ) );
    829750        $this->assertArrayHasKey( 'type_label', $properties );
    830751        $this->assertArrayHasKey( 'attr_title', $properties );
    831752        $this->assertArrayHasKey( 'classes', $properties );
    832753        $this->assertArrayHasKey( 'description', $properties );
    833         $this->assertArrayHasKey( 'content', $properties );
    834754        $this->assertArrayHasKey( 'id', $properties );
    835755        $this->assertArrayHasKey( 'url', $properties );
     
    958878        }
    959879
    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 
    974880        // post_parent.
    975881        $this->assertArrayHasKey( 'parent', $data );
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r52145 r52184  
    37063706                                "post_type",
    37073707                                "post_type_archive",
    3708                                 "custom",
    3709                                 "block"
     3708                                "custom"
    37103709                            ],
    37113710                            "required": false
     
    37663765                            "type": "integer",
    37673766                            "minimum": 0,
    3768                             "required": false
    3769                         },
    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": true
    3792                                 },
    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": true
    3800                                 }
    3801                             },
    38023767                            "required": false
    38033768                        },
     
    39323897                                "post_type",
    39333898                                "post_type_archive",
    3934                                 "custom",
    3935                                 "block"
     3899                                "custom"
    39363900                            ],
    39373901                            "required": false
     
    39883952                            "type": "integer",
    39893953                            "minimum": 0,
    3990                             "required": false
    3991                         },
    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": true
    4014                                 },
    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": true
    4022                                 }
    4023                             },
    40243954                            "required": false
    40253955                        },
     
    41584088                                "post_type",
    41594089                                "post_type_archive",
    4160                                 "custom",
    4161                                 "block"
     4090                                "custom"
    41624091                            ],
    41634092                            "required": false
     
    42084137                            "type": "integer",
    42094138                            "minimum": 0,
    4210                             "required": false
    4211                         },
    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": true
    4234                                 },
    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": true
    4242                                 }
    4243                             },
    42444139                            "required": false
    42454140                        },
Note: See TracChangeset for help on using the changeset viewer.