WP_Customize_Nav_Menu_Item_Setting::sanitize()publicWP 4.3.0

Sanitize an input.

Note that parent::sanitize() erroneously does wp_unslash() on $value, but we remove that in this override.

Method of the class: WP_Customize_Nav_Menu_Item_Setting{}

Return

Array|false|null|WP_Error. Null or WP_Error if an input isn't valid. False if it is marked for deletion. Otherwise the sanitized value.

Usage

$WP_Customize_Nav_Menu_Item_Setting = new WP_Customize_Nav_Menu_Item_Setting();
$WP_Customize_Nav_Menu_Item_Setting->sanitize( $value );
$value(array) (required)
The menu item value to sanitize.

Changelog

Since 4.3.0 Introduced.
Since 5.9.0 Renamed $menu_item_value to $value for PHP 8 named parameter support.

WP_Customize_Nav_Menu_Item_Setting::sanitize() code WP 6.5.2

public function sanitize( $value ) {
	// Restores the more descriptive, specific name for use within this method.
	$menu_item_value = $value;

	// Menu is marked for deletion.
	if ( false === $menu_item_value ) {
		return $menu_item_value;
	}

	// Invalid.
	if ( ! is_array( $menu_item_value ) ) {
		return null;
	}

	$default                     = array(
		'object_id'        => 0,
		'object'           => '',
		'menu_item_parent' => 0,
		'position'         => 0,
		'type'             => 'custom',
		'title'            => '',
		'url'              => '',
		'target'           => '',
		'attr_title'       => '',
		'description'      => '',
		'classes'          => '',
		'xfn'              => '',
		'status'           => 'publish',
		'original_title'   => '',
		'nav_menu_term_id' => 0,
		'_invalid'         => false,
	);
	$menu_item_value             = array_merge( $default, $menu_item_value );
	$menu_item_value             = wp_array_slice_assoc( $menu_item_value, array_keys( $default ) );
	$menu_item_value['position'] = (int) $menu_item_value['position'];

	foreach ( array( 'object_id', 'menu_item_parent', 'nav_menu_term_id' ) as $key ) {
		// Note we need to allow negative-integer IDs for previewed objects not inserted yet.
		$menu_item_value[ $key ] = (int) $menu_item_value[ $key ];
	}

	foreach ( array( 'type', 'object', 'target' ) as $key ) {
		$menu_item_value[ $key ] = sanitize_key( $menu_item_value[ $key ] );
	}

	foreach ( array( 'xfn', 'classes' ) as $key ) {
		$value = $menu_item_value[ $key ];
		if ( ! is_array( $value ) ) {
			$value = explode( ' ', $value );
		}
		$menu_item_value[ $key ] = implode( ' ', array_map( 'sanitize_html_class', $value ) );
	}

	$menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );

	// Apply the same filters as when calling wp_insert_post().

	/** This filter is documented in wp-includes/post.php */
	$menu_item_value['title'] = wp_unslash( apply_filters( 'title_save_pre', wp_slash( $menu_item_value['title'] ) ) );

	/** This filter is documented in wp-includes/post.php */
	$menu_item_value['attr_title'] = wp_unslash( apply_filters( 'excerpt_save_pre', wp_slash( $menu_item_value['attr_title'] ) ) );

	/** This filter is documented in wp-includes/post.php */
	$menu_item_value['description'] = wp_unslash( apply_filters( 'content_save_pre', wp_slash( $menu_item_value['description'] ) ) );

	if ( '' !== $menu_item_value['url'] ) {
		$menu_item_value['url'] = sanitize_url( $menu_item_value['url'] );
		if ( '' === $menu_item_value['url'] ) {
			return new WP_Error( 'invalid_url', __( 'Invalid URL.' ) ); // Fail sanitization if URL is invalid.
		}
	}
	if ( 'publish' !== $menu_item_value['status'] ) {
		$menu_item_value['status'] = 'draft';
	}

	$menu_item_value['_invalid'] = (bool) $menu_item_value['_invalid'];

	/** This filter is documented in wp-includes/class-wp-customize-setting.php */
	return apply_filters( "customize_sanitize_{$this->id}", $menu_item_value, $this );
}