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

Option page as sub-page of another menu broken in 2.8.0 #1410

Closed
JiveDig opened this issue Feb 24, 2021 · 17 comments
Closed

Option page as sub-page of another menu broken in 2.8.0 #1410

JiveDig opened this issue Feb 24, 2021 · 17 comments

Comments

@JiveDig
Copy link

JiveDig commented Feb 24, 2021

Describe the bug

I have a plugin that uses CMB2 to register an options page as a sub-menu of another custom admin page. This works in 2.7.0 but breaks the url in 2.8.0.

Steps to reproduce (I have confirmed I can reproduce this issue on the develop branch):

The following works with 2.7 producing an admin url with /wp-admin/admin.php?page=some_sub_page

but breaks in 2.8 producing an admin url with /wp-admin/some_sub_page

add_action( 'admin_menu', function() {
	add_menu_page(
		esc_html__( 'Some Page', 'textdomain' ),
		esc_html__( 'Some Page', 'textdomain' ),
		'edit_posts',
		'some-page',
		'prefix_render_admin_menu_page'
	);
});

add_action( 'cmb2_admin_init', function() {
	$cmb = new_cmb2_box( array(
		'id'           => 'some_sub_page_metabox',
		'title'        => __( 'Some Sub-Page', 'textdomain' ),
		'object_types' => array( 'options-page' ),
		'option_key'   => 'some_sub_page',
		'parent_slug'  => 'some-page',
	) );
});

function prefix_render_admin_menu_page() {
	echo '<h1>Some Page</h1>';
}
@tw2113
Copy link
Contributor

tw2113 commented Feb 24, 2021

I'd need to test and try to confirm, but I wonder if it's coming from the changes that were a part of #1380

@sonnysunzu
Copy link

I'm also having the same issue

@tw2113
Copy link
Contributor

tw2113 commented Mar 2, 2021

@sonnysunzu can you provide your CMB2 configuration code as well, in case variations are producing different results, and so we could just verify that a change is indeed affecting both.

@sonnysunzu
Copy link

sonnysunzu commented Mar 2, 2021

as soon as i remove the 'parent_slug' the side menu will then show the link as a stand alone menu item outside of the submenu and works. so something has happened for submenu items to no longer work

wp add menu settings

add_menu_page('Woocommerce Image Sales', 'Image Sales', $settingsCap, 'wis', array($this, 'wis_about'), 'dashicons-products');

cmb2 setup

$cmb_options = new_cmb2_box(array( 'id' => 'wis_options', 'title' => 'Options', 'menu_title' => __('Options', 'Options'), 'object_types' => array('options-page'), 'option_key' => 'wis_options', 'parent_slug' => 'wis', 'capability' => 'manage_options', 'display_cb' => array($this, 'wis_options_page_output'), 'description' => '', ));

I hope this is what you asked for.

@tw2113
Copy link
Contributor

tw2113 commented Mar 2, 2021

yep that's the part i was looking for. thanks.

@sonnysunzu
Copy link

sonnysunzu commented Mar 3, 2021

@tw2113, @JiveDig, I found the issue.

inside includes/CMB2_Options_Hookup.php line 71

add_action( $hook, array( $this, 'options_page_menu_hooks' ), $this->get_priority() );

if you remove $this->get_priority() from the above line. Submenus then work again.

so looking further into this. Too fix my issue without modifying the cmb2 direct i had to add 'priority' => 'low' to my new_cmb2_box

so like this

$cmb_options = new_cmb2_box(array( 'id' => 'wis_options', 'title' => 'Options', 'menu_title' => __('Options', 'Options'), 'object_types' => array('options-page'), 'option_key' => 'wis_options', 'parent_slug' => 'wis', 'capability' => 'manage_options', 'display_cb' => array($this, 'wis_options_page_output'), 'description' => '', 'priority' => 'low' ));

without going to far as its 1am here and i need to be up for work in the morning. I'm going to take a guess that the hook for the submenu is running before the hook for the parent menu item. I could be wrong I'm tired

@tw2113
Copy link
Contributor

tw2113 commented Mar 3, 2021

I must not be doing something right, because my submenu items with both code samples above put me on 404 pages on the frontend.

For example, I'm ending up on https://wds.test/wp-admin/some_sub_page for @JiveDig's code. I've reverted myself back to the 2.7.0 release as well, just to help. What URLs are you two getting for your submenus?

@JiveDig
Copy link
Author

JiveDig commented Mar 3, 2021

I'm on mobile at the moment, but the exact URLs I get with my reduced code example are all in the OP.

@sonnylloyd
Copy link

sonnylloyd commented Mar 3, 2021

@tw2113 have you set the priority to low ? like i suggested as this solved the issue for me. (sorry i have two accounts one for work and a personal one. this is my personal one, and the sonnysunzu is my work)

@JiveDig
Copy link
Author

JiveDig commented Mar 3, 2021

Great find @sonnysunzu. Adding any priority works for me. I tested first with low, and it worked but was in the wrong place. Same with high. But just adding 'priority' => 10, put it in the correct place with a link that actually works without 404'ing.

So it looks like the change to get_priority() in 94e80d7 may be missing a fallback/default?

https://github.com/CMB2/CMB2/blob/develop/includes/CMB2_Options_Hookup.php#L71

Is currently:

add_action( $hook, array( $this, 'options_page_menu_hooks' ), $this->get_priority() );

