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

Commit 02733d3

Browse files
committed
Add Customize menu to allow access user with 'customize' cap
1 parent 7910d6b commit 02733d3

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

php/class-post-type.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function init() {
9090

9191
add_filter( 'post_link', array( $this, 'filter_post_type_link' ), 10, 2 );
9292
add_action( 'add_meta_boxes_' . static::SLUG, array( $this, 'setup_metaboxes' ), 10, 1 );
93-
add_action( 'admin_menu',array( $this, 'add_admin_menu_item' ) );
93+
add_action( 'admin_menu',array( $this, 'add_admin_menu_item' ), 99 );
9494
add_filter( 'map_meta_cap', array( $this, 'remap_customize_meta_cap' ), 5, 4 );
9595
add_filter( 'bulk_actions-edit-' . static::SLUG, array( $this, 'add_snapshot_bulk_actions' ) );
9696
add_filter( 'handle_bulk_actions-edit-' . static::SLUG, array( $this, 'handle_snapshot_merge' ), 10, 3 );
@@ -131,7 +131,19 @@ public function add_admin_menu_item() {
131131
$page_title = $post_type_object->labels->name;
132132
$menu_title = $post_type_object->labels->name;
133133
$menu_slug = 'edit.php?post_type=' . static::SLUG;
134-
add_theme_page( $page_title, $menu_title, $capability, $menu_slug );
134+
if ( current_user_can( 'edit_theme_options' ) ) {
135+
add_theme_page( $page_title, $menu_title, $capability, $menu_slug );
136+
} elseif ( current_user_can( 'customize' ) ) {
137+
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
138+
139+
// Remove exiting menu from appearance as it will require 'edit_theme_options' cap.
140+
remove_menu_page( esc_url( $customize_url ) );
141+
142+
// Add customize menu on top and add Changeset menu as submenu.
143+
$customize_page_title = __( 'Customize', 'customize-snapshots' );
144+
add_menu_page( $customize_page_title, $customize_page_title, 'customize', esc_url( $customize_url ), '', 'dashicons-admin-customizer', 65 );
145+
add_submenu_page( $customize_url, $page_title, $menu_title, $capability, esc_url( $menu_slug ) );
146+
}
135147
}
136148

137149
/**

tests/php/test-class-post-type.php

+45-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function test_init() {
8484

8585
$this->assertEquals( 10, has_filter( 'post_link', array( $post_type_obj, 'filter_post_type_link' ) ) );
8686
$this->assertEquals( 10, has_action( 'add_meta_boxes_' . Post_Type::SLUG, array( $post_type_obj, 'setup_metaboxes' ) ) );
87-
$this->assertEquals( 10, has_action( 'admin_menu', array( $post_type_obj, 'add_admin_menu_item' ) ) );
87+
$this->assertEquals( 99, has_action( 'admin_menu', array( $post_type_obj, 'add_admin_menu_item' ) ) );
8888
$this->assertEquals( 5, has_filter( 'map_meta_cap', array( $post_type_obj, 'remap_customize_meta_cap' ) ) );
8989
$this->assertEquals( 10, has_filter( 'bulk_actions-edit-' . Post_Type::SLUG, array( $post_type_obj, 'add_snapshot_bulk_actions' ) ) );
9090
$this->assertEquals( 10, has_filter( 'handle_bulk_actions-edit-' . Post_Type::SLUG, array( $post_type_obj, 'handle_snapshot_merge' ) ) );
@@ -158,6 +158,50 @@ public function test_add_admin_menu_item() {
158158
$this->assertTrue( in_array( $menu_slug, $submenu['themes.php'][0], true ) );
159159
}
160160

161+
/**
162+
* Allow customize caps to all users for testing.
163+
*
164+
* @see test_menu_for_customize_cap.
165+
* @param array $allcaps all caps.
166+
* @param array $caps caps.
167+
* @param array $args arg for current_user_can.
168+
*
169+
* @return array
170+
*/
171+
public function hack_user_can( $allcaps, $caps, $args ) {
172+
if ( 'customize' === $args[0] ) {
173+
$allcaps = array_merge( $allcaps, array_fill_keys( $caps, true ) );
174+
}
175+
176+
return $allcaps;
177+
}
178+
179+
/**
180+
* Test add_admin_menu_item
181+
*
182+
* @covers \CustomizeSnapshots\Post_Type::add_admin_menu_item()
183+
*/
184+
public function test_menu_for_customize_cap() {
185+
$this->mark_incompatible();
186+
global $submenu, $menu;
187+
if ( null === $submenu ) {
188+
$submenu = array(); // WPCS: global override ok.
189+
}
190+
if ( null === $menu ) {
191+
$menu = array(); // WPCS: global override ok.
192+
}
193+
add_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10, 3 );
194+
$editor_user_id = $this->factory()->user->create( array( 'role' => 'editor' ) );
195+
wp_set_current_user( $editor_user_id );
196+
$post_type_obj = new Post_Type( $this->plugin->customize_snapshot_manager );
197+
$post_type_obj->add_admin_menu_item();
198+
$menu_slug = 'edit.php?post_type=' . Post_Type::SLUG;
199+
$customize_url = add_query_arg( 'return', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), 'customize.php' );
200+
$this->assertArrayHasKey( $customize_url, $submenu );
201+
$this->assertEquals( $menu_slug, $submenu[ $customize_url ][1][2] );
202+
remove_filter( 'user_has_cap', array( $this, 'hack_user_can' ), 10 );
203+
}
204+
161205
/**
162206
* Test filter_post_type_link.
163207
*

0 commit comments

Comments
 (0)