Skip to content

Commit

Permalink
Leave CPTs in wp-admin
Browse files Browse the repository at this point in the history
Props jeherve.
  • Loading branch information
obenland committed Dec 2, 2020
1 parent 71ae80d commit 9f6cc03
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 125 deletions.
65 changes: 0 additions & 65 deletions modules/masterbar/admin-menu/class-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,6 @@ public function reregister_menu_items() {
$this->add_posts_menu( $domain, $calypso );
$this->add_media_menu( $domain, $calypso );
$this->add_page_menu( $domain, $calypso );

// Custom Post Types (except Feedback).
$post_types = (array) get_post_types(
array(
'show_ui' => true,
'_builtin' => false,
'show_in_menu' => true,
),
'objects'
);
foreach ( $post_types as $post_type ) {
if ( 'feedback' === $post_type->name ) {
continue;
}

// Check if it should be a submenu.
if ( true !== $post_type->show_in_menu ) {
continue;
}

$this->add_custom_post_type_menu( $post_type, $domain, $calypso );
}

$this->add_comments_menu( $domain, $calypso );
$this->add_jetpack_menu( $domain );
$this->add_appearance_menu( $domain, $calypso );
Expand Down Expand Up @@ -358,48 +335,6 @@ public function add_page_menu( $domain, $calypso = true ) {
$this->migrate_submenus( 'edit.php?post_type=page', $menu_slug );
}

/**
* Adds custom post type menus.
*
* @param \WP_Post_Type $ptype_obj Post type object.
* @param string $domain Site domain.
* @param bool $calypso Optional. Whether links should point to Calypso or wp-admin.
* Default true (Calypso).
*/
public function add_custom_post_type_menu( \WP_Post_Type $ptype_obj, $domain, $calypso = true ) {
if ( ! $calypso ) {
return;
}

if ( ! $ptype_obj->show_in_menu ) {
return;
}

$post_type = $ptype_obj->name;
$menu_icon = 'dashicons-admin-post';
if ( is_string( $ptype_obj->menu_icon ) ) {
// Special handling for data:image/svg+xml and Dashicons.
if ( 0 === strpos( $ptype_obj->menu_icon, 'data:image/svg+xml;base64,' ) || 0 === strpos( $ptype_obj->menu_icon, 'dashicons-' ) ) {
$menu_icon = $ptype_obj->menu_icon;
} else {
$menu_icon = esc_url( $ptype_obj->menu_icon );
}
}

$old_slug = 'edit.php?post_type=' . $post_type;
$ptype_file = 'https://wordpress.com/types/' . $post_type . '/' . $domain;

remove_menu_page( $old_slug );
remove_submenu_page( $old_slug, $old_slug );
remove_submenu_page( $old_slug, 'post-new.php?post_type=' . $post_type );

add_menu_page( esc_attr( $ptype_obj->labels->menu_name ), $ptype_obj->labels->menu_name, $ptype_obj->cap->edit_posts, $ptype_file, null, $menu_icon, $ptype_obj->menu_position );
add_submenu_page( $ptype_file, $ptype_obj->labels->all_items, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, $ptype_file, null, 5 );
add_submenu_page( $ptype_file, $ptype_obj->labels->add_new, $ptype_obj->labels->add_new, $ptype_obj->cap->create_posts, 'https://wordpress.com/edit/' . $post_type . '/' . $domain, null, 10 );

$this->migrate_submenus( $old_slug, $ptype_file );
}

/**
* Adds Comments menu.
*
Expand Down
61 changes: 1 addition & 60 deletions tests/php/modules/masterbar/test-class-admin-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Class Test_Admin_Menu
*
* @coversDefaultClass Automattic\Jetpack\Admin_Menu
* @coversDefaultClass Automattic\Jetpack\Dashboard_Customizations\Admin_Menu
*/
class Test_Admin_Menu extends WP_UnitTestCase {

Expand Down Expand Up @@ -424,65 +424,6 @@ public function test_add_page_menu() {
$this->assertEmpty( $submenu['edit.php?post_type=page'] );
}

/**
* Tests add_custom_post_type_menu
*
* @covers ::add_custom_post_type_menu
*/
public function test_add_custom_post_type_menu() {
global $menu, $submenu;

// Don't show post types that don't want to be shown.
$revision = get_post_type_object( 'revision' );
static::$admin_menu->add_custom_post_type_menu( $revision, static::$domain );

$last_item = array_pop( $menu );
$this->assertNotSame( 'https://wordpress.com/types/revision/' . static::$domain, $last_item[2] );

$post_type = register_post_type(
'custom_test_type',
array(
'label' => 'Custom Test Types',
'show_ui' => true,
'menu_position' => 2020,
)
);
static::$admin_menu->add_custom_post_type_menu( $post_type, static::$domain );

// Clean up.
unregister_post_type( 'custom_test_type' );

$slug = 'https://wordpress.com/types/custom_test_type/' . static::$domain;

$custom_menu_item = array(
'Custom Test Types',
'edit_posts',
$slug,
'Custom Test Types',
'menu-top toplevel_page_' . $slug,
'toplevel_page_' . $slug,
'dashicons-admin-post',
);

$this->assertSame( $menu[2020], $custom_menu_item );

$custom_submenu_item = array(
'Custom Test Types',
'edit_posts',
'https://wordpress.com/types/custom_test_type/' . static::$domain,
'Custom Test Types',
);
$this->assertContains( $custom_submenu_item, $submenu[ $slug ] );

$add_new_submenu_item = array(
'Add New',
'edit_posts',
'https://wordpress.com/edit/custom_test_type/' . static::$domain,
'Add New',
);
$this->assertContains( $add_new_submenu_item, $submenu[ $slug ] );
}

/**
* Tests add_comments_menu
*
Expand Down

0 comments on commit 9f6cc03

Please sign in to comment.