But should be:

add_action( $hook, array( $this, 'options_page_menu_hooks' ), $this->get_priority( 10 ) );

@JiveDig
Copy link
Author

JiveDig commented Mar 3, 2021

Actually, the get_priority method has a default set like public function get_priority( $default = 10 ) { so I'm a bit lost without diving any deeper in.

JiveDig pushed a commit to maithemewp/mai-ads-extra-content that referenced this issue Mar 3, 2021
@sonnylloyd
Copy link

@JiveDig @tw2113

I strongly believe the default priority of high is causing the admin_menu_hook hook found in CMB2_Options_Hookup.php on line 71. to run before the parent hook has had chance to run. And because of this the parent page does not exist so it cannot add the sub menu

hence when setting the priority to low corrected the order the hooks was run. meaning the hook for the parent was ran first then the hook for cmb2

This would explain why. when removing the parent id and having the menu option as standalone it worked. because it does not depend on a parent existing first

@tw2113
Copy link
Contributor

tw2113 commented Mar 3, 2021

@sonnylloyd i was trying with version 2.7.0 and no priority argument passed and still wasn't seeing successful option pages. I may try again later tonight or tomorrow with all this new information, but I was really hoping to have a git bisect chance to try and nail down the exact code change that introduced things.

@JiveDig
Copy link
Author

JiveDig commented Mar 3, 2021

@tw2113 Did you try my original code?

With a clean install of WP and Twenty Twenty One theme active, if I put the following code in functions.php it works in 2.7.0 and doesn't work in 2.8.0.

add_action( 'admin_menu', function() {
	add_menu_page(
		esc_html__( 'Some Page', 'textdomain' ),
		esc_html__( 'Some Page', 'textdomain' ),
		'edit_posts',
		'some-page',
		'prefix_render_admin_menu_page'
	);
});

add_action( 'cmb2_admin_init', function() {
	$cmb = new_cmb2_box( array(
		'id'           => 'some_sub_page_metabox',
		'title'        => __( 'Some Sub-Page', 'textdomain' ),
		'object_types' => array( 'options-page' ),
		'option_key'   => 'some_sub_page',
		'parent_slug'  => 'some-page',
	) );
});

function prefix_render_admin_menu_page() {
	echo '<h1>Some Page</h1>';
}

@JiveDig
Copy link
Author

JiveDig commented Mar 3, 2021

Markup 2021-03-03 at 17 12 32

Markup 2021-03-03 at 17 13 06

@jtsternberg
Copy link
Member

I strongly believe the default priority of high is causing the admin_menu_hook hook found in CMB2_Options_Hookup.php on line 71. to run before the parent hook has had chance to run. And because of this the parent page does not exist so it cannot add the sub menu

That's exactly right. Priority defaults to high for boxes: https://github.com/CMB2/CMB2/blob/develop/includes/CMB2.php#L77

Which is translated to a hook priority of 5: https://github.com/CMB2/CMB2/blob/develop/includes/CMB2_Hookup.php#L814-L817

I didn't notice/realize that by adding this priority thing in 94e80d7 that I would be breaking back-compatibility. Ugh. I'll get this updated so that the priority is default for options pages, that way it will get a priority of 10, putting it back to what it was previously.

@tw2113
Copy link
Contributor

tw2113 commented Mar 4, 2021

@JiveDig i was only ever getting the second one, even though I'm pretty sure I checked out the right git tag to go back to 2.7.0.

Regardless it looks like Justin is on the case as well and has a good lead on things, so I'm going to let him take over from here.

lipemat added a commit to lipemat/CMB2 that referenced this issue Mar 30, 2021
* upstream/develop:
  Clean up and add props for CMB2#1413
  Sanitize URLs with HTTPS
  Add develop suffix to init class
  Add am-cli-tools
  Update changelong and version numbers and readmes, and prepare release
  Set default priority to 10 for options pages. Fixes CMB2#1410
  build field-cache key manually to remove unnecessary |'s
  Better generated array key for cached fields, fixes issue where wrong field is found. Fixes CMB2#1405
  Add to list of valid image types from get_allowed_mime_types(). Fixes CMB2#1223
  Move tab markup output to separate method, options_page_tab_nav_output. Fixes CMB2#1407
  Add cmb2_tab_group_tabs filter for adding arbitrary menu page urls to the cmb2 tabs. See CMB2#1407
  Update since tag, and add props for CMB2#1340
  Limit use of italic, including removing from field descriptions. Fixes CMB2#1404
  Add props for CMB2#1400
  move $args in deprecated_param method for 7.4
  Add develop suffix to init class
  Prepare release and changelog for 2.8.0
  Fix tests since WP_Error signature changed
  move $args in deprecated_param method for 7.4
  Use the already-existing get_priority method. Re CMB2#1380 and CMB2#1398
  Use existing "priority" field param. Fixes CMB2#1380. Closes CMB2#1398
  Add admin_menu_hook_priority box property for options boxes. Fixes CMB2#1380. Closes CMB2#1398
  Make field_can first param required to address php 8 "Required parameter follows optional parameter". Fixes CMB2#1396
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/CMB2_Utils.php
  Prevent array to string conversion
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Update includes/types/CMB2_Type_Colorpicker.php
  Added sanitize_color() function and remove PHP warnings suppresions
  Fixes PHP warnings on repeatable ColorPicker with an array as default
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants