wppaste
WordPress

WP_Customize_Nav_Menu_Item_Setting::sanitize( array|false $value ): array|false|null|WP_Error

Since
4.3.0, 5.9.0
Source
wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:672
Sanitize an input.

Description

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

Compatibility

WordPress
since 5.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$valuearray|false
The menu item value to sanitize.

Return value

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.

Performance profile

How much work a call to WP_Customize_Nav_Menu_Item_Setting::sanitize() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–103

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 143.

Plugin surface
4 hooks

Third-party callbacks on 'title_save_pre', 'excerpt_save_pre', 'content_save_pre' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Customize_Nav_Menu_Item_Setting::sanitize() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5–7none
$menu_item_value !== false && is_array($menu_item_value)88–93array_merge(), wp_array_slice_assoc(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), apply_filters()
$menu_item_value !== false && is_array($menu_item_value)90–93array_merge(), wp_array_slice_assoc(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), sanitize_url(), __()
$menu_item_value !== false && is_array($menu_item_value)98–103array_merge(), wp_array_slice_assoc(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), wp_slash(), apply_filters(), wp_unslash(), sanitize_url(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1435–10312
8.51435–10312
8.41435–103123 fewer instructions than PHP 8.3
8.31465–10312
8.21465–10312
8.11465–10312
7.41465–10312

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while WP_Customize_Nav_Menu_Item_Setting::sanitize() runs, in this order:

  1. apply_filters( title_save_pre )filterline 728 (+56 into the body)
  2. apply_filters( excerpt_save_pre )filterline 731 (+59 into the body)
  3. apply_filters( content_save_pre )filterline 734 (+62 into the body)
  4. apply_filters( customize_sanitize_{$this->id} )filterline 749 (+77 into the body)

    Filters a Customize setting value in un-slashed form.

Uses · 8

Source code

	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 ) );		} 		// 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 );	}

Changelog

Introduced in 4.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

5.9.0
Renamed $menu_item_value to $value for PHP 8 named parameter support.from the docblock
4.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/customize/class-wp-customize-nav-menu-item-setting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.