Make WordPress Core

Opened 5 years ago

Last modified 5 days ago

#46338 new defect (bug)

taxonomy_meta_box_sanitize_cb_checkboxes: Warning: array_map(): Expected parameter 2 to be an array, string given

Reported by: conner_bw's profile conner_bw Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.1
Component: Taxonomy Keywords:
Focuses: Cc:

Description

Since upgrading to WordPress 5.1.0, when editing a custom post type I get a PHP error:

( ! ) Warning: array_map(): Expected parameter 2 to be an array, string given in wp-admin/includes/post.php on line 2005

/**
* Sanitizes POST values from a checkbox taxonomy metabox.
*
* @since 5.1.0
*
* @param mixed $terms Raw term data from the 'tax_input' field.
* @return array
*/
function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) {
   return array_map( 'intval', $terms );
}

register_taxonomy: hierarchical is set to true.
The $_POST looks like: tax_input[chapter-type]: -1
The value of $terms passed to taxonomy_meta_box_sanitize_cb_checkboxes is equal to -1
The HTML that sends this info along, rendered by extended-cpts, looks like:

<select name="tax_input[chapter-type]" id="chapter-typedropdown" class="postform">
	<option value="-1">No chapter type</option>
	<option class="level-0" value="55">Numberless</option>
	<option class="level-0" value="54">Standard</option>
</select>

If I manually specify 'meta_box_sanitize_cb' => 'taxonomy_meta_box_sanitize_cb_input' as an arg in register_taxonomy the bug goes away.

The problem is, since no callbacks are specified, and since hierarchical is true, WordPress 5.1 sets up taxonomy_meta_box_sanitize_cb_checkboxes which doesn't work on $terms that are not an array.

It was working fine before.

Commit that broke things:

Change in 5.1.0 that weren't in 5.0.3

Related:

Attachments (1)

chat-post-type.php (1.6 KB) - added by oglekler 7 days ago.
Code example that triggers a fatal error during Quick Edit saving.

Download all attachments as: .zip

Change History (5)

#1 @oglekler
8 days ago

/wp-admin/includes/post.php:438

PHP 8.0.30, WordPress 6.5 (6.5.2 - the same).

	// Convert taxonomy input to term IDs, to avoid ambiguity.
	if ( isset( $post_data['tax_input'] ) ) {
		foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
			$tax_object = get_taxonomy( $taxonomy );

			if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) {
				$translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) );
			}
		}
	}

During Post edit $terms is an array:

Array
(
    [0] => 0
    [1] => 21
    [2] => 22
)

During Quick edit, it is a string:

 test, test2, 

And with PHP 8+ I am getting PHP Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, string given in ...\wp-admin\includes\post.php:2233

This ticket was mentioned in Slack in #core-test by oglekler. View the logs.


8 days ago

@oglekler
7 days ago

Code example that triggers a fatal error during Quick Edit saving.

#3 @oglekler
7 days ago

According to this: https://developer.wordpress.org/reference/functions/register_taxonomy/
'meta_box_cb' can have a callback function name and be equal to 'post_categories_meta_box' for hierarchical taxonomies and 'post_tags_meta_box' for non-hierarchical taxonomies. This was my mistake; I didn't check the documentation properly, and taxonomy is not hierarchical by default. On the other hand, this situation with settings that are not compatible with each other can possibly be handled without fatal error.

This ticket was mentioned in Slack in #core by oglekler. View the logs.


5 days ago

Note: See TracTickets for help on using tickets.