Skip to content

Commit

Permalink
Use rest_is_field_included function in menu endpoints (#34673)
Browse files Browse the repository at this point in the history
* Use rest_is_field_included function.

* Add more examples of rest_is_field_included.

* Fix lints
  • Loading branch information
spacedmonkey authored Sep 14, 2021
1 parent 0e7c88c commit a721891
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
51 changes: 27 additions & 24 deletions lib/class-wp-rest-menu-items-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,17 @@ public function prepare_item_for_response( $post, $request ) {
// Base fields for every post.
$menu_item = wp_setup_nav_menu_item( $post );
$data = array();
if ( in_array( 'id', $fields, true ) ) {
if ( rest_is_field_included( 'id', $fields ) ) {
$data['id'] = $menu_item->ID;
}

if ( in_array( 'title', $fields, true ) ) {
if ( rest_is_field_included( 'title', $fields ) ) {
$data['title'] = array();
}
if ( rest_is_field_included( 'title.raw', $fields ) ) {
$data['title']['raw'] = $menu_item->title;
}
if ( rest_is_field_included( 'title.rendered', $fields ) ) {
add_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );

/** This filter is documented in wp-includes/post-template.php */
Expand All @@ -652,47 +658,44 @@ public function prepare_item_for_response( $post, $request ) {
/** This filter is documented in wp-includes/class-walker-nav-menu.php */
$title = apply_filters( 'nav_menu_item_title', $title, $menu_item, null, 0 );

$data['title'] = array(
'raw' => $menu_item->title,
'rendered' => $title,
);
$data['title']['rendered'] = $title;

remove_filter( 'protected_title_format', array( $this, 'protected_title_format' ) );
}

if ( in_array( 'status', $fields, true ) ) {
if ( rest_is_field_included( 'status', $fields ) ) {
$data['status'] = $menu_item->post_status;
}

if ( in_array( 'url', $fields, true ) ) {
if ( rest_is_field_included( 'url', $fields ) ) {
$data['url'] = $menu_item->url;
}

if ( in_array( 'attr_title', $fields, true ) ) {
if ( rest_is_field_included( 'attr_title', $fields ) ) {
// Same as post_excerpt.
$data['attr_title'] = $menu_item->attr_title;
}

if ( in_array( 'description', $fields, true ) ) {
if ( rest_is_field_included( 'description', $fields ) ) {
// Same as post_content.
$data['description'] = $menu_item->description;
}

if ( in_array( 'type', $fields, true ) ) {
if ( rest_is_field_included( 'type', $fields ) ) {
// Using 'item_type' since 'type' already exists.
$data['type'] = $menu_item->type;
}

if ( in_array( 'type_label', $fields, true ) ) {
if ( rest_is_field_included( 'type_label', $fields ) ) {
// Using 'item_type_label' to match up with 'item_type' - IS READ ONLY!
$data['type_label'] = $menu_item->type_label;
}

if ( in_array( 'object', $fields, true ) ) {
if ( rest_is_field_included( 'object', $fields ) ) {
$data['object'] = $menu_item->object;
}

if ( in_array( 'object_id', $fields, true ) ) {
if ( rest_is_field_included( 'object_id', $fields ) ) {
// Usually is a string, but lets expose as an integer.
$data['object_id'] = absint( $menu_item->object_id );
}
Expand All @@ -711,33 +714,33 @@ public function prepare_item_for_response( $post, $request ) {
$data['content']['block_version'] = block_version( $menu_item->content );
}

if ( in_array( 'parent', $fields, true ) ) {
if ( rest_is_field_included( 'parent', $fields ) ) {
// Same as post_parent, expose as integer.
$data['parent'] = absint( $menu_item->menu_item_parent );
$data['parent'] = (int) $menu_item->post_parent;
}

if ( in_array( 'menu_order', $fields, true ) ) {
if ( rest_is_field_included( 'menu_order', $fields ) ) {
// Same as post_parent, expose as integer.
$data['menu_order'] = absint( $menu_item->menu_order );
$data['menu_order'] = (int) $menu_item->menu_order;
}

if ( in_array( 'menu_id', $fields, true ) ) {
if ( rest_is_field_included( 'menu_id', $fields ) ) {
$data['menu_id'] = $this->get_menu_id( $menu_item->ID );
}

if ( in_array( 'target', $fields, true ) ) {
if ( rest_is_field_included( 'target', $fields ) ) {
$data['target'] = $menu_item->target;
}

if ( in_array( 'classes', $fields, true ) ) {
if ( rest_is_field_included( 'classes', $fields ) ) {
$data['classes'] = (array) $menu_item->classes;
}

if ( in_array( 'xfn', $fields, true ) ) {
if ( rest_is_field_included( 'xfn', $fields ) ) {
$data['xfn'] = array_map( 'sanitize_html_class', explode( ' ', $menu_item->xfn ) );
}

if ( in_array( 'meta', $fields, true ) ) {
if ( rest_is_field_included( 'meta', $fields ) ) {
$data['meta'] = $this->meta->get_value( $menu_item->ID, $request );
}

Expand All @@ -746,7 +749,7 @@ public function prepare_item_for_response( $post, $request ) {
foreach ( $taxonomies as $taxonomy ) {
$base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;

if ( in_array( $base, $fields, true ) ) {
if ( rest_is_field_included( $base, $fields ) ) {
$terms = get_the_terms( $post, $taxonomy->name );
$data[ $base ] = $terms ? array_values( wp_list_pluck( $terms, 'term_id' ) ) : array();
}
Expand Down
20 changes: 15 additions & 5 deletions lib/class-wp-rest-menu-locations-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ public function get_item( $request ) {
public function prepare_item_for_response( $location, $request ) {
$locations = get_nav_menu_locations();
$menu = ( isset( $locations[ $location->name ] ) ) ? $locations[ $location->name ] : 0;
$data = array(
'name' => $location->name,
'description' => $location->description,
'menu' => $menu,
);

$fields = $this->get_fields_for_response( $request );
$data = array();

if ( rest_is_field_included( 'name', $fields ) ) {
$data['name'] = $location->name;
}

if ( rest_is_field_included( 'description', $fields ) ) {
$data['description'] = $location->description;
}

if ( rest_is_field_included( 'menu', $fields ) ) {
$data['menu'] = (int) $menu;
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
Expand Down
4 changes: 2 additions & 2 deletions lib/class-wp-rest-menus-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,11 @@ public function prepare_item_for_response( $term, $request ) {
$fields = $this->get_fields_for_response( $request );
$data = $response->get_data();

if ( in_array( 'locations', $fields, true ) ) {
if ( rest_is_field_included( 'locations', $fields ) ) {
$data['locations'] = $this->get_menu_locations( $nav_menu->term_id );
}

if ( in_array( 'auto_add', $fields, true ) ) {
if ( rest_is_field_included( 'auto_add', $fields ) ) {
$auto_add = $this->get_menu_auto_add( $nav_menu->term_id );
$data['auto_add'] = $auto_add;
}
Expand Down

0 comments on commit a721891

Please sign in to comment.