wppaste
WordPress

WP_Customize_Nav_Menu_Item_Setting::update( array|false $value ): null|void

Since
4.3.0
Source
wp-includes/customize/class-wp-customize-nav-menu-item-setting.php:770
Creates/updates the nav_menu_item post for this setting.

Description

Any created menu items will have their assigned post IDs exported to the client via the 'customize_save_response' filter. Likewise, any errors will be exported to the client via the customize_save_response() filter.

To delete a menu, the client can send false as the value.

Compatibility

WordPress
since 4.3.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 array to update. If false, then the menu item will be deleted entirely. See WP_Customize_Nav_Menu_Item_Setting::$default for what the value should consist of.

Return value

null|void

Performance profile

How much work a call to WP_Customize_Nav_Menu_Item_Setting::update() 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
Heavy

Reaches the database via WP_Customize_Nav_Menu_Item_Setting::update().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–126

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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

What it touches

  • sqldatabase queryWP_Customize_Nav_Menu_Item_Setting::update()this function does it
  • querycontent queryget_post()one call below WP_Customize_Nav_Menu_Item_Setting::update()
  • hookthird-party callbacksapply_filters()one call below WP_Customize_Nav_Menu_Item_Setting::update()

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

What one call costs · 10 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::update() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always4–22none
$value === false && $r !== false31wp_delete_post()
$value === false && $r === false36wp_delete_post(), add_filter()
$value !== false39–44->get_setting()
$value !== false45–53->get_setting(), ->save()
$value !== false && $nav_menu_setting instanceof66–68->get_setting(), ->save(), ->get_setting()
$value !== false72–78wp_slash(), wp_update_nav_menu_item(), is_wp_error()
$value !== false && $nav_menu_setting instanceof && $parent_nav_menu_item_setting instanceof72–77->get_setting(), ->save(), ->get_setting(), ->save()
$value !== false96–102->get_setting(), ->save(), wp_slash(), wp_update_nav_menu_item(), is_wp_error()
$value !== false && $nav_menu_setting instanceof && $parent_nav_menu_item_setting instanceof120–126->get_setting(), ->save(), ->get_setting(), ->save(), wp_slash(), wp_update_nav_menu_item(), is_wp_error()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2084–12617
8.52084–12617
8.42084–126174 fewer instructions than PHP 8.3
8.32124–13017
8.22124–13017
8.12124–130171 fewer instruction than PHP 7.4
7.42134–13017

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.

Uses · 6

Source code

	protected function update( $value ) {		if ( $this->is_updated ) {			return;		} 		$this->is_updated = true;		$is_placeholder   = ( $this->post_id < 0 );		$is_delete        = ( false === $value ); 		// Update the cached value.		$this->value = $value; 		add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) ); 		if ( $is_delete ) {			// If the current setting post is a placeholder, a delete request is a no-op.			if ( $is_placeholder ) {				$this->update_status = 'deleted';			} else {				$r = wp_delete_post( $this->post_id, true ); 				if ( false === $r ) {					$this->update_error  = new WP_Error( 'delete_failure' );					$this->update_status = 'error';				} else {					$this->update_status = 'deleted';				}				// @todo send back the IDs for all associated nav menu items deleted, so these settings (and controls) can be removed from Customizer?			}		} else { 			// Handle saving menu items for menus that are being newly-created.			if ( $value['nav_menu_term_id'] < 0 ) {				$nav_menu_setting_id = sprintf( 'nav_menu[%s]', $value['nav_menu_term_id'] );				$nav_menu_setting    = $this->manager->get_setting( $nav_menu_setting_id ); 				if ( ! $nav_menu_setting || ! ( $nav_menu_setting instanceof WP_Customize_Nav_Menu_Setting ) ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'unexpected_nav_menu_setting' );					return;				} 				if ( false === $nav_menu_setting->save() ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'nav_menu_setting_failure' );					return;				} 				if ( (int) $value['nav_menu_term_id'] !== $nav_menu_setting->previous_term_id ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'unexpected_previous_term_id' );					return;				} 				$value['nav_menu_term_id'] = $nav_menu_setting->term_id;			} 			// Handle saving a nav menu item that is a child of a nav menu item being newly-created.			if ( $value['menu_item_parent'] < 0 ) {				$parent_nav_menu_item_setting_id = sprintf( 'nav_menu_item[%s]', $value['menu_item_parent'] );				$parent_nav_menu_item_setting    = $this->manager->get_setting( $parent_nav_menu_item_setting_id ); 				if ( ! $parent_nav_menu_item_setting || ! ( $parent_nav_menu_item_setting instanceof WP_Customize_Nav_Menu_Item_Setting ) ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'unexpected_nav_menu_item_setting' );					return;				} 				if ( false === $parent_nav_menu_item_setting->save() ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'nav_menu_item_setting_failure' );					return;				} 				if ( (int) $value['menu_item_parent'] !== $parent_nav_menu_item_setting->previous_post_id ) {					$this->update_status = 'error';					$this->update_error  = new WP_Error( 'unexpected_previous_post_id' );					return;				}

Changelog

Introduced in 4.3.0. One change between 6.7.7 and 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.

7.0.4
Return type changed from null|void to bool.verified against source
4.3.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 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.