WP_Customize_Nav_Menu_Item_Setting::update() │ protected │ WP 4.3.0 Creates/updates the nav_menu_item post for this setting.
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.
Method of the class: WP_Customize_Nav_Menu_Item_Setting{}
No Hooks.
Return
null|null
.
Usage
// protected - for code of main (parent) or child class
$result = $this->update( $value );
$value(array|false) (required)
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.
Notes
Changelog
WP_Customize_Nav_Menu_Item_Setting::update() WP Customize Nav Menu Item Setting::update code
WP 6.6.2
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;
}
$value['menu_item_parent'] = $parent_nav_menu_item_setting->post_id;
}
// Insert or update menu.
$menu_item_data = array(
'menu-item-object-id' => $value['object_id'],
'menu-item-object' => $value['object'],
'menu-item-parent-id' => $value['menu_item_parent'],
'menu-item-position' => $value['position'],
'menu-item-type' => $value['type'],
'menu-item-title' => $value['title'],
'menu-item-url' => $value['url'],
'menu-item-description' => $value['description'],
'menu-item-attr-title' => $value['attr_title'],
'menu-item-target' => $value['target'],
'menu-item-classes' => $value['classes'],
'menu-item-xfn' => $value['xfn'],
'menu-item-status' => $value['status'],
);
$r = wp_update_nav_menu_item(
$value['nav_menu_term_id'],
$is_placeholder ? 0 : $this->post_id,
wp_slash( $menu_item_data )
);
if ( is_wp_error( $r ) ) {
$this->update_status = 'error';
$this->update_error = $r;
} else {
if ( $is_placeholder ) {
$this->previous_post_id = $this->post_id;
$this->post_id = $r;
$this->update_status = 'inserted';
} else {
$this->update_status = 'updated';
}
}
}
}