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

WP REST API shows all metaboxes for all custom post types #1158

Closed
sparkdevelopment opened this issue Aug 10, 2018 · 3 comments
Closed

WP REST API shows all metaboxes for all custom post types #1158

sparkdevelopment opened this issue Aug 10, 2018 · 3 comments

Comments

@sparkdevelopment
Copy link

sparkdevelopment commented Aug 10, 2018

Versions

  • CMB 2.4.2
  • WP 4.9.8

Expected Behavior:

When registering metaboxes for two different custom post types to be visible in the REST API (using /wp/v2/{post_type}) the 'cmb2' object in REST response will only contain the metaboxes for that post type.

Actual Behavior:

The REST response for both post types contains all metaboxes, albeit the fields in the boxes not registered to that post type all have empty values

image

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

  1. Register two custom post types, each with show_in_rest => true
  2. Create a CMB2 metabox for each post type, again with show_in_rest => true
  3. Ensure they are initialised on the cmb2_init hook instead of cmb2_admin_init
  4. Inspect the REST API response for either post type - they should show object for both metaboxes under the cmb2 key, the correct one will be populated with values, the other will not

CMB2 Field Registration Code:

// Metabox for 'office' CPT
add_action( 'cmb2_init', 'office_register_metabox' );
function office_register_metabox() {
	$cmb = new_cmb2_box( array(
		'id'           => 'office_description_metabox',
		'title'        => esc_html__( 'Office info', 'domain' ),
		'object_types' => 'office',
		'show_in_rest' => true,
	) );

	// Fields are irrelevant - they could be any fields
	$cmb->add_field( array(
		// Field Config...
	) );
}

// Metabox for 'country' CPT
add_action( 'cmb2_init', 'country_register_metabox' );
function country_register_metabox() {
	$cmb = new_cmb2_box( array(
		'id'           => 'country_description_metabox',
		'title'        => esc_html__( 'Country info', 'domain' ),
		'object_types' => 'country',
		'show_in_rest' => true,
	) );

	// Fields are irrelevant - they could be any fields
	$cmb->add_field( array(
		// Field Config...
	) );
}
@Mte90
Copy link
Contributor

Mte90 commented Mar 12, 2019

I can confirm this issue, I am working for a project to redo all the endpoint to avoid this problem.

@Mte90
Copy link
Contributor

Mte90 commented Mar 12, 2019

Seems that https://github.com/CMB2/CMB2/blob/develop/includes/rest-api/CMB2_REST.php#L404 rest_box include the right metabox but read_fields return all the fiedls also of other metabox

@Mte90
Copy link
Contributor

Mte90 commented Mar 12, 2019

And I did a pr with the bugfix #1238 :-)

lipemat added a commit to lipemat/CMB2 that referenced this issue Apr 28, 2020
* upstream/develop: (93 commits)
  Ensure to enqueue wp-color-picker
  Add props for CMB2#1331
  Update "develop" bootstrap classname
  Proposal of CODE OF CONDUCT File
  Update version/copyright in scripts/stylesheets
  Fix issues with some tests to get them all passing
  `CMB2::is_box_type()` now also checks for taxonomies if box is registered to "term" object type.
  Prepare for 2.7.0 release
  Add all missing props/udpates to changelog
  bump tested-to value
  Rename CMB2_hookup to CMB2_Hookup Fixes CMB2#1328
  Validate composer.json
  Tested Up To confirmations.
  Update issue templates
  Add license file to meet Community standards
  Add changelog for the register_rest_field_cb param
  Abstract some aspects of the getting/setting of box rest fields to make custom handling easier
  Add ability to short-circuit the register_rest_field with a callback. Allows registering callback with a different rest slug
  Oops, send the fallback value to the box_types method
  Add props for CMB2#1238
  Fixes CMB2#1238 by merging with changes, and add CMB2::is_box_type method for checking. Fixes CMB2#1158
  Add props for CMB2#1314
  minify
  minify
  Re-add padding to textareas for readability
  Fix alpha color picker sample color styling
  clarify coment
  limit to 5.3 branch
  update block editor radio fix
  initial 5.3 prep work
  Add new "cmb2_display_class_{$fieldtype}" filter and field "display_class" parameter to allow specifying the class to use to display the field (in admin columns, etc)
  Fix another place where taxonomy_select_hierarchical was missed
  Add props for CMB2#1296 & CMB2#1297
  Add props for CMB2#1300
  Add props for CMB2#1307
  Update npm dependencis. Fixes CMB2#1308
  Remove grunt-contrib-compress and grunt-combine-media-queries to cut down on npm dependencies
  taxonomy_select_hierarchical is added in order to save correctly in to database
  Fix tests broke by introduction of character counter field property
  Fix issue introduced in 7cb3e00 where field was updated not not returned, breaking things
  If field has supporting data (e.g. file), add supporting data to field data (i.e supporting field id/value)
  Add changelog and props for CMB2#1300
  Update .min file for CMB2#1300
  Add comments to document changes in CMB2#1300
  Include .CodeMirror class in sortable exclusions
  Run the field-specific actions for all fields (including fields in groups). Fixes CMB2#1157
  Escaping Improvements to File Base
  Escaping improvements to File Fields
  Add changelog and props for CMB2#1276
  Refactor character-counter integration
  ...
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

Successfully merging a pull request may close this issue.

2 participants