Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #114 from xwp/feature/customizer-top-menu
Browse files Browse the repository at this point in the history
Add customize as top menu for all user
  • Loading branch information
westonruter committed Feb 11, 2017
2 parents 07c607f + 5ac476d commit cd5a18f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
7 changes: 3 additions & 4 deletions php/class-post-type.php
Expand Up @@ -131,17 +131,16 @@ public function add_admin_menu_item() {
$page_title = $post_type_object->labels->name;
$menu_title = $post_type_object->labels->name;
$menu_slug = 'edit.php?post_type=' . static::SLUG;
if ( current_user_can( 'edit_theme_options' ) ) {
add_theme_page( $page_title, $menu_title, $capability, $menu_slug );
} elseif ( current_user_can( 'customize' ) ) {
if ( current_user_can( 'customize' ) ) {
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );

// Remove exiting menu from appearance as it will require 'edit_theme_options' cap.
remove_submenu_page( 'themes.php', esc_url( $customize_url ) );
remove_menu_page( esc_url( $customize_url ) );

// Add customize menu on top and add Changeset menu as submenu.
$customize_page_title = __( 'Customize', 'default' );
add_menu_page( $customize_page_title, $customize_page_title, 'customize', esc_url( $customize_url ), '', 'dashicons-admin-customizer', 65 );
add_menu_page( $customize_page_title, $customize_page_title, 'customize', esc_url( $customize_url ), '', 'dashicons-admin-customizer', 61 );
add_submenu_page( $customize_url, $page_title, $menu_title, $capability, esc_url( $menu_slug ) );
}
}
Expand Down
52 changes: 21 additions & 31 deletions tests/php/test-class-post-type.php
Expand Up @@ -147,15 +147,31 @@ public function test_extend_changeset_post_type_object() {
*/
public function test_add_admin_menu_item() {
$this->mark_incompatible();
global $submenu;
$admin_user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
global $submenu, $menu;
$menu = $submenu = array(); // WPCS: global override ok.
$admin_user_id = $this->factory()->user->create( array(
'role' => 'administrator',
) );
wp_set_current_user( $admin_user_id );
$post_type_obj = new Post_Type( $this->plugin->customize_snapshot_manager );
$post_type_obj->add_admin_menu_item();
$menu_slug = 'edit.php?post_type=' . Post_Type::SLUG;
$this->assertArrayHasKey( 'themes.php', $submenu );
$this->assertArrayHasKey( 0, $submenu['themes.php'] );
$this->assertTrue( in_array( $menu_slug, $submenu['themes.php'][0], true ) );

$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
$this->assertArrayHasKey( $customize_url, $submenu );
$this->assertEquals( $menu_slug, $submenu[ $customize_url ][1][2] );

// Check with user with customize access.
$submenu = $menu = array(); // WPCS: global override ok.
add_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10, 3 );
$editor_user_id = $this->factory()->user->create( array(
'role' => 'editor',
) );
wp_set_current_user( $editor_user_id );
$post_type_obj->add_admin_menu_item();
$this->assertArrayHasKey( $customize_url, $submenu );
$this->assertEquals( $menu_slug, $submenu[ $customize_url ][1][2] );
remove_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10 );
}

/**
Expand All @@ -176,32 +192,6 @@ public function hack_user_can( $allcaps, $caps, $args ) {
return $allcaps;
}

/**
* Test add_admin_menu_item
*
* @covers \CustomizeSnapshots\Post_Type::add_admin_menu_item()
*/
public function test_menu_for_customize_cap() {
$this->mark_incompatible();
global $submenu, $menu;
if ( null === $submenu ) {
$submenu = array(); // WPCS: global override ok.
}
if ( null === $menu ) {
$menu = array(); // WPCS: global override ok.
}
add_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10, 3 );
$editor_user_id = $this->factory()->user->create( array( 'role' => 'editor' ) );
wp_set_current_user( $editor_user_id );
$post_type_obj = new Post_Type( $this->plugin->customize_snapshot_manager );
$post_type_obj->add_admin_menu_item();
$menu_slug = 'edit.php?post_type=' . Post_Type::SLUG;
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
$this->assertArrayHasKey( $customize_url, $submenu );
$this->assertEquals( $menu_slug, $submenu[ $customize_url ][1][2] );
remove_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10 );
}

/**
* Test filter_post_type_link.
*
Expand Down

0 comments on commit cd5a18f

Please sign in to comment.