WP_REST_Menus_Controller::handle_auto_add()protectedWP 5.9.0

Updates the menu's auto add from a REST request.

{} It's a method of the class: WP_REST_Menus_Controller{}

Hooks from the method

Return

true|false. True if the auto add setting was successfully updated.

Usage

// protected - for code of main (parent) or child class
$result = $this->handle_auto_add( $menu_id, $request );
$menu_id(int) (required)
The menu id to update.
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 5.9.0 Introduced.

Code of WP_REST_Menus_Controller::handle_auto_add() WP 6.0

protected function handle_auto_add( $menu_id, $request ) {
	if ( ! isset( $request['auto_add'] ) ) {
		return true;
	}

	$nav_menu_option = (array) get_option( 'nav_menu_options', array( 'auto_add' => array() ) );

	if ( ! isset( $nav_menu_option['auto_add'] ) ) {
		$nav_menu_option['auto_add'] = array();
	}

	$auto_add = $request['auto_add'];

	$i = array_search( $menu_id, $nav_menu_option['auto_add'], true );

	if ( $auto_add && false === $i ) {
		$nav_menu_option['auto_add'][] = $menu_id;
	} elseif ( ! $auto_add && false !== $i ) {
		array_splice( $nav_menu_option['auto_add'], $i, 1 );
	}

	$update = update_option( 'nav_menu_options', $nav_menu_option );

	/** This action is documented in wp-includes/nav-menu.php */
	do_action( 'wp_update_nav_menu', $menu_id );

	return $update;
}