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

Fix the issue of submenus not appearing #40

Merged
merged 5 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion php/PostTypes/BlockPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct() {
*/
public function register_hooks() {
add_action( 'init', [ $this, 'register_post_type' ] );
add_action( 'admin_init', [ $this, 'add_caps' ] );
add_action( 'plugins_loaded', [ $this, 'add_caps' ] );
Copy link
Contributor Author

@kienstra kienstra Oct 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe admin_init was a little too late to add the capabilities.

By moving it earlier to plugins_loaded, the capability is there in time for the submenus to be there.

add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] );
add_action( 'add_meta_boxes', [ $this, 'remove_meta_boxes' ] );
add_action( 'edit_form_before_permalink', [ $this, 'template_location' ] );
Expand Down Expand Up @@ -218,6 +218,10 @@ public function register_post_type() {
* @return void
*/
public function add_caps() {
if ( ! is_admin() ) {
return;
}

$admin = get_role( 'administrator' );
if ( ! $admin ) {
return;
Expand Down
2 changes: 2 additions & 0 deletions tests/php/Integration/TestPostCapabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ public function get_users() {
* @param bool $expected The expected result for those capability and roles.
*/
public function test_user_capability( $user_role, $capability, $expected ) {
set_current_screen( 'edit-post' );
wp_set_current_user( $this->factory()->user->create( [ 'role' => $user_role ] ) );

$this->assertEquals( $expected, current_user_can( $capability, $this->post_id ) );
}
}
2 changes: 1 addition & 1 deletion tests/php/Unit/PostTypes/TestBlockPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function test_register_hooks() {
$this->instance->register_hooks();

$this->assertEquals( 10, has_action( 'init', [ $this->instance, 'register_post_type' ] ) );
$this->assertEquals( 10, has_action( 'admin_init', [ $this->instance, 'add_caps' ] ) );
$this->assertEquals( 10, has_action( 'plugins_loaded', [ $this->instance, 'add_caps' ] ) );
$this->assertEquals( 10, has_action( 'add_meta_boxes', [ $this->instance, 'add_meta_boxes' ] ) );
$this->assertEquals( 10, has_action( 'add_meta_boxes', [ $this->instance, 'remove_meta_boxes' ] ) );
$this->assertEquals( 10, has_action( 'post_submitbox_start', [ $this->instance, 'save_draft_button' ] ) );
Expand Down