Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation: Try filtering by show_in_nav_menus for post types and taxonomies #1002

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,20 @@ public function register_routes() {
*/
public function get_items_permissions_check( $request ) {
if ( 'edit' === $request['context'] ) {
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );

if ( ! empty( $request['show_in_nav_menus'] ) ) {
$types = get_post_types(
array(
'show_in_nav_menus' => filter_var(
$request['show_in_nav_menus'],
FILTER_VALIDATE_BOOLEAN
),
'show_in_rest' => true,
),
'objects'
);
} else {
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
}
foreach ( $types as $type ) {
if ( current_user_can( $type->cap->edit_posts ) ) {
return true;
Expand All @@ -109,9 +121,21 @@ public function get_items_permissions_check( $request ) {
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/
public function get_items( $request ) {
$data = array();
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );

$data = array();
if ( ! empty( $request['show_in_nav_menus'] ) ) {
$types = get_post_types(
array(
'show_in_nav_menus' => filter_var(
$request['show_in_nav_menus'],
FILTER_VALIDATE_BOOLEAN
),
'show_in_rest' => true,
),
'objects'
);
} else {
$types = get_post_types( array( 'show_in_rest' => true ), 'objects' );
}
foreach ( $types as $type ) {
if ( 'edit' === $request['context'] && ! current_user_can( $type->cap->edit_posts ) ) {
continue;
Expand Down Expand Up @@ -222,6 +246,13 @@ public function prepare_item_for_response( $post_type, $request ) {
$data['rest_base'] = $base;
}

if ( in_array( 'visibility', $fields, true ) ) {
$data['visibility'] = array(
'show_ui' => (bool) $post_type->show_ui,
'show_in_nav_menus' => (bool) $post_type->show_in_nav_menus,
);
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
Expand Down Expand Up @@ -334,6 +365,22 @@ public function get_item_schema() {
'context' => array( 'view', 'edit', 'embed' ),
'readonly' => true,
),
'visibility' => array(
'description' => __( 'The visibility settings for the post type.' ),
'type' => 'object',
'context' => array( 'edit' ),
'readonly' => true,
'properties' => array(
'show_ui' => array(
'description' => __( 'Whether to generate and allow a UI for managing this post type in the admin.' ),
'type' => 'boolean',
),
'show_in_nav_menus' => array(
'description' => __( 'Whether to make this post type available for selection in navigation menus.' ),
'type' => 'boolean',
),
),
),
),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function get_items_permissions_check( $request ) {
if ( 'edit' === $request['context'] ) {
if ( ! empty( $request['type'] ) ) {
$taxonomies = get_object_taxonomies( $request['type'], 'objects' );
} elseif ( ! empty( $request['show_in_nav_menus'] ) ) {
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => (bool) $request['show_in_nav_menus'] ), 'objects' );
} else {
$taxonomies = get_taxonomies( '', 'objects' );
}
Expand Down Expand Up @@ -119,6 +121,8 @@ public function get_items( $request ) {

if ( isset( $registered['type'] ) && ! empty( $request['type'] ) ) {
$taxonomies = get_object_taxonomies( $request['type'], 'objects' );
} elseif ( ! empty( $request['show_in_nav_menus'] ) ) {
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => filter_var( $request['show_in_nav_menus'], FILTER_VALIDATE_BOOLEAN ) ), 'objects' );
} else {
$taxonomies = get_taxonomies( '', 'objects' );
}
Expand Down