WP_REST_Menus_Controller::handle_auto_add()protectedWP 5.9.0

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

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.

WP_REST_Menus_Controller::handle_auto_add() code WP 6.6.2

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