WP_Customize_Nav_Menu_Setting::update( array|false $value ): bool
- Since
- 4.3.0, 7.0.0
- Source
wp-includes/customize/class-wp-customize-nav-menu-setting.php:484
Description
Any created menus will have their assigned term 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 7.0.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 value to update. Note that slug cannot be updated via wp_update_nav_menu_object(). If false, then the menu will be deleted entirely.
$namestringThe name of the menu to save.$descriptionstringdefault: empty stringThe term description.$parentintdefault: 0The id of the parent term.$auto_addbooldefault: falseWhether pages will auto_add to this menu.
Return value
bool- Whether updated.
Performance profile
How much work a call to WP_Customize_Nav_Menu_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
- Scaling
- Scales with input
- Instructions
- 6–102
- Plugin surface
- None
- Called by
- 0
Reaches the database via WP_Customize_Nav_Menu_Setting::update().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 222.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- sqldatabase query
WP_Customize_Nav_Menu_Setting::update()this function does it - optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action()one call below WP_Customize_Nav_Menu_Setting::update() - cacheobject cache
wp_cache_get()one call below WP_Customize_Nav_Menu_Setting::update() - serializeserialisation
maybe_unserialize()one call below WP_Customize_Nav_Menu_Setting::update()
Further down the call graph this can also reach query 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 · 16 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Customize_Nav_Menu_Setting::update() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–29 | none |
$value === false && $auto_add === null | 40–41 | wp_delete_nav_menu(), is_wp_error() |
$value === false && $auto_add === null | 42–44 | ->settings(), ->unsanitized_post_values(), array_keys() |
$value === false && $auto_add !== null | 46 | get_option(), ->filter_nav_menu_options_value(), update_option() |
$value === false && $auto_add === null | 53–56 | wp_delete_nav_menu(), is_wp_error(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value === false && $auto_add !== null | 57–58 | wp_delete_nav_menu(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option() |
$value !== false && !is_wp_error() && $auto_add === null | 59–66 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), is_wp_error() |
$value === false && $auto_add !== null | 59–61 | get_option(), ->filter_nav_menu_options_value(), update_option(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value !== false && is_wp_error() && $auto_add === null | 63–70 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_code(), is_wp_error() |
$value === false && $auto_add !== null | 70–73 | wp_delete_nav_menu(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value !== false && !is_wp_error() && $auto_add === null | 72–81 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), is_wp_error(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value !== false && !is_wp_error() && $auto_add !== null | 76–83 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option() |
4 further outcomes, up to 102 instructions
$value !== false && is_wp_error() && $auto_add === null | 76–85 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_code(), is_wp_error(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value !== false && is_wp_error() && $auto_add !== null | 80–87 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_code(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option() |
$value !== false && !is_wp_error() && $auto_add !== null | 89–98 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option(), ->settings(), ->unsanitized_post_values(), array_keys() |
$value !== false && is_wp_error() && $auto_add !== null | 93–102 | wp_array_slice_assoc(), wp_slash(), wp_update_nav_menu_object(), is_wp_error(), ->get_error_code(), is_wp_error(), get_option(), ->filter_nav_menu_options_value(), update_option(), ->settings(), ->unsanitized_post_values(), array_keys() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 222 | 6–102 | 22 | |
| 8.5 | 222 | 6–102 | 22 | |
| 8.4 | 222 | 6–102 | 22 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 228 | 6–102 | 22 | |
| 8.2 | 228 | 6–102 | 22 | |
| 8.1 | 228 | 6–102 | 22 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 229 | 6–102 | 22 |
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 · 10
- add_filter()Adds a callback function to a filter hook.
- wp_delete_nav_menu()Deletes a navigation menu.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_array_slice_assoc()Extracts a slice of an array, given a list of keys.
- wp_update_nav_menu_object()Saves the properties of a menu or create a new menu with those properties.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- __()Retrieves the translation of $text.
- get_option()Retrieves an option value based on an option name.
- update_option()Updates the value of an option that was already added.
- WP_Customize_Nav_Menu_Setting::filter_nav_menu_options_value()Updates a nav_menu_options array.
Source code
protected function update( $value ) { if ( $this->is_updated ) { return ( 'error' !== $this->update_status ); } $this->is_updated = true; $is_placeholder = ( $this->term_id < 0 ); $is_delete = ( false === $value ); add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) ); $auto_add = null; if ( $is_delete ) { // If the current setting term is a placeholder, a delete request is a no-op. if ( $is_placeholder ) { $this->update_status = 'deleted'; } else { $r = wp_delete_nav_menu( $this->term_id ); if ( is_wp_error( $r ) ) { $this->update_status = 'error'; $this->update_error = $r; } else { $this->update_status = 'deleted'; $auto_add = false; } } } else { // Insert or update menu. $menu_data = wp_array_slice_assoc( $value, array( 'description', 'parent' ) ); $menu_data['menu-name'] = $value['name']; $menu_id = $is_placeholder ? 0 : $this->term_id; $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) ); $original_name = $menu_data['menu-name']; $name_conflict_suffix = 1; while ( is_wp_error( $r ) && 'menu_exists' === $r->get_error_code() ) { $name_conflict_suffix += 1; /* translators: 1: Original menu name, 2: Duplicate count. */ $menu_data['menu-name'] = sprintf( __( '%1$s (%2$d)' ), $original_name, $name_conflict_suffix ); $r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) ); } if ( is_wp_error( $r ) ) { $this->update_status = 'error'; $this->update_error = $r; } else { if ( $is_placeholder ) { $this->previous_term_id = $this->term_id; $this->term_id = $r; $this->update_status = 'inserted'; } else { $this->update_status = 'updated'; } $auto_add = $value['auto_add']; } } if ( null !== $auto_add ) { $nav_menu_options = $this->filter_nav_menu_options_value( (array) get_option( 'nav_menu_options', array() ), $this->term_id, $auto_add ); update_option( 'nav_menu_options', $nav_menu_options ); } if ( 'inserted' === $this->update_status ) { // Make sure that new menus assigned to nav menu locations use their new IDs. foreach ( $this->manager->settings() as $setting ) { if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) { continue; } $post_value = $setting->post_value( null ); if ( ! is_null( $post_value ) && (int) $post_value === $this->previous_term_id ) { $this->manager->set_post_value( $setting->id, $this->term_id ); $setting->save(); }Changelog
Introduced in 4.3.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
null|void to bool.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/customize/class-wp-customize-nav-menu-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.